Skip to content

Commit 95098ba

Browse files
committed
Shortcut in derivesFrom for high bound Any.
Any is a supertype of every other type, so no need to analyze types in detail. This also fixes the cyclic reference error observed for sets.scala, but only for the special case where the base class is Any.
1 parent b593958 commit 95098ba

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

src/dotty/tools/dotc/core/Types.scala

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -141,20 +141,23 @@ object Types {
141141
}
142142

143143
/** Is this type an instance of a non-bottom subclass of the given class `cls`? */
144-
final def derivesFrom(cls: Symbol)(implicit ctx: Context): Boolean = this match {
145-
case tp: TypeRef =>
146-
val sym = tp.symbol
147-
if (sym.isClass) sym.derivesFrom(cls) else tp.underlying.derivesFrom(cls)
148-
case tp: TypeProxy =>
149-
tp.underlying.derivesFrom(cls)
150-
case tp: AndType =>
151-
tp.tp1.derivesFrom(cls) || tp.tp2.derivesFrom(cls)
152-
case tp: OrType =>
153-
tp.tp1.derivesFrom(cls) && tp.tp2.derivesFrom(cls)
154-
case tp: JavaArrayType =>
155-
cls == defn.ObjectClass
156-
case _ =>
157-
false
144+
final def derivesFrom(cls: Symbol)(implicit ctx: Context): Boolean = {
145+
def loop(tp: Type) = tp match {
146+
case tp: TypeRef =>
147+
val sym = tp.symbol
148+
if (sym.isClass) sym.derivesFrom(cls) else tp.underlying.derivesFrom(cls)
149+
case tp: TypeProxy =>
150+
tp.underlying.derivesFrom(cls)
151+
case tp: AndType =>
152+
tp.tp1.derivesFrom(cls) || tp.tp2.derivesFrom(cls)
153+
case tp: OrType =>
154+
tp.tp1.derivesFrom(cls) && tp.tp2.derivesFrom(cls)
155+
case tp: JavaArrayType =>
156+
cls == defn.ObjectClass
157+
case _ =>
158+
false
159+
}
160+
cls == defn.AnyClass || loop(this)
158161
}
159162

160163
/** Is this type guaranteed not to have `null` as a value?

0 commit comments

Comments
 (0)