Skip to content

Commit

Permalink
cgen: fix closure variable in smartcast (#19796)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Nov 7, 2023
1 parent 60ba140 commit cd5c556
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
6 changes: 5 additions & 1 deletion vlib/v/gen/c/fn.v
Expand Up @@ -1777,7 +1777,11 @@ fn (mut g Gen) fn_call(node ast.CallExpr) {
cast_sym := g.table.sym(g.unwrap_generic(typ))
mut is_ptr := false
if i == 0 {
g.write(node.name)
if obj.is_inherited {
g.write(c.closure_ctx + '->' + node.name)
} else {
g.write(node.name)
}
if obj.orig_type.is_ptr() {
is_ptr = true
}
Expand Down
25 changes: 25 additions & 0 deletions vlib/v/tests/closure_variable_in_smartcast_test.v
@@ -0,0 +1,25 @@
pub type MyCallback = fn () | fn (ctx voidptr)

fn my_lower_level_func(func fn (ctx voidptr), ctx voidptr) {
println('Bar')
}

fn my_func(cb MyCallback, ctx voidptr) {
my_lower_level_func(fn [cb] (ctx voidptr) {
match cb {
fn () {
cb()
}
fn (ctx voidptr) {
cb(ctx)
}
}
}, ctx)
}

fn test_closure_variable_in_smartcast() {
my_func(fn () {
println('Foo')
}, unsafe { nil })
assert true
}

0 comments on commit cd5c556

Please sign in to comment.