cgen: fix comptimecall with map receiver (fix #24448) (#24449)

This commit is contained in:
Felipe Pena 2025-05-10 15:03:11 -03:00 committed by GitHub
parent f3baaa0fb8
commit d0de08338f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 3 deletions

View File

@ -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++

View File

@ -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
}