Skip to content

Commit 26305ec

Browse files
authored
cgen: fix iterator on alias to fn ptr (fix #25911) (#25978)
1 parent 076fae5 commit 26305ec

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

vlib/v/gen/c/for.v

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -548,19 +548,25 @@ fn (mut g Gen) for_in_stmt(node_ ast.ForInStmt) {
548548
g.writeln('\tif (${t_var}.state != 0) break;')
549549
val := if node.val_var in ['', '_'] { g.new_tmp_var() } else { node.val_var }
550550
val_styp := g.styp(ret_typ.clear_option_and_result())
551+
ret_sym := g.table.final_sym(ret_typ)
551552
if node.val_is_mut {
552553
if ret_typ.is_any_kind_of_pointer() {
553554
g.writeln('\t${val_styp} ${val} = *(${val_styp}*)${t_var}.data;')
554555
} else {
555556
g.writeln('\t${val_styp}* ${val} = (${val_styp}*)${t_var}.data;')
556557
}
557558
} else {
558-
ret_is_fixed_array := g.table.sym(ret_typ).is_array_fixed()
559+
ret_is_fixed_array := ret_sym.is_array_fixed()
559560
if ret_is_fixed_array {
560561
g.writeln('\t${val_styp} ${val} = {0};')
561562
g.write('\tmemcpy(${val}, ${t_var}.data, sizeof(${val_styp}));')
562563
} else {
563-
g.writeln('\t${val_styp} ${val} = *(${val_styp}*)${t_var}.data;')
564+
if ret_sym.info is ast.FnType {
565+
g.write_fntype_decl(val, ret_sym.info)
566+
g.writeln(' = **(${val_styp}**)&${t_var}.data;')
567+
} else {
568+
g.writeln('\t${val_styp} ${val} = *(${val_styp}*)${t_var}.data;')
569+
}
564570
}
565571
}
566572
} else if node.kind == .aggregate {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
type MyFn = fn ()
2+
3+
struct Iter {
4+
f MyFn = fn () {}
5+
mut:
6+
done bool
7+
}
8+
9+
fn (mut it Iter) next() ?&MyFn {
10+
if it.done {
11+
return none
12+
}
13+
it.done = true
14+
return &it.f
15+
}
16+
17+
fn test_main() {
18+
mut c := 0
19+
for p in Iter{} {
20+
println(p)
21+
p()
22+
c += 1
23+
}
24+
assert c == 1
25+
}

0 commit comments

Comments
 (0)