mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix struct field array index error (#14417)
This commit is contained in:
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`:
|
||||
// `(*(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 {
|
||||
|
17
vlib/v/tests/struct_field_array_index_test.v
Normal file
17
vlib/v/tests/struct_field_array_index_test.v
Normal 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']
|
||||
}
|
Loading…
Reference in New Issue
Block a user