checker: change wording of the error message for complex boolean expressions

pull/9739/head
Delyan Angelov 2021-04-14 19:55:26 +03:00
parent 7071e8b682
commit 345868853e
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 6 additions and 7 deletions

View File

@ -1043,11 +1043,11 @@ pub fn (mut c Checker) infix_expr(mut infix_expr ast.InfixExpr) ast.Type {
if infix_expr.right_type != ast.bool_type_idx {
c.error('right operand for `$infix_expr.op` is not a boolean', infix_expr.right.position())
}
// use `()` to make the boolean expression clear error
// for example: `(a && b) || c` instead of `a && b || c`
if mut infix_expr.left is ast.InfixExpr {
if infix_expr.left.op != infix_expr.op && infix_expr.left.op in [.logical_or, .and] {
c.error('use `()` to make the boolean expression clear', infix_expr.pos)
// for example: `(a && b) || c` instead of `a && b || c`
c.error('ambiguous boolean expression. use `()` to ensure correct order of operations',
infix_expr.pos)
}
}
}

View File

@ -61,22 +61,21 @@ vlib/v/checker/tests/infix_err.vv:18:13: error: right operand for `||` is not a
| ^
19 |
20 | // boolean expressions
vlib/v/checker/tests/infix_err.vv:21:22: error: use `()` to make the boolean expression clear
vlib/v/checker/tests/infix_err.vv:21:22: error: ambiguous boolean expression. use `()` to ensure correct order of operations
19 |
20 | // boolean expressions
21 | _ = 1 == 1 && 2 == 2 || 3 == 3
| ~~
22 | _ = 1 == 1
23 | && 2 == 2 || 3 == 3
vlib/v/checker/tests/infix_err.vv:23:12: error: use `()` to make the boolean expression clear
vlib/v/checker/tests/infix_err.vv:23:12: error: ambiguous boolean expression. use `()` to ensure correct order of operations
21 | _ = 1 == 1 && 2 == 2 || 3 == 3
22 | _ = 1 == 1
23 | && 2 == 2 || 3 == 3
| ~~
24 | && 4 == 4
vlib/v/checker/tests/infix_err.vv:24:2: error: use `()` to make the boolean expression clear
vlib/v/checker/tests/infix_err.vv:24:2: error: ambiguous boolean expression. use `()` to ensure correct order of operations
22 | _ = 1 == 1
23 | && 2 == 2 || 3 == 3
24 | && 4 == 4
| ~~