Skip to content

Commit 021c251

Browse files
oderskyDarkDimius
authored andcommitted
Merge refined types when distributing via "|".
Use the equality (where ~ is any form of refinement) T1 { x ~ R1 } & T2 { x ~ R2 } == T1 & T2 { x ~ R1 & R2 } We already did the same thing when distributing via "&".
1 parent 88c4a6c commit 021c251

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

src/dotty/tools/dotc/core/TypeComparer.scala

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,19 @@ class TypeComparer(initctx: Context) extends DotClass {
890890

891891
/** Try to distribute `&` inside type, detect and handle conflicts */
892892
private def distributeAnd(tp1: Type, tp2: Type): Type = tp1 match {
893+
// opportunistically merge same-named refinements
894+
// this does not change anything semantically (i.e. merging or not merging
895+
// gives =:= types), but it keeps the type smaller.
896+
case tp1: RefinedType =>
897+
tp2 match {
898+
case tp2: RefinedType if tp1.refinedName == tp2.refinedName =>
899+
tp1.derivedRefinedType(
900+
tp1.parent & tp2.parent,
901+
tp1.refinedName,
902+
tp1.refinedInfo & tp2.refinedInfo)
903+
case _ =>
904+
NoType
905+
}
893906
case tp1: TypeBounds =>
894907
tp2 match {
895908
case tp2: TypeBounds => tp1 & tp2
@@ -935,18 +948,6 @@ class TypeComparer(initctx: Context) extends DotClass {
935948
case _ =>
936949
rt1 & tp2
937950
}
938-
case tp1: RefinedType =>
939-
// opportunistically merge same-named refinements
940-
// this does not change anything semantically (i.e. merging or not merging
941-
// gives =:= types), but it keeps the type smaller.
942-
tp2 match {
943-
case tp2: RefinedType if tp1.refinedName == tp2.refinedName =>
944-
tp1.derivedRefinedType(
945-
tp1.parent & tp2.parent, tp1.refinedName,
946-
tp1.refinedInfo & tp2.refinedInfo)
947-
case _ =>
948-
NoType
949-
}
950951
case tp1: TypeVar if tp1.isInstantiated =>
951952
tp1.underlying & tp2
952953
case tp1: AnnotatedType =>
@@ -957,6 +958,16 @@ class TypeComparer(initctx: Context) extends DotClass {
957958

958959
/** Try to distribute `|` inside type, detect and handle conflicts */
959960
private def distributeOr(tp1: Type, tp2: Type): Type = tp1 match {
961+
case tp1: RefinedType =>
962+
tp2 match {
963+
case tp2: RefinedType if tp1.refinedName == tp2.refinedName =>
964+
tp1.derivedRefinedType(
965+
tp1.parent | tp2.parent,
966+
tp1.refinedName,
967+
tp1.refinedInfo | tp2.refinedInfo)
968+
case _ =>
969+
NoType
970+
}
960971
case tp1: TypeBounds =>
961972
tp2 match {
962973
case tp2: TypeBounds => tp1 | tp2

0 commit comments

Comments
 (0)