From ab707dce5c84f4ea4f9ad5ccc24e0e123837236b Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Mon, 30 Dec 2024 19:26:39 -0300 Subject: [PATCH] checker: fix `for` iterator method `.next()`, not marked as used (fix #23312) (#23321) --- vlib/v/checker/for.v | 2 ++ .../skip_unused/generic_iterator_loop.run.out | 0 .../generic_iterator_loop.skip_unused.run.out | 0 .../skip_unused/generic_iterator_loop.vv | 23 +++++++++++++++++++ 4 files changed, 25 insertions(+) create mode 100644 vlib/v/tests/skip_unused/generic_iterator_loop.run.out create mode 100644 vlib/v/tests/skip_unused/generic_iterator_loop.skip_unused.run.out create mode 100644 vlib/v/tests/skip_unused/generic_iterator_loop.vv diff --git a/vlib/v/checker/for.v b/vlib/v/checker/for.v index 6e1dc735636a61..9ef3e116b3a492 100644 --- a/vlib/v/checker/for.v +++ b/vlib/v/checker/for.v @@ -173,6 +173,8 @@ fn (mut c Checker) for_in_stmt(mut node ast.ForInStmt) { unwrapped_typ := c.unwrap_generic(typ) unwrapped_sym := c.table.sym(unwrapped_typ) + c.table.used_features.comptime_calls['${int(unwrapped_typ)}.next'] = true + if node.key_var.len > 0 { key_type := match unwrapped_sym.kind { .map { unwrapped_sym.map_info().key_type } diff --git a/vlib/v/tests/skip_unused/generic_iterator_loop.run.out b/vlib/v/tests/skip_unused/generic_iterator_loop.run.out new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/vlib/v/tests/skip_unused/generic_iterator_loop.skip_unused.run.out b/vlib/v/tests/skip_unused/generic_iterator_loop.skip_unused.run.out new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/vlib/v/tests/skip_unused/generic_iterator_loop.vv b/vlib/v/tests/skip_unused/generic_iterator_loop.vv new file mode 100644 index 00000000000000..b34e36d70d0843 --- /dev/null +++ b/vlib/v/tests/skip_unused/generic_iterator_loop.vv @@ -0,0 +1,23 @@ +module main + +struct Foo {} + +fn (f Foo) next() ?Foo { + return none +} + +struct Bar {} + +fn (f Bar) next() ?Bar { + return none +} + +fn loop[T](iter T) { + for _ in iter { + } +} + +fn main() { + loop(Foo{}) + loop(Bar{}) +}