1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker, cgen: fix for mut i in arr { i = i * i } (#17020)

This commit is contained in:
yuyi 2023-01-18 15:22:38 +08:00 committed by GitHub
parent 1cad788779
commit 6a9688ce9d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 2 deletions

View File

@ -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])) {

View File

@ -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) {

View File

@ -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]
}