From 2c6191aa4da97acdc6dc40e9a959e1ec75fae4d4 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Tue, 9 Jun 2026 04:59:16 +0800 Subject: [PATCH] fixes #22122; raise effects for complex expressions (#25845) fixes #22122 The root cause is in the effect tracker: raise was recording the whole conditional expression as one exception source, so semantic checking only saw the widened common base type instead of the concrete exception classes from each branch. (cherry picked from commit e942da94b5b52fa7e09b133d52125037ba69c7b7) --- compiler/sempass2.nim | 22 +++++++++- tests/effects/tcase_raises.nim | 74 ++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 tests/effects/tcase_raises.nim diff --git a/compiler/sempass2.nim b/compiler/sempass2.nim index 4ff8ca3126..73c849b3a9 100644 --- a/compiler/sempass2.nim +++ b/compiler/sempass2.nim @@ -496,6 +496,26 @@ proc addRaiseEffect(a: PEffects, e, comesFrom: PNode) = if not isDefectException(e.typ): throws(a.exc, e, comesFrom) +proc addRaiseEffectsFromExpr(a: PEffects, e, comesFrom: PNode) = + if e.isNil: + return + let x = skipConvCastAndClosure(e) + case x.kind + of nkStmtList, nkStmtListExpr, nkBlockStmt, nkBlockExpr: + if x.len > 0: + addRaiseEffectsFromExpr(a, x.lastSon, comesFrom) + of nkIfExpr, nkIfStmt: + for branch in items(x): + if branch.len > 0: + addRaiseEffectsFromExpr(a, branch.lastSon, comesFrom) + of nkCaseStmt: + for i in 1.. 0: + addRaiseEffectsFromExpr(a, branch.lastSon, comesFrom) + else: + addRaiseEffect(a, x, x) + proc addTag(a: PEffects, e, comesFrom: PNode) = var aa = a.tags for i in 0..