Skip to content

Cache AppliedType#tryNormalize #14864

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

Closed
Closed
Changes from all 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
35 changes: 24 additions & 11 deletions compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,12 @@ object Types {
case t: TypeRef =>
!t.currentSymbol.isStatic && {
(t: Type).mightBeProvisional = false // break cycles
t.symbol.is(Provisional)
// We can use flagsUNSAFE here because in the worth case, we
// will overapproximate isProvisional by returning true if the
// denotation has not been completed yet.
t.symbol.flagsUNSAFE.is(Provisional)
|| test(t.prefix, theAcc)
|| t.info.match
|| t.denot.infoOrCompleter.match
case info: AliasingBounds => test(info.alias, theAcc)
case TypeBounds(lo, hi) => test(lo, theAcc) || test(hi, theAcc)
case _ => false
Expand Down Expand Up @@ -2072,7 +2075,7 @@ object Types {
private var myNarrow: TermRef | Null = null
override def narrow(using Context): TermRef = {
if (myNarrow == null) myNarrow = super.narrow
myNarrow.nn
myNarrow.uncheckedNN
}
}

Expand Down Expand Up @@ -3024,7 +3027,7 @@ object Types {

def recThis: RecThis = {
if (myRecThis == null) myRecThis = new RecThisImpl(this)
myRecThis.nn
myRecThis.uncheckedNN
}

override def underlying(using Context): Type = parent
Expand Down Expand Up @@ -3446,7 +3449,7 @@ object Types {
case _ :: rest => newParamRef(i) :: recur(rest, i + 1)
case _ => Nil
myParamRefs = recur(paramNames, 0)
myParamRefs.nn
myParamRefs.uncheckedNN
}

/** Like `paramInfos` but substitute parameter references with the given arguments */
Expand Down Expand Up @@ -4212,7 +4215,17 @@ object Types {
def isUnreducibleWild(using Context): Boolean =
tycon.isLambdaSub && hasWildcardArg && !isMatchAlias

def tryCompiletimeConstantFold(using Context): Type = tycon match {
var myTryCompiletimeConstantFold: Type | Null = null
var myTryCompiletimeConstantFoldPeriod: Period = Nowhere

def tryCompiletimeConstantFold(using Context): Type =
if myTryCompiletimeConstantFoldPeriod != ctx.period then
myTryCompiletimeConstantFold = tryCompiletimeConstantFoldImpl
if !isProvisional then
myTryCompiletimeConstantFoldPeriod = ctx.period
myTryCompiletimeConstantFold.uncheckedNN

private def tryCompiletimeConstantFoldImpl(using Context): Type = tycon match {
case tycon: TypeRef if defn.isCompiletimeAppliedType(tycon.symbol) =>
extension (tp: Type) def fixForEvaluation: Type =
tp.normalized.dealias match {
Expand Down Expand Up @@ -4634,7 +4647,7 @@ object Types {
private var myRepr: Name | Null = null
def repr(using Context): Name = {
if (myRepr == null) myRepr = SkolemName.fresh()
myRepr.nn
myRepr.uncheckedNN
}

override def toString: String = s"Skolem($hashCode)"
Expand Down Expand Up @@ -4873,7 +4886,7 @@ object Types {

TypeComparer.tracked(matchCases)
}
myReduced.nn
myReduced.uncheckedNN
}

override def computeHash(bs: Binders): Int = doHash(bs, scrutinee, bound :: cases)
Expand Down Expand Up @@ -4932,14 +4945,14 @@ object Types {
else if (ctx.erasedTypes) appliedRef
else AndType(givenSelf, appliedRef)
}
selfTypeCache.nn
selfTypeCache.uncheckedNN
}

def appliedRef(using Context): Type = {
if (appliedRefCache == null)
appliedRefCache =
TypeRef(prefix, cls).appliedTo(cls.classDenot.typeParams.map(_.typeRef))
appliedRefCache.nn
appliedRefCache.uncheckedNN
}

// cached because baseType needs parents
Expand All @@ -4948,7 +4961,7 @@ object Types {
override def parents(using Context): List[Type] = {
if (parentsCache == null)
parentsCache = declaredParents.mapConserve(_.asSeenFrom(prefix, cls.owner))
parentsCache.nn
parentsCache.uncheckedNN
}

protected def newLikeThis(prefix: Type, declaredParents: List[Type], decls: Scope, selfInfo: TypeOrSymbol)(using Context): ClassInfo =
Expand Down