Skip to content

Commit

Permalink
Use cached underlyingMatchType when normalizing applied match aliases
Browse files Browse the repository at this point in the history
Also fixes underlyingMatchType to not use the resType of HKTypeLambdas
It should only be in `isMatch` used for `AliasingBounds`, not `isMatchAlias`
  • Loading branch information
EugeneFlesselle committed Apr 30, 2024
1 parent ee323ef commit a0839bc
Showing 1 changed file with 11 additions and 25 deletions.
36 changes: 11 additions & 25 deletions compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -468,12 +468,10 @@ object Types extends TypeUtils {
/** Does this application expand to a match type? */
def isMatchAlias(using Context): Boolean = underlyingMatchType.exists

def underlyingMatchType(using Context): Type = stripped match {
def underlyingMatchType(using Context): Type = stripped match
case tp: MatchType => tp
case tp: HKTypeLambda => tp.resType.underlyingMatchType
case tp: AppliedType => tp.underlyingMatchType
case _ => NoType
}

/** Is this a higher-kinded type lambda with given parameter variances?
* These lambdas are used as the RHS of higher-kinded abstract types or
Expand Down Expand Up @@ -4647,35 +4645,23 @@ object Types extends TypeUtils {

/** Exists if the tycon is a TypeRef of an alias with an underlying match type.
* 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 tryNormalize(using Context): Type = tycon.stripTypeVar match {
case tycon: TypeRef =>
def tryMatchAlias = tycon.info match
case AliasingBounds(alias) if isMatchAlias =>
trace(i"normalize $this", typr, show = true) {
MatchTypeTrace.recurseWith(this) {
alias.applyIfParameterized(args).tryNormalize
/* `applyIfParameterized` may reduce several HKTypeLambda applications
* before the underlying MatchType is reached.
* Even if they do not involve any match type normalizations yet,
* we still want to record these reductions in the MatchTypeTrace.
* They should however only be attempted if they eventually expand
* to a match type, which is ensured by the `isMatchAlias` guard.
*/
}
}
case _ =>
NoType
tryCompiletimeConstantFold.orElse(tryMatchAlias)
case _ =>
NoType
}
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
else NoType
tryCompiletimeConstantFold.orElse(tryMatchAlias)

/** Is this an unreducible application to wildcard arguments?
* This is the case if tycon is higher-kinded. This means
Expand Down

0 comments on commit a0839bc

Please sign in to comment.