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
if node.op == .amp && !right_type.is_ptr() {
match node.right {
ast.IntegerLiteral { c.error('cannot take the address of an int', node.pos) }
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',
ast.IntegerLiteral { c.error('cannot take the address of an int literal',
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 {}
}
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
| ^
2 | _ := &10
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
2 | _ := &10
| ^
3 | _ := &"Hi"
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
2 | _ := &10
3 | _ := &"Hi"
| ^
4 | _ := &"${b}"
5 |
vlib/v/checker/tests/prefix_err.vv:4:6: error: cannot take the address of a string
5 | _ := &`c`
vlib/v/checker/tests/prefix_err.vv:4:6: error: cannot take the address of a string literal
2 | _ := &10
3 | _ := &"Hi"
4 | _ := &"${b}"
| ^
5 |
6 | a := 1
vlib/v/checker/tests/prefix_err.vv:7:5: error: invalid indirect of `int`
5 |
6 | a := 1
7 | _ = *a
5 | _ := &`c`
6 | _ := 12.3
vlib/v/checker/tests/prefix_err.vv:5:6: error: cannot take the address of a char literal
3 | _ := &"Hi"
4 | _ := &"${b}"
5 | _ := &`c`
| ^
6 | _ := 12.3
7 |
vlib/v/checker/tests/prefix_err.vv:9:5: error: invalid indirect of `int`
7 |
8 | a := 1
9 | _ = *a
| ^
8 | a <- 4
9 |
vlib/v/checker/tests/prefix_err.vv:8:1: error: cannot push on non-channel `int`
6 | a := 1
7 | _ = *a
8 | a <- 4
10 | a <- 4
11 |
vlib/v/checker/tests/prefix_err.vv:10:1: error: cannot push on non-channel `int`
8 | a := 1
9 | _ = *a
10 | a <- 4
| ^
9 |
10 | _ = ~true
vlib/v/checker/tests/prefix_err.vv:10:5: error: operator ~ only defined on int types
8 | a <- 4
9 |
10 | _ = ~true
11 |
12 | _ = ~true
vlib/v/checker/tests/prefix_err.vv:12:5: error: operator ~ only defined on int types
10 | a <- 4
11 |
12 | _ = ~true
| ^
11 | _ = !4
vlib/v/checker/tests/prefix_err.vv:11:5: error: ! operator can only be used with bool types
9 |
10 | _ = ~true
11 | _ = !4
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
_ := &"Hi"
_ := &"${b}"
_ := &`c`
_ := 12.3
a := 1
_ = *a