1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix map of complex array (#17660)

This commit is contained in:
yuyi 2023-03-18 05:12:59 +08:00 committed by GitHub
parent 24ea00da0c
commit 268cee82fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 2 deletions

View File

@ -123,6 +123,7 @@ mut:
inside_map_infix bool // inside map<</+=/-= infix expr inside_map_infix bool // inside map<</+=/-= infix expr
inside_assign bool inside_assign bool
inside_map_index bool inside_map_index bool
inside_array_index bool
inside_opt_or_res bool inside_opt_or_res bool
inside_opt_data bool inside_opt_data bool
inside_if_option bool inside_if_option bool

View File

@ -202,7 +202,14 @@ fn (mut g Gen) index_of_array(node ast.IndexExpr, sym ast.TypeSymbol) {
g.write('&') g.write('&')
} }
} }
if node.left is ast.IndexExpr {
g.inside_array_index = true
g.expr(node.left) g.expr(node.left)
g.inside_array_index = false
} else {
g.expr(node.left)
}
if node.left_type.has_flag(.shared_f) { if node.left_type.has_flag(.shared_f) {
if left_is_ptr { if left_is_ptr {
g.write('->val') g.write('->val')
@ -393,7 +400,7 @@ fn (mut g Gen) index_of_map(node ast.IndexExpr, sym ast.TypeSymbol) {
g.typ(elem_type.clear_flag(.option).clear_flag(.result)) g.typ(elem_type.clear_flag(.option).clear_flag(.result))
} }
} }
get_and_set_types := elem_sym.kind in [.struct_, .map] get_and_set_types := elem_sym.kind in [.struct_, .map, .array]
if g.is_assign_lhs && !g.is_arraymap_set && !get_and_set_types { if g.is_assign_lhs && !g.is_arraymap_set && !get_and_set_types {
if g.assign_op == .assign || info.value_type == ast.string_type { if g.assign_op == .assign || info.value_type == ast.string_type {
g.is_arraymap_set = true g.is_arraymap_set = true
@ -434,6 +441,7 @@ fn (mut g Gen) index_of_map(node ast.IndexExpr, sym ast.TypeSymbol) {
g.write('${zero} })))') g.write('${zero} })))')
} }
} else if g.inside_map_postfix || g.inside_map_infix || g.inside_map_index } else if g.inside_map_postfix || g.inside_map_infix || g.inside_map_index
|| g.inside_array_index
|| (g.is_assign_lhs && !g.is_arraymap_set && get_and_set_types) { || (g.is_assign_lhs && !g.is_arraymap_set && get_and_set_types) {
zero := g.type_default(info.value_type) zero := g.type_default(info.value_type)
if node.is_setter { if node.is_setter {

View File

@ -0,0 +1,19 @@
struct Instr {
mut:
a int
b int
}
fn test_map_complex_array() {
mut map1 := map[string][]&Instr{}
instr := &Instr{
a: 1
b: 2
}
arr := [instr]
map1['Hello'] = arr
map1['Hello'][0].a = 2
println(map1['Hello'][0].a)
assert map1['Hello'][0].a == 2
assert map1['Hello'][0].b == 2
}