From cb79e57c1a1ed2df2fceb5ba9daaf63a15d0e00c Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sun, 29 Jan 2023 07:11:30 -0300 Subject: [PATCH] cgen: fix generated code for `match bar()?.a {` (matchexpr with call expr using propagation) (#17150) --- vlib/v/gen/c/match.v | 4 ++- .../v/tests/match_expr_with_opt_result_test.v | 32 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/match_expr_with_opt_result_test.v diff --git a/vlib/v/gen/c/match.v b/vlib/v/gen/c/match.v index 5e75c1531..dd8131e27 100644 --- a/vlib/v/gen/c/match.v +++ b/vlib/v/gen/c/match.v @@ -67,7 +67,9 @@ fn (mut g Gen) match_expr(node ast.MatchExpr) { } if node.cond in [ast.Ident, ast.IntegerLiteral, ast.StringLiteral, ast.FloatLiteral] || (node.cond is ast.SelectorExpr - && (node.cond as ast.SelectorExpr).or_block.kind == .absent) { + && (node.cond as ast.SelectorExpr).or_block.kind == .absent + && ((node.cond as ast.SelectorExpr).expr !is ast.CallExpr + || ((node.cond as ast.SelectorExpr).expr as ast.CallExpr).or_block.kind == .absent)) { cond_var = g.expr_string(node.cond) } else { line := if is_expr { diff --git a/vlib/v/tests/match_expr_with_opt_result_test.v b/vlib/v/tests/match_expr_with_opt_result_test.v new file mode 100644 index 000000000..167f33c7c --- /dev/null +++ b/vlib/v/tests/match_expr_with_opt_result_test.v @@ -0,0 +1,32 @@ +struct Foo { +pub: + a int +} + +fn foo() !Foo { + return Foo{1} +} + +fn bar() ?Foo { + return Foo{2} +} + +fn test_main() { + match foo()!.a { + 1 { + assert true + } + else { + assert false + } + } + + match bar()?.a { + 2 { + assert true + } + else { + assert false + } + } +} -- 2.30.2