cgen: fix struct field array index error (#14417)

yuyi 2022-05-17 00:37:39 +08:00 committed by Jef Roosens
parent 1064085656
commit 57055a9c63
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
2 changed files with 18 additions and 2 deletions

View File

@ -172,8 +172,7 @@ fn (mut g Gen) index_of_array(node ast.IndexExpr, sym ast.TypeSymbol) {
}
// `vals[i].field = x` is an exception and requires `array_get`:
// `(*(Val*)array_get(vals, i)).field = x;`
is_selector := node.left is ast.SelectorExpr
if g.is_assign_lhs && !is_selector && node.is_setter {
if g.is_assign_lhs && node.is_setter {
is_direct_array_access := (g.fn_decl != 0 && g.fn_decl.is_direct_arr) || node.is_direct
is_op_assign := g.assign_op != .assign && info.elem_type != ast.string_type
if is_direct_array_access {

View File

@ -0,0 +1,17 @@
struct App {
mut:
buffer []string
}
fn test_struct_field_array_index() {
mut app := &App{
buffer: []string{len: 2}
}
app.buffer[0] += 'hello'
app.buffer[1] += 'world'
println(app)
assert app.buffer == ['hello', 'world']
}