diff --git a/vlib/v/checker/assign.v b/vlib/v/checker/assign.v index 586600a03a013b..04101c2b2c76bb 100644 --- a/vlib/v/checker/assign.v +++ b/vlib/v/checker/assign.v @@ -732,13 +732,18 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) { c.expr(right_node.right) c.inside_ref_lit = old_inside_ref_lit if right_node.op == .amp { - if right_node.right is ast.Ident { - if right_node.right.obj is ast.Var { - v := right_node.right.obj + expr := if right_node.right is ast.ParExpr { + right_node.right.expr + } else { + right_node.right + } + if expr is ast.Ident { + if expr.obj is ast.Var { + v := expr.obj right_type0 = v.typ } - if !c.inside_unsafe && assigned_var.is_mut() && !right_node.right.is_mut() { - c.error('`${right_node.right.name}` is immutable, cannot have a mutable reference to it', + if !c.inside_unsafe && assigned_var.is_mut() && !expr.is_mut() { + c.error('`${expr.name}` is immutable, cannot have a mutable reference to it', right_node.pos) } } diff --git a/vlib/v/checker/tests/assign_immutable_reference_var_with_parenthesis_err.out b/vlib/v/checker/tests/assign_immutable_reference_var_with_parenthesis_err.out new file mode 100644 index 00000000000000..be8c568c1d33df --- /dev/null +++ b/vlib/v/checker/tests/assign_immutable_reference_var_with_parenthesis_err.out @@ -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 | } diff --git a/vlib/v/checker/tests/assign_immutable_reference_var_with_parenthesis_err.vv b/vlib/v/checker/tests/assign_immutable_reference_var_with_parenthesis_err.vv new file mode 100644 index 00000000000000..f92707982b724f --- /dev/null +++ b/vlib/v/checker/tests/assign_immutable_reference_var_with_parenthesis_err.vv @@ -0,0 +1,12 @@ +struct MyChar { + c string +} + +const a_char = MyChar{ + c: 'a' +} + +fn foo() { + mut c := &(a_char) + println(c) +}