From ec7863d174ec90defda40007e078107c8555ba34 Mon Sep 17 00:00:00 2001 From: yuyi Date: Mon, 25 May 2020 10:45:16 +0800 Subject: [PATCH] cgen: fix array_init of struct error --- vlib/v/gen/cgen.v | 40 ++++++++++++++++++++-------------- vlib/v/tests/array_init_test.v | 13 +++++++++++ 2 files changed, 37 insertions(+), 16 deletions(-) diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index c971195492..3bdc9e7bbd 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -967,9 +967,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { if is_call { g.expr(val) } else { - if val is ast.ArrayInit { - g.gen_default_init_value(val as ast.ArrayInit) - } + g.gen_default_init_value(val) g.write('{$styp _ = ') g.expr(val) g.writeln(';}') @@ -978,9 +976,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { right_sym := g.table.get_type_symbol(assign_stmt.right_types[i]) mut is_fixed_array_init := false mut has_val := false - if val is ast.ArrayInit { - is_fixed_array_init, has_val = g.gen_default_init_value(val as ast.ArrayInit) - } + is_fixed_array_init, has_val = g.gen_default_init_value(val) is_inside_ternary := g.inside_ternary != 0 cur_line := if is_inside_ternary { g.register_ternary_name(ident.name) @@ -1045,16 +1041,28 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { } } -fn (mut g Gen) gen_default_init_value(val ast.ArrayInit) (bool, bool) { - is_fixed_array_init := val.is_fixed - has_val := val.has_val - elem_type_str := g.typ(val.elem_type) - if val.has_default { - g.write('$elem_type_str _val_$val.pos.pos = ') - g.expr(val.default_expr) - g.writeln(';') - } else if val.has_len && val.elem_type == table.string_type { - g.writeln('$elem_type_str _val_$val.pos.pos = tos_lit("");') +fn (mut g Gen) gen_default_init_value(val ast.Expr) (bool, bool) { + mut is_fixed_array_init := false + mut has_val := false + match val { + ast.ArrayInit { + is_fixed_array_init = it.is_fixed + has_val = it.has_val + elem_type_str := g.typ(it.elem_type) + if it.has_default { + g.write('$elem_type_str _val_$it.pos.pos = ') + g.expr(it.default_expr) + g.writeln(';') + } else if it.has_len && it.elem_type == table.string_type { + g.writeln('$elem_type_str _val_$it.pos.pos = tos_lit("");') + } + } + ast.StructInit { + for field in it.fields { + g.gen_default_init_value(field.expr) + } + } + else {} } return is_fixed_array_init, has_val } diff --git a/vlib/v/tests/array_init_test.v b/vlib/v/tests/array_init_test.v index cc19a50cd3..0c5c11421c 100644 --- a/vlib/v/tests/array_init_test.v +++ b/vlib/v/tests/array_init_test.v @@ -127,3 +127,16 @@ fn test_array_string_full_options() { assert c.cap >= c.len assert c.str() == "['a', 'b', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', '11', '12', '13', '14', '15', '16']" } + +struct MyStruct { +pub mut: + ar []f32 +} + +fn test_array_init_in_struct_field() { + m := MyStruct { + ar: []f32{len: 4, init:1.2} + } + println(m) + assert m.ar.str() == '[1.2, 1.2, 1.2, 1.2]' +}