Skip to content

Commit f926147

Browse files
authored
Merge pull request #5985 from dotty-staging/fix-5970
Fix #5970: suppress spurious warning in isInstanceOf check
2 parents 4d4521d + e8b7038 commit f926147

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
@@ -1571,6 +1571,17 @@ object SymDenotations {
15711571
(symbol eq defn.NothingClass) ||
15721572
(symbol eq defn.NullClass) && (base ne defn.NothingClass))
15731573

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

15761587
/** 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.exists && P.classSymbol.exists && !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)