From ba9d358d5f170f40e46234ae6b9ad788270189be Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Thu, 9 Jan 2025 13:15:05 -0300 Subject: [PATCH] checker: fix missing check for invalid prefixexpr expression `&(&var)` (fix #23365) (#23418) --- vlib/v/checker/checker.v | 6 ++++++ vlib/v/checker/tests/addr_of_invalid_expr.out | 6 ++++++ vlib/v/checker/tests/addr_of_invalid_expr.vv | 4 ++++ 3 files changed, 16 insertions(+) create mode 100644 vlib/v/checker/tests/addr_of_invalid_expr.out create mode 100644 vlib/v/checker/tests/addr_of_invalid_expr.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 14ee89bf4ed362..2d6773ed6cd1b3 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -4679,6 +4679,12 @@ 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 node.right is ast.SelectorExpr { if node.right.expr.is_literal() { c.error('cannot take the address of a literal value', node.pos.extend(node.right.pos)) diff --git a/vlib/v/checker/tests/addr_of_invalid_expr.out b/vlib/v/checker/tests/addr_of_invalid_expr.out new file mode 100644 index 00000000000000..ecb855a76b9cba --- /dev/null +++ b/vlib/v/checker/tests/addr_of_invalid_expr.out @@ -0,0 +1,6 @@ +vlib/v/checker/tests/addr_of_invalid_expr.vv:3:8: error: cannot take the address of this expression + 1 | fn main() { + 2 | a := 1 + 3 | _ := &(&a) + | ~~~~ + 4 | } diff --git a/vlib/v/checker/tests/addr_of_invalid_expr.vv b/vlib/v/checker/tests/addr_of_invalid_expr.vv new file mode 100644 index 00000000000000..ac2c5d62c497bc --- /dev/null +++ b/vlib/v/checker/tests/addr_of_invalid_expr.vv @@ -0,0 +1,4 @@ +fn main() { + a := 1 + _ := &(&a) +}