Skip to content

Commit

Permalink
checker, cgen: fix for mut i in arr { i = i * i } (#17020)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Jan 18, 2023
1 parent 1cad788 commit 6a9688c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion vlib/v/checker/infix.v
Expand Up @@ -69,7 +69,7 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type {
c.error('invalid number of operand for `${node.op}`. Only one allowed on each side.',
left_right_pos)
}
if left_type.is_any_kind_of_pointer()
if left_type.is_any_kind_of_pointer() && !node.left.is_auto_deref_var()
&& node.op in [.plus, .minus, .mul, .div, .mod, .xor, .amp, .pipe] {
if !c.pref.translated && ((right_type.is_any_kind_of_pointer() && node.op != .minus)
|| (!right_type.is_any_kind_of_pointer() && node.op !in [.plus, .minus])) {
Expand Down
7 changes: 6 additions & 1 deletion vlib/v/gen/c/infix.v
Expand Up @@ -966,7 +966,12 @@ fn (mut g Gen) gen_plain_infix_expr(node ast.InfixExpr) {
}
g.expr(node.left)
g.write(' ${node.op.str()} ')
g.expr_with_cast(node.right, node.right_type, node.left_type)
if node.right_type.is_ptr() && node.right.is_auto_deref_var() {
g.write('*')
g.expr(node.right)
} else {
g.expr_with_cast(node.right, node.right_type, node.left_type)
}
}

fn (mut g Gen) op_arg(expr ast.Expr, expected ast.Type, got ast.Type) {
Expand Down
8 changes: 8 additions & 0 deletions vlib/v/tests/for_in_mut_array_with_mul_test.v
@@ -0,0 +1,8 @@
fn test_for_in_mut_array_with_mul() {
mut ints_a := [1, 2]
for mut i in ints_a {
i = i * i
}
print('${ints_a}')
assert ints_a == [1, 4]
}

0 comments on commit 6a9688c

Please sign in to comment.