From d3530102ad41efd2fef60c1027a32d7dae61b80b Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Wed, 29 Jun 2022 21:54:39 +0100 Subject: [PATCH] space: Fix how sealed abstract classes decompose --- .../dotty/tools/dotc/transform/patmat/Space.scala | 2 +- tests/pos/i15522.scala | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 tests/pos/i15522.scala diff --git a/compiler/src/dotty/tools/dotc/transform/patmat/Space.scala b/compiler/src/dotty/tools/dotc/transform/patmat/Space.scala index a2f178df438e..a8432833d42a 100644 --- a/compiler/src/dotty/tools/dotc/transform/patmat/Space.scala +++ b/compiler/src/dotty/tools/dotc/transform/patmat/Space.scala @@ -631,7 +631,7 @@ class SpaceEngine(using Context) extends SpaceLogic { case tp => 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... + if child eq sym then List(sym) // 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) diff --git a/tests/pos/i15522.scala b/tests/pos/i15522.scala new file mode 100644 index 000000000000..9b6b99872080 --- /dev/null +++ b/tests/pos/i15522.scala @@ -0,0 +1,13 @@ +// scalac: -Werror +sealed trait Coverage +sealed abstract case class Range(min: Double, max: Double) extends Coverage +case object Empty extends Coverage + +object Test: + def mkCoverage(min: Double, max: Double): Coverage = + if min < max then new Range(min, max) {} else Empty + + def meth(c1: Coverage, c2: Coverage): Coverage = (c1, c2) match + case (Empty, _) => Empty + case (_, Empty) => Empty // was: Unreachable case + case (Range(a1, b1), Range(a2, b2)) => mkCoverage(a1 max a2, b1 min b2)