Skip to content

Fix #10217: Avoid exponential behavior in derivesFrom #10271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 11, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1175,11 +1175,11 @@ class Definitions {
def isBottomClassAfterErasure(cls: Symbol): Boolean = cls == NothingClass || cls == NullClass

def isBottomType(tp: Type): Boolean =
if (ctx.explicitNulls && !ctx.phase.erasedTypes) tp.derivesFrom(NothingClass)
if (ctx.explicitNulls && !ctx.phase.erasedTypes) tp.isCombinedRef(NothingClass)
else isBottomTypeAfterErasure(tp)

def isBottomTypeAfterErasure(tp: Type): Boolean =
tp.derivesFrom(NothingClass) || tp.derivesFrom(NullClass)
tp.isCombinedRef(NothingClass) || tp.isCombinedRef(NullClass)

/** Is a function class.
* - FunctionXXL
Expand Down
13 changes: 11 additions & 2 deletions compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,12 @@ object Types {
false
}

/** Like isRef, but also extends to unions and intersections */
def isCombinedRef(sym: Symbol)(using Context): Boolean = stripped match
case AndType(tp1: Type, tp2: Type) => tp1.isRef(sym) || tp2.isRef(sym)
case OrType(tp1: Type, tp2: Type) => tp1.isRef(sym) && tp2.isRef(sym)
case _ => isRef(sym)

def isAny(using Context): Boolean = isRef(defn.AnyClass, skipRefined = false)
def isAnyRef(using Context): Boolean = isRef(defn.ObjectClass, skipRefined = false)
def isAnyKind(using Context): Boolean = isRef(defn.AnyKindClass, skipRefined = false)
Expand Down Expand Up @@ -242,8 +248,8 @@ object Types {
// If the type is `T | Null` or `T | Nothing`, and `T` derivesFrom the class,
// then the OrType derivesFrom the class. Otherwise, we need to check both sides
// derivesFrom the class.
if defn.isBottomType(tp.tp1) then loop(tp.tp2)
else loop(tp.tp1) && (defn.isBottomType(tp.tp2) || loop(tp.tp2))
loop(tp.tp1) && (loop(tp.tp2) || defn.isBottomType(tp.tp2))
|| defn.isBottomType(tp.tp1) && loop(tp.tp2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the behaviour of this is same as before, but if tp = Null | (Null | (Null | ( ... | A), wouldn't tp.derivesFrom(NullClass) cause the expensive loop(tp.tp2) to run two times? So I think use if is better here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, do we need to make sure OrType(NullType, NothingType).derivesFrom(NothingClass) == false?

case tp: JavaArrayType =>
cls == defn.ObjectClass
case _ =>
Expand Down Expand Up @@ -1533,6 +1539,9 @@ object Types {
/** Is this (an alias of) the `scala.Null` type? */
final def isNullType(using Context) = isRef(defn.NullClass)

/** Is this (an alias of) the `scala.Null` or `scala.Nothing` type? */
final def isNullOrNothingType(using Context) = isNullType || isRef(defn.NothingClass)

/** The resultType of a LambdaType, or ExprType, the type itself for others */
def resultType(using Context): Type = this

Expand Down
27 changes: 27 additions & 0 deletions tests/pos/i10217.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
trait A
trait B
trait C
trait D
trait E
trait F
trait G
trait H
trait I
trait J
trait K
trait L
trait M
trait N
trait O
trait P
trait Q
trait R
trait S
trait T
trait U
trait V
trait W

class Foo[T]

val f = Foo[A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add another test Null | (Null | (Null | ( ... | A) here?