From 0c84ad847e4e0e84a91bfb04bdac90514404a27c Mon Sep 17 00:00:00 2001 From: yuyi Date: Sun, 4 Sep 2022 18:21:16 +0800 Subject: [PATCH] cgen: fix match expr with optional (#15658) --- vlib/v/gen/c/match.v | 2 +- vlib/v/tests/match_expr_with_struct_init_test.v | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/match_expr_with_struct_init_test.v diff --git a/vlib/v/gen/c/match.v b/vlib/v/gen/c/match.v index ad5120fcd..f93b9aa19 100644 --- a/vlib/v/gen/c/match.v +++ b/vlib/v/gen/c/match.v @@ -28,7 +28,7 @@ fn (mut g Gen) need_tmp_var_in_match(node ast.MatchExpr) bool { if branch.stmts.len == 1 { if branch.stmts[0] is ast.ExprStmt { stmt := branch.stmts[0] as ast.ExprStmt - if stmt.expr in [ast.CallExpr, ast.IfExpr, ast.MatchExpr] + if stmt.expr in [ast.CallExpr, ast.IfExpr, ast.MatchExpr, ast.StructInit] || (stmt.expr is ast.IndexExpr && (stmt.expr as ast.IndexExpr).or_expr.kind != .absent) { return true diff --git a/vlib/v/tests/match_expr_with_struct_init_test.v b/vlib/v/tests/match_expr_with_struct_init_test.v new file mode 100644 index 000000000..172dc78ab --- /dev/null +++ b/vlib/v/tests/match_expr_with_struct_init_test.v @@ -0,0 +1,17 @@ +struct Test { + a string +} + +fn test_match_expr_with_struct_init() { + a := map[string]string{} + b := match 'test' { + 'test' { + Test{a['test'] or { '' }} + } + else { + Test{''} + } + } + println(b) + assert b.a == '' +} -- 2.30.2