cgen: fix child struct's default values not assigned (#8202)

pull/8209/head
yuyi 2021-01-20 00:10:22 +08:00 committed by GitHub
parent d9532eda30
commit 985ef52872
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 10 deletions

View File

@ -5459,24 +5459,32 @@ fn (mut g Gen) type_default(typ_ table.Type) string {
// User struct defined in another module.
// if typ.contains('__') {
if sym.kind == .struct_ {
mut has_array_map := false
mut zero_str := '{'
mut has_none_zero := false
mut init_str := '{'
info := sym.info as table.Struct
for field in info.fields {
field_sym := g.table.get_type_symbol(field.typ)
if field_sym.kind in [.array, .map] {
zero_str += '.$field.name=${g.type_default(field.typ)},'
has_array_map = true
if field_sym.kind in [.array, .map] || field.has_default_expr {
if field.has_default_expr {
pos := g.out.len
g.expr(ast.fe2ex(field.default_expr))
expr_str := g.out.after(pos)
g.out.go_back(expr_str.len)
init_str += '.$field.name = $expr_str,'
} else {
init_str += '.$field.name = ${g.type_default(field.typ)},'
}
has_none_zero = true
}
}
if has_array_map {
zero_str += '}'
if has_none_zero {
init_str += '}'
type_name := g.typ(typ)
zero_str = '($type_name)' + zero_str
init_str = '($type_name)' + init_str
} else {
zero_str += '0}'
init_str += '0}'
}
return zero_str
return init_str
}
// if typ.ends_with('Fn') { // TODO
// return '0'

View File

@ -0,0 +1,22 @@
struct Position {
line_nr int = 1
}
struct Statement {
pos Position
}
struct Expression {
pos Position
}
fn test_child_struct_field_default() {
stmt := Statement{
pos: Position{}
}
expr := Expression{}
println(stmt)
println(expr)
assert stmt.pos.line_nr == 1
assert expr.pos.line_nr == 1
}