mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: disallow assigning mutable reference to immutable ParExpr (#18420)
This commit is contained in:
parent
ada702ec66
commit
2ca3046bf8
@ -732,13 +732,18 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
|
|||||||
c.expr(right_node.right)
|
c.expr(right_node.right)
|
||||||
c.inside_ref_lit = old_inside_ref_lit
|
c.inside_ref_lit = old_inside_ref_lit
|
||||||
if right_node.op == .amp {
|
if right_node.op == .amp {
|
||||||
if right_node.right is ast.Ident {
|
expr := if right_node.right is ast.ParExpr {
|
||||||
if right_node.right.obj is ast.Var {
|
right_node.right.expr
|
||||||
v := right_node.right.obj
|
} else {
|
||||||
|
right_node.right
|
||||||
|
}
|
||||||
|
if expr is ast.Ident {
|
||||||
|
if expr.obj is ast.Var {
|
||||||
|
v := expr.obj
|
||||||
right_type0 = v.typ
|
right_type0 = v.typ
|
||||||
}
|
}
|
||||||
if !c.inside_unsafe && assigned_var.is_mut() && !right_node.right.is_mut() {
|
if !c.inside_unsafe && assigned_var.is_mut() && !expr.is_mut() {
|
||||||
c.error('`${right_node.right.name}` is immutable, cannot have a mutable reference to it',
|
c.error('`${expr.name}` is immutable, cannot have a mutable reference to it',
|
||||||
right_node.pos)
|
right_node.pos)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,7 @@
|
|||||||
|
vlib/v/checker/tests/assign_immutable_reference_var_with_parenthesis_err.vv:10:11: error: `a_char` is immutable, cannot have a mutable reference to it
|
||||||
|
8 |
|
||||||
|
9 | fn foo() {
|
||||||
|
10 | mut c := &(a_char)
|
||||||
|
| ^
|
||||||
|
11 | println(c)
|
||||||
|
12 | }
|
@ -0,0 +1,12 @@
|
|||||||
|
struct MyChar {
|
||||||
|
c string
|
||||||
|
}
|
||||||
|
|
||||||
|
const a_char = MyChar{
|
||||||
|
c: 'a'
|
||||||
|
}
|
||||||
|
|
||||||
|
fn foo() {
|
||||||
|
mut c := &(a_char)
|
||||||
|
println(c)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user