Skip to content

Fix #4661: Missing unreachable case warnings #4869

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ trait CaseDefOpsImpl extends scala.tasty.reflect.CaseDefOps with TastyCoreImpl w
}

object CaseDef extends CaseDefExtractor {
def unapply(x: CaseDef): Option[(Pattern, Option[Term], Term)] = x match {
case x: tpd.CaseDef =>
Some(x.pat, optional(x.guard), x.body)
case _ => None
}
def unapply(x: CaseDef): Some[(Pattern, Option[Term], Term)] = Some(x.pat, optional(x.guard), x.body)
}

}
10 changes: 2 additions & 8 deletions compiler/src/dotty/tools/dotc/tastyreflect/ConstantOpsImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,11 @@ trait ConstantOpsImpl extends scala.tasty.reflect.ConstantOps with TastyCoreImpl
object Constant extends ConstantModule {

object Unit extends UnitExtractor {
def unapply(x: Constant): Boolean = x match {
case x: Constants.Constant => x.tag == Constants.UnitTag
case _ => false
}
def unapply(x: Constant): Boolean = x.tag == Constants.UnitTag
}

object Null extends NullExtractor {
def unapply(x: Constant): Boolean = x match {
case x: Constants.Constant => x.tag == Constants.NullTag
case _ => false
}
def unapply(x: Constant): Boolean = x.tag == Constants.NullTag
}

object Boolean extends BooleanExtractor {
Expand Down
6 changes: 5 additions & 1 deletion compiler/src/dotty/tools/dotc/transform/patmat/Space.scala
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,12 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
private val nullSpace = Typ(nullType)

override def intersectUnrelatedAtomicTypes(tp1: Type, tp2: Type): Space = {
// Precondition: !isSubType(tp1, tp2) && !isSubType(tp2, tp1)
if (tp1 == nullType || tp2 == nullType) {
// Since projections of types don't include null, intersection with null is empty.
return Empty
}
val and = AndType(tp1, tp2)
// Precondition: !(tp1 <:< tp2) && !(tp2 <:< tp1)
// Then, no leaf of the and-type tree `and` is a subtype of `and`.
val res = inhabited(and)

Expand Down
1 change: 1 addition & 0 deletions tests/patmat/i4880a.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
14: Match case Unreachable
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

15 changes: 15 additions & 0 deletions tests/patmat/t4661.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
9: Match case Unreachable
10: Match case Unreachable
11: Match case Unreachable
26: Match case Unreachable
27: Match case Unreachable
28: Match case Unreachable
33: Match case Unreachable
36: Match case Unreachable
37: Match case Unreachable
38: Match case Unreachable
39: Match case Unreachable
40: Only null matched
46: Match case Unreachable
52: Match case Unreachable
59: Only null matched
61 changes: 61 additions & 0 deletions tests/patmat/t4661.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
trait Foo
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Blaisorblade I can break this up into smaller tests, if you want. Not sure want the convention is.

class One extends Foo
class Two extends Foo
class Three extends Foo

object Test {
def test(f: Foo) = f match {
case f: Foo =>
case f: One => // unreachable
case f: Two => // unreachable
case f: Three => // unreachable
}
}

trait Prefix {
sealed trait Bar
class BarOne extends Bar
class BarTwo extends Bar
class BarThree extends Bar
}

class TestPrefix(val p: Prefix) {
import p._
def test(b: Bar) = b match {
case b: Bar =>
case b: BarOne => // unreachable
case b: BarTwo => // unreachable
case b: BarThree => // unreachable
}

def test2(b: Bar) = b match {
case b: Prefix#BarOne =>
case b: BarOne => // unreachable
case b: Prefix#BarTwo =>
case b: Prefix#BarThree =>
case b: BarTwo => // unreachable
case b: BarThree => // unreachable
case b: Bar => // unreachable
case b: Prefix#Bar => // unreachable
case _ => // only null matches
}

def test3(b: Prefix#Bar) = b match {
case b: Bar =>
case b: Prefix#BarOne =>
case b: BarOne => // unreachable
case _ =>
}

def test4(b: Bar) = b match {
case b: Bar =>
case b:Prefix#Bar => // unreachable
}

def test5(b: Bar) = b match {
case b: Prefix#BarOne =>
case b: Prefix#BarTwo =>
case b: Prefix#BarThree =>
case _ => // only null matches
}
}