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

cgen: fix default zero in map fields (fix #7328) (#7394)

This commit is contained in:
yuyi 2020-12-19 13:55:13 +08:00 committed by GitHub
parent c0f4c525d2
commit 598d18cbd9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 1 deletions

View File

@ -440,3 +440,16 @@ fn test_map_clone() {
assert nums2['foo'] == 2
assert nums2['bar'] == 8
}
struct MValue {
name string
misc map[string]string
}
fn test_map_default_zero() {
m := map[string]MValue{}
v := m['unknown']
x := v.misc['x']
println(x)
assert x == ''
}

View File

@ -5414,7 +5414,24 @@ fn (mut g Gen) type_default(typ_ table.Type) string {
// User struct defined in another module.
// if typ.contains('__') {
if sym.kind == .struct_ {
return '{0}'
mut has_array_map := false
mut zero_str := '{'
info := sym.info as table.Struct
for field in info.fields {
field_sym := g.table.get_type_symbol(field.typ)
if field_sym.kind in [.array, .map] {
zero_str += '.$field.name=${g.type_default(field.typ)},'
has_array_map = true
}
}
if has_array_map {
zero_str += '}'
type_name := g.typ(typ)
zero_str = '($type_name)' + zero_str
} else {
zero_str += '0}'
}
return zero_str
}
// if typ.ends_with('Fn') { // TODO
// return '0'