Skip to content

Fix #16899: Better handle X instanceOf P where X is T1 | T2 #17382

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 8 commits into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
51 changes: 47 additions & 4 deletions compiler/src/dotty/tools/dotc/transform/TypeTestsCasts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import util.Spans._
import reporting._
import config.Printers.{ transforms => debug }

import patmat.Typ

/** This transform normalizes type tests and type casts,
* also replacing type tests with singleton argument type with reference equality check
* Any remaining type tests
Expand Down Expand Up @@ -51,7 +53,8 @@ object TypeTestsCasts {
* 6. if `P = T1 | T2` or `P = T1 & T2`, checkable(X, T1) && checkable(X, T2).
* 7. if `P` is a refinement type, "it's a refinement type"
* 8. if `P` is a local class which is not statically reachable from the scope where `X` is defined, "it's a local class"
* 9. otherwise, ""
* 9. if `X` is `T1 | T2`, checkable(T1, P) && checkable(T2, P) or (isCheckDefinitelyFalse(T1, P) && checkable(T2, P)) or (checkable(T1, P) && isCheckDefinitelyFalse(T2, P)).
* 10. otherwise, ""
*/
def whyUncheckable(X: Type, P: Type, span: Span)(using Context): String = atPhase(Phases.refchecksPhase.next) {
extension (inline s1: String) inline def &&(inline s2: String): String = if s1 == "" then s2 else s1
Expand Down Expand Up @@ -129,6 +132,34 @@ object TypeTestsCasts {

}

/** Whether the check X.isInstanceOf[P] is definitely false? */
def isCheckDefinitelyFalse(X: Type, P: Type)(using Context): Boolean = trace(s"isCheckDefinitelyFalse(${X.show}, ${P.show})") {
X.dealias match
case AndType(x1, x2) =>
isCheckDefinitelyFalse(x1, P) || isCheckDefinitelyFalse(x2, P)

case x =>
P.dealias match
case AndType(p1, p2) =>
isCheckDefinitelyFalse(x, p1) || isCheckDefinitelyFalse(x, p2)

case p =>
val pSpace = Typ(p)
val xSpace = Typ(x)
if pSpace.canDecompose then
val ps = pSpace.decompose.map(_.tp)
ps.forall(p => isCheckDefinitelyFalse(x, p))
else if xSpace.canDecompose then
val xs = xSpace.decompose.map(_.tp)
xs.forall(x => isCheckDefinitelyFalse(x, p))
else
val xClass = effectiveClass(x.widen)
val pClass = effectiveClass(p.widen)

!xClass.derivesFrom(pClass)
&& (xClass.is(Final) || pClass.is(Final) || !xClass.is(Trait) && !pClass.is(Trait))
}

def recur(X: Type, P: Type): String = (X <:< P) ||| (P.dealias match {
case _: SingletonType => ""
case _: TypeProxy
Expand All @@ -146,7 +177,19 @@ object TypeTestsCasts {
// - T1 <:< T2 | T3
// - T1 & T2 <:< T3
// See TypeComparer#either
recur(tp1, P) && recur(tp2, P)
val res1 = recur(tp1, P)
val res2 = recur(tp2, P)

if res1.isEmpty && res2.isEmpty then res1
else if res2.isEmpty then
if isCheckDefinitelyFalse(tp1, P) then res2
else i"it cannot be checked against the type $tp1"
else if res1.isEmpty then
if isCheckDefinitelyFalse(tp2, P) then res1
else i"it cannot be checked against the type $tp2"
else
i"it cannot be checked against the type $tp2"

case _ =>
// always false test warnings are emitted elsewhere
X.classSymbol.exists && P.classSymbol.exists &&
Expand Down Expand Up @@ -302,8 +345,8 @@ object TypeTestsCasts {

/** Transform isInstanceOf
*
* expr.isInstanceOf[A | B] ~~> expr.isInstanceOf[A] | expr.isInstanceOf[B]
* expr.isInstanceOf[A & B] ~~> expr.isInstanceOf[A] & expr.isInstanceOf[B]
* expr.isInstanceOf[A | B] ~~> expr.isInstanceOf[A] | expr.isInstanceOf[B]
* expr.isInstanceOf[A & B] ~~> expr.isInstanceOf[A] & expr.isInstanceOf[B]
* expr.isInstanceOf[Tuple] ~~> scala.runtime.Tuples.isInstanceOfTuple(expr)
* expr.isInstanceOf[EmptyTuple] ~~> scala.runtime.Tuples.isInstanceOfEmptyTuple(expr)
* expr.isInstanceOf[NonEmptyTuple] ~~> scala.runtime.Tuples.isInstanceOfNonEmptyTuple(expr)
Expand Down
2 changes: 1 addition & 1 deletion tests/neg-custom-args/isInstanceOf/i5826.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Foo {
def test[A]: List[Int] | A => Int = {
case ls: List[Int] => ls.head // error
case ls: List[Int] => ls.head // ok, List decomposes to Some and None
case _ => 0
}

Expand Down
5 changes: 5 additions & 0 deletions tests/pos-special/isInstanceOf/i16899.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sealed trait Unset

def foo(v: Unset|Option[Int]): Unit = v match
case v: Unset => ()
case v: Option[Int] => ()