checker: more errors for illegal referencing (#7755)

pull/7844/head
StunxFS 2021-01-03 10:56:15 -04:00 committed by GitHub
parent 38e0aa350d
commit b7f83e2f50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 31 deletions

View File

@ -4459,10 +4459,13 @@ pub fn (mut c Checker) prefix_expr(mut node ast.PrefixExpr) table.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() {
match node.right { match node.right {
ast.IntegerLiteral { c.error('cannot take the address of an int', node.pos) } ast.IntegerLiteral { c.error('cannot take the address of an int literal',
ast.BoolLiteral { c.error('cannot take the address of a bool', node.pos) }
ast.StringLiteral, ast.StringInterLiteral { c.error('cannot take the address of a string',
node.pos) } node.pos) }
ast.BoolLiteral { c.error('cannot take the address of a bool literal', node.pos) }
ast.StringLiteral, ast.StringInterLiteral { c.error('cannot take the address of a string literal',
node.pos) }
ast.FloatLiteral { c.error('cannot take the address of a float literal', node.pos) }
ast.CharLiteral { c.error('cannot take the address of a char literal', node.pos) }
else {} else {}
} }
if mut node.right is ast.IndexExpr { if mut node.right is ast.IndexExpr {

View File

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

View File

@ -2,6 +2,8 @@ b := &true
_ := &10 _ := &10
_ := &"Hi" _ := &"Hi"
_ := &"${b}" _ := &"${b}"
_ := &`c`
_ := 12.3
a := 1 a := 1
_ = *a _ = *a