Skip to content

Commit acaaeb3

Browse files
committed
Use cached underlyingMatchType when normalizing applied match aliases
Also fixes underlyingMatchType to not use the resType of HKTypeLambdas It should only be in `isMatch` used for `AliasingBounds`, not `isMatchAlias` [Cherry-picked ac98023][modified]
1 parent 50fd96b commit acaaeb3

File tree

1 file changed

+24
-27
lines changed

1 file changed

+24
-27
lines changed

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

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -456,12 +456,10 @@ object Types extends TypeUtils {
456456
*/
457457
def isMatch(using Context): Boolean = underlyingMatchType.exists
458458

459-
def underlyingMatchType(using Context): Type = stripped match {
459+
def underlyingMatchType(using Context): Type = stripped match
460460
case tp: MatchType => tp
461-
case tp: HKTypeLambda => tp.resType.underlyingMatchType
462-
case tp: AppliedType if tp.isMatchAlias => tp.superType.underlyingMatchType
461+
case tp: AppliedType => tp.underlyingMatchType
463462
case _ => NoType
464-
}
465463

466464
/** Is this a higher-kinded type lambda with given parameter variances?
467465
* These lambdas are used as the RHS of higher-kinded abstract types or
@@ -4455,6 +4453,9 @@ object Types extends TypeUtils {
44554453
private var myEvalRunId: RunId = NoRunId
44564454
private var myEvalued: Type = uninitialized
44574455

4456+
private var validUnderlyingMatch: Period = Nowhere
4457+
private var cachedUnderlyingMatch: Type = uninitialized
4458+
44584459
def isGround(acc: TypeAccumulator[Boolean])(using Context): Boolean =
44594460
if myGround == 0 then myGround = if acc.foldOver(true, this) then 1 else -1
44604461
myGround > 0
@@ -4511,29 +4512,25 @@ object Types extends TypeUtils {
45114512
case nil => x
45124513
foldArgs(op(x, tycon), args)
45134514

4514-
override def tryNormalize(using Context): Type = tycon.stripTypeVar match {
4515-
case tycon: TypeRef =>
4516-
def tryMatchAlias = tycon.info match {
4517-
case MatchAlias(alias) =>
4518-
trace(i"normalize $this", typr, show = true) {
4519-
MatchTypeTrace.recurseWith(this) {
4520-
alias.applyIfParameterized(args).tryNormalize
4521-
/* `applyIfParameterized` may reduce several HKTypeLambda applications
4522-
* before the underlying MatchType is reached.
4523-
* Even if they do not involve any match type normalizations yet,
4524-
* we still want to record these reductions in the MatchTypeTrace.
4525-
* They should however only be attempted if they eventually expand
4526-
* to a match type, which is ensured by the `isMatchAlias` guard.
4527-
*/
4528-
}
4529-
}
4530-
case _ =>
4531-
NoType
4532-
}
4533-
tryCompiletimeConstantFold.orElse(tryMatchAlias)
4534-
case _ =>
4535-
NoType
4536-
}
4515+
/** Exists if the tycon is a TypeRef of an alias with an underlying match type.
4516+
* Anything else should have already been reduced in `appliedTo` by the TypeAssigner.
4517+
* May reduce several HKTypeLambda applications before the underlying MatchType is reached.
4518+
*/
4519+
override def underlyingMatchType(using Context): Type =
4520+
if ctx.period != validUnderlyingMatch then
4521+
cachedUnderlyingMatch = superType.underlyingMatchType
4522+
validUnderlyingMatch = validSuper
4523+
cachedUnderlyingMatch
4524+
4525+
override def tryNormalize(using Context): Type =
4526+
def tryMatchAlias =
4527+
if isMatchAlias then trace(i"normalize $this", typr, show = true):
4528+
if MatchTypeTrace.isRecording then
4529+
MatchTypeTrace.recurseWith(this)(superType.tryNormalize)
4530+
else
4531+
underlyingMatchType.tryNormalize
4532+
else NoType
4533+
tryCompiletimeConstantFold.orElse(tryMatchAlias)
45374534

45384535
/** Does this application expand to a match type? */
45394536
def isMatchAlias(using Context): Boolean = tycon.stripTypeVar match

0 commit comments

Comments
 (0)