Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/nimony/sem.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4011,7 +4011,11 @@ proc tryForLoopPlugin(c: var SemContext; dest: var TokenBuf; it: var Item;
var pluginOutput = createTokenBuf(30)
runPlugin(c, pluginOutput, pp.info, pool.strings[pp.strId], b)
var expandedItem = Item(n: cursorAt(pluginOutput, 0), typ: c.types.autoType)
# as for a template or macro expansion: the locals the plugin declares are
# exempt from the unused check (`semPragmas`)
c.templCallInfos.add info
semExpr c, dest, expandedItem
discard c.templCallInfos.pop()
producesNoReturn c, dest, info, it.typ

proc semFor(c: var SemContext; dest: var TokenBuf; it: var Item) =
Expand Down
10 changes: 10 additions & 0 deletions tests/nimony/plugins/deps/mforloop.nim
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ proc transform(n: NifCursor): NifBuilder =
for i in 1..3:
var body = forLoopBody(n)
emitSubst(result, body, loopSym, i)
elif mode == "withLocal":
# declares a local that the loop body never references
result.withTree StmtsS, info:
result.openTree("let")
result.addIdent("pluginTmp")
result.addEmptyNode3()
result.addIntLit(1)
result.closeTree()
var body = forLoopBody(n)
result.addSubtree(body)
elif mode == "valueSource":
result = emitValueSource(n)
elif mode == "pluginValues":
Expand Down
17 changes: 17 additions & 0 deletions tests/nimony/plugins/tforloopplugin_local.nim
Original file line number Diff line number Diff line change
@@ -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.

# one from a template or macro expansion is: the caller cannot mark it
# `{.used.}`.
import std / syncio

iterator withLocal(): int {.plugin: "deps/mforloop".}

proc named =
for x in withLocal():
echo "named"

proc underscore =
for _ in withLocal():
echo "underscore"

named()
underscore()
2 changes: 2 additions & 0 deletions tests/nimony/plugins/tforloopplugin_local.output
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
named
underscore
Loading