From 21d1f86ead842b95443f1cedf639725eb33adbd9 Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 27 Apr 2021 01:01:01 +0800 Subject: [PATCH] checker: fix generics that return reference generics struct (fix #6218) (#9869) --- vlib/v/checker/checker.v | 4 ++-- ...nerics_return_reference_generics_struct_test.v | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 vlib/v/tests/generics_return_reference_generics_struct_test.v diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index a61606fe8d..eb8abcd1de 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1457,7 +1457,7 @@ fn (mut c Checker) check_return_generics_struct(return_type ast.Type, mut call_e idx := c.table.type_idxs[nrt] if idx != 0 { c.ensure_type_exists(idx, call_expr.pos) or {} - call_expr.return_type = ast.new_type(idx).derive(return_type) + call_expr.return_type = ast.new_type(idx).derive(return_type).clear_flag(.generic) } else { mut fields := rts.info.fields.clone() if rts.info.generic_types.len == concrete_types.len { @@ -1481,7 +1481,7 @@ fn (mut c Checker) check_return_generics_struct(return_type ast.Type, mut call_e mod: c.mod info: info }) - call_expr.return_type = ast.new_type(stru_idx) + call_expr.return_type = ast.new_type(stru_idx).derive(return_type).clear_flag(.generic) } } } diff --git a/vlib/v/tests/generics_return_reference_generics_struct_test.v b/vlib/v/tests/generics_return_reference_generics_struct_test.v new file mode 100644 index 0000000000..0b4e9b5322 --- /dev/null +++ b/vlib/v/tests/generics_return_reference_generics_struct_test.v @@ -0,0 +1,15 @@ +struct Foo { + data []T +} + +fn new_foo(len int) &Foo { + return &Foo{ + data: []T{len: len} + } +} + +fn test_generics_return_reference_generics_struct() { + f := new_foo(4) + println(f) + assert f.data == [0, 0, 0, 0] +}