From c0ef6dbde8fcff3f3c46cc4970f6810000f5b4bc Mon Sep 17 00:00:00 2001 From: yuyi Date: Sun, 29 May 2022 01:47:54 +0800 Subject: [PATCH] cgen: fix cross assign in closure (#14549) --- vlib/v/gen/c/assign.v | 5 +++-- vlib/v/tests/assign_literal_with_closure_test.v | 3 +-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/vlib/v/gen/c/assign.v b/vlib/v/gen/c/assign.v index f45f40deaf..d59affa85f 100644 --- a/vlib/v/gen/c/assign.v +++ b/vlib/v/gen/c/assign.v @@ -625,12 +625,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 { '' } if left_sym.kind == .function { g.write_fn_ptr_decl(left_sym.info as ast.FnType, '_var_$left.pos.pos') - g.writeln(' = ${c_name(left.name)};') + g.writeln(' = $anon_ctx${c_name(left.name)};') } else { styp := g.typ(left_typ) - g.writeln('$styp _var_$left.pos.pos = ${c_name(left.name)};') + g.writeln('$styp _var_$left.pos.pos = $anon_ctx${c_name(left.name)};') } } ast.IndexExpr { diff --git a/vlib/v/tests/assign_literal_with_closure_test.v b/vlib/v/tests/assign_literal_with_closure_test.v index 597b48226e..1fc5f209b5 100644 --- a/vlib/v/tests/assign_literal_with_closure_test.v +++ b/vlib/v/tests/assign_literal_with_closure_test.v @@ -10,8 +10,7 @@ fn sma(period int) fn (f64) f64 { } sum += input - storage[i] - storage[i] = input - i = (i + 1) % period + storage[i], i = input, (i + 1) % period return sum / f64(storage.len) } }