diff --git a/compiler/sempass2.nim b/compiler/sempass2.nim index 88106b635f..1aca972261 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)] @@ -577,11 +578,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..