From 5d3795e876689f9adba3de094e98330ba3c23bc3 Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 28 Sep 2021 23:45:50 +0800 Subject: [PATCH] cgen: fix the C codegen error for '_ = map[key]' (fix #11999) (#12000) --- vlib/v/gen/c/cgen.v | 3 +++ vlib/v/tests/map_get_assign_blank_test.v | 11 ++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index c8b561413d..85f016da73 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -2925,6 +2925,9 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { g.right_is_opt = true } if blank_assign { + if val is ast.IndexExpr { + g.assign_op = .decl_assign + } if is_call { old_is_void_expr_stmt := g.is_void_expr_stmt g.is_void_expr_stmt = true diff --git a/vlib/v/tests/map_get_assign_blank_test.v b/vlib/v/tests/map_get_assign_blank_test.v index fe6647d4ae..12be778695 100644 --- a/vlib/v/tests/map_get_assign_blank_test.v +++ b/vlib/v/tests/map_get_assign_blank_test.v @@ -1,6 +1,6 @@ type Abc = int | string -fn test_map_get_assign_blank() { +fn test_map_get_decl_assign_blank() { x := map[string]Abc{} _ := x['nonexisting'] if y := x['nonexisting'] { @@ -8,3 +8,12 @@ fn test_map_get_assign_blank() { } assert true } + +fn test_map_get_assign_blank() { + x := map[string]Abc{} + _ = x['nonexisting'] + if y := x['nonexisting'] { + println(y) + } + assert true +}