Skip to content

Handle assertion error in TyperState #23665

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions compiler/src/dotty/tools/dotc/core/TyperState.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,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)
Expand All @@ -43,7 +45,7 @@ object TyperState {
}

class TyperState() {
import TyperState.LevelMap
import TyperState.{LevelMap, BadTyperStateAssertion}

private var myId: Int = uninitialized
def id: Int = myId
Expand Down Expand Up @@ -269,8 +271,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

Expand Down
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/typer/Migrations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ trait Migrations:
private def checkParentheses(tree: Tree, pt: FunProto)(using Context): Boolean =
val ptSpan = pt.args.head.span
ptSpan.exists
&& tree.span.exists
&& ctx.source.content
.slice(tree.span.end, ptSpan.start)
.exists(_ == '(')
Expand Down
13 changes: 11 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4368,11 +4368,20 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
|| !isFullyDefined(formal, ForceDegree.none)
// more context might constrain type variables which could make implicit scope larger

// 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
Expand Down
Loading