From 64c1034d2498c7415450223281e215c4cdba74b6 Mon Sep 17 00:00:00 2001 From: CreeperFace <165158232+dy-tea@users.noreply.github.com> Date: Wed, 26 Nov 2025 06:49:34 +0000 Subject: [PATCH] checker: skip init check for options (fix #25798) (#25830) --- vlib/v/checker/struct.v | 8 +++++++ .../struct_embed_option_reference_test.v | 23 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 vlib/v/tests/structs/struct_embed_option_reference_test.v diff --git a/vlib/v/checker/struct.v b/vlib/v/checker/struct.v index 55a096d75..62e1df03a 100644 --- a/vlib/v/checker/struct.v +++ b/vlib/v/checker/struct.v @@ -1188,6 +1188,10 @@ fn (mut c Checker) check_ref_fields_initialized(struct_sym &ast.TypeSymbol, mut // an embedded struct field continue } + if field.typ.has_flag(.option) { + // defaults to `none` + continue + } checked_types << field.typ c.check_ref_fields_initialized(sym, mut checked_types, '${linked_name}.${field.name}', pos) @@ -1231,6 +1235,10 @@ fn (mut c Checker) check_ref_fields_initialized_note(struct_sym &ast.TypeSymbol, // an embedded struct field continue } + if field.typ.has_flag(.option) { + // defaults to `none` + continue + } checked_types << field.typ c.check_ref_fields_initialized(sym, mut checked_types, '${linked_name}.${field.name}', pos) diff --git a/vlib/v/tests/structs/struct_embed_option_reference_test.v b/vlib/v/tests/structs/struct_embed_option_reference_test.v new file mode 100644 index 000000000..35cf240b8 --- /dev/null +++ b/vlib/v/tests/structs/struct_embed_option_reference_test.v @@ -0,0 +1,23 @@ +struct Foo { + data &int +} + +struct FooHolder { + foo ?&Foo +} + +struct FooMain { + foo_holder FooHolder +} + +struct FooMain2 { + foo_main FooMain +} + +fn test_main() { + a := FooMain{} + assert a.foo_holder.foo == none + + b := FooMain2{} + assert b.foo_main.foo_holder.foo == none +} -- 2.39.5