Skip to content

Generics and Custom Iterators #23927

Description

@mecolosimo
V version: V 0.4.9 9f3f129, press to see full `v doctor` output
V full version V 0.4.9 2684ef9.9f3f129
OS linux, Linux Mint 22
Processor 8 cpus, 64bit, little endian, AMD Ryzen 5 7520U with Radeon Graphics
Memory 0.66GB/14.86GB
V executable /home/mcolosimo/github/v/v
V last modified time 2025-03-13 16:39:39
V home dir OK, value: /home/mcolosimo/github/v
VMODULES OK, value: /home/mcolosimo/.vmodules
VTMP OK, value: /tmp/v_1000
Current working dir OK, value: /home/mcolosimo/SyncProjects/mecolosimo/generics
Git version git version 2.43.0
V git status weekly.2025.11-25-g9f3f1291
.git/config present true
cc version cc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
gcc version gcc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
clang version N/A
tcc version tcc version 0.9.28rc 2024-07-31 HEAD@1cee0908 (x86_64 Linux)
tcc git status thirdparty-linux-amd64 0134e9b9
emcc version N/A
glibc version ldd (Ubuntu GLIBC 2.39-0ubuntu8.4) 2.39

What did you do?
./v -g -o vdbg cmd/v && ./vdbg src/main.v && src/main

module main

struct SquareIterator {
    arr []int
mut:
    idx int
}

fn (mut iter SquareIterator) next[K]() ?K {
    if iter.idx >= iter.arr.len {
        return none
    }
    defer {
        iter.idx++
    }
    return iter.arr[iter.idx] * iter.arr[iter.idx]
}

fn main() {
	nums := [1, 2, 3, 4, 5]
	iter := SquareIterator{
		arr: nums
	}

	for squared in iter {
		println(squared)
	}
}

What did you see?

/tmp/v_1000/../../../../../../home/mcolosimo/github/v/vlib/v/markused/walker.v:602: at v__markused__Walker_fn_decl: RUNTIME ERROR: invalid memory access
/tmp/v_1000/../../../../../../home/mcolosimo/github/v/vlib/v/markused/walker.v:216: by v__markused__Walker_stmt
/tmp/v_1000/../../../../../../home/mcolosimo/github/v/vlib/v/markused/walker.v:279: by v__markused__Walker_stmts
/tmp/v_1000/../../../../../../home/mcolosimo/github/v/vlib/v/markused/walker.v:614: by v__markused__Walker_fn_decl
/tmp/v_1000/../../../../../../home/mcolosimo/github/v/vlib/v/markused/walker.v:125: by v__markused__Walker_mark_root_fns
/tmp/v_1000/../../../../../../home/mcolosimo/github/v/vlib/v/markused/markused.v:477: by v__markused__mark_used
/tmp/v_1000/../../../../../../home/mcolosimo/github/v/vlib/v/builder/builder.v:154: by v__builder__Builder_middle_stages
/tmp/v_1000/../../../../../../home/mcolosimo/github/v/vlib/v/builder/builder.v:163: by v__builder__Builder_front_and_middle_stages
/tmp/v_1000/../../../../../../home/mcolosimo/github/v/vlib/v/builder/cbuilder/cbuilder.v:72: by v__builder__cbuilder__gen_c
/tmp/v_1000/../../../../../../home/mcolosimo/github/v/vlib/v/builder/cbuilder/cbuilder.v:60: by v__builder__cbuilder__build_c
/tmp/v_1000/../../../../../../home/mcolosimo/github/v/vlib/v/builder/cbuilder/cbuilder.v:50: by v__builder__cbuilder__compile_c
/tmp/v_1000/../../../../../../home/mcolosimo/github/v/vlib/v/builder/rebuilding.v:325: by v__builder__Builder_rebuild
/tmp/v_1000/../../../../../../home/mcolosimo/github/v/vlib/v/builder/compile.v:17: by v__builder__compile
/tmp/v_1000/../../../../../../home/mcolosimo/github/v/cmd/v/v.v:192: by main__rebuild
/tmp/v_1000/../../../../../../home/mcolosimo/github/v/cmd/v/v.v:150: by main__main
/tmp/v_1000/../../../../../../tmp/v_1000/vdbg.01JP86431QD8JBGYTR2GNG1AAK.tmp.c:39624: by main

What did you expect to see?

It compiles

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

Metadata

Metadata

Assignees

Labels

BugThis tag is applied to issues which reports bugs.Nicer V ErrorsBugs/feature requests, related to improving V error messages.Status: ConfirmedThis bug has been confirmed to be valid by a contributor.Unit: CheckerBugs/feature requests, that are related to the type checker.Unit: CompilerBugs/feature requests, that are related to the V compiler in general.Unit: cgenBugs/feature requests, that are related to the default C generating backend.

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions