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

master
yuyi 2022-05-17 00:37:39 +08:00 committed by GitHub
parent e5c7fe3006
commit c624de8523
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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`: // `vals[i].field = x` is an exception and requires `array_get`:
// `(*(Val*)array_get(vals, i)).field = x;` // `(*(Val*)array_get(vals, i)).field = x;`
is_selector := node.left is ast.SelectorExpr if g.is_assign_lhs && node.is_setter {
if g.is_assign_lhs && !is_selector && node.is_setter {
is_direct_array_access := (g.fn_decl != 0 && g.fn_decl.is_direct_arr) || node.is_direct 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 is_op_assign := g.assign_op != .assign && info.elem_type != ast.string_type
if is_direct_array_access { 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']
}