Skip to content

Commit b084eb1

Browse files
committed
Fix #4661: Missing unreachable case warnings
1 parent ab84cfb commit b084eb1

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -294,16 +294,21 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
294294

295295
override def intersectUnrelatedAtomicTypes(tp1: Type, tp2: Type) = {
296296
val and = AndType(tp1, tp2)
297-
// Precondition: !(tp1 <:< tp2) && !(tp2 <:< tp1)
298-
// Then, no leaf of the and-type tree `and` is a subtype of `and`.
299-
val res = inhabited(and)
297+
// Precondition: !isSubType(tp1, tp2) && !isSubType(tp2, tp1)
298+
if (tp1 == nullType || tp2 == nullType) {
299+
// Since projections of types don't include null, intersection with null is empty.
300+
Empty
301+
} else {
302+
// Then, no leaf of the and-type tree `and` is a subtype of `and`.
303+
val res = inhabited(and)
300304

301-
debug.println(s"atomic intersection: ${and.show} = ${res}")
305+
debug.println(s"atomic intersection: ${and.show} = ${res}")
302306

303-
if (!res) Empty
304-
else if (tp1.isSingleton) Typ(tp1, true)
305-
else if (tp2.isSingleton) Typ(tp2, true)
306-
else Typ(and, true)
307+
if (!res) Empty
308+
else if (tp1.isSingleton) Typ(tp1, true)
309+
else if (tp2.isSingleton) Typ(tp2, true)
310+
else Typ(and, true)
311+
}
307312
}
308313

309314
/** Whether the extractor is irrefutable */

tests/patmat/t4661.check

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
9: Match case Unreachable
2+
10: Match case Unreachable
3+
11: Match case Unreachable

tests/patmat/t4661.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
trait Foo
2+
class One extends Foo
3+
class Two extends Foo
4+
class Three extends Foo
5+
6+
object Test {
7+
def test(f: Foo) = f match {
8+
case f: Foo =>
9+
case f: One =>
10+
case f: Two =>
11+
case f: Three =>
12+
}
13+
}

0 commit comments

Comments
 (0)