From 4e1a09c9f50248e0b677017c29f03d1653c35386 Mon Sep 17 00:00:00 2001 From: yuyi Date: Mon, 15 Jun 2020 01:09:27 +0800 Subject: [PATCH] cgen: fix array_init temporary variable error --- vlib/v/gen/cgen.v | 50 ++++++++++++---------------------- vlib/v/tests/array_init_test.v | 5 ++++ 2 files changed, 23 insertions(+), 32 deletions(-) diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index adadbd2d15..1347f81aa0 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -1097,7 +1097,6 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { if is_call { g.expr(val) } else { - g.gen_default_init_value(val) g.write('{$styp _ = ') g.expr(val) g.writeln(';}') @@ -1106,7 +1105,13 @@ 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 - is_fixed_array_init, has_val = g.gen_default_init_value(val) + match val { + ast.ArrayInit { + is_fixed_array_init = it.is_fixed + has_val = it.has_val + } + else {} + } is_inside_ternary := g.inside_ternary != 0 cur_line := if is_inside_ternary { g.register_ternary_name(ident.name) @@ -1209,33 +1214,6 @@ fn (mut g Gen) gen_cross_tmp_variable(idents []ast.Ident, val ast.Expr) { } } -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.gen_default_init_value(it.default_expr) - 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 -} - fn (mut g Gen) register_ternary_name(name string) { level_key := g.inside_ternary.str() if level_key !in g.ternary_level_names { @@ -4348,9 +4326,17 @@ fn (mut g Gen) array_init(it ast.ArrayInit) { } g.write('sizeof($elem_type_str), ') if is_default_array { - g.write('_val_$it.pos.pos)') - } else if it.has_default || (it.has_len && it.elem_type == table.string_type) { - g.write('&_val_$it.pos.pos)') + g.write('($elem_type_str[]){') + g.expr(it.default_expr) + g.write('}[0])') + } else if it.has_default { + g.write('&($elem_type_str[]){') + g.expr(it.default_expr) + g.write('})') + } else if it.has_len && it.elem_type == table.string_type { + g.write('&($elem_type_str[]){') + g.write('tos_lit("")') + g.write('})') } else { g.write('0)') } diff --git a/vlib/v/tests/array_init_test.v b/vlib/v/tests/array_init_test.v index d72f6cc5f8..268842cb57 100644 --- a/vlib/v/tests/array_init_test.v +++ b/vlib/v/tests/array_init_test.v @@ -189,3 +189,8 @@ fn test_multi_dimensional_array_init() { f := [][]int{len:3} assert '$f' == '[[], [], []]' } + +fn test_array_init_direct_call() { + assert []int{len: 2, init: 0}.len == 2 + assert []int{len: 3, init: 1}.map(it*2) == [2,2,2] +}