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

cgen: fix setting nested map elements fields (#6744)

This commit is contained in:
Ned Palacios 2020-11-06 22:32:44 +08:00 committed by GitHub
parent e6828560d1
commit 6d8a7ced24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 21 deletions

View File

@ -3512,7 +3512,8 @@ fn (mut g Gen) index_expr(node ast.IndexExpr) {
info := sym.info as table.Map info := sym.info as table.Map
elem_type_str := g.typ(info.value_type) elem_type_str := g.typ(info.value_type)
elem_typ := g.table.get_type_symbol(info.value_type) elem_typ := g.table.get_type_symbol(info.value_type)
if g.is_assign_lhs && !g.is_array_set && elem_typ.kind != .struct_ { get_and_set_types := elem_typ.kind in [.struct_, .map]
if g.is_assign_lhs && !g.is_array_set && !get_and_set_types {
g.is_array_set = true g.is_array_set = true
g.write('map_set(') g.write('map_set(')
if !left_is_ptr { if !left_is_ptr {
@ -3527,7 +3528,7 @@ fn (mut g Gen) index_expr(node ast.IndexExpr) {
g.write(', &($elem_type_str[]) { ') g.write(', &($elem_type_str[]) { ')
} }
} else if (g.inside_map_postfix || g.inside_map_infix) || } else if (g.inside_map_postfix || g.inside_map_infix) ||
(g.is_assign_lhs && !g.is_array_set && elem_typ.kind == .struct_) { (g.is_assign_lhs && !g.is_array_set && get_and_set_types) {
zero := g.type_default(info.value_type) zero := g.type_default(info.value_type)
g.write('(*($elem_type_str*)map_get_and_set(') g.write('(*($elem_type_str*)map_get_and_set(')
if !left_is_ptr { if !left_is_ptr {

View File

@ -1,20 +1,32 @@
fn test_nested_maps() { struct Foo {
if true{} mut:
// name string
else{} }
mut x := map[string]map[string]int
x["a"] = map[string]int fn test_nested_maps() {
assert x["a"]["b"] == 0 if true {
x["a"]["b"] = 5 }
assert x["a"]["b"] == 5 //
x["a"]["b"] = 7 else {
assert x["a"]["b"] == 7 }
mut y := map[string]map[string]map[string]int mut x := map[string]map[string]int{}
y["a"] = map[string]map[string]int x['a'] = map[string]int{}
y["a"]["b"] = map[string]int assert x['a']['b'] == 0
assert y["a"]["b"]["c"] == 0 x['a']['b'] = 5
y["a"]["b"]["c"] = 5 assert x['a']['b'] == 5
assert y["a"]["b"]["c"] == 5 x['a']['b'] = 7
y["a"]["b"]["c"] = 7 assert x['a']['b'] == 7
assert y["a"]["b"]["c"] == 7 mut y := map[string]map[string]map[string]int{}
y['a'] = map[string]map[string]int{}
y['a']['b'] = map[string]int{}
assert y['a']['b']['c'] == 0
y['a']['b']['c'] = 5
assert y['a']['b']['c'] == 5
y['a']['b']['c'] = 7
assert y['a']['b']['c'] == 7
mut foos := map[string]map[string]Foo{}
foos['a']['b'] = Foo{'bar'}
assert foos['a']['b'].name == 'bar'
foos['a']['b'].name = 'baz'
assert foos['a']['b'].name == 'baz'
} }