From 3b31699a0d65489e253ff1752a58919d22ababce Mon Sep 17 00:00:00 2001 From: Swastik Baranwal Date: Fri, 10 Jan 2025 14:52:16 +0530 Subject: [PATCH] checker: disallow `&((&a))` and similar expressions, with innermost `ast.PrefixExpr` (enhance #23418) (#23419) --- vlib/v/checker/checker.v | 18 ++++++++---------- vlib/v/checker/tests/addr_of_invalid_expr.out | 4 ++-- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 2d6773ed6..37d75f54a 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 ecb855a76..4d0b31dd4 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 | } -- 2.39.5