From 0236931c787359fda56e5b891c77cc5e39f11f11 Mon Sep 17 00:00:00 2001 From: yuyi Date: Sat, 19 Feb 2022 21:19:50 +0800 Subject: [PATCH] cgen: fix generics with reference generic arguments (#13525) --- vlib/v/gen/c/cgen.v | 2 +- vlib/v/gen/c/fn.v | 2 +- .../generics_with_reference_generic_args_test.v | 15 +++++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 vlib/v/tests/generics_with_reference_generic_args_test.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 413a2d8c86..35f24fad72 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -3852,7 +3852,7 @@ fn (mut g Gen) return_stmt(node ast.Return) { return } tmpvar := g.new_tmp_var() - ret_typ := g.typ(g.fn_decl.return_type) + ret_typ := g.typ(g.unwrap_generic(g.fn_decl.return_type)) mut use_tmp_var := g.defer_stmts.len > 0 || g.defer_profile_code.len > 0 // handle promoting none/error/function returning 'Option' if fn_return_is_optional { diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index 7e59bedd95..b57110d681 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -214,7 +214,7 @@ fn (mut g Gen) gen_fn_decl(node &ast.FnDecl, skip bool) { } // mut name := g.c_fn_name(node) or { return } - mut type_name := g.typ(node.return_type) + mut type_name := g.typ(g.unwrap_generic(node.return_type)) if g.pref.obfuscate && g.cur_mod.name == 'main' && name.starts_with('main__') && !node.is_main && node.name != 'str' { diff --git a/vlib/v/tests/generics_with_reference_generic_args_test.v b/vlib/v/tests/generics_with_reference_generic_args_test.v new file mode 100644 index 0000000000..504f9cabea --- /dev/null +++ b/vlib/v/tests/generics_with_reference_generic_args_test.v @@ -0,0 +1,15 @@ +module main + +fn foo(val T) ?T { + return val +} + +struct Bar { + num int +} + +fn test_generics_with_reference_generic_args() { + ret := foo<&Bar>(&Bar{ num: 123 }) or { panic(err) } + println(ret) + assert ret.num == 123 +}