Skip to content

Fix #4226: allow infallible boolean extractors to return true #4236

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 3 commits into from
Apr 11, 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 @@ -289,7 +289,7 @@ object PatternMatcher {

if (isSyntheticScala2Unapply(unapp.symbol) && caseAccessors.length == args.length)
matchArgsPlan(caseAccessors.map(ref(scrutinee).select(_)), args, onSuccess)
else if (unapp.tpe.isRef(defn.BooleanClass))
else if (unapp.tpe.widenSingleton.isRef(defn.BooleanClass))
TestPlan(GuardTest, unapp, unapp.pos, onSuccess, onFailure)
else {
letAbstract(unapp) { unappResult =>
Expand Down
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/transform/patmat/Space.scala
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
def irrefutable(unapp: Tree): Boolean = {
// TODO: optionless patmat
unapp.tpe.widen.finalResultType.isRef(scalaSomeClass) ||
unapp.tpe.widen.finalResultType =:= ConstantType(Constant(true)) ||
(unapp.symbol.is(Synthetic) && unapp.symbol.owner.linkedClass.is(Case)) ||
productArity(unapp.tpe.widen.finalResultType) > 0
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Applications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ object Applications {
productSelectorTypes(unapplyResult)
else if (isGetMatch(unapplyResult, pos))
getUnapplySelectors(getTp, args, pos)
else if (unapplyResult isRef defn.BooleanClass)
else if (unapplyResult.widenSingleton isRef defn.BooleanClass)
Nil
else if (defn.isProductSubType(unapplyResult))
productSelectorTypes(unapplyResult)
Expand Down
1 change: 1 addition & 0 deletions tests/patmat/i4226.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
11: Pattern Match Exhaustivity: Just(_)
15 changes: 15 additions & 0 deletions tests/patmat/i4226.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
sealed abstract class Maybe[A]
final case class Just[A](a: A) extends Maybe[A]
class Empty[A] extends Maybe[A]
object Empty {
def apply[A](): Maybe[A] = new Empty[A]
def unapply[A](e: Empty[A]): true = true
}

object Test {
val a: Maybe[Int] = Just(2)
def main(args: Array[String]): Unit = a match {
case Just(2) => true
case Empty() =>
}
}
1 change: 1 addition & 0 deletions tests/patmat/i4226b.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
11: Pattern Match Exhaustivity: _: Empty, Just(_)
15 changes: 15 additions & 0 deletions tests/patmat/i4226b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
sealed abstract class Maybe[A]
final case class Just[A](a: A) extends Maybe[A]
class Empty[A] extends Maybe[A]
object Empty {
def apply[A](): Maybe[A] = new Empty[A]
def unapply[A](e: Empty[A]): false = false
}

object Test {
val a: Maybe[Int] = Just(2)
def main(args: Array[String]): Unit = a match {
case Just(2) => true
case Empty() =>
}
}