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

cgen: fix complex map_fixed_array (#8209)

This commit is contained in:
yuyi 2021-01-21 19:45:37 +08:00 committed by GitHub
parent f13ba3a9a8
commit 59c3e98c16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 5 deletions

View File

@ -1950,6 +1950,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
right_sym := g.table.get_type_symbol(val_type)
is_fixed_array_copy := right_sym.kind == .array_fixed && val is ast.Ident
g.is_assign_lhs = true
g.assign_op = assign_stmt.op
if is_interface && right_sym.kind == .interface_ {
is_interface = false
}
@ -1969,25 +1970,42 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
if right.has_val {
for j, expr in right.exprs {
g.expr(left)
g.write('[$j] = ')
if g.is_array_set {
g.out.go_back(2)
} else {
g.write('[$j] = ')
}
g.expr(expr)
g.writeln(';')
if g.is_array_set {
g.writeln(')')
g.is_array_set = false
} else {
g.writeln(';')
}
}
} else {
fixed_array := right_sym.info as table.ArrayFixed
for j in 0 .. fixed_array.size {
g.expr(left)
g.write('[$j] = ')
if g.is_array_set {
g.out.go_back(2)
} else {
g.write('[$j] = ')
}
if right.has_default {
g.expr(right.default_expr)
} else {
g.write(g.type_default(right.elem_type))
}
g.writeln(';')
if g.is_array_set {
g.writeln(')')
g.is_array_set = false
} else {
g.writeln(';')
}
}
}
} else {
g.assign_op = assign_stmt.op
is_inside_ternary := g.inside_ternary != 0
cur_line := if is_inside_ternary && is_decl {
g.register_ternary_name(ident.name)

View File

@ -0,0 +1,10 @@
fn foo(mut m map[string][1][2]map[string]int) {
m['foo'] = [[{'bar': 1}, {'baz':3}]!]!
}
fn test_complex_map_fixed_array() {
mut m := map[string][1][2]map[string]int
foo(mut m)
println(m)
assert '$m' == "{'foo': [[{'bar': 1}, {'baz': 3}]]}"
}