File tree Expand file tree Collapse file tree 2 files changed +18
-5
lines changed
compiler/src/dotty/tools/dotc Expand file tree Collapse file tree 2 files changed +18
-5
lines changed Original file line number Diff line number Diff line change @@ -27,6 +27,8 @@ object TyperState {
27
27
28
28
opaque type Snapshot = (Constraint , TypeVars , LevelMap )
29
29
30
+ class BadTyperStateAssertion (msg : String ) extends AssertionError (msg)
31
+
30
32
extension (ts : TyperState )
31
33
def snapshot ()(using Context ): Snapshot =
32
34
(ts.constraint, ts.ownedVars, ts.upLevels)
@@ -42,7 +44,7 @@ object TyperState {
42
44
}
43
45
44
46
class TyperState () {
45
- import TyperState .LevelMap
47
+ import TyperState .{ LevelMap , BadTyperStateAssertion }
46
48
47
49
private var myId : Int = _
48
50
def id : Int = myId
@@ -268,8 +270,10 @@ class TyperState() {
268
270
*/
269
271
private def includeVar (tvar : TypeVar )(using Context ): Unit =
270
272
val oldState = tvar.owningState.nn.get
271
- assert(oldState == null || ! oldState.isCommittable,
272
- i " $this attempted to take ownership of $tvar which is already owned by committable $oldState" )
273
+
274
+ if oldState != null && oldState.isCommittable then
275
+ throw BadTyperStateAssertion (
276
+ i " $this attempted to take ownership of $tvar which is already owned by committable $oldState" )
273
277
tvar.owningState = new WeakReference (this )
274
278
ownedVars += tvar
275
279
Original file line number Diff line number Diff line change @@ -3869,11 +3869,20 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
3869
3869
// But in this case we should search with additional arguments typed only if there
3870
3870
// is no default argument.
3871
3871
3872
+ // Try to constrain the result using `pt1`, but back out if a BadTyperStateAssertion
3873
+ // is thrown. TODO Find out why the bad typer state arises and prevent it. The try-catch
3874
+ // is a temporary hack to keep projects compiling that would fail otherwise due to
3875
+ // searching more arguments to instantiate implicits (PR #23532). A failing project
3876
+ // is described in issue #23609.
3877
+ def tryConstrainResult (pt : Type ): Boolean =
3878
+ try constrainResult(tree.symbol, wtp, pt)
3879
+ catch case ex : TyperState .BadTyperStateAssertion => false
3880
+
3872
3881
arg.tpe match
3873
3882
case failed : SearchFailureType if canProfitFromMoreConstraints =>
3874
3883
val pt1 = pt.deepenProtoTrans
3875
- if (pt1 `ne` pt) && (pt1 ne sharpenedPt) && constrainResult(tree.symbol, wtp, pt1)
3876
- then return implicitArgs(formals, argIndex, pt1)
3884
+ if (pt1 `ne` pt) && (pt1 ne sharpenedPt) && tryConstrainResult( pt1) then
3885
+ return implicitArgs(formals, argIndex, pt1)
3877
3886
case _ =>
3878
3887
3879
3888
arg.tpe match
You can’t perform that action at this time.
0 commit comments