diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index a4636be585..a2430204fc 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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 diff --git a/vlib/v/gen/c/assert.v b/vlib/v/gen/c/assert.v index 83420cb660..eba17effde 100644 --- a/vlib/v/gen/c/assert.v +++ b/vlib/v/gen/c/assert.v @@ -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(' {') diff --git a/vlib/v/tests/assert_if_guard_expr_test.v b/vlib/v/tests/assert_if_guard_expr_test.v new file mode 100644 index 0000000000..c0f89bc672 --- /dev/null +++ b/vlib/v/tests/assert_if_guard_expr_test.v @@ -0,0 +1,11 @@ +fn foo() !int { + return error('error') +} + +fn test_assert_if_guard_expr() { + assert if _ := foo() { + false + } else { + true + } +}