Skip to content

Commit cd5c556

Browse files
authored
cgen: fix closure variable in smartcast (#19796)
1 parent 60ba140 commit cd5c556

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

vlib/v/gen/c/fn.v

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1777,7 +1777,11 @@ fn (mut g Gen) fn_call(node ast.CallExpr) {
17771777
cast_sym := g.table.sym(g.unwrap_generic(typ))
17781778
mut is_ptr := false
17791779
if i == 0 {
1780-
g.write(node.name)
1780+
if obj.is_inherited {
1781+
g.write(c.closure_ctx + '->' + node.name)
1782+
} else {
1783+
g.write(node.name)
1784+
}
17811785
if obj.orig_type.is_ptr() {
17821786
is_ptr = true
17831787
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
pub type MyCallback = fn () | fn (ctx voidptr)
2+
3+
fn my_lower_level_func(func fn (ctx voidptr), ctx voidptr) {
4+
println('Bar')
5+
}
6+
7+
fn my_func(cb MyCallback, ctx voidptr) {
8+
my_lower_level_func(fn [cb] (ctx voidptr) {
9+
match cb {
10+
fn () {
11+
cb()
12+
}
13+
fn (ctx voidptr) {
14+
cb(ctx)
15+
}
16+
}
17+
}, ctx)
18+
}
19+
20+
fn test_closure_variable_in_smartcast() {
21+
my_func(fn () {
22+
println('Foo')
23+
}, unsafe { nil })
24+
assert true
25+
}

0 commit comments

Comments
 (0)