cgen: fix for in mut reference selector val (fix #10524) (#10536)

pull/10540/head
yuyi 2021-06-22 00:25:18 +08:00 committed by GitHub
parent da4b6b934d
commit 6dbe8a8a2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 11 deletions

View File

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

View File

@ -3492,7 +3492,15 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
}
}
}
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)')
}

View File

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