mirror of
https://github.com/vlang/v.git
synced 2025-09-16 16:02:29 +03:00
cgen: fix closure with fixed array variable (#17707)
This commit is contained in:
parent
37af8bbd27
commit
c18bf48833
2 changed files with 36 additions and 2 deletions
|
@ -487,12 +487,30 @@ fn (mut g Gen) gen_anon_fn(mut node ast.AnonFn) {
|
||||||
if obj is ast.Var {
|
if obj is ast.Var {
|
||||||
if obj.has_inherited {
|
if obj.has_inherited {
|
||||||
has_inherited = true
|
has_inherited = true
|
||||||
g.writeln('.${var.name} = ${c.closure_ctx}->${var.name},')
|
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 {
|
if !has_inherited {
|
||||||
g.writeln('.${var.name} = ${var.name},')
|
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.indent--
|
||||||
|
|
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…
Add table
Add a link
Reference in a new issue