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

checker: error taking the address of a boolean literal (#7716)

This commit is contained in:
StunxFS 2020-12-30 12:21:15 -04:00 committed by GitHub
parent b4f02adc32
commit 13cd7e88ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 25 deletions

View File

@ -4367,11 +4367,12 @@ pub fn (mut c Checker) prefix_expr(mut node ast.PrefixExpr) table.Type {
node.right_type = right_type
// TODO: testing ref/deref strategy
if node.op == .amp && !right_type.is_ptr() {
if node.right is ast.IntegerLiteral {
c.error('cannot take the address of an int', node.pos)
}
if node.right is ast.StringLiteral || node.right is ast.StringInterLiteral {
c.error('cannot take the address of a string', node.pos)
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',
node.pos) }
else {}
}
if mut node.right is ast.IndexExpr {
typ_sym := c.table.get_type_symbol(node.right.left_type)

View File

@ -1,24 +1,50 @@
vlib/v/checker/tests/prefix_err.vv:2:5: error: invalid indirect of `int`
1 | a := 1
2 | _ = *a
vlib/v/checker/tests/prefix_err.vv:1:6: error: cannot take the address of a bool
1 | b := &true
| ^
2 | _ := &10
3 | _ := &"Hi"
vlib/v/checker/tests/prefix_err.vv:2:6: error: cannot take the address of an int
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
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
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
| ^
3 | a <- 4
4 |
vlib/v/checker/tests/prefix_err.vv:3:1: error: cannot push on non-channel `int`
1 | a := 1
2 | _ = *a
3 | a <- 4
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
| ^
4 |
5 | _ = ~true
vlib/v/checker/tests/prefix_err.vv:5:5: error: operator ~ only defined on int types
3 | a <- 4
4 |
5 | _ = ~true
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
| ^
6 | _ = !4
vlib/v/checker/tests/prefix_err.vv:6:5: error: ! operator can only be used with bool types
4 |
5 | _ = ~true
6 | _ = !4
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
| ^

View File

@ -1,3 +1,8 @@
b := &true
_ := &10
_ := &"Hi"
_ := &"${b}"
a := 1
_ = *a
a <- 4