checker: add error for amp on literals (#6467)

pull/6473/head
Daniel Däschle 2020-09-24 21:13:46 +02:00 committed by GitHub
parent 47a62b12d4
commit e384dea8ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 2 deletions

View File

@ -2670,6 +2670,12 @@ pub fn (mut c Checker) expr(node ast.Expr) 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() {
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)
}
return right_type.to_ptr() return right_type.to_ptr()
} }
if node.op == .mul { if node.op == .mul {
@ -2678,7 +2684,7 @@ pub fn (mut c Checker) expr(node ast.Expr) table.Type {
} }
if !right_type.is_pointer() { if !right_type.is_pointer() {
s := c.table.type_to_str(right_type) s := c.table.type_to_str(right_type)
c.error('prefix operator `*` not defined for type `$s`', node.pos) c.error('invalid indirect of `$s`', node.pos)
} }
} }
if node.op == .bit_not && !right_type.is_int() && !c.pref.translated { if node.op == .bit_not && !right_type.is_int() && !c.pref.translated {

View File

@ -1,4 +1,4 @@
vlib/v/checker/tests/prefix_err.vv:2:5: error: prefix operator `*` not defined for type `int` vlib/v/checker/tests/prefix_err.vv:2:5: error: invalid indirect of `int`
1 | a := 1 1 | a := 1
2 | _ = *a 2 | _ = *a
| ^ | ^