Skip to content

Commit d85111e

Browse files
authored
cgen: fix comptime for_in methods call (#12741)
1 parent 3ab82a2 commit d85111e

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

vlib/v/gen/c/cgen.v

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3003,6 +3003,10 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
30033003
left.obj.typ = var_type
30043004
}
30053005
}
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
30063010
}
30073011
is_auto_heap = left.obj.is_auto_heap
30083012
}

vlib/v/gen/c/fn.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,12 +1146,12 @@ fn (mut g Gen) fn_call(node ast.CallExpr) {
11461146
// g.generate_tmp_autofree_arg_vars(node, name)
11471147
// Handle `print(x)`
11481148
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 {
11501150
mut typ := node.args[0].typ
11511151
if typ == 0 {
11521152
g.checker_bug('print arg.typ is 0', node.pos)
11531153
}
1154-
if typ != ast.string_type {
1154+
if typ != ast.string_type || g.comptime_for_method.len > 0 {
11551155
expr := node.args[0].expr
11561156
typ_sym := g.table.get_type_symbol(typ)
11571157
if typ_sym.kind == .interface_ && (typ_sym.info as ast.Interface).defines_method('str') {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
}

0 commit comments

Comments
 (0)