From 2144471ce138cf61a8cb6a68ef8e07b85da03817 Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 2 Dec 2021 16:53:42 +0800 Subject: [PATCH] cgen: fix generic fn with anon fn in body (#12647) --- vlib/v/gen/c/fn.v | 4 +++- vlib/v/tests/generic_fn_with_anon_fn_test.v | 11 +++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/generic_fn_with_anon_fn_test.v diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index 4a37b7002d..62769f8dce 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -217,7 +217,9 @@ fn (mut g Gen) gen_fn_decl(node &ast.FnDecl, skip bool) { } mut type_name := g.typ(node.return_type) - name = g.generic_fn_name(g.cur_concrete_types, name, true) + if node.generic_names.len > 0 { + name = g.generic_fn_name(g.cur_concrete_types, name, true) + } if g.pref.translated && node.attrs.contains('c') { // This fixes unknown symbols errors when building separate .c => .v files diff --git a/vlib/v/tests/generic_fn_with_anon_fn_test.v b/vlib/v/tests/generic_fn_with_anon_fn_test.v new file mode 100644 index 0000000000..ef43c07aef --- /dev/null +++ b/vlib/v/tests/generic_fn_with_anon_fn_test.v @@ -0,0 +1,11 @@ +fn foo() string { + x := fn () string { + return 'ok' + } + return x() +} + +fn test_generic_fn_with_anon_fn() { + ret := foo() + assert ret == 'ok' +}