@@ -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 confirm
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 confirms to the
1167+ // type assigned to the if tree by inserting GADT casts.
11631168 gadtAdaptBranch(t, resType)
11641169 }: @ unchecked
11651170
@@ -4220,7 +4225,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
42204225 if tree.tpe.isSingleton then
42214226 // In the target type, when the singleton type is intersected, we also intersect
42224227 // the GADT-approximated type of the singleton to avoid the loss of
4223- // information. See #14776 .
4228+ // information. See #15646 .
42244229 val gadtApprox = Inferencing .approximateGADT(wtp)
42254230 gadts.println(i " gadt approx $wtp ~~~ $gadtApprox" )
42264231 val conj =
0 commit comments