cgen: fix closure parameter judgment when var cross assign inside anon fn(fix #19734) (#19736)

This commit is contained in:
shove 2023-11-04 02:47:42 +08:00 committed by GitHub
parent 24befa0ba6
commit 1a53a46d69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 1 deletions

View File

@ -891,7 +891,13 @@ fn (mut g Gen) gen_cross_var_assign(node &ast.AssignStmt) {
ast.Ident {
left_typ := node.left_types[i]
left_sym := g.table.sym(left_typ)
anon_ctx := if g.anon_fn { '${closure_ctx}->' } else { '' }
mut anon_ctx := ''
if g.anon_fn {
obj := left.scope.find(left.name)
if obj is ast.Var && obj.is_inherited {
anon_ctx = '${closure_ctx}->'
}
}
if left_sym.kind == .function {
g.write_fn_ptr_decl(left_sym.info as ast.FnType, '_var_${left.pos.pos}')
g.writeln(' = ${anon_ctx}${c_name(left.name)};')

View File

@ -223,3 +223,23 @@ fn test_closure_over_variable_that_is_returned_from_a_multi_value_function() {
a()
println(two)
}
fn test_cross_var_assign_without_inherited() {
f := fn () {
mut left := 1
mut right := 2
left, right = right, left
assert left == 2 && right == 1
}
f()
}
fn test_cross_var_assign_with_inherited() {
mut left := 1
mut right := 2
f := fn [mut left, mut right] () {
left, right = right, left
assert left == 2 && right == 1
}
f()
}