From 81dbd724127b2ddbae548b3e50c12f4807e194f3 Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 2 Mar 2021 16:26:27 +0800 Subject: [PATCH] cgen: fix anon_fn in containers (fix #8965) (#9049) --- vlib/v/gen/c/cgen.v | 11 +++++++++++ vlib/v/tests/anon_fn_in_containers_test.v | 13 +++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 vlib/v/tests/anon_fn_in_containers_test.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 1199cf725f..83bd4d51f7 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -1952,7 +1952,18 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { g.definitions.go_back(g.definitions.len - def_pos) g.write(') = ') } else { + g.is_assign_lhs = true + g.assign_op = assign_stmt.op g.expr(left) + g.is_assign_lhs = false + if left is ast.IndexExpr { + sym := g.table.get_type_symbol(left.left_type) + if sym.kind in [.map, .array] { + g.expr(val) + g.writeln('});') + continue + } + } g.write(' = ') } g.expr(val) diff --git a/vlib/v/tests/anon_fn_in_containers_test.v b/vlib/v/tests/anon_fn_in_containers_test.v new file mode 100644 index 0000000000..b4fdbbaf3e --- /dev/null +++ b/vlib/v/tests/anon_fn_in_containers_test.v @@ -0,0 +1,13 @@ +fn test_anon_fn_in_map() { + mut woop := map{ + 'what': fn() string { + return 'whoopity whoop' + } + } + assert woop['what']() == 'whoopity whoop' + + woop['shat'] = fn() string { + return 'shoopity shoop' + } + assert woop['shat']() == 'shoopity shoop' +}