From 833da3026dd6ad08878a99aa5e6f8ac7fc92da39 Mon Sep 17 00:00:00 2001 From: Jarvis Carroll Date: Mon, 18 Mar 2024 13:14:02 +1100 Subject: [PATCH] cgen: fix for/in codegen when iterating over C structs (#21052) --- vlib/v/gen/c/for.v | 2 +- .../tests/c_structs/cstruct_iterator_test.c.v | 22 +++++++++++++++++++ vlib/v/tests/c_structs/iterator.h | 3 +++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/c_structs/cstruct_iterator_test.c.v create mode 100644 vlib/v/tests/c_structs/iterator.h diff --git a/vlib/v/gen/c/for.v b/vlib/v/gen/c/for.v index 0b3199825..312d7344c 100644 --- a/vlib/v/gen/c/for.v +++ b/vlib/v/gen/c/for.v @@ -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 { diff --git a/vlib/v/tests/c_structs/cstruct_iterator_test.c.v b/vlib/v/tests/c_structs/cstruct_iterator_test.c.v new file mode 100644 index 000000000..a17f9d216 --- /dev/null +++ b/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 + } +} diff --git a/vlib/v/tests/c_structs/iterator.h b/vlib/v/tests/c_structs/iterator.h new file mode 100644 index 000000000..2b809b719 --- /dev/null +++ b/vlib/v/tests/c_structs/iterator.h @@ -0,0 +1,3 @@ +struct MyCStruct { + int x; +}; -- 2.39.5