Skip to content

Fix #13455: Also consider sealed classes closed sums #13483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2526,17 +2526,6 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
case _ => false
})

/** Can we enumerate all instantiations of this type? */
def isClosedSum(tp: Symbol): Boolean =
tp.is(Sealed) && tp.isOneOf(AbstractOrTrait) && !tp.hasAnonymousChild

/** Splits a closed type into a disjunction of smaller types.
* It should hold that `tp` and `decompose(tp).reduce(_ or _)`
* denote the same set of values.
*/
def decompose(sym: Symbol, tp: Type): List[Type] =
sym.children.map(x => refineUsingParent(tp, x)).filter(_.exists)

def fullyInstantiated(tp: Type): Boolean = new TypeAccumulator[Boolean] {
override def apply(x: Boolean, t: Type) =
x && {
Expand All @@ -2558,6 +2547,10 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
case (tp1: TypeRef, tp2: TypeRef) if tp1.symbol.isClass && tp2.symbol.isClass =>
val cls1 = tp1.classSymbol
val cls2 = tp2.classSymbol
def isDecomposable(tp: Symbol): Boolean =
tp.is(Sealed) && !tp.hasAnonymousChild
def decompose(sym: Symbol, tp: Type): List[Type] =
sym.children.map(x => refineUsingParent(tp, x)).filter(_.exists)
if (cls1.derivesFrom(cls2) || cls2.derivesFrom(cls1))
false
else
Expand All @@ -2570,9 +2563,13 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
// subtype, so they must be unrelated by single inheritance
// of classes.
true
else if (isClosedSum(cls1))
else if (isDecomposable(cls1))
// At this point, !cls1.derivesFrom(cls2): we know that direct
// instantiations of `cls1` (terms of the form `new cls1`) are not
// of type `tp2`. Therefore, we can safely decompose `cls1` using
// `.children`, even if `cls1` is non abstract.
decompose(cls1, tp1).forall(x => provablyDisjoint(x, tp2))
else if (isClosedSum(cls2))
else if (isDecomposable(cls2))
decompose(cls2, tp2).forall(x => provablyDisjoint(x, tp1))
else
false
Expand Down
4 changes: 2 additions & 2 deletions tests/patmat/andtype-opentype-interaction.check
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
23: Pattern Match Exhaustivity: _: Trait & OpenTrait, _: Clazz & OpenTrait, _: AbstractClass & OpenTrait, _: SealedClass & OpenTrait
27: Pattern Match Exhaustivity: _: Trait & OpenTrait & OpenTrait2, _: Clazz & OpenTrait & OpenTrait2, _: AbstractClass & OpenTrait & OpenTrait2, _: SealedClass & OpenTrait & OpenTrait2
23: Pattern Match Exhaustivity: _: Trait & OpenTrait, _: Clazz & OpenTrait, _: AbstractClass & OpenTrait
27: Pattern Match Exhaustivity: _: Trait & OpenTrait & OpenTrait2, _: Clazz & OpenTrait & OpenTrait2, _: AbstractClass & OpenTrait & OpenTrait2
31: Pattern Match Exhaustivity: _: Trait & OpenClass
35: Pattern Match Exhaustivity: _: Trait & OpenTrait & OpenClass
43: Pattern Match Exhaustivity: _: Trait & OpenAbstractClass
Expand Down
10 changes: 10 additions & 0 deletions tests/pos/13455.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
sealed class R

type X[T] = T match {
case R => String
case (z => r) => Int
}
def x[T]: X[T] = ???

def i(i0: Int): Unit = ???
val a = i(x[Int => String])