cgen: fix assignment to nested maps

pull/4417/head
Kris Cherven 2020-04-14 20:42:00 -04:00 committed by GitHub
parent f34352faf9
commit 4dd8796dba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 1 deletions

View File

@ -1710,7 +1710,7 @@ fn (g mut Gen) index_expr(node ast.IndexExpr) {
} else if sym.kind == .map {
info := sym.info as table.Map
elem_type_str := g.typ(info.value_type)
if g.is_assign_lhs {
if g.is_assign_lhs && !g.is_array_set {
g.is_array_set = true
g.write('map_set(')
if !left_is_ptr {

View File

@ -0,0 +1,17 @@
fn test_nested_maps() {
x := map[string]map[string]int
x["a"] = map[string]int
assert x["a"]["b"] == 0
x["a"]["b"] = 5
assert x["a"]["b"] == 5
x["a"]["b"] = 7
assert x["a"]["b"] == 7
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
}