Skip to content

cgen: fix for/in codegen when iterating over C structs#21052

Merged
spytheman merged 3 commits intovlang:masterfrom
spiveeworks:master
Mar 18, 2024
Merged

cgen: fix for/in codegen when iterating over C structs#21052
spytheman merged 3 commits intovlang:masterfrom
spiveeworks:master

Conversation

@spiveeworks
Copy link
Copy Markdown
Contributor

@spiveeworks spiveeworks commented Mar 18, 2024

When implementing next for a C struct, and then looping over it with for/in, it was generating MyCStruct_next(&_t) or even struct MyCStruct_next(&_t) instead of C__MyCStruct_next(&_t). See issue #21047 for more details.

@spytheman
Copy link
Copy Markdown
Contributor

spytheman commented Mar 18, 2024

It can be tested similarly to https://github.com/vlang/v/pull/21042/files, i.e.
0) create vlib/v/tests/c_structs/v.mod (it can be empty text file).

  1. create vlib/v/tests/c_structs/iterator.c:
struct MyCStruct {
        int x;
};
  1. create vlib/v/tests/c_structs/cstruct_iterator_test.c.v:
#include "@VMODROOT/iterator.c"

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
	}
}

@spytheman spytheman merged commit 833da30 into vlang:master Mar 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants