Skip to content

Commit 1dc5b99

Browse files
Cache underlyingMatchType for AppliedTypes
`def underlyingMatchType` had an `isMatchAlias` guard for `AppliedType`s. This used to be a quick check preventing unnecessary recursions and superType computations. But `isMatchAlias` is now itself mutually recursive with `underlyingMatchType`, so we cache it for AppliedTypes to alleviate this.
1 parent 389f5be commit 1dc5b99

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

Diff for: compiler/src/dotty/tools/dotc/core/Types.scala

+13-1
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ object Types extends TypeUtils {
468468
def underlyingMatchType(using Context): Type = stripped match {
469469
case tp: MatchType => tp
470470
case tp: HKTypeLambda => tp.resType.underlyingMatchType
471-
case tp: AppliedType if tp.isMatchAlias => tp.superType.underlyingMatchType
471+
case tp: AppliedType => tp.underlyingMatchType
472472
case _ => NoType
473473
}
474474

@@ -4534,6 +4534,9 @@ object Types extends TypeUtils {
45344534
private var myEvalRunId: RunId = NoRunId
45354535
private var myEvalued: Type = uninitialized
45364536

4537+
private var validUnderlyingMatch: Period = Nowhere
4538+
private var cachedUnderlyingMatch: Type = uninitialized
4539+
45374540
def isGround(acc: TypeAccumulator[Boolean])(using Context): Boolean =
45384541
if myGround == 0 then myGround = if acc.foldOver(true, this) then 1 else -1
45394542
myGround > 0
@@ -4590,6 +4593,15 @@ object Types extends TypeUtils {
45904593
case nil => x
45914594
foldArgs(op(x, tycon), args)
45924595

4596+
/** Exists if the tycon is a TypeRef of an alias with an underlying match type.
4597+
* Anything else should have already been reduced in `appliedTo` by the TypeAssigner.
4598+
*/
4599+
override def underlyingMatchType(using Context): Type =
4600+
if ctx.period != validUnderlyingMatch then
4601+
validUnderlyingMatch = if tycon.isProvisional then Nowhere else ctx.period
4602+
cachedUnderlyingMatch = superType.underlyingMatchType
4603+
cachedUnderlyingMatch
4604+
45934605
override def tryNormalize(using Context): Type = tycon.stripTypeVar match {
45944606
case tycon: TypeRef =>
45954607
def tryMatchAlias = tycon.info match

0 commit comments

Comments
 (0)