From ba8e61ebf37de388e4318b7bd49a1721884dd61a Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Fri, 9 Dec 2022 12:24:47 -0300 Subject: [PATCH] cgen: fix `return typeof[T]()` in generic functions (#16626) --- vlib/v/gen/c/cgen.v | 4 +--- vlib/v/tests/fn_return_typeof_test.v | 12 ++++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) create mode 100644 vlib/v/tests/fn_return_typeof_test.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index f3bb3c63fd..66656d41ec 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -3525,9 +3525,7 @@ fn (mut g Gen) typeof_expr(node ast.TypeOf) { varg_elem_type_sym := g.table.sym(g.table.value_type(typ)) g.write('_SLIT("...${util.strip_main_name(varg_elem_type_sym.name)}")') } else { - x := g.table.type_to_str(typ) - y := util.strip_main_name(x) - g.write('_SLIT("${y}")') + g.type_name(typ) } } diff --git a/vlib/v/tests/fn_return_typeof_test.v b/vlib/v/tests/fn_return_typeof_test.v new file mode 100644 index 0000000000..6616f7d12f --- /dev/null +++ b/vlib/v/tests/fn_return_typeof_test.v @@ -0,0 +1,12 @@ +fn foo[T]() string { + return typeof[T]().name +} + +fn bar[T]() string { + return typeof[T]() +} + +fn test_main() { + assert foo[int]() == 'int' + assert bar[int]() == 'int' +}