File tree Expand file tree Collapse file tree 3 files changed +29
-2
lines changed Expand file tree Collapse file tree 3 files changed +29
-2
lines changed Original file line number Diff line number Diff line change @@ -3003,6 +3003,10 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
3003
3003
left.obj.typ = var_type
3004
3004
}
3005
3005
}
3006
+ } else if val is ast.ComptimeCall {
3007
+ key_str := '${val.method_name} .return_type'
3008
+ var_type = g.comptime_var_type_map[key_str] or { var_type }
3009
+ left.obj.typ = var_type
3006
3010
}
3007
3011
is_auto_heap = left.obj.is_auto_heap
3008
3012
}
Original file line number Diff line number Diff line change @@ -1146,12 +1146,12 @@ fn (mut g Gen) fn_call(node ast.CallExpr) {
1146
1146
// g.generate_tmp_autofree_arg_vars(node, name)
1147
1147
// Handle `print(x)`
1148
1148
mut print_auto_str := false
1149
- if is_print && node.args[0 ].typ != ast.string_type { // && !free_tmp_arg_vars {
1149
+ if is_print && ( node.args[0 ].typ != ast.string_type || g.comptime_for_method.len > 0 ) { // && !free_tmp_arg_vars {
1150
1150
mut typ := node.args[0 ].typ
1151
1151
if typ == 0 {
1152
1152
g.checker_bug ('print arg.typ is 0' , node.pos)
1153
1153
}
1154
- if typ != ast.string_type {
1154
+ if typ != ast.string_type || g.comptime_for_method.len > 0 {
1155
1155
expr := node.args[0 ].expr
1156
1156
typ_sym := g.table.get_type_symbol (typ)
1157
1157
if typ_sym.kind == .interface_ && (typ_sym.info as ast.Interface ).defines_method ('str' ) {
Original file line number Diff line number Diff line change
1
+ struct Foo {}
2
+
3
+ fn (f Foo) a () int {
4
+ return 1
5
+ }
6
+
7
+ fn (f Foo) b () int {
8
+ return 2
9
+ }
10
+
11
+ fn test_comptime_for_method_call () {
12
+ f := Foo{}
13
+ mut rets := []string {}
14
+
15
+ $for method in Foo.methods {
16
+ x := f.$method ()
17
+ println (x)
18
+ rets << x.str ()
19
+ }
20
+ assert rets.len == 2
21
+ assert rets[0 ] == '1'
22
+ assert rets[1 ] == '2'
23
+ }
You can’t perform that action at this time.
0 commit comments