Skip to content

Commit e4d0cf8

Browse files
committed
Allow test for abstract types if it is always true
1 parent b599a40 commit e4d0cf8

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ object Checkable {
4747
*
4848
* 1. if `P` is a singleton type, TRUE
4949
* 2. if `P` is WildcardType, TRUE
50-
* 3. if `P` refers to an abstract type member or type parameter, FALSE
50+
* 3. if `P` refers to an abstract type member or type parameter, `X <:< P`
5151
* 4. if `P = Array[T]`, checkable(E, T) where `E` is the element type of `X`, defaults to `Any`.
5252
* 5. if `P` is `pre.F[Ts]` and `pre.F` refers to a class which is not `Array`:
5353
* (a) replace `Ts` with fresh type variables `Xs`
@@ -88,19 +88,20 @@ object Checkable {
8888
}
8989

9090
val res = replaceBinderMap.apply(P) match {
91+
case _ if isAbstract => X <:< P
9192
case _: SingletonType => true
9293
case WildcardType => true
9394
case defn.ArrayOf(tpT) =>
94-
X match {
95+
X.widen match {
9596
case defn.ArrayOf(tpE) => checkable(tpE, tpT)
9697
case _ => checkable(defn.AnyType, tpT)
9798
}
98-
case tpe: AppliedType => !isAbstract && isClassDetermined(tpe)
99+
case tpe: AppliedType => isClassDetermined(tpe)
99100
case AndType(tp1, tp2) => checkable(X, tp1) && checkable(X, tp2)
100101
case OrType(tp1, tp2) => checkable(X, tp1) && checkable(X, tp2)
101102
case AnnotatedType(t, an) => checkable(X, t)
102103
case _: RefinedType => false
103-
case _ => !isAbstract
104+
case _ => true
104105
}
105106

106107
debug.println(i"checking ${X.show} isInstanceOf ${P} = $res")
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class C[T] {
2+
val x: T = ???
3+
x.isInstanceOf[T]
4+
5+
val y: Array[T] = ???
6+
7+
y match {
8+
case x: Array[T] =>
9+
}
10+
11+
type F[X]
12+
13+
val z: F[T] = ???
14+
z match {
15+
case x: F[T] =>
16+
}
17+
18+
def foo(x: Any): Boolean =
19+
x.isInstanceOf[List[String]] // error
20+
}

0 commit comments

Comments
 (0)