cgen: fix struct field array index error (#14417)
parent
e5c7fe3006
commit
c624de8523
|
@ -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 {
|
||||||
|
|
|
@ -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']
|
||||||
|
}
|
Loading…
Reference in New Issue