From 59a88c8486b60f1d68f332814bc721e9d2dbaad4 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Wed, 20 Aug 2025 08:34:39 -0300 Subject: [PATCH] fix --- vlib/v/checker/struct.v | 5 +++- .../generic_struct_field_unwrap_test.v | 23 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/generics/generic_struct_field_unwrap_test.v diff --git a/vlib/v/checker/struct.v b/vlib/v/checker/struct.v index 6e0d774bcd..31153c2298 100644 --- a/vlib/v/checker/struct.v +++ b/vlib/v/checker/struct.v @@ -851,7 +851,10 @@ or use an explicit `unsafe{ a[..] }`, if you do not want a copy of the slice.', if mut init_field.expr is ast.CallExpr && init_field.expr.return_type.has_flag(.generic) { expected_type := c.unwrap_generic(init_field.expected_type) - got_type_ret := c.unwrap_generic(init_field.expr.return_type) + mut got_type_ret := c.unwrap_generic(init_field.expr.return_type) + if init_field.expr.or_block.kind != .absent { + got_type_ret = got_type_ret.clear_option_and_result() + } if expected_type != got_type_ret { c.error('cannot assign `${c.table.type_to_str(got_type_ret)}` to struct field `${init_field.name}` with type `${c.table.type_to_str(expected_type)}`', init_field.expr.pos) diff --git a/vlib/v/tests/generics/generic_struct_field_unwrap_test.v b/vlib/v/tests/generics/generic_struct_field_unwrap_test.v new file mode 100644 index 0000000000..c9179a8d05 --- /dev/null +++ b/vlib/v/tests/generics/generic_struct_field_unwrap_test.v @@ -0,0 +1,23 @@ +struct Test[T] { +pub: + value T +} + +struct AuxTest[T] { +pub: + any_value Test[T] +} + +fn decode[T](arg Test[T]) !Test[T] { + return Test[T]{} +} + +pub fn initializing[T]() !AuxTest[T] { + return AuxTest[T]{ + any_value: decode[T](Test[T]{})! + } +} + +fn test_main() { + dump(initializing[int]()!) +}