From 14f90b1196c02a2a0cafd9e3e6f6d2cbbe21eacc Mon Sep 17 00:00:00 2001 From: Taegon Kim Date: Thu, 3 Nov 2022 19:16:01 +0900 Subject: [PATCH] cgen: fix compile time reflection for method types (.typ was always 0) (#16310) --- vlib/v/gen/c/comptime.v | 15 +++++++++------ vlib/v/tests/comptime_for_test.v | 11 +++++++++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/vlib/v/gen/c/comptime.v b/vlib/v/gen/c/comptime.v index 0910b849d6..f6d874d947 100644 --- a/vlib/v/gen/c/comptime.v +++ b/vlib/v/gen/c/comptime.v @@ -512,20 +512,23 @@ fn (mut g Gen) comptime_for(node ast.ComptimeFor) { } g.writeln('}));\n') } - mut sig := 'anon_fn_' + mut sig := 'fn (' // skip the first (receiver) arg for j, arg in method.params[1..] { // TODO: ignore mut/pts in sig for now typ := arg.typ.set_nr_muls(0) - sig += '$typ' + sig += g.table.sym(typ).name if j < method.params.len - 2 { - sig += '_' + sig += ', ' } } - sig += '_$method.return_type' + sig += ')' + ret_type := g.table.sym(method.return_type).name + if ret_type != 'void' { + sig += ' $ret_type' + } styp := g.table.find_type_idx(sig) - // println(styp) - // if styp == 0 { } + // TODO: type aliases ret_typ := method.return_type.idx() g.writeln('\t${node.val_var}.typ = $styp;') diff --git a/vlib/v/tests/comptime_for_test.v b/vlib/v/tests/comptime_for_test.v index b7991e7ba5..6760e5a9fa 100644 --- a/vlib/v/tests/comptime_for_test.v +++ b/vlib/v/tests/comptime_for_test.v @@ -48,18 +48,29 @@ fn test_comptime_for() { fn test_comptime_for_with_if() { println(@FN) + mut methods_found := map[string]int{} $for method in App.methods { println(' method: ' + no_lines('$method')) $if method.typ is fn () { + methods_found['fn()'] += 1 assert method.name in ['run', 'method2'] } + $if method.typ is fn () int { + methods_found['fn() int'] += 1 + } $if method.return_type is int { assert method.name in ['int_method1', 'int_method2'] } + $if method.typ is fn (string) { + methods_found['fn(string)'] += 1 + } $if method.args[0].typ is string { assert method.name == 'string_arg' } } + assert methods_found['fn()'] == 2 + assert methods_found['fn() int'] == 2 + assert methods_found['fn(string)'] == 1 } fn test_comptime_for_fields() {