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

cgen: handle default anonymous structs

This commit is contained in:
Alexander Medvednikov 2022-07-08 15:40:54 +03:00
parent 1a49db4ea1
commit 70890b27a5
2 changed files with 12 additions and 4 deletions

View File

@ -5390,8 +5390,13 @@ fn (mut g Gen) type_default(typ_ ast.Type) string {
}
if has_none_zero {
init_str += '}'
type_name := g.typ(typ)
init_str = '($type_name)' + init_str
type_name := if info.is_anon {
// No name needed for anon structs, C figures it out on its own.
''
} else {
'(${g.typ(typ)})'
}
init_str = type_name + init_str
} else {
init_str += '0}'
}

View File

@ -425,6 +425,9 @@ struct Book {
}
fn test_anon() {
// book := Book{author:struct{'sdf', 23}}
// println(book.author.age)
empty_book := Book{} // author:struct{'sdf', 23}}
assert empty_book.author.age == 0
assert empty_book.author.name == ''
println(empty_book.author.age)
}