Skip to content

Fix #4225: always check redundant cases except for @unchecked #4232

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

Closed
wants to merge 1 commit into from
Closed
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
7 changes: 2 additions & 5 deletions compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,8 @@ class PatternMatcher extends MiniPhase {

// check exhaustivity and unreachability
val engine = new patmat.SpaceEngine

if (engine.checkable(tree)) {
engine.checkExhaustivity(tree)
engine.checkRedundancy(tree)
}
engine.checkExhaustivity(tree)
engine.checkRedundancy(tree)

translated.ensureConforms(tree.tpe)
}
Expand Down
12 changes: 8 additions & 4 deletions compiler/src/dotty/tools/dotc/transform/patmat/Space.scala
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,7 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
flatten(s).map(doShow(_, false)).distinct.mkString(", ")
}

def checkable(tree: Match): Boolean = {
private def exhaustivityCheckable(sel: Tree): Boolean = {
// Possible to check everything, but be compatible with scalac by default
def isCheckable(tp: Type): Boolean =
!tp.hasAnnotation(defn.UncheckedAnnot) && {
Expand All @@ -857,9 +857,8 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
(defn.isTupleType(tpw) && tpw.argInfos.exists(isCheckable(_)))
}

val Match(sel, cases) = tree
val res = isCheckable(sel.tpe)
debug.println(s"checkable: ${sel.show} = $res")
debug.println(s"exhaustivity checkable: ${sel.show} = $res")
res
}

Expand All @@ -877,6 +876,7 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
val Match(sel, cases) = _match
val selTyp = sel.tpe.widen.dealias

if (!exhaustivityCheckable(sel)) return

val patternSpace = cases.map({ x =>
val space = project(x.pat)
Expand All @@ -894,11 +894,15 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
ctx.warning(PatternMatchExhaustivity(show(Or(uncovered))), sel.pos)
}

private def redundancyCheckable(sel: Tree): Boolean =
!sel.tpe.hasAnnotation(defn.UncheckedAnnot)

def checkRedundancy(_match: Match): Unit = {
val Match(sel, cases) = _match
// ignore selector type for now
val selTyp = sel.tpe.widen.dealias

if (!redundancyCheckable(sel)) return

(0 until cases.length).foreach { i =>
// in redundancy check, take guard as false in order to soundly approximate
val prevs =
Expand Down
1 change: 1 addition & 0 deletions tests/patmat/i4225.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10: Match case Unreachable
12 changes: 12 additions & 0 deletions tests/patmat/i4225.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
object Bar {
def unapply(x: Int): Some[Int] =
Some(0)
}

object Test {
def test(x: Int) =
x match {
case Bar(a) => a
case _ => x // should be unreachable
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add a test case for objects:

object Bar {
  def unapply(x: String): Some[Int] =
    Some(0)
}

object Test {
  def test(x: String) =
    x match {
      case Bar(a) => a
      case _ => x // this case is reachable, i.e. test(null)
    }
}

Copy link
Contributor Author

@liufengyun liufengyun Apr 3, 2018

Choose a reason for hiding this comment

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

@allanrenucci : the exhaustivity check both in Dotty and Scalac ignores null. Otherwise, all pattern matchess without a _ will not be exhaustive. Thus in the example above, both Dotty & Scalac will report a warning.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I was wrong, for reachability check we can handle null thus avoid the warning:

It's implemented in https://github.com/lampepfl/dotty/compare/master...dotty-staging:fix-4225?expand=1#diff-5353f08e8d531fe0ee59335438e6cf7a.

}
}