From 6353c4e5b0b6e3194f1eb888cdcd6f329bc8ea54 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Fri, 10 Apr 2026 21:57:26 +0800 Subject: [PATCH] fixes #25724; Invalid C code generation with iterator/nimvm (#25728) fixes #25724 This pull request introduces a small but important fix in the compiler and adds a new test case related to iterators. The main change in the compiler ensures that lambda-like constructs are handled consistently with other procedure definitions, while the new test in the suite covers a previously untested scenario. **Compiler improvements:** * Updated `introduceNewLocalVars` in `compiler/transf.nim` to handle all `nkLambdaKinds` in addition to `nkProcDef`, `nkFuncDef`, `nkMethodDef`, and `nkConverterDef`, ensuring consistent transformation of all lambda-like constructs. **Testing:** * Added a block to `tests/iter/titer_issues.nim` to test iterator behavior in both compile-time and run-time contexts, addressing bug #25724. --- compiler/transf.nim | 2 +- tests/iter/titer_issues.nim | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/compiler/transf.nim b/compiler/transf.nim index e85ecd3e07..399a8c468a 100644 --- a/compiler/transf.nim +++ b/compiler/transf.nim @@ -336,7 +336,7 @@ proc introduceNewLocalVars(c: PTransf, n: PNode): PNode = if a.kind == nkSym: n[1] = transformSymAux(c, a) return n - of nkProcDef, nkFuncDef, nkMethodDef, nkConverterDef: # todo optimize nosideeffects? + of nkLambdaKinds, nkProcDef, nkFuncDef, nkMethodDef, nkConverterDef: # todo optimize nosideeffects? result = newTransNode(n) let x = newSymNode(copySym(n[namePos].sym, c.idgen)) c.transCon.mapping[n[namePos].sym.itemId] = x diff --git a/tests/iter/titer_issues.nim b/tests/iter/titer_issues.nim index 5070a54713..468c230997 100644 --- a/tests/iter/titer_issues.nim +++ b/tests/iter/titer_issues.nim @@ -457,3 +457,12 @@ let runes1 = buggyVersion("en") # <-- CRASHES HERE doAssert runes1.len == runes2.len # echo "Got ", runes1.len, " runes" + + +block: # bug #25724 + iterator c(): int = + when nimvm: yield 0 + else: yield 1 + for w in c(): + let n = w + (proc() = discard n)() \ No newline at end of file