Skip to content

Commit

Permalink
Rollback on failure to narrow GADT bounds
Browse files Browse the repository at this point in the history
All credit to Linyxus!
  • Loading branch information
dwijnand committed Jul 26, 2022
1 parent b5bb7ce commit c4f85a4
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
7 changes: 5 additions & 2 deletions compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1945,8 +1945,11 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
val tparam = tr.symbol
gadts.println(i"narrow gadt bound of $tparam: ${tparam.info} from ${if (isUpper) "above" else "below"} to $bound ${bound.toString} ${bound.isRef(tparam)}")
if (bound.isRef(tparam)) false
else if (isUpper) gadtAddUpperBound(tparam, bound)
else gadtAddLowerBound(tparam, bound)
else
val savedGadt = ctx.gadt.fresh
val success = if isUpper then gadtAddUpperBound(tparam, bound) else gadtAddLowerBound(tparam, bound)
if !success then ctx.gadt.restore(savedGadt)
success
}
}

Expand Down
11 changes: 1 addition & 10 deletions compiler/src/dotty/tools/dotc/core/TypeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -712,16 +712,7 @@ object TypeOps:

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

val ctx1 = ctx.fresh.setExploreTyperState().setFreshGADTBounds
val ctx2 = parent match
case _: RefinedType =>
ctx1
// patmat/t9657
// When running Bicycle.type <:< Vehicle { A = P }
// TypeComparer is happy to infer GADT bounds P >: Pedal.type <: Petrol.type & Pedal.type
// Despite the fact that Bicycle is an object, and thus final, so its type A can only be Pedal.type.
case _ => ctx1.addMode(Mode.GadtConstraintInference)
inContext(ctx2) {
inContext(ctx.fresh.setExploreTyperState().setFreshGADTBounds.addMode(Mode.GadtConstraintInference)) {
instantiateToSubType(childTp, parent).dealias
}
}
Expand Down

0 comments on commit c4f85a4

Please sign in to comment.