Skip to content

Commit b6e72aa

Browse files
committed
hanle Unit as a decomposable type with a single value
1 parent 0a746fb commit b6e72aa

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,8 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
443443
Typ(ConstantType(Constant(true)), true),
444444
Typ(ConstantType(Constant(false)), true)
445445
)
446+
case tp if tp.isRef(defn.UnitClass) =>
447+
Typ(ConstantType(Constant(())), true) :: Nil
446448
case tp if tp.classSymbol.is(Enum) =>
447449
children.map(sym => Typ(sym.termRef, true))
448450
case tp =>
@@ -708,6 +710,7 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
708710
canDecompose(and.tp1) || canDecompose(and.tp2)
709711
}) ||
710712
tp.isRef(defn.BooleanClass) ||
713+
tp.isRef(defn.UnitClass) ||
711714
tp.classSymbol.is(allOf(Enum, Sealed)) // Enum value doesn't have Sealed flag
712715

713716
debug.println(s"decomposable: ${tp.show} = $res")

tests/patmat/i4227b.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
sealed abstract class Maybe[A]
2+
final case class Just[A](a: A) extends Maybe[A]
3+
class Empty[A] extends Maybe[A]
4+
object Empty {
5+
def apply[A](): Maybe[A] = new Empty[A]
6+
def unapply[A](e: Empty[A]): Some[Unit] = Some(())
7+
}
8+
9+
object Test {
10+
val a: Maybe[Int] = Just(2)
11+
def main(args: Array[String]): Unit = a match {
12+
case Just(_) =>
13+
// case Empty(_) => // ok
14+
case Empty(()) => // match may not be exhaustive. It would fail on: Empty(_)
15+
}
16+
}

0 commit comments

Comments
 (0)