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

cgen: fix array_init temporary variable error

This commit is contained in:
yuyi 2020-06-15 01:09:27 +08:00 committed by GitHub
parent 90279a7108
commit 4e1a09c9f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 32 deletions

View File

@ -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)')
}

View File

@ -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]
}