cgen: fix array_init_with_default

pull/4943/head
yuyi 2020-05-17 01:05:26 +08:00 committed by GitHub
parent 48659f4145
commit 6855996cca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 6 deletions

View File

@ -979,7 +979,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
has_val = it.has_val has_val = it.has_val
if it.has_default { if it.has_default {
elem_type_str := g.typ(it.elem_type) elem_type_str := g.typ(it.elem_type)
g.write('$elem_type_str ${elem_type_str}_val_t = ') g.write('$elem_type_str _val_$it.pos.pos = ')
g.expr(it.default_expr) g.expr(it.default_expr)
g.writeln(';') g.writeln(';')
} }
@ -3862,7 +3862,7 @@ fn (mut g Gen) array_init(it ast.ArrayInit) {
} }
g.write('sizeof($elem_type_str), ') g.write('sizeof($elem_type_str), ')
if it.has_default { if it.has_default {
g.write('&${elem_type_str}_val_t)') g.write('&_val_$it.pos.pos)')
} else { } else {
g.write('0)') g.write('0)')
} }

View File

@ -16,9 +16,15 @@ fn test_array_init() {
} }
fn test_array_init_with_default() { fn test_array_init_with_default() {
a := []int{len: 4, init: 2} a1 := []int{len: 4, init: 2}
assert '$a' == '[2, 2, 2, 2]' assert '$a1' == '[2, 2, 2, 2]'
b := []string{len: 3, init: 'abc'} a2 := []int{len: 3, init: 12345}
assert '$b' == "['abc', 'abc', 'abc']" assert '$a2' == '[12345, 12345, 12345]'
b1 := []string{len: 3, init: 'abc'}
assert '$b1' == "['abc', 'abc', 'abc']"
b2 := []string{len: 2, init: '111'}
assert '$b2' == "['111', '111']"
} }