Skip to content

Commit 6a9688c

Browse files
authored
checker, cgen: fix for mut i in arr { i = i * i } (#17020)
1 parent 1cad788 commit 6a9688c

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

vlib/v/checker/infix.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type {
6969
c.error('invalid number of operand for `${node.op}`. Only one allowed on each side.',
7070
left_right_pos)
7171
}
72-
if left_type.is_any_kind_of_pointer()
72+
if left_type.is_any_kind_of_pointer() && !node.left.is_auto_deref_var()
7373
&& node.op in [.plus, .minus, .mul, .div, .mod, .xor, .amp, .pipe] {
7474
if !c.pref.translated && ((right_type.is_any_kind_of_pointer() && node.op != .minus)
7575
|| (!right_type.is_any_kind_of_pointer() && node.op !in [.plus, .minus])) {

vlib/v/gen/c/infix.v

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,12 @@ fn (mut g Gen) gen_plain_infix_expr(node ast.InfixExpr) {
966966
}
967967
g.expr(node.left)
968968
g.write(' ${node.op.str()} ')
969-
g.expr_with_cast(node.right, node.right_type, node.left_type)
969+
if node.right_type.is_ptr() && node.right.is_auto_deref_var() {
970+
g.write('*')
971+
g.expr(node.right)
972+
} else {
973+
g.expr_with_cast(node.right, node.right_type, node.left_type)
974+
}
970975
}
971976

972977
fn (mut g Gen) op_arg(expr ast.Expr, expected ast.Type, got ast.Type) {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn test_for_in_mut_array_with_mul() {
2+
mut ints_a := [1, 2]
3+
for mut i in ints_a {
4+
i = i * i
5+
}
6+
print('${ints_a}')
7+
assert ints_a == [1, 4]
8+
}

0 commit comments

Comments
 (0)