1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker, cgen: fix asserting if guard expr (#17793)

This commit is contained in:
yuyi 2023-03-28 16:40:33 +08:00 committed by GitHub
parent 5e48817dc8
commit da153aa780
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 0 deletions

View File

@ -1906,6 +1906,7 @@ fn (mut c Checker) stmt(node_ ast.Stmt) {
fn (mut c Checker) assert_stmt(node ast.AssertStmt) {
cur_exp_typ := c.expected_type
c.expected_type = ast.bool_type
assert_type := c.check_expr_opt_call(node.expr, c.expr(node.expr))
if assert_type != ast.bool_type_idx {
atype_name := c.table.sym(assert_type).name

View File

@ -22,7 +22,10 @@ fn (mut g Gen) assert_stmt(original_assert_statement ast.AssertStmt) {
g.inside_ternary++
if g.pref.is_test {
g.write('if (')
prev_inside_ternary := g.inside_ternary
g.inside_ternary = 0
g.expr(node.expr)
g.inside_ternary = prev_inside_ternary
g.write(')')
g.decrement_inside_ternary()
g.writeln(' {')
@ -35,7 +38,10 @@ fn (mut g Gen) assert_stmt(original_assert_statement ast.AssertStmt) {
g.writeln('}')
} else {
g.write('if (!(')
prev_inside_ternary := g.inside_ternary
g.inside_ternary = 0
g.expr(node.expr)
g.inside_ternary = prev_inside_ternary
g.write('))')
g.decrement_inside_ternary()
g.writeln(' {')

View File

@ -0,0 +1,11 @@
fn foo() !int {
return error('error')
}
fn test_assert_if_guard_expr() {
assert if _ := foo() {
false
} else {
true
}
}