1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix _ := []byte{init:1}

This commit is contained in:
Delyan Angelov 2020-05-22 18:17:13 +03:00
parent b5af6d44c2
commit aef751861d

View File

@ -969,6 +969,9 @@ 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.write('{$styp _ = ')
g.expr(val)
g.writeln(';}')
@ -977,26 +980,8 @@ 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 = val.is_fixed
has_val = val.has_val
}
*/
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("");')
}
}
else {}
is_fixed_array_init, has_val = g.gen_default_init_value( val as ast.ArrayInit )
}
is_inside_ternary := g.inside_ternary != 0
cur_line := if is_inside_ternary {
@ -1062,6 +1047,20 @@ 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("");')
}
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 {