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:
parent
5e48817dc8
commit
da153aa780
@ -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
|
||||
|
@ -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(' {')
|
||||
|
11
vlib/v/tests/assert_if_guard_expr_test.v
Normal file
11
vlib/v/tests/assert_if_guard_expr_test.v
Normal file
@ -0,0 +1,11 @@
|
||||
fn foo() !int {
|
||||
return error('error')
|
||||
}
|
||||
|
||||
fn test_assert_if_guard_expr() {
|
||||
assert if _ := foo() {
|
||||
false
|
||||
} else {
|
||||
true
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user