Skip to content

Commit 430251c

Browse files
committed
insert GADT casts for if branches
1 parent b184009 commit 430251c

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,12 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
11351135
case elsep: untpd.If => isIncomplete(elsep)
11361136
case _ => false
11371137

1138+
def gadtAdaptBranch(tree: Tree, branchPt: Type): Tree =
1139+
TypeComparer.testSubType(tree.tpe.widenExpr, branchPt) match {
1140+
case CompareResult.OKwithGADTUsed => tree.cast(branchPt)
1141+
case _ => tree
1142+
}
1143+
11381144
val branchPt = if isIncomplete(tree) then defn.UnitType else pt.dropIfProto
11391145

11401146
val result =
@@ -1148,7 +1154,13 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
11481154
val elsep0 = typed(tree.elsep, branchPt)(using cond1.nullableContextIf(false))
11491155
thenp0 :: elsep0 :: Nil
11501156
}: @unchecked
1151-
assignType(cpy.If(tree)(cond1, thenp1, elsep1), thenp1, elsep1)
1157+
1158+
val resType = thenp1.tpe | elsep1.tpe
1159+
1160+
val thenp2 = gadtAdaptBranch(thenp1, resType)
1161+
val elsep2 = gadtAdaptBranch(elsep1, resType)
1162+
1163+
cpy.If(tree)(cond1, thenp2, elsep2).withType(resType)
11521164

11531165
def thenPathInfo = cond1.notNullInfoIf(true).seq(result.thenp.notNullInfo)
11541166
def elsePathInfo = cond1.notNullInfoIf(false).seq(result.elsep.notNullInfo)

0 commit comments

Comments
 (0)