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

parser: display correct position on boolean expression error (#8563)

This commit is contained in:
zakuro 2021-02-06 00:52:35 +09:00 committed by GitHub
parent 44ab0154b2
commit 6b776e686e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 11 deletions

View File

@ -17,17 +17,17 @@ vlib/v/checker/tests/infix_err.vv:9:9: error: `+` cannot be used with `?string`
8 | _ = f() + ''
9 | _ = f() + f()
| ^
10 |
10 |
11 | _ = 4 + g()
vlib/v/checker/tests/infix_err.vv:11:7: error: `+` cannot be used with `?int`
9 | _ = f() + f()
10 |
10 |
11 | _ = 4 + g()
| ^
12 | _ = int(0) + g() // FIXME not detected
13 | _ = g() + int(3)
vlib/v/checker/tests/infix_err.vv:12:12: error: unwrapped optional cannot be used in an infix expression
10 |
10 |
11 | _ = 4 + g()
12 | _ = int(0) + g() // FIXME not detected
| ^
@ -45,16 +45,37 @@ vlib/v/checker/tests/infix_err.vv:14:9: error: `+` cannot be used with `?int`
13 | _ = g() + int(3)
14 | _ = g() + 3
| ^
15 |
15 |
16 | // binary operands
vlib/v/checker/tests/infix_err.vv:17:5: error: left operand for `&&` is not a boolean
15 |
15 |
16 | // binary operands
17 | _ = 1 && 2
| ^
18 | _ = true || 2
19 |
vlib/v/checker/tests/infix_err.vv:18:13: error: right operand for `||` is not a boolean
16 | // binary operands
17 | _ = 1 && 2
18 | _ = true || 2
| ^
19 |
20 | // boolean expressions
vlib/v/checker/tests/infix_err.vv:21:22: error: use `()` to make the boolean expression clear
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
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
22 | _ = 1 == 1
23 | && 2 == 2 || 3 == 3
24 | && 4 == 4
| ~~

View File

@ -16,3 +16,9 @@ _ = g() + 3
// binary operands
_ = 1 && 2
_ = true || 2
// boolean expressions
_ = 1 == 1 && 2 == 2 || 3 == 3
_ = 1 == 1
&& 2 == 2 || 3 == 3
&& 4 == 4

View File

@ -397,12 +397,6 @@ fn (mut p Parser) infix_expr(left ast.Expr) ast.Expr {
}
precedence := p.tok.precedence()
mut pos := p.tok.position()
if left.position().line_nr < pos.line_nr {
pos = token.Position{
...pos
line_nr: left.position().line_nr
}
}
p.next()
mut right := ast.Expr{}
prev_expecting_type := p.expecting_type