cgen: fix closure variable in smartcast (#19796)

This commit is contained in:
yuyi 2023-11-07 20:29:03 +08:00 committed by GitHub
parent 60ba14038a
commit cd5c5561db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 1 deletions

View File

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

View File

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