Skip to content

Commit 859214a

Browse files
authored
Merge pull request #14599 from dwijnand/patmat-private-sealed
2 parents db7872e + 9e3f86a commit 859214a

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,15 @@ class SpaceEngine(using Context) extends SpaceLogic {
628628
case tp if tp.classSymbol.isAllOf(JavaEnumTrait) =>
629629
tp.classSymbol.children.map(sym => Typ(sym.termRef, true))
630630
case tp =>
631-
val children = tp.classSymbol.children
631+
def getChildren(sym: Symbol): List[Symbol] =
632+
sym.children.flatMap { child =>
633+
if child eq sym then Nil // i3145: sealed trait Baz, val x = new Baz {}, Baz.children returns Baz...
634+
else if tp.classSymbol == defn.TupleClass || tp.classSymbol == defn.NonEmptyTupleClass then
635+
List(child) // TupleN and TupleXXL classes are used for Tuple, but they aren't Tuple's children
636+
else if (child.is(Private) || child.is(Sealed)) && child.isOneOf(AbstractOrTrait) then getChildren(child)
637+
else List(child)
638+
}
639+
val children = getChildren(tp.classSymbol)
632640
debug.println(s"candidates for ${tp.show} : [${children.map(_.show).mkString(", ")}]")
633641

634642
val parts = children.map { sym =>

tests/patmat/i14579.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
trait A {
2+
sealed abstract class X
3+
private class X1 extends X with X2 { }
4+
private trait X2 extends X
5+
sealed trait X3 extends X
6+
7+
def f(x: X) = x match {
8+
case _: X1 => 0
9+
}
10+
}

tests/patmat/patmatexhaust.check

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
53: Pattern Match Exhaustivity: _: Gp
66
59: Pattern Match Exhaustivity: Nil
77
75: Pattern Match Exhaustivity: _: B
8-
87: Pattern Match Exhaustivity: _: C1
98
100: Pattern Match Exhaustivity: _: C1
109
114: Pattern Match Exhaustivity: D1, D2()
1110
126: Pattern Match Exhaustivity: _: C1

tests/patmat/patmatexhaust.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class TestSealedExhaustive { // compile only
8484
case class C3() extends C
8585
case object C4 extends C
8686

87-
def ma10(x: C) = x match { // treat abstract sealed C1 is as inhabited.
87+
def ma10(x: C) = x match { // exhaustive: abstract sealed C1 is dead end.
8888
case C3() => true
8989
case C2 | C4 => true
9090
}

0 commit comments

Comments
 (0)