From ba2d8ba468bff23ca8789b9cd6f9ffabbc2af23d Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Sat, 22 Jul 2023 12:37:27 +0800 Subject: [PATCH] fixes #22297; return in the finally in the closure iterators (#22300) ref #22297; return in the finally in the closure iterators (cherry picked from commit b02c1dd6ca96548b47d978f96278c67bf59e9d9e) --- compiler/closureiters.nim | 4 +++- tests/closure/tclosure.nim | 11 +++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/compiler/closureiters.nim b/compiler/closureiters.nim index e4137abcc0..83d55b4444 100644 --- a/compiler/closureiters.nim +++ b/compiler/closureiters.nim @@ -852,7 +852,9 @@ proc transformReturnsInTry(ctx: var Ctx, n: PNode): PNode = case n.kind of nkReturnStmt: # We're somewhere in try, transform to finally unrolling - assert(ctx.nearestFinally != 0) + if ctx.nearestFinally == 0: + # return is within the finally + return result = newNodeI(nkStmtList, n.info) diff --git a/tests/closure/tclosure.nim b/tests/closure/tclosure.nim index 8ae6c44bbe..829f16e013 100644 --- a/tests/closure/tclosure.nim +++ b/tests/closure/tclosure.nim @@ -491,3 +491,14 @@ block tnoclosure: row = zip(row & @[0], @[0] & row).mapIt(it[0] + it[1]) echo row pascal(10) + +block: # bug #22297 + iterator f: int {.closure.} = + try: + yield 12 + finally: + return 14 + + let s = f + doAssert s() == 12 + doAssert s() == 14