cgen: fix multiple assign array index (#10926)

pull/10932/head
yuyi 2021-07-23 20:46:18 +08:00 committed by GitHub
parent 26db3b0995
commit f5776eb259
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 8 deletions

View File

@ -2230,6 +2230,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
}
mut return_type := ast.void_type
is_decl := assign_stmt.op == .decl_assign
g.assign_op = assign_stmt.op
op := if is_decl { token.Kind.assign } else { assign_stmt.op }
right_expr := assign_stmt.right[0]
match right_expr {
@ -2359,21 +2360,41 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
}
g.expr(lx)
noscan := if is_auto_heap { g.check_noscan(return_type) } else { '' }
if is_opt {
mr_base_styp := g.base_type(return_type)
if is_auto_heap {
g.writeln(' = HEAP${noscan}($mr_base_styp, *($mr_base_styp*)${mr_var_name}.data).arg$i);')
if g.is_arraymap_set {
if is_opt {
mr_base_styp := g.base_type(return_type)
if is_auto_heap {
g.writeln('HEAP${noscan}($mr_base_styp, *($mr_base_styp*)${mr_var_name}.data).arg$i) });')
} else {
g.writeln('(*($mr_base_styp*)${mr_var_name}.data).arg$i });')
}
} else {
g.writeln(' = (*($mr_base_styp*)${mr_var_name}.data).arg$i;')
if is_auto_heap {
g.writeln('HEAP${noscan}($styp, ${mr_var_name}.arg$i) });')
} else {
g.writeln('${mr_var_name}.arg$i });')
}
}
} else {
if is_auto_heap {
g.writeln(' = HEAP${noscan}($styp, ${mr_var_name}.arg$i);')
if is_opt {
mr_base_styp := g.base_type(return_type)
if is_auto_heap {
g.writeln(' = HEAP${noscan}($mr_base_styp, *($mr_base_styp*)${mr_var_name}.data).arg$i);')
} else {
g.writeln(' = (*($mr_base_styp*)${mr_var_name}.data).arg$i;')
}
} else {
g.writeln(' = ${mr_var_name}.arg$i;')
if is_auto_heap {
g.writeln(' = HEAP${noscan}($styp, ${mr_var_name}.arg$i);')
} else {
g.writeln(' = ${mr_var_name}.arg$i;')
}
}
}
}
if g.is_arraymap_set {
g.is_arraymap_set = false
}
return
}
}

View File

@ -0,0 +1,24 @@
fn round(a int, b int, c int) (int, int, int) {
mut ax := a
mut bx := b
mut cx := c
ax += bx
bx ^= cx
cx -= 1
return ax, bx, cx
}
fn round_assignment(mut res []int) {
_ = res[2]
res[0], res[1], res[2] = round(res[0], res[1], res[2])
}
fn test_multiple_assign_array_index() {
mut a := []int{len: 3}
round_assignment(mut a)
println(a)
assert a == [0, 0, -1]
}