From ed7c8d5e190027b83d66dc1e64018ea315dcb293 Mon Sep 17 00:00:00 2001 From: yuyi Date: Sat, 8 May 2021 19:33:09 +0800 Subject: [PATCH] checker: fix generics return recursive generic struct (fix #10028) (#10031) --- vlib/v/checker/checker.v | 4 ++-- ...ics_return_recursive_generics_struct_test.v | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 vlib/v/tests/generics_return_recursive_generics_struct_test.v diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 0197c199c7..2c8e1dea5b 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1639,9 +1639,9 @@ fn (mut c Checker) check_return_generics_struct(return_type ast.Type, mut call_e mut fields := rts.info.fields.clone() if rts.info.generic_types.len == concrete_types.len { generic_names := rts.info.generic_types.map(c.table.get_type_symbol(it).name) - for i, _ in fields { + for i in 0 .. fields.len { if t_typ := c.table.resolve_generic_to_concrete(fields[i].typ, - generic_names, concrete_types, false) + generic_names, concrete_types, true) { fields[i].typ = t_typ } diff --git a/vlib/v/tests/generics_return_recursive_generics_struct_test.v b/vlib/v/tests/generics_return_recursive_generics_struct_test.v new file mode 100644 index 0000000000..9c78d289b8 --- /dev/null +++ b/vlib/v/tests/generics_return_recursive_generics_struct_test.v @@ -0,0 +1,18 @@ +struct Node { +mut: + val T + next &Node +} + +fn make_node(val []T) Node { + return Node{ + val: val[0] + next: 0 + } +} + +fn test_generics_return_recursive_generics_struct() { + n := make_node([1, 2, 3]) + println(n.val) + assert n.val == 1 +}