Skip to content

Commit

Permalink
cgen: fix for/in codegen when iterating over C structs (#21052)
Browse files Browse the repository at this point in the history
  • Loading branch information
spiveeworks committed Mar 18, 2024
1 parent 2798a06 commit 833da30
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion vlib/v/gen/c/for.v
Expand Up @@ -435,7 +435,7 @@ fn (mut g Gen) for_in_stmt(node_ ast.ForInStmt) {
}
t_var := g.new_tmp_var()
receiver_typ := g.unwrap_generic(next_fn.params[0].typ)
receiver_styp := g.typ(receiver_typ)
receiver_styp := g.cc_type(receiver_typ, false)
mut fn_name := receiver_styp.replace_each(['*', '', '.', '__']) + '_next'
receiver_sym := g.table.sym(receiver_typ)
if receiver_sym.info is ast.Struct {
Expand Down
22 changes: 22 additions & 0 deletions vlib/v/tests/c_structs/cstruct_iterator_test.c.v
@@ -0,0 +1,22 @@
#include "@VMODROOT/iterator.h"

struct C.MyCStruct {
mut:
x int
}

fn (mut self C.MyCStruct) next() ?int {
if self.x >= 10 {
return none
}
self.x++
return self.x
}

fn test_iterating_over_cstructs() {
iter := C.MyCStruct{}
for x in iter {
println(x)
assert true
}
}
3 changes: 3 additions & 0 deletions vlib/v/tests/c_structs/iterator.h
@@ -0,0 +1,3 @@
struct MyCStruct {
int x;
};

0 comments on commit 833da30

Please sign in to comment.