File tree Expand file tree Collapse file tree 9 files changed +93
-2
lines changed
compiler/src/dotty/tools/dotc Expand file tree Collapse file tree 9 files changed +93
-2
lines changed Original file line number Diff line number Diff line change @@ -1020,6 +1020,12 @@ object Types {
10201020 case _ => this
10211021 }
10221022
1023+ /** Widen singleton type modulo constant types */
1024+ final def widenNonConstant (implicit ctx : Context ): Type = this match {
1025+ case _ : ConstantType => this
1026+ case _ => widen
1027+ }
1028+
10231029 /** Widen type if it is unstable (i.e. an ExprType, or TermRef to unstable symbol */
10241030 final def widenIfUnstable (implicit ctx : Context ): Type = stripTypeVar match {
10251031 case tp : ExprType => tp.resultType.widenIfUnstable
Original file line number Diff line number Diff line change @@ -725,7 +725,7 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
725725
726726 def checkExhaustivity (_match : Match ): Unit = {
727727 val Match (sel, cases) = _match
728- val selTyp = sel.tpe.widen .dealias
728+ val selTyp = sel.tpe.widenNonConstant .dealias
729729
730730 if (! exhaustivityCheckable(sel)) return
731731
@@ -756,7 +756,7 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
756756
757757 def checkRedundancy (_match : Match ): Unit = {
758758 val Match (sel, cases) = _match
759- val selTyp = sel.tpe.widen .dealias
759+ val selTyp = sel.tpe.widenNonConstant .dealias
760760
761761 if (! redundancyCheckable(sel)) return
762762
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ class C {
2+ def m (x : true ) = x match {
3+ case true => println(" the one true path" )
4+ }
5+ }
Original file line number Diff line number Diff line change 1+ 36: Pattern Match Exhaustivity: B(A2(_, _))
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ 3: Pattern Match Exhaustivity: (_, _)
2+ 7: Pattern Match Exhaustivity: (_, _)
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments