cgen: fix error for print comptime for method call (#13108)

pull/13113/head
yuyi 2022-01-10 01:00:23 +08:00 committed by GitHub
parent 0ac450927c
commit 236a1d0255
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 0 deletions

View File

@ -869,6 +869,15 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
return
}
}
} else if node.left is ast.ComptimeCall {
if node.left.method_name == 'method' {
sym := g.table.sym(g.unwrap_generic(node.left.left_type))
if m := sym.find_method(g.comptime_for_method) {
rec_type = m.return_type
g.gen_expr_to_string(node.left, rec_type)
return
}
}
} else if node.left is ast.Ident {
if node.left.obj is ast.Var {
if g.comptime_var_type_map.len > 0 {
@ -1219,6 +1228,13 @@ fn (mut g Gen) fn_call(node ast.CallExpr) {
typ = g.comptime_var_type_map[key_str] or { typ }
}
}
} else if expr is ast.ComptimeCall {
if expr.method_name == 'method' {
sym := g.table.sym(g.unwrap_generic(expr.left_type))
if m := sym.find_method(g.comptime_for_method) {
typ = m.return_type
}
}
} else if expr is ast.Ident {
if expr.obj is ast.Var {
typ = expr.obj.typ

View File

@ -0,0 +1,34 @@
module main
struct S1 {}
fn test_comptime_for_method_call_str() {
s1 := S1{}
mut rets := []string{}
$for method in S1.methods {
println(s1.$method('ll'))
rets << s1.$method('ll').str()
}
assert rets.len == 4
assert rets[0] == '7'
assert rets[1] == '7.7'
assert rets[2] == 'foo'
assert rets[3] == 'true'
}
fn (t S1) m1(s string) int {
return 7
}
fn (t S1) m2(s string) f64 {
return 7.7
}
fn (t S1) m3(s string) string {
return 'foo'
}
fn (t S1) m4(s string) bool {
return true
}