From d0de08338f8992f11b4b07eb87dafde619f0d8bc Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sat, 10 May 2025 15:03:11 -0300 Subject: [PATCH] cgen: fix comptimecall with map receiver (fix #24448) (#24449) --- vlib/v/gen/c/comptime.v | 7 ++--- .../comptime_call_map_receiver_test.v | 26 +++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 vlib/v/tests/comptime/comptime_call_map_receiver_test.v diff --git a/vlib/v/gen/c/comptime.v b/vlib/v/gen/c/comptime.v index cdee0a1554..623130f60c 100644 --- a/vlib/v/gen/c/comptime.v +++ b/vlib/v/gen/c/comptime.v @@ -139,7 +139,8 @@ fn (mut g Gen) comptime_call(mut node ast.ComptimeCall) { } return } - sym := g.table.sym(g.unwrap_generic(node.left_type)) + left_type := g.unwrap_generic(node.left_type) + sym := g.table.sym(left_type) g.trace_autofree('// \$method call. sym="${sym.name}"') if node.method_name == 'method' { // `app.$method()` @@ -187,7 +188,7 @@ fn (mut g Gen) comptime_call(mut node ast.ComptimeCall) { } } // TODO: check argument types - g.write('${util.no_dots(sym.name)}_${g.comptime.comptime_for_method.name}(') + g.write('${g.cc_type(left_type, false)}_${g.comptime.comptime_for_method.name}(') // try to see if we need to pass a pointer if mut node.left is ast.Ident { @@ -279,7 +280,7 @@ fn (mut g Gen) comptime_call(mut node ast.ComptimeCall) { } g.write('if (string__eq(${node.method_name}, _SLIT("${method.name}"))) ') } - g.write('${util.no_dots(sym.name)}_${method.name}(${amp} ') + g.write('${g.cc_type(left_type, false)}_${method.name}(${amp} ') g.expr(node.left) g.writeln(');') j++ diff --git a/vlib/v/tests/comptime/comptime_call_map_receiver_test.v b/vlib/v/tests/comptime/comptime_call_map_receiver_test.v new file mode 100644 index 0000000000..1f00276212 --- /dev/null +++ b/vlib/v/tests/comptime/comptime_call_map_receiver_test.v @@ -0,0 +1,26 @@ +type Any = string | int + +fn (m map[string]Any) to_toml() string { + mut t := '' + return t +} + +fn test_main() { + mut doc := map[string]Any{} + _ := encode(doc) +} + +fn encode[T](typ T) string { + $for method in T.methods { + $if method.name == 'to_toml' { + return typ.$method() + } + } + mp := encode_struct[T](typ) + return mp.to_toml() +} + +fn encode_struct[T](typ T) map[string]Any { + mut mp := map[string]Any{} + return mp +}