diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 2d6773ed6cd1b3..37d75f54a7368f 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -4671,6 +4671,11 @@ fn (mut c Checker) prefix_expr(mut node ast.PrefixExpr) ast.Type { right_type := c.expr(mut node.right) c.inside_ref_lit = old_inside_ref_lit node.right_type = right_type + mut expr := node.right + // if ParExpr get the innermost expr + for mut expr is ast.ParExpr { + expr = expr.expr + } if node.op == .amp { if node.right is ast.Nil { c.error('invalid operation: cannot take address of nil', node.right.pos()) @@ -4679,11 +4684,9 @@ fn (mut c Checker) prefix_expr(mut node ast.PrefixExpr) ast.Type { if node.right.op == .amp { c.error('unexpected `&`, expecting expression', node.right.pos) } - } else if mut node.right is ast.ParExpr { - if mut node.right.expr is ast.PrefixExpr { - if node.right.expr.op == .amp { - c.error('cannot take the address of this expression', node.right.pos) - } + } else if mut expr is ast.PrefixExpr { + if expr.op == .amp { + c.error('cannot take the address of this expression', expr.pos) } } else if mut node.right is ast.SelectorExpr { if node.right.expr.is_literal() { @@ -4710,11 +4713,6 @@ fn (mut c Checker) prefix_expr(mut node ast.PrefixExpr) ast.Type { // TODO: testing ref/deref strategy right_is_ptr := right_type.is_ptr() if node.op == .amp && (!right_is_ptr || (right_is_ptr && node.right is ast.CallExpr)) { - mut expr := node.right - // if ParExpr get the innermost expr - for mut expr is ast.ParExpr { - expr = expr.expr - } if expr in [ast.BoolLiteral, ast.CallExpr, ast.CharLiteral, ast.FloatLiteral, ast.IntegerLiteral, ast.InfixExpr, ast.StringLiteral, ast.StringInterLiteral] { c.error('cannot take the address of ${expr}', node.pos) diff --git a/vlib/v/checker/tests/addr_of_invalid_expr.out b/vlib/v/checker/tests/addr_of_invalid_expr.out index ecb855a76b9cba..4d0b31dd46450e 100644 --- a/vlib/v/checker/tests/addr_of_invalid_expr.out +++ b/vlib/v/checker/tests/addr_of_invalid_expr.out @@ -1,6 +1,6 @@ -vlib/v/checker/tests/addr_of_invalid_expr.vv:3:8: error: cannot take the address of this expression +vlib/v/checker/tests/addr_of_invalid_expr.vv:3:9: error: cannot take the address of this expression 1 | fn main() { 2 | a := 1 3 | _ := &(&a) - | ~~~~ + | ^ 4 | }