@@ -1135,6 +1135,10 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
1135
1135
case elsep : untpd.If => isIncomplete(elsep)
1136
1136
case _ => false
1137
1137
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.
1138
1142
def gadtAdaptBranch (tree : Tree , branchPt : Type ): Tree =
1139
1143
TypeComparer .testSubType(tree.tpe.widenExpr, branchPt) match {
1140
1144
case CompareResult .OKwithGADTUsed =>
@@ -1157,9 +1161,10 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
1157
1161
}: @ unchecked
1158
1162
1159
1163
val resType = thenp1.tpe | elsep1.tpe
1160
-
1161
1164
val thenp2 :: elsep2 :: Nil =
1162
1165
(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.
1163
1168
gadtAdaptBranch(t, resType)
1164
1169
}: @ unchecked
1165
1170
@@ -4218,7 +4223,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
4218
4223
if tree.tpe.isSingleton then
4219
4224
// In the target type, when the singleton type is intersected, we also intersect
4220
4225
// the GADT-approximated type of the singleton to avoid the loss of
4221
- // information. See #14776 .
4226
+ // information. See #15646 .
4222
4227
val gadtApprox = Inferencing .approximateGADT(wtp)
4223
4228
gadts.println(i " gadt approx $wtp ~~~ $gadtApprox" )
4224
4229
val conj =
0 commit comments