Skip to content

Commit 2255fdb

Browse files
committed
add documentation and point to #15646
1 parent 3d52d1e commit 2255fdb

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

compiler/src/dotty/tools/dotc/typer/Typer.scala

+7-2
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,10 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
11351135
case elsep: untpd.If => isIncomplete(elsep)
11361136
case _ => false
11371137

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

11591163
val resType = thenp1.tpe | elsep1.tpe
1160-
11611164
val thenp2 :: elsep2 :: Nil =
11621165
(thenp1 :: elsep1 :: Nil) map { t =>
1166+
// Adapt each branch to ensure that their types conforms to the
1167+
// type assigned to the if tree by inserting GADT casts.
11631168
gadtAdaptBranch(t, resType)
11641169
}: @unchecked
11651170

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

0 commit comments

Comments
 (0)