1
0
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:
yuyi 2023-03-19 16:11:11 +08:00 committed by GitHub
parent 37af8bbd27
commit c18bf48833
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 2 deletions

View File

@ -487,12 +487,30 @@ fn (mut g Gen) gen_anon_fn(mut node ast.AnonFn) {
if obj is ast.Var {
if obj.has_inherited {
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 {
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--

View 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
}