Skip to content

Commit 512a307

Browse files
committed
Space: Move out Typ-wrapping in decompose
1 parent e316f65 commit 512a307

File tree

1 file changed

+15
-21
lines changed
  • compiler/src/dotty/tools/dotc/transform/patmat

1 file changed

+15
-21
lines changed

compiler/src/dotty/tools/dotc/transform/patmat/Space.scala

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -584,46 +584,40 @@ object SpaceEngine {
584584

585585
/** Decompose a type into subspaces -- assume the type can be decomposed */
586586
def decompose(tp: Type)(using Context): List[Typ] = trace(i"decompose($tp)", debug, showSpaces) {
587-
def rec(tp: Type, mixins: List[Type]): List[Typ] = tp.dealias match {
587+
def rec(tp: Type, mixins: List[Type]): List[Type] = tp.dealias match {
588588
case AndType(tp1, tp2) =>
589-
def decomposeComponent(tpA: Type, tpB: Type): List[Typ] =
590-
rec(tpA, tpB :: mixins).flatMap {
591-
case Typ(tp, _) =>
592-
if tp <:< tpB then
593-
Typ(tp, decomposed = true) :: Nil
594-
else if tpB <:< tp then
595-
Typ(tpB, decomposed = true) :: Nil
596-
else if TypeComparer.provablyDisjoint(tp, tpB) then
597-
Nil
598-
else
599-
Typ(AndType(tp, tpB), decomposed = true) :: Nil
589+
def decomposeComponent(tpA: Type, tpB: Type): List[Type] =
590+
rec(tpA, tpB :: mixins).collect {
591+
case tp if tp <:< tpB => tp
592+
case tp if tpB <:< tp => tpB
593+
case tp if !TypeComparer.provablyDisjoint(tp, tpB) => AndType(tp, tpB)
600594
}
601595

602596
if canDecompose(tp1) then
603597
decomposeComponent(tp1, tp2)
604598
else
605599
decomposeComponent(tp2, tp1)
606600

607-
case OrType(tp1, tp2) => List(Typ(tp1, decomposed = true), Typ(tp2, decomposed = true))
601+
case OrType(tp1, tp2) => List(tp1, tp2)
608602
case tp if tp.isRef(defn.BooleanClass) =>
609603
List(
610-
Typ(ConstantType(Constant(true)), decomposed = true),
611-
Typ(ConstantType(Constant(false)), decomposed = true)
604+
ConstantType(Constant(true)),
605+
ConstantType(Constant(false))
612606
)
613607
case tp if tp.isRef(defn.UnitClass) =>
614-
Typ(ConstantType(Constant(())), decomposed = true) :: Nil
608+
ConstantType(Constant(())) :: Nil
615609
case tp if tp.classSymbol.isAllOf(JavaEnumTrait) =>
616-
tp.classSymbol.children.map(sym => Typ(sym.termRef, decomposed = true))
610+
tp.classSymbol.children.map(_.termRef)
617611

618612
case tp @ AppliedType(tycon, targs) if tp.classSymbol.children.isEmpty && canDecompose(tycon) =>
619613
// It might not obvious that it's OK to apply the type arguments of a parent type to child types.
620614
// But this is guarded by `tp.classSymbol.children.isEmpty`,
621615
// meaning we'll decompose to the same class, just not the same type.
622616
// For instance, from i15029, `decompose((X | Y).Field[T]) = [X.Field[T], Y.Field[T]]`.
623-
rec(tycon, Nil).map(typ => Typ(tp.derivedAppliedType(typ.tp, targs), decomposed = true))
617+
rec(tycon, Nil).map(tp.derivedAppliedType(_, targs))
624618

625619
case tp: NamedType if canDecompose(tp.prefix) =>
626-
rec(tp.prefix, Nil).map(typ => Typ(tp.derivedSelect(typ.tp), decomposed = true))
620+
rec(tp.prefix, Nil).map(tp.derivedSelect)
627621

628622
case tp =>
629623
def getChildren(sym: Symbol): List[Symbol] =
@@ -655,9 +649,9 @@ object SpaceEngine {
655649

656650
debug.println(i"$tp decomposes to $parts")
657651

658-
parts.map(Typ(_, decomposed = true))
652+
parts
659653
}
660-
rec(tp, Nil)
654+
rec(tp, Nil).map(Typ(_, decomposed = true))
661655
}
662656

663657
/** Abstract sealed types, or-types, Boolean and Java enums can be decomposed */

0 commit comments

Comments
 (0)