checker: do not allow assigning immutable refs to mutable vars
parent
a552674254
commit
34907f7605
|
@ -1591,6 +1591,25 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) {
|
||||||
}
|
}
|
||||||
return
|
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
|
is_decl := assign_stmt.op == .decl_assign
|
||||||
for i, left in assign_stmt.left {
|
for i, left in assign_stmt.left {
|
||||||
is_blank_ident := left.is_blank_ident()
|
is_blank_ident := left.is_blank_ident()
|
||||||
|
|
Loading…
Reference in New Issue