From 6d8086747d67f32333cf21b28fb6b140a2758080 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Thu, 26 Feb 2026 10:02:42 +0300 Subject: [PATCH] checker: skip ref field init check when default expr exists (fixes #25213) --- vlib/v/checker/struct.v | 16 ++++++++++++---- ..._struct_with_linked_list_of_refs_field_test.v | 12 ++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 vlib/v/tests/generics/generic_struct_with_linked_list_of_refs_field_test.v diff --git a/vlib/v/checker/struct.v b/vlib/v/checker/struct.v index 01ec407e5..3e35c1151 100644 --- a/vlib/v/checker/struct.v +++ b/vlib/v/checker/struct.v @@ -1226,8 +1226,12 @@ fn (mut c Checker) check_ref_fields_initialized(struct_sym &ast.TypeSymbol, mut if field.typ in checked_types { continue } - if field.typ.is_ptr() && !field.typ.has_flag(.shared_f) && !field.typ.has_flag(.option) - && !field.has_default_expr { + if field.has_default_expr { + // The field is already initialized by its default expression. + // Its nested reference fields should not be treated as missing. + continue + } + if field.typ.is_ptr() && !field.typ.has_flag(.shared_f) && !field.typ.has_flag(.option) { c.error('reference field `${linked_name}.${field.name}` must be initialized (part of struct `${struct_sym.name}`)', pos) continue @@ -1273,8 +1277,12 @@ fn (mut c Checker) check_ref_fields_initialized_note(struct_sym &ast.TypeSymbol, if field.typ in checked_types { continue } - if field.typ.is_ptr() && !field.typ.has_flag(.shared_f) && !field.typ.has_flag(.option) - && !field.has_default_expr { + if field.has_default_expr { + // The field is already initialized by its default expression. + // Its nested reference fields should not be treated as missing. + continue + } + if field.typ.is_ptr() && !field.typ.has_flag(.shared_f) && !field.typ.has_flag(.option) { c.note('reference field `${linked_name}.${field.name}` must be initialized (part of struct `${struct_sym.name}`)', pos) continue diff --git a/vlib/v/tests/generics/generic_struct_with_linked_list_of_refs_field_test.v b/vlib/v/tests/generics/generic_struct_with_linked_list_of_refs_field_test.v new file mode 100644 index 000000000..d2f7a60ef --- /dev/null +++ b/vlib/v/tests/generics/generic_struct_with_linked_list_of_refs_field_test.v @@ -0,0 +1,12 @@ +import datatypes + +struct OtherType {} + +struct SomeType { + cfg datatypes.LinkedList[&OtherType] +} + +fn test_generic_struct_with_linked_list_of_refs_field() { + some_type := SomeType{} + assert some_type.cfg.len() == 0 +} -- 2.39.5