From e1c762a616ed446bac6354e04724f0719e9b56f0 Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 17 Aug 2021 23:21:15 +0800 Subject: [PATCH] cgen: fix assert fn_call with parentheses (fix #11207) (#11214) --- vlib/v/gen/c/assert.v | 8 ++++++++ vlib/v/tests/assert_fn_call_with_parentheses_test.v | 7 +++++++ 2 files changed, 15 insertions(+) create mode 100644 vlib/v/tests/assert_fn_call_with_parentheses_test.v diff --git a/vlib/v/gen/c/assert.v b/vlib/v/gen/c/assert.v index 5390d73bfe..46b48f7f00 100644 --- a/vlib/v/gen/c/assert.v +++ b/vlib/v/gen/c/assert.v @@ -14,9 +14,17 @@ fn (mut g Gen) gen_assert_stmt(original_assert_statement ast.AssertStmt) { if mut node.expr is ast.InfixExpr { if mut node.expr.left is ast.CallExpr { node.expr.left = g.new_ctemp_var_then_gen(node.expr.left, node.expr.left_type) + } else if mut node.expr.left is ast.ParExpr { + if node.expr.left.expr is ast.CallExpr { + node.expr.left = g.new_ctemp_var_then_gen(node.expr.left.expr, node.expr.left_type) + } } if mut node.expr.right is ast.CallExpr { node.expr.right = g.new_ctemp_var_then_gen(node.expr.right, node.expr.right_type) + } else if mut node.expr.right is ast.ParExpr { + if node.expr.right.expr is ast.CallExpr { + node.expr.right = g.new_ctemp_var_then_gen(node.expr.right.expr, node.expr.right_type) + } } } g.inside_ternary++ diff --git a/vlib/v/tests/assert_fn_call_with_parentheses_test.v b/vlib/v/tests/assert_fn_call_with_parentheses_test.v new file mode 100644 index 0000000000..4a40c920bf --- /dev/null +++ b/vlib/v/tests/assert_fn_call_with_parentheses_test.v @@ -0,0 +1,7 @@ +fn foo(fail bool) ?string { + return if fail { error('failure') } else { 'success' } +} + +fn test_assert_fn_call_with_parentheses() { + assert (foo(true) or { '' }) == '' +}