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

checker: support ParExpr for trying to take address errors & simplify messages

This commit is contained in:
Joe Conigliaro 2021-02-13 02:49:22 +11:00
parent 1675b6f3e0
commit c904c9178d
No known key found for this signature in database
GPG Key ID: C12F7136C08206F1
3 changed files with 104 additions and 67 deletions

View File

@ -5125,25 +5125,15 @@ pub fn (mut c Checker) prefix_expr(mut node ast.PrefixExpr) table.Type {
node.right_type = right_type node.right_type = right_type
// TODO: testing ref/deref strategy // TODO: testing ref/deref strategy
if node.op == .amp && !right_type.is_ptr() { if node.op == .amp && !right_type.is_ptr() {
right_expr := node.right mut expr := node.right
match right_expr { // if ParExpr get the innermost expr
ast.BoolLiteral { for mut expr is ast.ParExpr {
c.error('cannot take the address of a bool literal', node.pos) expr = expr.expr
} }
ast.CallExpr { match expr {
c.error('cannot take the address of $node.right', node.pos) ast.BoolLiteral, ast.CallExpr, ast.CharLiteral, ast.FloatLiteral, ast.IntegerLiteral,
} ast.InfixExpr, ast.StringLiteral, ast.StringInterLiteral {
ast.CharLiteral { c.error('cannot take the address of $expr', node.pos)
c.error('cannot take the address of a char literal', node.pos)
}
ast.FloatLiteral {
c.error('cannot take the address of a float literal', node.pos)
}
ast.IntegerLiteral {
c.error('cannot take the address of an int literal', node.pos)
}
ast.StringLiteral, ast.StringInterLiteral {
c.error('cannot take the address of a string literal', node.pos)
} }
else {} else {}
} }

View File

@ -1,57 +1,95 @@
vlib/v/checker/tests/prefix_err.vv:1:6: error: cannot take the address of a bool literal vlib/v/checker/tests/prefix_err.vv:5:6: error: cannot take the address of true
1 | b := &true 3 | }
4 |
5 | b := &true
| ^ | ^
2 | _ := &10 6 | _ := &get()
3 | _ := &"Hi" 7 | _ := &(get())
vlib/v/checker/tests/prefix_err.vv:2:6: error: cannot take the address of an int literal vlib/v/checker/tests/prefix_err.vv:6:6: error: cannot take the address of get()
1 | b := &true 4 |
2 | _ := &10 5 | b := &true
6 | _ := &get()
| ^ | ^
3 | _ := &"Hi" 7 | _ := &(get())
4 | _ := &"${b}" 8 | _ := &(get() + 1)
vlib/v/checker/tests/prefix_err.vv:3:6: error: cannot take the address of a string literal vlib/v/checker/tests/prefix_err.vv:7:6: error: cannot take the address of get()
1 | b := &true 5 | b := &true
2 | _ := &10 6 | _ := &get()
3 | _ := &"Hi" 7 | _ := &(get())
| ^ | ^
4 | _ := &"${b}" 8 | _ := &(get() + 1)
5 | _ := &`c` 9 | _ := &10
vlib/v/checker/tests/prefix_err.vv:4:6: error: cannot take the address of a string literal vlib/v/checker/tests/prefix_err.vv:8:6: error: cannot take the address of get() + 1
2 | _ := &10 6 | _ := &get()
3 | _ := &"Hi" 7 | _ := &(get())
4 | _ := &"${b}" 8 | _ := &(get() + 1)
| ^ | ^
5 | _ := &`c` 9 | _ := &10
6 | _ := 12.3 10 | _ := &"Hi"
vlib/v/checker/tests/prefix_err.vv:5:6: error: cannot take the address of a char literal vlib/v/checker/tests/prefix_err.vv:9:6: error: cannot take the address of 10
3 | _ := &"Hi" 7 | _ := &(get())
4 | _ := &"${b}" 8 | _ := &(get() + 1)
5 | _ := &`c` 9 | _ := &10
| ^ | ^
6 | _ := 12.3 10 | _ := &"Hi"
7 | 11 | _ := &"${b}"
vlib/v/checker/tests/prefix_err.vv:9:5: error: invalid indirect of `int` vlib/v/checker/tests/prefix_err.vv:10:6: error: cannot take the address of "Hi"
7 | 8 | _ := &(get() + 1)
8 | a := 1 9 | _ := &10
9 | _ = *a 10 | _ := &"Hi"
| ^
11 | _ := &"${b}"
12 | _ := &`c`
vlib/v/checker/tests/prefix_err.vv:11:6: error: cannot take the address of '$b'
9 | _ := &10
10 | _ := &"Hi"
11 | _ := &"${b}"
| ^
12 | _ := &`c`
13 | _ := &1.2
vlib/v/checker/tests/prefix_err.vv:12:6: error: cannot take the address of `c`
10 | _ := &"Hi"
11 | _ := &"${b}"
12 | _ := &`c`
| ^
13 | _ := &1.2
14 | _ := &(1 + 2)
vlib/v/checker/tests/prefix_err.vv:13:6: error: cannot take the address of 1.2
11 | _ := &"${b}"
12 | _ := &`c`
13 | _ := &1.2
| ^
14 | _ := &(1 + 2)
15 | _ := 12.3
vlib/v/checker/tests/prefix_err.vv:14:6: error: cannot take the address of 1 + 2
12 | _ := &`c`
13 | _ := &1.2
14 | _ := &(1 + 2)
| ^
15 | _ := 12.3
16 |
vlib/v/checker/tests/prefix_err.vv:18:5: error: invalid indirect of `int`
16 |
17 | a := 1
18 | _ = *a
| ^ | ^
10 | a <- 4 19 | a <- 4
11 | 20 |
vlib/v/checker/tests/prefix_err.vv:10:1: error: cannot push on non-channel `int` vlib/v/checker/tests/prefix_err.vv:19:1: error: cannot push on non-channel `int`
8 | a := 1 17 | a := 1
9 | _ = *a 18 | _ = *a
10 | a <- 4 19 | a <- 4
| ^ | ^
11 | 20 |
12 | _ = ~true 21 | _ = ~true
vlib/v/checker/tests/prefix_err.vv:12:5: error: operator ~ only defined on int types vlib/v/checker/tests/prefix_err.vv:21:5: error: operator ~ only defined on int types
10 | a <- 4 19 | a <- 4
11 | 20 |
12 | _ = ~true 21 | _ = ~true
| ^ | ^
13 | _ = !4 22 | _ = !4
vlib/v/checker/tests/prefix_err.vv:13:5: error: ! operator can only be used with bool types vlib/v/checker/tests/prefix_err.vv:22:5: error: ! operator can only be used with bool types
11 | 20 |
12 | _ = ~true 21 | _ = ~true
13 | _ = !4 22 | _ = !4
| ^ | ^

View File

@ -1,8 +1,17 @@
fn get() int {
return 1
}
b := &true b := &true
_ := &get()
_ := &(get())
_ := &(get() + 1)
_ := &10 _ := &10
_ := &"Hi" _ := &"Hi"
_ := &"${b}" _ := &"${b}"
_ := &`c` _ := &`c`
_ := &1.2
_ := &(1 + 2)
_ := 12.3 _ := 12.3
a := 1 a := 1