mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix closure with fixed array variable (#17707)
This commit is contained in:
parent
37af8bbd27
commit
c18bf48833
@ -487,14 +487,32 @@ fn (mut g Gen) gen_anon_fn(mut node ast.AnonFn) {
|
||||
if obj is ast.Var {
|
||||
if obj.has_inherited {
|
||||
has_inherited = true
|
||||
var_sym := g.table.sym(var.typ)
|
||||
if var_sym.info is ast.ArrayFixed {
|
||||
g.write('.${var.name} = {')
|
||||
for i in 0 .. var_sym.info.size {
|
||||
g.write('${c.closure_ctx}->${var.name}[${i}],')
|
||||
}
|
||||
g.writeln('},')
|
||||
} else {
|
||||
g.writeln('.${var.name} = ${c.closure_ctx}->${var.name},')
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if !has_inherited {
|
||||
var_sym := g.table.sym(var.typ)
|
||||
if var_sym.info is ast.ArrayFixed {
|
||||
g.write('.${var.name} = {')
|
||||
for i in 0 .. var_sym.info.size {
|
||||
g.write('${var.name}[${i}],')
|
||||
}
|
||||
g.writeln('},')
|
||||
} else {
|
||||
g.writeln('.${var.name} = ${var.name},')
|
||||
}
|
||||
}
|
||||
}
|
||||
g.indent--
|
||||
g.write('}, sizeof(${ctx_struct})))')
|
||||
|
||||
|
16
vlib/v/tests/closure_with_fixed_array_var_test.v
Normal file
16
vlib/v/tests/closure_with_fixed_array_var_test.v
Normal file
@ -0,0 +1,16 @@
|
||||
struct Crasher {
|
||||
value int
|
||||
}
|
||||
|
||||
fn crash(c [1]Crasher) fn () int {
|
||||
return fn [c] () int {
|
||||
println(c[0].value)
|
||||
return c[0].value
|
||||
}
|
||||
}
|
||||
|
||||
fn test_closure_with_fixed_array_var() {
|
||||
crash_fn := crash([Crasher{1}]!)
|
||||
ret := crash_fn()
|
||||
assert ret == 1
|
||||
}
|
Loading…
Reference in New Issue
Block a user