From e239968b80413076937ba9a206ee5ec33f481bc4 Mon Sep 17 00:00:00 2001 From: metagn Date: Sat, 16 Nov 2024 00:52:38 +0300 Subject: [PATCH] fix wrong error for iterators with no body and pragma macro (#24440) fixes #16413 `semIterator` checks if the original iterator passed to it has no body, but it should check the processed node created by `semProcAux`. --- compiler/semstmts.nim | 2 +- tests/pragmas/titeratormacro.nim | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 tests/pragmas/titeratormacro.nim diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index 8061f316df..5ca9c1f983 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -2648,7 +2648,7 @@ proc semIterator(c: PContext, n: PNode): PNode = incl(s.typ.flags, tfCapturesEnv) else: s.typ.callConv = ccInline - if n[bodyPos].kind == nkEmpty and s.magic == mNone and c.inConceptDecl == 0: + if result[bodyPos].kind == nkEmpty and s.magic == mNone and c.inConceptDecl == 0: localError(c.config, n.info, errImplOfXexpected % s.name.s) if optOwnedRefs in c.config.globalOptions and result.typ != nil: result.typ() = makeVarType(c, result.typ, tyOwned) diff --git a/tests/pragmas/titeratormacro.nim b/tests/pragmas/titeratormacro.nim new file mode 100644 index 0000000000..7546995569 --- /dev/null +++ b/tests/pragmas/titeratormacro.nim @@ -0,0 +1,17 @@ +# issue #16413 + +import std/macros + +macro identity(x: untyped) = + result = x.copy() + result[6] = quote do: + yield 1 + discard result.toStrLit + +iterator demo(): int {.identity.} +iterator demo2(): int {.identity.} = discard # but this works as expected + +var s: seq[int] = @[] +for e in demo(): + s.add(e) +doAssert s == @[1]