cgen: fix multi fixed arrays with default init (fix #8038) (#8064)

pull/8101/head
yuyi 2021-01-14 04:57:33 +08:00 committed by GitHub
parent dc948e18af
commit 1d28c4de2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 0 deletions

View File

@ -1337,3 +1337,9 @@ fn test_array_of_multi_map() {
assert nums.odds == [3, 5, 7] assert nums.odds == [3, 5, 7]
assert nums.evens == [2, 6, 10] assert nums.evens == [2, 6, 10]
} }
fn test_multi_fixed_array_with_default_init() {
a := [3][3]int{init: [3]int{init: 10}}
println(a)
assert a == [[10, 10, 10]!, [10, 10, 10]!, [10, 10, 10]!]!
}

View File

@ -2768,6 +2768,9 @@ pub fn (mut c Checker) array_init(mut array_init ast.ArrayInit) table.Type {
1) 1)
array_type := table.new_type(idx) array_type := table.new_type(idx)
array_init.typ = array_type array_init.typ = array_type
if array_init.has_default {
c.expr(array_init.default_expr)
}
} }
return array_init.typ return array_init.typ
} }

View File

@ -35,6 +35,13 @@ fn (mut g Gen) array_init(it ast.ArrayInit) {
g.write(', ') g.write(', ')
} }
} }
} else if it.has_default {
g.expr(it.default_expr)
info := type_sym.info as table.ArrayFixed
for _ in 1 .. info.size {
g.write(', ')
g.expr(it.default_expr)
}
} else { } else {
g.write('0') g.write('0')
} }