Skip to content

Commit 54b08e1

Browse files
Merge pull request #6894 from dotty-staging/add-exhaustivity-tests
Add exhaustivity check tests
2 parents 671aa3a + ee18ae7 commit 54b08e1

File tree

7 files changed

+85
-0
lines changed

7 files changed

+85
-0
lines changed

tests/patmat/t11283.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
sealed trait Color
2+
case object Red extends Color
3+
case object Blue extends Color
4+
case object Green extends Color
5+
6+
class Test {
7+
8+
val R: Red.type = Red
9+
val B: Blue.type = Blue
10+
val G: Green.type = Green
11+
12+
def go(c: Color): Int = c match {
13+
case R => 0
14+
case G => 1
15+
case B => 2
16+
}
17+
}

tests/patmat/t11457.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
sealed abstract class Foo(val a: String)
2+
3+
object Foo {
4+
def unapply(foo: Foo): Some[String] =
5+
Some(foo.a)
6+
}
7+
8+
class Issue11457 {
9+
val root: PartialFunction[Foo, Boolean] = {
10+
case Foo("a") => true
11+
case Foo("b") => false
12+
}
13+
}

tests/patmat/t11603.scala.bak

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class C {
2+
def m(x: true) = x match {
3+
case true => println("the one true path")
4+
}
5+
}

tests/patmat/t11620.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
36: Pattern Match Exhaustivity: B(A2(_, _))

tests/patmat/t11620.scala

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
sealed trait A[+T]
2+
case class A1[+T](t : T ) extends A[T]
3+
case class A2[+T](t1: T, t2: T) extends A[T]
4+
5+
sealed trait B[+T] {
6+
type AA[+U] <: A[U]
7+
def a: AA[T]
8+
}
9+
object B {
10+
type Aux[+_A[+_], +T] = B[T] { type AA[+U] <: _A[U] }
11+
object Aux {
12+
def unapply[_A[+U] <: A[U], T](b: Aux[_A, T]): Some[_A[T]] = Some(b.a)
13+
}
14+
15+
def apply[_A[+U] <: A[U], T](_a: _A[T]): Aux[_A, T] =
16+
new B[T] { type AA[+U] = _A[U] ; val a: _A[T] = _a }
17+
18+
def unapply[T](b: B[T]): Some[b.AA[T]] = Some(b.a)
19+
}
20+
21+
def foo[T](b: B[T]) = b match {
22+
case B(A1(t)) t
23+
case B(A2(t, _)) t
24+
}
25+
26+
def foo2[_A[+U] <: A[U], T](b: B.Aux[_A, T]) = b match {
27+
case B.Aux(a @ A1(_ )) a.t
28+
case B.Aux(a @ A2(_, _)) a.t1 // 👎 (false-positive): unreachable code
29+
}
30+
31+
def foo3[_A[+U] <: A[U], T](b: B.Aux[_A, T]) = b match {
32+
case B.Aux(a: A1[T]) a.t
33+
case B.Aux(a: A2[T]) a.t1 // 👎 (false-positive): unreachable code
34+
}
35+
36+
def foo4[T](b: B[T]) = b match {
37+
case B(A1(t)) t // 👎 (false-negative): incomplete match
38+
}

tests/patmat/t9809.check

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
3: Pattern Match Exhaustivity: (_, _)
2+
7: Pattern Match Exhaustivity: (_, _)

tests/patmat/t9809.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
object Example {
2+
val op1: (Any, Any) => Unit = {
3+
case (_, b: Int) =>
4+
}
5+
6+
val op2: (Unit, Any) => Unit = {
7+
case (_, b: Int) =>
8+
}
9+
}

0 commit comments

Comments
 (0)