Skip to content

Commit

Permalink
cgen: fix for in mut reference selector val (fix #10524) (#10536)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Jun 21, 2021
1 parent da4b6b9 commit 6dbe8a8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
16 changes: 6 additions & 10 deletions vlib/v/gen/c/array.v
Expand Up @@ -279,14 +279,10 @@ fn (mut g Gen) gen_array_sort(node ast.CallExpr) {
g.definitions.writeln('\tif (${styp}__lt(*b, *a)) { return -1; } else { return 1; }}')
} else {
field_type := g.typ(infix_expr.left_type)
mut left_expr_str := g.write_expr_to_string(infix_expr.left)
mut right_expr_str := g.write_expr_to_string(infix_expr.right)
if typ.is_ptr() {
left_expr_str = left_expr_str.replace_once('a', '(*a)')
right_expr_str = right_expr_str.replace_once('b', '(*b)')
}
g.definitions.writeln('$field_type a_ = $left_expr_str;')
g.definitions.writeln('$field_type b_ = $right_expr_str;')
left_expr_str := g.write_expr_to_string(infix_expr.left)
right_expr_str := g.write_expr_to_string(infix_expr.right)
g.definitions.writeln('\t$field_type a_ = $left_expr_str;')
g.definitions.writeln('\t$field_type b_ = $right_expr_str;')
mut op1, mut op2 := '', ''
if infix_expr.left_type == ast.string_type {
if is_reverse {
Expand All @@ -306,8 +302,8 @@ fn (mut g Gen) gen_array_sort(node ast.CallExpr) {
op2 = '${deref_str}a_ > ${deref_str}b_'
}
}
g.definitions.writeln('if ($op1) return -1;')
g.definitions.writeln('if ($op2) return 1; else return 0; }\n')
g.definitions.writeln('\tif ($op1) return -1;')
g.definitions.writeln('\tif ($op2) return 1; \n\telse return 0; \n}\n')
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion vlib/v/gen/c/cgen.v
Expand Up @@ -3492,7 +3492,15 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
}
}
}
g.expr(node.expr)
n_ptr := node.expr_type.nr_muls() - 1
if n_ptr > 0 {
g.write('(')
g.write('*'.repeat(n_ptr))
g.expr(node.expr)
g.write(')')
} else {
g.expr(node.expr)
}
if is_optional {
g.write('.data)')
}
Expand Down
26 changes: 26 additions & 0 deletions vlib/v/tests/for_in_mut_reference_selector_val_test.v
@@ -0,0 +1,26 @@
pub struct AA {
id string
}

pub struct BB {
pub mut:
arr []&AA
}

fn test_for_in_mut_reference_selector_val() {
bb := BB{
arr: [&AA{
id: 'Test1'
}, &AA{
id: 'Test2'
}]
}

mut ret := []string{}
for mut aa in bb.arr {
println(aa.id)
ret << aa.id
}
println(ret)
assert ret == ['Test1', 'Test2']
}

0 comments on commit 6dbe8a8

Please sign in to comment.