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

cgen: fix struct initialization for nested option struct (fix #17524) (#17525)

This commit is contained in:
Felipe Pena 2023-03-07 05:30:45 -03:00 committed by GitHub
parent 1e416de627
commit 9826310882
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 18 deletions

View File

@ -323,7 +323,12 @@ fn (mut g Gen) zero_struct_field(field ast.StructField) bool {
typ: field.typ
}
g.write('.${field_name} = ')
g.struct_init(default_init)
if field.typ.has_flag(.option) {
tmp_var := g.new_tmp_var()
g.expr_with_tmp_var(default_init, field.typ, field.typ, tmp_var)
} else {
g.struct_init(default_init)
}
return true
}
}

View File

@ -1,22 +1,20 @@
struct Foo {
a int
b int
c int
pub struct MyStruct {
pub mut:
valueb ?int
}
struct Holder {
mut:
i int
pub struct MyStruct2 {
pub mut:
valuea int
valueb ?MyStruct
}
fn add(mut h Holder) ?int {
h.i++
return h.i
}
fn test_struct_init_with_multiple_options() {
mut h := Holder{}
foo := Foo{add(mut h) or { 0 }, add(mut h) or { 0 }, add(mut h) or { 0 }}
assert foo == Foo{1, 2, 3}
fn test_main() {
a := MyStruct2{
valuea: 1
}
assert a.str() == 'MyStruct2{
valuea: 1
valueb: Option(error: none)
}'
}