@@ -490,14 +490,7 @@ object Types extends TypeUtils {
490
490
case _ => false
491
491
492
492
/** Does this application expand to a match type? */
493
- def isMatchAlias (using Context ): Boolean = underlyingMatchType.exists
494
-
495
- def underlyingMatchType (using Context ): Type = stripped match {
496
- case tp : MatchType => tp
497
- case tp : HKTypeLambda => tp.resType.underlyingMatchType
498
- case tp : AppliedType => tp.underlyingMatchType
499
- case _ => NoType
500
- }
493
+ def isMatchAlias (using Context ): Boolean = underlyingNormalizable.isMatch
501
494
502
495
/** Is this a higher-kinded type lambda with given parameter variances?
503
496
* These lambdas are used as the RHS of higher-kinded abstract types or
@@ -1550,19 +1543,24 @@ object Types extends TypeUtils {
1550
1543
}
1551
1544
deskolemizer(this )
1552
1545
1553
- /** The result of normalization using `tryNormalize`, or the type itself if
1554
- * tryNormlize yields NoType
1546
+ /** The result of normalization, or the type itself if none apply. */
1547
+ final def normalized (using Context ): Type = tryNormalize.orElse(this )
1548
+
1549
+ /** If this type has an underlying match type or applied compiletime.ops,
1550
+ * then the result after applying all toplevel normalizations, otherwise NoType.
1555
1551
*/
1556
- final def normalized (using Context ): Type = {
1557
- val normed = tryNormalize
1558
- if (normed.exists) normed else this
1559
- }
1552
+ def tryNormalize (using Context ): Type = underlyingNormalizable match
1553
+ case mt : MatchType => mt.reduced.normalized
1554
+ case tp : AppliedType => tp.tryCompiletimeConstantFold
1555
+ case _ => NoType
1560
1556
1561
- /** If this type can be normalized at the top-level by rewriting match types
1562
- * of S[n] types, the result after applying all toplevel normalizations,
1563
- * otherwise NoType
1557
+ /** Perform successive strippings, and beta-reductions of applied types until
1558
+ * a match type or applied compiletime.ops is reached, if any, otherwise NoType.
1564
1559
*/
1565
- def tryNormalize (using Context ): Type = NoType
1560
+ def underlyingNormalizable (using Context ): Type = stripped.stripLazyRef match
1561
+ case tp : MatchType => tp
1562
+ case tp : AppliedType => tp.underlyingNormalizable
1563
+ case _ => NoType
1566
1564
1567
1565
private def widenDealias1 (keep : AnnotatedType => Context ?=> Boolean )(using Context ): Type = {
1568
1566
val res = this .widen.dealias1(keep, keepOpaques = false )
@@ -3258,8 +3256,6 @@ object Types extends TypeUtils {
3258
3256
private var myRef : Type | Null = null
3259
3257
private var computed = false
3260
3258
3261
- override def tryNormalize (using Context ): Type = ref.tryNormalize
3262
-
3263
3259
def ref (using Context ): Type =
3264
3260
if computed then
3265
3261
if myRef == null then
@@ -4615,8 +4611,8 @@ object Types extends TypeUtils {
4615
4611
private var myEvalRunId : RunId = NoRunId
4616
4612
private var myEvalued : Type = uninitialized
4617
4613
4618
- private var validUnderlyingMatch : Period = Nowhere
4619
- private var cachedUnderlyingMatch : Type = uninitialized
4614
+ private var validUnderlyingNormalizable : Period = Nowhere
4615
+ private var cachedUnderlyingNormalizable : Type = uninitialized
4620
4616
4621
4617
def isGround (acc : TypeAccumulator [Boolean ])(using Context ): Boolean =
4622
4618
if myGround == 0 then myGround = if acc.foldOver(true , this ) then 1 else - 1
@@ -4680,37 +4676,25 @@ object Types extends TypeUtils {
4680
4676
case nil => x
4681
4677
foldArgs(op(x, tycon), args)
4682
4678
4683
- /** Exists if the tycon is a TypeRef of an alias with an underlying match type.
4684
- * Anything else should have already been reduced in `appliedTo` by the TypeAssigner.
4679
+ /** Exists if the tycon is a TypeRef of an alias with an underlying match type,
4680
+ * or a compiletime applied type. Anything else should have already been
4681
+ * reduced in `appliedTo` by the TypeAssigner. This may reduce several
4682
+ * HKTypeLambda applications before the underlying normalizable type is reached.
4685
4683
*/
4686
- override def underlyingMatchType (using Context ): Type =
4687
- if ctx.period != validUnderlyingMatch then
4688
- cachedUnderlyingMatch = superType.underlyingMatchType
4689
- validUnderlyingMatch = validSuper
4690
- cachedUnderlyingMatch
4684
+ override def underlyingNormalizable (using Context ): Type =
4685
+ if ctx.period != validUnderlyingNormalizable then tycon match
4686
+ case tycon : TypeRef if defn.isCompiletimeAppliedType(tycon.symbol) =>
4687
+ cachedUnderlyingNormalizable = this
4688
+ validUnderlyingNormalizable = ctx.period
4689
+ case _ =>
4690
+ cachedUnderlyingNormalizable = superType.underlyingNormalizable
4691
+ validUnderlyingNormalizable = validSuper
4692
+ cachedUnderlyingNormalizable
4691
4693
4692
- override def tryNormalize (using Context ): Type = tycon.stripTypeVar match {
4693
- case tycon : TypeRef =>
4694
- def tryMatchAlias = tycon.info match
4695
- case AliasingBounds (alias) if isMatchAlias =>
4696
- trace(i " normalize $this" , typr, show = true ) {
4697
- MatchTypeTrace .recurseWith(this ) {
4698
- alias.applyIfParameterized(args.map(_.normalized)).tryNormalize
4699
- /* `applyIfParameterized` may reduce several HKTypeLambda applications
4700
- * before the underlying MatchType is reached.
4701
- * Even if they do not involve any match type normalizations yet,
4702
- * we still want to record these reductions in the MatchTypeTrace.
4703
- * They should however only be attempted if they eventually expand
4704
- * to a match type, which is ensured by the `isMatchAlias` guard.
4705
- */
4706
- }
4707
- }
4708
- case _ =>
4709
- NoType
4710
- tryCompiletimeConstantFold.orElse(tryMatchAlias)
4711
- case _ =>
4712
- NoType
4713
- }
4694
+ override def tryNormalize (using Context ): Type =
4695
+ if isMatchAlias && MatchTypeTrace .isRecording then
4696
+ MatchTypeTrace .recurseWith(this )(superType.tryNormalize)
4697
+ else super .tryNormalize
4714
4698
4715
4699
/** Is this an unreducible application to wildcard arguments?
4716
4700
* This is the case if tycon is higher-kinded. This means
@@ -5173,13 +5157,6 @@ object Types extends TypeUtils {
5173
5157
private var myReduced : Type | Null = null
5174
5158
private var reductionContext : util.MutableMap [Type , Type ] | Null = null
5175
5159
5176
- override def tryNormalize (using Context ): Type =
5177
- try
5178
- reduced.normalized
5179
- catch
5180
- case ex : Throwable =>
5181
- handleRecursive(" normalizing" , s " ${scrutinee.show} match ... " , ex)
5182
-
5183
5160
private def thisMatchType = this
5184
5161
5185
5162
def reduced (using Context ): Type = atPhaseNoLater(elimOpaquePhase) {
@@ -5282,7 +5259,7 @@ object Types extends TypeUtils {
5282
5259
def apply (bound : Type , scrutinee : Type , cases : List [Type ])(using Context ): MatchType =
5283
5260
unique(new CachedMatchType (bound, scrutinee, cases))
5284
5261
5285
- def thatReducesUsingGadt (tp : Type )(using Context ): Boolean = tp.underlyingMatchType match
5262
+ def thatReducesUsingGadt (tp : Type )(using Context ): Boolean = tp.underlyingNormalizable match
5286
5263
case mt : MatchType => mt.reducesUsingGadt
5287
5264
case _ => false
5288
5265
@@ -5731,7 +5708,8 @@ object Types extends TypeUtils {
5731
5708
/** Common supertype of `TypeAlias` and `MatchAlias` */
5732
5709
abstract class AliasingBounds (val alias : Type ) extends TypeBounds (alias, alias) {
5733
5710
5734
- def derivedAlias (alias : Type )(using Context ): AliasingBounds
5711
+ def derivedAlias (alias : Type )(using Context ): AliasingBounds =
5712
+ if alias eq this .alias then this else AliasingBounds (alias)
5735
5713
5736
5714
override def computeHash (bs : Binders ): Int = doHash(bs, alias)
5737
5715
override def hashIsStable : Boolean = alias.hashIsStable
@@ -5753,10 +5731,7 @@ object Types extends TypeUtils {
5753
5731
5754
5732
/** = T
5755
5733
*/
5756
- class TypeAlias (alias : Type ) extends AliasingBounds (alias) {
5757
- def derivedAlias (alias : Type )(using Context ): AliasingBounds =
5758
- if (alias eq this .alias) this else TypeAlias (alias)
5759
- }
5734
+ class TypeAlias (alias : Type ) extends AliasingBounds (alias)
5760
5735
5761
5736
/** = T where `T` is a `MatchType`
5762
5737
*
@@ -5765,10 +5740,7 @@ object Types extends TypeUtils {
5765
5740
* If we assumed full substitutivity, we would have to reject all recursive match
5766
5741
* aliases (or else take the jump and allow full recursive types).
5767
5742
*/
5768
- class MatchAlias (alias : Type ) extends AliasingBounds (alias) {
5769
- def derivedAlias (alias : Type )(using Context ): AliasingBounds =
5770
- if (alias eq this .alias) this else MatchAlias (alias)
5771
- }
5743
+ class MatchAlias (alias : Type ) extends AliasingBounds (alias)
5772
5744
5773
5745
object TypeBounds {
5774
5746
def apply (lo : Type , hi : Type )(using Context ): TypeBounds =
0 commit comments