From 7b25040d91db7236007d855f4781abf0041d898f Mon Sep 17 00:00:00 2001 From: odersky Date: Tue, 5 Aug 2025 21:52:01 +0200 Subject: [PATCH 1/2] Handle assertion error in TyperState (#23665) explanation seems to be in ProtoTypes.scala: ```scala // To respect the pre-condition of `mergeConstraintWith` and keep // `protoTyperState` committable we must ensure that it does not // contain any type variable which don't already exist in the passed // TyperState. This is achieved by instantiating any such type // variable. NOTE: this does not suffice to discard type variables // in ancestors of `protoTyperState`, if this situation ever // comes up, an assertion in TyperState will trigger and this code // will need to be generalized. ``` We should go to the bottom of it and fix the assertion. But before that's done this PR offers a temporary hack to catch the exception when it is triggered from a new code path created by PR #23532. This should fix the regression reported in #23609. We should leave the issue open as a reminder that we still need a better fix. Also: handle crash due to missing span in a migration helper. --- compiler/src/dotty/tools/dotc/core/TyperState.scala | 10 +++++++--- compiler/src/dotty/tools/dotc/typer/Typer.scala | 13 +++++++++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/core/TyperState.scala b/compiler/src/dotty/tools/dotc/core/TyperState.scala index 046cfe3e935c..55a7560d976f 100644 --- a/compiler/src/dotty/tools/dotc/core/TyperState.scala +++ b/compiler/src/dotty/tools/dotc/core/TyperState.scala @@ -27,6 +27,8 @@ object TyperState { opaque type Snapshot = (Constraint, TypeVars, LevelMap) + class BadTyperStateAssertion(msg: String) extends AssertionError(msg) + extension (ts: TyperState) def snapshot()(using Context): Snapshot = (ts.constraint, ts.ownedVars, ts.upLevels) @@ -42,7 +44,7 @@ object TyperState { } class TyperState() { - import TyperState.LevelMap + import TyperState.{LevelMap, BadTyperStateAssertion} private var myId: Int = _ def id: Int = myId @@ -268,8 +270,10 @@ class TyperState() { */ private def includeVar(tvar: TypeVar)(using Context): Unit = val oldState = tvar.owningState.nn.get - assert(oldState == null || !oldState.isCommittable, - i"$this attempted to take ownership of $tvar which is already owned by committable $oldState") + + if oldState != null && oldState.isCommittable then + throw BadTyperStateAssertion( + i"$this attempted to take ownership of $tvar which is already owned by committable $oldState") tvar.owningState = new WeakReference(this) ownedVars += tvar diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index 6289f310ac1e..94cabdbc7b22 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -3869,11 +3869,20 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer // But in this case we should search with additional arguments typed only if there // is no default argument. + // Try to constrain the result using `pt1`, but back out if a BadTyperStateAssertion + // is thrown. TODO Find out why the bad typer state arises and prevent it. The try-catch + // is a temporary hack to keep projects compiling that would fail otherwise due to + // searching more arguments to instantiate implicits (PR #23532). A failing project + // is described in issue #23609. + def tryConstrainResult(pt: Type): Boolean = + try constrainResult(tree.symbol, wtp, pt) + catch case ex: TyperState.BadTyperStateAssertion => false + arg.tpe match case failed: SearchFailureType if canProfitFromMoreConstraints => val pt1 = pt.deepenProtoTrans - if (pt1 `ne` pt) && (pt1 ne sharpenedPt) && constrainResult(tree.symbol, wtp, pt1) - then return implicitArgs(formals, argIndex, pt1) + if (pt1 `ne` pt) && (pt1 ne sharpenedPt) && tryConstrainResult(pt1) then + return implicitArgs(formals, argIndex, pt1) case _ => arg.tpe match From ab25ded9732eb468628513d7a83e26f86196d3c1 Mon Sep 17 00:00:00 2001 From: Tomasz Godzik Date: Thu, 28 Aug 2025 13:24:56 +0200 Subject: [PATCH 2/2] Handle assertion error in TyperState (#23665) #23609 triggers an assertion error in TyperState. The relevant explanation seems to be in ProtoTypes.scala: ```scala // To respect the pre-condition of `mergeConstraintWith` and keep // `protoTyperState` committable we must ensure that it does not // contain any type variable which don't already exist in the passed // TyperState. This is achieved by instantiating any such type // variable. NOTE: this does not suffice to discard type variables // in ancestors of `protoTyperState`, if this situation ever // comes up, an assertion in TyperState will trigger and this code // will need to be generalized. ``` We should go to the bottom of it and fix the assertion. But before that's done this PR offers a temporary hack to catch the exception when it is triggered from a new code path created by PR #23532. This should fix the regression reported in #23609. We should leave the issue open as a reminder that we still need a better fix. Also: handle crash due to missing span in a migration helper. [Cherry-picked 408298d7bf73921303c31c330deae8c618b589e7][modified]