From 196f4446993d280a14e13f7fd54927144083262f Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Fri, 16 Jan 2026 22:25:14 +0800 Subject: [PATCH] fixes #25400; Naked raised causes wrong exception effect (#25422) fixes #25400 infers `Exception` for Naked raised (cherry picked from commit 9a23ff36bd3f9d3d4c15540f65d21fa9ed709bdd) --- compiler/sempass2.nim | 24 +++++++++++++++++++++++- tests/effects/teffects8.nim | 2 +- tests/effects/teffectsmisc.nim | 21 +++++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/compiler/sempass2.nim b/compiler/sempass2.nim index 9c864feee1..2245c32ed1 100644 --- a/compiler/sempass2.nim +++ b/compiler/sempass2.nim @@ -84,6 +84,7 @@ type gcUnsafe, isRecursive, isTopLevel, hasSideEffect, inEnforcedGcSafe: bool isInnerProc: bool inEnforcedNoSideEffects: bool + currentExceptType: PType unknownRaises: seq[(PSym, TLineInfo)] currOptions: TOptions optionsStack: seq[(TOptions, TNoteKinds)] @@ -576,11 +577,25 @@ proc trackTryStmt(tracked: PEffects, n: PNode) = let b = n[i] if b.kind == nkExceptBranch: setLen(tracked.init, oldState) + # If this except branch catches exactly one type, record it so an + # empty `raise` inside the branch can be inferred as re-raising that + # specific exception type instead of the generic `Exception`. + var savedExcept: PType = tracked.currentExceptType + var inferredExcept: PType = nil + if b.len == 2: + if b[0].isInfixAs(): + assert(b[0][1].kind == nkType) + inferredExcept = b[0][1].typ + else: + assert(b[0].kind == nkType) + inferredExcept = b[0].typ + tracked.currentExceptType = inferredExcept for j in 0..