Skip to content

Commit 35f3bb1

Browse files
committed
Fix scala#4226: allow infallible boolean extractors to return true
1 parent 14187e2 commit 35f3bb1

File tree

6 files changed

+34
-2
lines changed

6 files changed

+34
-2
lines changed

compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ object PatternMatcher {
292292

293293
if (isSyntheticScala2Unapply(unapp.symbol) && caseAccessors.length == args.length)
294294
matchArgsPlan(caseAccessors.map(ref(scrutinee).select(_)), args, onSuccess)
295-
else if (unapp.tpe.isRef(defn.BooleanClass))
295+
else if (unapp.tpe.widenSingleton.isRef(defn.BooleanClass))
296296
TestPlan(GuardTest, unapp, unapp.pos, onSuccess, onFailure)
297297
else {
298298
letAbstract(unapp) { unappResult =>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
303303
def irrefutable(unapp: Tree): Boolean = {
304304
// TODO: optionless patmat
305305
unapp.tpe.widen.finalResultType.isRef(scalaSomeClass) ||
306+
unapp.tpe.widen.finalResultType =:= ConstantType(Constant(true)) ||
306307
(unapp.symbol.is(Synthetic) && unapp.symbol.owner.linkedClass.is(Case)) ||
307308
productArity(unapp.tpe.widen.finalResultType) > 0
308309
}

compiler/src/dotty/tools/dotc/typer/Applications.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ object Applications {
114114
productSelectorTypes(unapplyResult)
115115
else if (isGetMatch(unapplyResult, pos))
116116
getUnapplySelectors(getTp, args, pos)
117-
else if (unapplyResult isRef defn.BooleanClass)
117+
else if (unapplyResult.widenSingleton isRef defn.BooleanClass)
118118
Nil
119119
else if (defn.isProductSubType(unapplyResult))
120120
productSelectorTypes(unapplyResult)

tests/patmat/i4226.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
11: Pattern Match Exhaustivity: Just(_)

tests/patmat/i4226.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
sealed abstract class Maybe[A]
2+
final case class Just[A](a: A) extends Maybe[A]
3+
class Empty[A] extends Maybe[A]
4+
object Empty {
5+
def apply[A](): Maybe[A] = new Empty[A]
6+
def unapply[A](e: Empty[A]): true = true
7+
}
8+
9+
object Test {
10+
val a: Maybe[Int] = Just(2)
11+
def main(args: Array[String]): Unit = a match {
12+
case Just(2) => true
13+
case Empty() =>
14+
}
15+
}

tests/pos/i4226.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
sealed abstract class Maybe[A]
2+
final case class Just[A](a: A) extends Maybe[A]
3+
class Empty[A] extends Maybe[A]
4+
object Empty {
5+
def apply[A](): Maybe[A] = new Empty[A]
6+
def unapply[A](e: Empty[A]): true = true
7+
}
8+
9+
object Test {
10+
val a: Maybe[Int] = Just(2)
11+
def main(args: Array[String]): Unit = a match {
12+
case Just(2) => true
13+
case Empty() =>
14+
}
15+
}

0 commit comments

Comments
 (0)