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

checker: allow aa := AA{}, when AA has an option field, that is a struct, that has a [required] tag on its fields (#17516)

This commit is contained in:
yuyi 2023-03-06 16:16:05 +08:00 committed by GitHub
parent 77b6bc1c1a
commit 65a627d72b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 1 deletions

View File

@ -648,7 +648,7 @@ fn (mut c Checker) struct_init(mut node ast.StructInit, is_field_zero_struct_ini
} }
} }
if !field.has_default_expr && field.name !in inited_fields && !field.typ.is_ptr() if !field.has_default_expr && field.name !in inited_fields && !field.typ.is_ptr()
&& c.table.final_sym(field.typ).kind == .struct_ { && !field.typ.has_flag(.option) && c.table.final_sym(field.typ).kind == .struct_ {
mut zero_struct_init := ast.StructInit{ mut zero_struct_init := ast.StructInit{
pos: node.pos pos: node.pos
typ: field.typ typ: field.typ

View File

@ -14,3 +14,17 @@ fn test_nested_option_struct_init() {
assert d2.d.b != none assert d2.d.b != none
assert d2.d.b? == 1 assert d2.d.b? == 1
} }
struct AA {
bb ?BB // defaults to none
}
struct BB {
x string [required]
}
fn test_nested_option_struct_with_attr_init() {
aa := AA{}
println(aa)
assert aa.bb == none
}