Skip to content

Commit c4f85a4

Browse files
committed
Rollback on failure to narrow GADT bounds
All credit to Linyxus!
1 parent b5bb7ce commit c4f85a4

File tree

2 files changed

+6
-12
lines changed

2 files changed

+6
-12
lines changed

compiler/src/dotty/tools/dotc/core/TypeComparer.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1945,8 +1945,11 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
19451945
val tparam = tr.symbol
19461946
gadts.println(i"narrow gadt bound of $tparam: ${tparam.info} from ${if (isUpper) "above" else "below"} to $bound ${bound.toString} ${bound.isRef(tparam)}")
19471947
if (bound.isRef(tparam)) false
1948-
else if (isUpper) gadtAddUpperBound(tparam, bound)
1949-
else gadtAddLowerBound(tparam, bound)
1948+
else
1949+
val savedGadt = ctx.gadt.fresh
1950+
val success = if isUpper then gadtAddUpperBound(tparam, bound) else gadtAddLowerBound(tparam, bound)
1951+
if !success then ctx.gadt.restore(savedGadt)
1952+
success
19501953
}
19511954
}
19521955

compiler/src/dotty/tools/dotc/core/TypeOps.scala

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -712,16 +712,7 @@ object TypeOps:
712712

713713
val childTp = if (child.isTerm) child.termRef else child.typeRef
714714

715-
val ctx1 = ctx.fresh.setExploreTyperState().setFreshGADTBounds
716-
val ctx2 = parent match
717-
case _: RefinedType =>
718-
ctx1
719-
// patmat/t9657
720-
// When running Bicycle.type <:< Vehicle { A = P }
721-
// TypeComparer is happy to infer GADT bounds P >: Pedal.type <: Petrol.type & Pedal.type
722-
// Despite the fact that Bicycle is an object, and thus final, so its type A can only be Pedal.type.
723-
case _ => ctx1.addMode(Mode.GadtConstraintInference)
724-
inContext(ctx2) {
715+
inContext(ctx.fresh.setExploreTyperState().setFreshGADTBounds.addMode(Mode.GadtConstraintInference)) {
725716
instantiateToSubType(childTp, parent).dealias
726717
}
727718
}

0 commit comments

Comments
 (0)