Skip to content

Commit ac6a8f4

Browse files
Refactor decomposition logic
This commit renames isClosedSum to isDecomposable, and moves that function and the decompose function to where they are used locally. The logic in question only makes sense at the precise point where it's used (see the added comment), so having those two functions defined locally helps to avoid confusion.
1 parent 7d4f46e commit ac6a8f4

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

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

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2526,17 +2526,6 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
25262526
case _ => false
25272527
})
25282528

2529-
/** Can we enumerate all instantiations of this type? */
2530-
def isClosedSum(tp: Symbol): Boolean =
2531-
tp.is(Sealed) && !tp.hasAnonymousChild
2532-
2533-
/** Splits a closed type into a disjunction of smaller types.
2534-
* It should hold that `tp` and `decompose(tp).reduce(_ or _)`
2535-
* denote the same set of values.
2536-
*/
2537-
def decompose(sym: Symbol, tp: Type): List[Type] =
2538-
sym.children.map(x => refineUsingParent(tp, x)).filter(_.exists)
2539-
25402529
def fullyInstantiated(tp: Type): Boolean = new TypeAccumulator[Boolean] {
25412530
override def apply(x: Boolean, t: Type) =
25422531
x && {
@@ -2558,6 +2547,10 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
25582547
case (tp1: TypeRef, tp2: TypeRef) if tp1.symbol.isClass && tp2.symbol.isClass =>
25592548
val cls1 = tp1.classSymbol
25602549
val cls2 = tp2.classSymbol
2550+
def isDecomposable(tp: Symbol): Boolean =
2551+
tp.is(Sealed) && !tp.hasAnonymousChild
2552+
def decompose(sym: Symbol, tp: Type): List[Type] =
2553+
sym.children.map(x => refineUsingParent(tp, x)).filter(_.exists)
25612554
if (cls1.derivesFrom(cls2) || cls2.derivesFrom(cls1))
25622555
false
25632556
else
@@ -2570,9 +2563,12 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
25702563
// subtype, so they must be unrelated by single inheritance
25712564
// of classes.
25722565
true
2573-
else if (isClosedSum(cls1))
2566+
else if (isDecomposable(cls1))
2567+
// At this point, !cls1.derivesFrom(cls2): we know that `new cls2`
2568+
// is disjoint from `tp2`. Therefore, we can safely decompose
2569+
// `cls1` using `.children`, even if `cls1` is non abstract.
25742570
decompose(cls1, tp1).forall(x => provablyDisjoint(x, tp2))
2575-
else if (isClosedSum(cls2))
2571+
else if (isDecomposable(cls2))
25762572
decompose(cls2, tp2).forall(x => provablyDisjoint(x, tp1))
25772573
else
25782574
false

0 commit comments

Comments
 (0)