Skip to content

for-loop plugins: a local the plugin declares is exempt from the unused check (fixes #3312) - #3341

Open
konaito wants to merge 1 commit into
nim-lang:masterfrom
konaito:fix/3312-forloop-plugin-unused
Open

konaito wants to merge 1 commit into
nim-lang:masterfrom
konaito:fix/3312-forloop-plugin-unused

Conversation

@konaito

@konaito konaito commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Fixes #3312.

A template expansion and a macro plugin expansion push their call site onto templCallInfos while their output is semchecked, so semPragmas marks the locals they declare (used) (#3276, #3310). tryForLoopPlugin re-semchecks the plugin output without doing so, so a local that a for-loop plugin declares and the loop body does not reference is reported as unused. In std/parfor, for i in 0 ||< 100: with a body that does not read i fails with 'pforI' is declared but not used.

  • src/nimony/sem.nim: tryForLoopPlugin pushes the for loop's position onto templCallInfos around the semExpr of the plugin output, as the macro plugin path does.
  • tests/nimony/plugins/deps/mforloop.nim: a withLocal mode that declares let pluginTmp = 1 before the loop body.
  • tests/nimony/plugins/tforloopplugin_local.nim: for x in withLocal() and for _ in withLocal() inside procs, where the body does not use the loop variable.

As with a template's untyped body, a local the user declares inside the loop body is semchecked as part of the expansion and is exempt as well: let unused = 1 in a ||< body is no longer reported, the same as in the body passed to a template once(body: untyped).

Tested on macOS arm64 with Nim devel: the new test fails on master ('pluginTmp' is declared but not used) and passes with this change; the ||< loop from the issue (with ||< in place of ||) prints its result instead of the error; nim c -r tools/tester.nim (with HASTUR_SKIP=tests/boot) reports 1530 / 1530 tests successful.

@@ -0,0 +1,17 @@
# A local that a for-loop plugin declares is exempt from the unused check, as

@Araq Araq Dec 5, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But why? for x in withLocal(): noUseOf(x) is like any other code, flag the unused x.

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.

For-loop plugin locals are "declared but not used" since #3276

2 participants