Skip to content

Commit c18bf48

Browse files
authored
cgen: fix closure with fixed array variable (#17707)
1 parent 37af8bb commit c18bf48

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

vlib/v/gen/c/fn.v

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,12 +487,30 @@ fn (mut g Gen) gen_anon_fn(mut node ast.AnonFn) {
487487
if obj is ast.Var {
488488
if obj.has_inherited {
489489
has_inherited = true
490-
g.writeln('.${var.name} = ${c.closure_ctx}->${var.name},')
490+
var_sym := g.table.sym(var.typ)
491+
if var_sym.info is ast.ArrayFixed {
492+
g.write('.${var.name} = {')
493+
for i in 0 .. var_sym.info.size {
494+
g.write('${c.closure_ctx}->${var.name}[${i}],')
495+
}
496+
g.writeln('},')
497+
} else {
498+
g.writeln('.${var.name} = ${c.closure_ctx}->${var.name},')
499+
}
491500
}
492501
}
493502
}
494503
if !has_inherited {
495-
g.writeln('.${var.name} = ${var.name},')
504+
var_sym := g.table.sym(var.typ)
505+
if var_sym.info is ast.ArrayFixed {
506+
g.write('.${var.name} = {')
507+
for i in 0 .. var_sym.info.size {
508+
g.write('${var.name}[${i}],')
509+
}
510+
g.writeln('},')
511+
} else {
512+
g.writeln('.${var.name} = ${var.name},')
513+
}
496514
}
497515
}
498516
g.indent--
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
struct Crasher {
2+
value int
3+
}
4+
5+
fn crash(c [1]Crasher) fn () int {
6+
return fn [c] () int {
7+
println(c[0].value)
8+
return c[0].value
9+
}
10+
}
11+
12+
fn test_closure_with_fixed_array_var() {
13+
crash_fn := crash([Crasher{1}]!)
14+
ret := crash_fn()
15+
assert ret == 1
16+
}

0 commit comments

Comments
 (0)