From 0f751695e4fb27c36ddc2048ea26c729e33b22f6 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Sat, 13 Jun 2026 13:03:28 +0800 Subject: [PATCH] fixes #25903 and #25904; add closure iterators with error handling (#25905) fixes #25903 fixes #25904 `nkExceptBranch` can have variable structure depending on the exception types and it should handle the last node of the `nkExceptBranch` --- compiler/closureiters.nim | 5 +---- tests/closure/tclosure_issues.nim | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/compiler/closureiters.nim b/compiler/closureiters.nim index 6eeffb4099..f96685cbbd 100644 --- a/compiler/closureiters.nim +++ b/compiler/closureiters.nim @@ -593,10 +593,7 @@ proc lowerStmtListExprs(ctx: var Ctx, n: PNode, needsSplit: var bool): PNode = let branch = n[i] case branch.kind of nkExceptBranch: - if branch[0].kind == nkType: - branch[1] = ctx.convertExprBodyToAsgn(branch[1], tmp) - else: - branch[0] = ctx.convertExprBodyToAsgn(branch[0], tmp) + branch[^1] = ctx.convertExprBodyToAsgn(branch[^1], tmp) of nkFinally: discard else: diff --git a/tests/closure/tclosure_issues.nim b/tests/closure/tclosure_issues.nim index b1a2d7c6b6..75dc6ada79 100644 --- a/tests/closure/tclosure_issues.nim +++ b/tests/closure/tclosure_issues.nim @@ -80,3 +80,20 @@ block tissue7104: sp do (): inc i echo "ok ", i + +block: # bug #25903 + iterator g: int {.closure.} = + discard try: + yield 0 + 0 + except IOError, OSError: + 0 + let _ = g + +block: # bug #25904 + iterator w: int {.closure.} = + discard try: 0 + except IOError, OSError: + yield 0 + 0 + let _ = w