Skip to content

Commit

Permalink
add documentation and point to scala#15646
Browse files Browse the repository at this point in the history
  • Loading branch information
Linyxus committed Jul 12, 2022
1 parent f1208af commit 682d968
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,10 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
case elsep: untpd.If => isIncomplete(elsep)
case _ => false

// Insert a GADT cast if the type of the branch does not conform
// to the type assigned to the whole if tree.
// This happens when the computation of the type of the if tree
// uses GADT constraints. See #15646.
def gadtAdaptBranch(tree: Tree, branchPt: Type): Tree =
TypeComparer.testSubType(tree.tpe.widenExpr, branchPt) match {
case CompareResult.OKwithGADTUsed =>
Expand All @@ -1157,9 +1161,10 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
}: @unchecked

val resType = thenp1.tpe | elsep1.tpe

val thenp2 :: elsep2 :: Nil =
(thenp1 :: elsep1 :: Nil) map { t =>
// Adapt each branch to ensure that their types conforms to the
// type assigned to the if tree by inserting GADT casts.
gadtAdaptBranch(t, resType)
}: @unchecked

Expand Down Expand Up @@ -4220,7 +4225,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
if tree.tpe.isSingleton then
// In the target type, when the singleton type is intersected, we also intersect
// the GADT-approximated type of the singleton to avoid the loss of
// information. See #14776.
// information. See #15646.
val gadtApprox = Inferencing.approximateGADT(wtp)
gadts.println(i"gadt approx $wtp ~~~ $gadtApprox")
val conj =
Expand Down

0 comments on commit 682d968

Please sign in to comment.