Skip to content

Commit

Permalink
Cache underlying applied compiletime.ops
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneFlesselle committed Apr 30, 2024
1 parent a0839bc commit e47f635
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
26 changes: 14 additions & 12 deletions compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -466,11 +466,11 @@ object Types extends TypeUtils {
case _ => false

/** Does this application expand to a match type? */
def isMatchAlias(using Context): Boolean = underlyingMatchType.exists
def isMatchAlias(using Context): Boolean = underlyingNormalizable.isMatch

def underlyingMatchType(using Context): Type = stripped match
def underlyingNormalizable(using Context): Type = stripped match
case tp: MatchType => tp
case tp: AppliedType => tp.underlyingMatchType
case tp: AppliedType => tp.underlyingNormalizable
case _ => NoType

/** Is this a higher-kinded type lambda with given parameter variances?
Expand Down Expand Up @@ -4584,8 +4584,8 @@ object Types extends TypeUtils {
private var myEvalRunId: RunId = NoRunId
private var myEvalued: Type = uninitialized

private var validUnderlyingMatch: Period = Nowhere
private var cachedUnderlyingMatch: Type = uninitialized
private var validUnderlyingNormalizable: Period = Nowhere
private var cachedUnderlyingNormalizable: Type = uninitialized

def isGround(acc: TypeAccumulator[Boolean])(using Context): Boolean =
if myGround == 0 then myGround = if acc.foldOver(true, this) then 1 else -1
Expand Down Expand Up @@ -4647,19 +4647,21 @@ object Types extends TypeUtils {
* Anything else should have already been reduced in `appliedTo` by the TypeAssigner.
* May reduce several HKTypeLambda applications before the underlying MatchType is reached.
*/
override def underlyingMatchType(using Context): Type =
if ctx.period != validUnderlyingMatch then
validUnderlyingMatch = if tycon.isProvisional then Nowhere else ctx.period
cachedUnderlyingMatch = superType.underlyingMatchType
cachedUnderlyingMatch
override def underlyingNormalizable(using Context): Type =
if ctx.period != validUnderlyingNormalizable then
validUnderlyingNormalizable = if tycon.isProvisional then Nowhere else ctx.period
cachedUnderlyingNormalizable = tycon match
case tycon: TypeRef if defn.isCompiletimeAppliedType(tycon.symbol) => this
case _ => superType.underlyingNormalizable
cachedUnderlyingNormalizable

override def tryNormalize(using Context): Type =
def tryMatchAlias =
if isMatchAlias then trace(i"normalize $this", typr, show = true):
if MatchTypeTrace.isRecording then
MatchTypeTrace.recurseWith(this)(superType.tryNormalize)
else
underlyingMatchType.tryNormalize
underlyingNormalizable.tryNormalize
else NoType
tryCompiletimeConstantFold.orElse(tryMatchAlias)

Expand Down Expand Up @@ -5197,7 +5199,7 @@ object Types extends TypeUtils {
def apply(bound: Type, scrutinee: Type, cases: List[Type])(using Context): MatchType =
unique(new CachedMatchType(bound, scrutinee, cases))

def thatReducesUsingGadt(tp: Type)(using Context): Boolean = tp.underlyingMatchType match
def thatReducesUsingGadt(tp: Type)(using Context): Boolean = tp.underlyingNormalizable match
case mt: MatchType => mt.reducesUsingGadt
case _ => false

Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1847,7 +1847,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
case _ => false
}

val result = pt.underlyingMatchType match {
val result = pt.underlyingNormalizable match {
case mt: MatchType if isMatchTypeShaped(mt) =>
typedDependentMatchFinish(tree, sel1, selType, tree.cases, mt)
case _ =>
Expand Down

0 comments on commit e47f635

Please sign in to comment.