Skip to content

Commit 3b31699

Browse files
authored
checker: disallow &((&a)) and similar expressions, with innermost ast.PrefixExpr (enhance #23418) (#23419)
1 parent ba9d358 commit 3b31699

2 files changed

Lines changed: 10 additions & 12 deletions

File tree

‎vlib/v/checker/checker.v‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4671,6 +4671,11 @@ fn (mut c Checker) prefix_expr(mut node ast.PrefixExpr) ast.Type {
46714671
right_type := c.expr(mut node.right)
46724672
c.inside_ref_lit = old_inside_ref_lit
46734673
node.right_type = right_type
4674+
mut expr := node.right
4675+
// if ParExpr get the innermost expr
4676+
for mut expr is ast.ParExpr {
4677+
expr = expr.expr
4678+
}
46744679
if node.op == .amp {
46754680
if node.right is ast.Nil {
46764681
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 {
46794684
if node.right.op == .amp {
46804685
c.error('unexpected `&`, expecting expression', node.right.pos)
46814686
}
4682-
} else if mut node.right is ast.ParExpr {
4683-
if mut node.right.expr is ast.PrefixExpr {
4684-
if node.right.expr.op == .amp {
4685-
c.error('cannot take the address of this expression', node.right.pos)
4686-
}
4687+
} else if mut expr is ast.PrefixExpr {
4688+
if expr.op == .amp {
4689+
c.error('cannot take the address of this expression', expr.pos)
46874690
}
46884691
} else if mut node.right is ast.SelectorExpr {
46894692
if node.right.expr.is_literal() {
@@ -4710,11 +4713,6 @@ fn (mut c Checker) prefix_expr(mut node ast.PrefixExpr) ast.Type {
47104713
// TODO: testing ref/deref strategy
47114714
right_is_ptr := right_type.is_ptr()
47124715
if node.op == .amp && (!right_is_ptr || (right_is_ptr && node.right is ast.CallExpr)) {
4713-
mut expr := node.right
4714-
// if ParExpr get the innermost expr
4715-
for mut expr is ast.ParExpr {
4716-
expr = expr.expr
4717-
}
47184716
if expr in [ast.BoolLiteral, ast.CallExpr, ast.CharLiteral, ast.FloatLiteral, ast.IntegerLiteral,
47194717
ast.InfixExpr, ast.StringLiteral, ast.StringInterLiteral] {
47204718
c.error('cannot take the address of ${expr}', node.pos)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
vlib/v/checker/tests/addr_of_invalid_expr.vv:3:8: error: cannot take the address of this expression
1+
vlib/v/checker/tests/addr_of_invalid_expr.vv:3:9: error: cannot take the address of this expression
22
1 | fn main() {
33
2 | a := 1
44
3 | _ := &(&a)
5-
| ~~~~
5+
| ^
66
4 | }

0 commit comments

Comments
 (0)