From 6855996cca426d8a67514ec29e7bfcfc7c73539e Mon Sep 17 00:00:00 2001 From: yuyi Date: Sun, 17 May 2020 01:05:26 +0800 Subject: [PATCH] cgen: fix array_init_with_default --- vlib/v/gen/cgen.v | 4 ++-- vlib/v/tests/array_init_test.v | 14 ++++++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 086aa675d3..fcf8482b00 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -979,7 +979,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { has_val = it.has_val if it.has_default { 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.writeln(';') } @@ -3862,7 +3862,7 @@ fn (mut g Gen) array_init(it ast.ArrayInit) { } g.write('sizeof($elem_type_str), ') if it.has_default { - g.write('&${elem_type_str}_val_t)') + g.write('&_val_$it.pos.pos)') } else { g.write('0)') } diff --git a/vlib/v/tests/array_init_test.v b/vlib/v/tests/array_init_test.v index 07d3f196ea..0847f5bdad 100644 --- a/vlib/v/tests/array_init_test.v +++ b/vlib/v/tests/array_init_test.v @@ -16,9 +16,15 @@ fn test_array_init() { } fn test_array_init_with_default() { - a := []int{len: 4, init: 2} - assert '$a' == '[2, 2, 2, 2]' + a1 := []int{len: 4, init: 2} + assert '$a1' == '[2, 2, 2, 2]' - b := []string{len: 3, init: 'abc'} - assert '$b' == "['abc', 'abc', 'abc']" + a2 := []int{len: 3, init: 12345} + 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']" }