From dc9068b4d3be98e95e9b1c9f984e579fdaa4cb64 Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 21 Apr 2022 18:02:31 +0800 Subject: [PATCH] cgen: fix error for if expr with nested match expr (#14122) --- vlib/v/gen/c/if.v | 3 +++ vlib/v/tests/if_expr_with_nested_match_expr_test.v | 12 ++++++++++++ 2 files changed, 15 insertions(+) create mode 100644 vlib/v/tests/if_expr_with_nested_match_expr_test.v diff --git a/vlib/v/gen/c/if.v b/vlib/v/gen/c/if.v index 74fec1f85e..07105e7138 100644 --- a/vlib/v/gen/c/if.v +++ b/vlib/v/gen/c/if.v @@ -20,6 +20,9 @@ fn (mut g Gen) need_tmp_var_in_if(node ast.IfExpr) bool { if is_noreturn_callexpr(stmt.expr) { return true } + if stmt.expr is ast.MatchExpr { + return true + } if stmt.expr is ast.CallExpr { if stmt.expr.is_method { left_sym := g.table.sym(stmt.expr.receiver_type) diff --git a/vlib/v/tests/if_expr_with_nested_match_expr_test.v b/vlib/v/tests/if_expr_with_nested_match_expr_test.v new file mode 100644 index 0000000000..6eb35a67d0 --- /dev/null +++ b/vlib/v/tests/if_expr_with_nested_match_expr_test.v @@ -0,0 +1,12 @@ +fn test_if_expr_with_nested_match_expr() { + a := if true { + match `a` { + `a` { 0 } + else { 1 } + } + } else { + 3 + } + println(a) + assert a == 0 +}