Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 22 additions & 6 deletions compiler/src/dotty/tools/dotc/transform/IsInstanceOfChecker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,33 @@ object Checkable {
}
}

def stripTypeParam(implicit ctx: Context) = new ApproximatingTypeMap {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe add a comment: /** Approximate type parameters depending on variance */

def apply(tp: Type): Type = tp match {
case tp: TypeRef if tp.underlying.isInstanceOf[TypeBounds] =>
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably equivalent to case tp: TypeRef if tp.symbol.isTypeParam. Maybe clearer?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

tp.symbol.isTypeParam doesn't cover the case of type members, so I think it's better to keep the original version here.

val lo = this(tp.info.loBound)
val hi = this(tp.info.hiBound)
Copy link
Contributor

Choose a reason for hiding this comment

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

Replace this by apply? It makes it clearer that this is a recursive call

range(lo, hi)
case _ =>
mapOver(tp)
}
}

def isClassDetermined(X: Type, P: AppliedType)(implicit ctx: Context) = {
val AppliedType(tycon, _) = P
val typeLambda = tycon.ensureLambdaSub.asInstanceOf[TypeLambda]
val tvars = constrained(typeLambda, untpd.EmptyTree, alwaysAddTypeVars = true)._2.map(_.tpe)
val P1 = tycon.appliedTo(tvars)

debug.println("P : " + P.show)
debug.println("P1 : " + P1.show)
debug.println("X : " + X.show)
debug.println("P : " + P)
debug.println("P1 : " + P1)
debug.println("X : " + X)

P1 <:< X // constraint P1

P1 <:< X // may fail, ignore
// use fromScala2x to avoid generating pattern bound symbols
maximizeType(P1, pos, fromScala2x = true)

val res = isFullyDefined(P1, ForceDegree.noBottom) && P1 <:< P
val res = P1 <:< P
debug.println("P1 : " + P1)
debug.println("P1 <:< P = " + res)

Expand All @@ -116,7 +130,9 @@ object Checkable {
case defn.ArrayOf(tpE) => recur(tpE, tpT)
case _ => recur(defn.AnyType, tpT)
}
case tpe: AppliedType => isClassDetermined(X, tpe)(ctx.fresh.setNewTyperState())
case tpe: AppliedType =>
isClassDetermined(X, tpe)(ctx.fresh.setNewTyperState()) ||
Copy link
Contributor

Choose a reason for hiding this comment

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

Here, you first try without stripping type parameters for performance reasons? Maybe add a comment

isClassDetermined(stripTypeParam.apply(X), tpe)(ctx.fresh.setNewTyperState())
Copy link
Contributor

Choose a reason for hiding this comment

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

Call it as stripTypeParam(X) instead of stripTypeParam.apply(X)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems scalac has problem with this syntax, however, I've changed the method so we don't need to call apply anymore.

case AndType(tp1, tp2) => recur(X, tp1) && recur(X, tp2)
case OrType(tp1, tp2) => recur(X, tp1) && recur(X, tp2)
case AnnotatedType(t, _) => recur(X, t)
Expand Down
11 changes: 11 additions & 0 deletions tests/neg-custom-args/isInstanceOf/i4297.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Test {
def test[X <: Option[Int]](x: X) = x.isInstanceOf[Some[Int]]
def test1[Y <: Int, X <: Option[Y]](x: X) = x.isInstanceOf[Some[Int]]
def test2(x: Any) = x.isInstanceOf[Function1[Nothing, _]]
def test3a(x: Any) = x.isInstanceOf[Function1[Any, _]] // error
def test3b(x: Any) = x.isInstanceOf[Function1[Int, _]] // error
def test4[Y <: Int, X <: Function1[Y, Unit]](x: X) = x.isInstanceOf[Function1[Int, _]] // error
def test5[Y <: Int, X <: Function1[Y, Unit]](x: X) = x.isInstanceOf[Function1[Int, Unit]] // error
def test6[Y <: Int, X <: Function1[Y, Unit]](x: X) = x.isInstanceOf[Function1[Int, Any]] // error
def test7[Y <: Int, X <: Function1[Y, Unit]](x: X) = x.isInstanceOf[Function1[_, Unit]]
}