diff --git a/vlib/v/checker/struct.v b/vlib/v/checker/struct.v index 3d682f01c9..4dbce66a54 100644 --- a/vlib/v/checker/struct.v +++ b/vlib/v/checker/struct.v @@ -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() - && 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{ pos: node.pos typ: field.typ diff --git a/vlib/v/tests/nested_option_struct_init_test.v b/vlib/v/tests/nested_option_struct_init_test.v index c975cc2e9b..b512eae506 100644 --- a/vlib/v/tests/nested_option_struct_init_test.v +++ b/vlib/v/tests/nested_option_struct_init_test.v @@ -14,3 +14,17 @@ fn test_nested_option_struct_init() { assert d2.d.b != none 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 +}