Skip to content

Commit 9452437

Browse files
committed
Fix #5970: suppress spurious warning in isInstanceOf check
1 parent b885862 commit 9452437

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

compiler/src/dotty/tools/dotc/core/SymDenotations.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,6 +1575,17 @@ object SymDenotations {
15751575
(symbol eq defn.NothingClass) ||
15761576
(symbol eq defn.NullClass) && (base ne defn.NothingClass))
15771577

1578+
/** Is it possible that a class inherits both `this` and `that`?
1579+
*
1580+
* @note The test is based on single-class inheritance and the closed
1581+
* hierarchy of final classes.
1582+
*
1583+
* @return The result may contain false positives, but never false negatives.
1584+
*/
1585+
final def mayHaveCommonChild(that: ClassSymbol)(implicit ctx: Context): Boolean =
1586+
!this.is(Final) && !that.is(Final) && (this.is(Trait) || that.is(Trait)) ||
1587+
this.derivesFrom(that) || that.derivesFrom(this.symbol)
1588+
15781589
final override def typeParamCreationFlags: FlagSet = ClassTypeParamCreationFlags
15791590

15801591
/** Hook to do a pre-enter test. Overridden in PackageDenotation */

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ object TypeTestsCasts {
132132
recur(tp1, P) && recur(tp2, P)
133133
case _ =>
134134
// first try withou striping type parameters for performance
135+
!X.classSymbol.asClass.mayHaveCommonChild(P.classSymbol.asClass) ||
135136
isClassDetermined(X, tpe)(ctx.fresh.setNewTyperState()) ||
136137
isClassDetermined(stripTypeParam(X), tpe)(ctx.fresh.setNewTyperState())
137138
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
object Test extends App {
2+
case class Foo[T](t: T)
3+
4+
def foo[T](ft: Unit|Foo[T]): T = {
5+
ft match {
6+
case Foo(t) => t
7+
case () => ???
8+
}
9+
}
10+
11+
foo(Foo(23))
12+
}

0 commit comments

Comments
 (0)