From e384dea8ac7d21d4c8aafecf4f0b84fb24d153b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20D=C3=A4schle?= Date: Thu, 24 Sep 2020 21:13:46 +0200 Subject: [PATCH] checker: add error for amp on literals (#6467) --- vlib/v/checker/checker.v | 8 +++++++- vlib/v/checker/tests/prefix_err.out | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 6cb77519ea..6b69112689 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2670,6 +2670,12 @@ pub fn (mut c Checker) expr(node ast.Expr) 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) + } return right_type.to_ptr() } if node.op == .mul { @@ -2678,7 +2684,7 @@ pub fn (mut c Checker) expr(node ast.Expr) table.Type { } if !right_type.is_pointer() { 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 { diff --git a/vlib/v/checker/tests/prefix_err.out b/vlib/v/checker/tests/prefix_err.out index 9763f32e51..c82b31df5a 100644 --- a/vlib/v/checker/tests/prefix_err.out +++ b/vlib/v/checker/tests/prefix_err.out @@ -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 2 | _ = *a | ^