diff --git a/compiler/src/dotty/tools/dotc/transform/patmat/Space.scala b/compiler/src/dotty/tools/dotc/transform/patmat/Space.scala index e69f563de149..e957f11d009b 100644 --- a/compiler/src/dotty/tools/dotc/transform/patmat/Space.scala +++ b/compiler/src/dotty/tools/dotc/transform/patmat/Space.scala @@ -628,7 +628,15 @@ class SpaceEngine(using Context) extends SpaceLogic { case tp if tp.classSymbol.isAllOf(JavaEnumTrait) => tp.classSymbol.children.map(sym => Typ(sym.termRef, true)) case tp => - val children = tp.classSymbol.children + def getChildren(sym: Symbol): List[Symbol] = + sym.children.flatMap { child => + if child eq sym then Nil // i3145: sealed trait Baz, val x = new Baz {}, Baz.children returns Baz... + else if tp.classSymbol == defn.TupleClass || tp.classSymbol == defn.NonEmptyTupleClass then + List(child) // TupleN and TupleXXL classes are used for Tuple, but they aren't Tuple's children + else if (child.is(Private) || child.is(Sealed)) && child.isOneOf(AbstractOrTrait) then getChildren(child) + else List(child) + } + val children = getChildren(tp.classSymbol) debug.println(s"candidates for ${tp.show} : [${children.map(_.show).mkString(", ")}]") val parts = children.map { sym => diff --git a/tests/patmat/i14579.scala b/tests/patmat/i14579.scala new file mode 100644 index 000000000000..f20ecde2292b --- /dev/null +++ b/tests/patmat/i14579.scala @@ -0,0 +1,10 @@ +trait A { + sealed abstract class X + private class X1 extends X with X2 { } + private trait X2 extends X + sealed trait X3 extends X + + def f(x: X) = x match { + case _: X1 => 0 + } +} \ No newline at end of file diff --git a/tests/patmat/patmatexhaust.check b/tests/patmat/patmatexhaust.check index 14fec2622f64..ee7187ee4b62 100644 --- a/tests/patmat/patmatexhaust.check +++ b/tests/patmat/patmatexhaust.check @@ -5,7 +5,6 @@ 53: Pattern Match Exhaustivity: _: Gp 59: Pattern Match Exhaustivity: Nil 75: Pattern Match Exhaustivity: _: B -87: Pattern Match Exhaustivity: _: C1 100: Pattern Match Exhaustivity: _: C1 114: Pattern Match Exhaustivity: D1, D2() 126: Pattern Match Exhaustivity: _: C1 diff --git a/tests/patmat/patmatexhaust.scala b/tests/patmat/patmatexhaust.scala index 8e6b3196cd5f..40c8c139ee54 100644 --- a/tests/patmat/patmatexhaust.scala +++ b/tests/patmat/patmatexhaust.scala @@ -84,7 +84,7 @@ class TestSealedExhaustive { // compile only case class C3() extends C case object C4 extends C - def ma10(x: C) = x match { // treat abstract sealed C1 is as inhabited. + def ma10(x: C) = x match { // exhaustive: abstract sealed C1 is dead end. case C3() => true case C2 | C4 => true }