diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index acf94e6a0c..b594d03ff3 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1591,6 +1591,25 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) { } return } + // Check `x := &y` + if right_first is ast.PrefixExpr { + node := right_first + left_first := assign_stmt.left[0] + if node.op == .amp && node.right is ast.Ident { + ident := node.right as ast.Ident + scope := c.file.scope.innermost(node.pos.pos) + if v := scope.find_var(ident.name) { + if left_first is ast.Ident { + assigned_var := left_first + if !v.is_mut && assigned_var.is_mut { + c.error('`$ident.name` is immutable, cannot have a mutable reference to it', + node.pos) + } + } + } + } + } + // is_decl := assign_stmt.op == .decl_assign for i, left in assign_stmt.left { is_blank_ident := left.is_blank_ident()