Skip to content

Commit 1f12656

Browse files
committed
Fix logic in isCheckDefinitelyFalse
1 parent 879ebde commit 1f12656

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,12 @@ object TypeTestsCasts {
157157
val xClass = effectiveClass(x)
158158
val pClass = effectiveClass(p)
159159

160-
!xClass.derivesFrom(pClass)
161-
&& (xClass.is(Final) || pClass.is(Final) || !xClass.is(Trait) && !pClass.is(Trait))
160+
val bothAreClasses = !xClass.is(Trait) && !pClass.is(Trait)
161+
val notXsubP = !xClass.derivesFrom(pClass)
162+
val notPsubX = !pClass.derivesFrom(xClass)
163+
bothAreClasses && notXsubP && notPsubX
164+
|| xClass.is(Final) && notXsubP
165+
|| pClass.is(Final) && notPsubX
162166
else
163167
false
164168
}
@@ -183,7 +187,8 @@ object TypeTestsCasts {
183187
val res1 = recur(tp1, P)
184188
val res2 = recur(tp2, P)
185189

186-
if res1.isEmpty && res2.isEmpty then res1
190+
if res1.isEmpty && res2.isEmpty then
191+
res1
187192
else if res2.isEmpty then
188193
if isCheckDefinitelyFalse(tp1, P) then res2
189194
else res1

tests/neg-custom-args/isInstanceOf/i5826.scala

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,23 @@ class Foo {
1919
}
2020

2121
def test4[A](x: List[Int] | (A => Int)) = x match {
22-
case ls: List[Int] => ls.head // ok, List decomposes to Some and None
22+
case ls: List[Int] => ls.head // error, List extends Int => T
23+
case _ => 0
24+
}
25+
26+
final class C[T] extends A[T]
27+
28+
def test5[T](x: A[T] | B[T] | Option[T]): Boolean = x.isInstanceOf[C[String]] // error
29+
30+
def test6[T](x: A[T] | B[T] | Option[T]): Boolean = x.isInstanceOf[C[T]]
31+
32+
def test7[A](x: Option[Int] | (A => Int)) = x match {
33+
case ls: Option[Int] => ls.head // OK, Option decomposes to Some and None
34+
case _ => 0
35+
}
36+
37+
def test8(x: List[Int] | A[String]) = x match {
38+
case ls: List[Int] => ls.head // OK, List decomposes to :: and Nil
2339
case _ => 0
2440
}
2541
}

0 commit comments

Comments
 (0)