cgen: fix assert fn_call with parentheses (fix #11207) (#11214)

pull/11226/head
yuyi 2021-08-17 23:21:15 +08:00 committed by GitHub
parent f8174c381c
commit e1c762a616
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 0 deletions

View File

@ -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 is ast.InfixExpr {
if mut node.expr.left is ast.CallExpr { if mut node.expr.left is ast.CallExpr {
node.expr.left = g.new_ctemp_var_then_gen(node.expr.left, node.expr.left_type) 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 { if mut node.expr.right is ast.CallExpr {
node.expr.right = g.new_ctemp_var_then_gen(node.expr.right, node.expr.right_type) 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++ g.inside_ternary++

View File

@ -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 { '' }) == ''
}