@@ -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
@@ -4625,8 +4621,8 @@ object Types extends TypeUtils {
4625
4621
private var myEvalRunId : RunId = NoRunId
4626
4622
private var myEvalued : Type = uninitialized
4627
4623
4628
- private var validUnderlyingMatch : Period = Nowhere
4629
- private var cachedUnderlyingMatch : Type = uninitialized
4624
+ private var validUnderlyingNormalizable : Period = Nowhere
4625
+ private var cachedUnderlyingNormalizable : Type = uninitialized
4630
4626
4631
4627
def isGround (acc : TypeAccumulator [Boolean ])(using Context ): Boolean =
4632
4628
if myGround == 0 then myGround = if acc.foldOver(true , this ) then 1 else - 1
@@ -4690,37 +4686,25 @@ object Types extends TypeUtils {
4690
4686
case nil => x
4691
4687
foldArgs(op(x, tycon), args)
4692
4688
4693
- /** Exists if the tycon is a TypeRef of an alias with an underlying match type.
4694
- * Anything else should have already been reduced in `appliedTo` by the TypeAssigner.
4689
+ /** Exists if the tycon is a TypeRef of an alias with an underlying match type,
4690
+ * or a compiletime applied type. Anything else should have already been
4691
+ * reduced in `appliedTo` by the TypeAssigner. This may reduce several
4692
+ * HKTypeLambda applications before the underlying normalizable type is reached.
4695
4693
*/
4696
- override def underlyingMatchType (using Context ): Type =
4697
- if ctx.period != validUnderlyingMatch then
4698
- cachedUnderlyingMatch = superType.underlyingMatchType
4699
- validUnderlyingMatch = validSuper
4700
- cachedUnderlyingMatch
4694
+ override def underlyingNormalizable (using Context ): Type =
4695
+ if ctx.period != validUnderlyingNormalizable then tycon match
4696
+ case tycon : TypeRef if defn.isCompiletimeAppliedType(tycon.symbol) =>
4697
+ cachedUnderlyingNormalizable = this
4698
+ validUnderlyingNormalizable = ctx.period
4699
+ case _ =>
4700
+ cachedUnderlyingNormalizable = superType.underlyingNormalizable
4701
+ validUnderlyingNormalizable = validSuper
4702
+ cachedUnderlyingNormalizable
4701
4703
4702
- override def tryNormalize (using Context ): Type = tycon.stripTypeVar match {
4703
- case tycon : TypeRef =>
4704
- def tryMatchAlias = tycon.info match
4705
- case AliasingBounds (alias) if isMatchAlias =>
4706
- trace(i " normalize $this" , typr, show = true ) {
4707
- MatchTypeTrace .recurseWith(this ) {
4708
- alias.applyIfParameterized(args.map(_.normalized)).tryNormalize
4709
- /* `applyIfParameterized` may reduce several HKTypeLambda applications
4710
- * before the underlying MatchType is reached.
4711
- * Even if they do not involve any match type normalizations yet,
4712
- * we still want to record these reductions in the MatchTypeTrace.
4713
- * They should however only be attempted if they eventually expand
4714
- * to a match type, which is ensured by the `isMatchAlias` guard.
4715
- */
4716
- }
4717
- }
4718
- case _ =>
4719
- NoType
4720
- tryCompiletimeConstantFold.orElse(tryMatchAlias)
4721
- case _ =>
4722
- NoType
4723
- }
4704
+ override def tryNormalize (using Context ): Type =
4705
+ if isMatchAlias && MatchTypeTrace .isRecording then
4706
+ MatchTypeTrace .recurseWith(this )(superType.tryNormalize)
4707
+ else super .tryNormalize
4724
4708
4725
4709
/** Is this an unreducible application to wildcard arguments?
4726
4710
* This is the case if tycon is higher-kinded. This means
@@ -5183,13 +5167,6 @@ object Types extends TypeUtils {
5183
5167
private var myReduced : Type | Null = null
5184
5168
private var reductionContext : util.MutableMap [Type , Type ] | Null = null
5185
5169
5186
- override def tryNormalize (using Context ): Type =
5187
- try
5188
- reduced.normalized
5189
- catch
5190
- case ex : Throwable =>
5191
- handleRecursive(" normalizing" , s " ${scrutinee.show} match ... " , ex)
5192
-
5193
5170
private def thisMatchType = this
5194
5171
5195
5172
def reduced (using Context ): Type = atPhaseNoLater(elimOpaquePhase) {
@@ -5292,7 +5269,7 @@ object Types extends TypeUtils {
5292
5269
def apply (bound : Type , scrutinee : Type , cases : List [Type ])(using Context ): MatchType =
5293
5270
unique(new CachedMatchType (bound, scrutinee, cases))
5294
5271
5295
- def thatReducesUsingGadt (tp : Type )(using Context ): Boolean = tp.underlyingMatchType match
5272
+ def thatReducesUsingGadt (tp : Type )(using Context ): Boolean = tp.underlyingNormalizable match
5296
5273
case mt : MatchType => mt.reducesUsingGadt
5297
5274
case _ => false
5298
5275
@@ -5741,7 +5718,8 @@ object Types extends TypeUtils {
5741
5718
/** Common supertype of `TypeAlias` and `MatchAlias` */
5742
5719
abstract class AliasingBounds (val alias : Type ) extends TypeBounds (alias, alias) {
5743
5720
5744
- def derivedAlias (alias : Type )(using Context ): AliasingBounds
5721
+ def derivedAlias (alias : Type )(using Context ): AliasingBounds =
5722
+ if alias eq this .alias then this else AliasingBounds (alias)
5745
5723
5746
5724
override def computeHash (bs : Binders ): Int = doHash(bs, alias)
5747
5725
override def hashIsStable : Boolean = alias.hashIsStable
@@ -5763,10 +5741,7 @@ object Types extends TypeUtils {
5763
5741
5764
5742
/** = T
5765
5743
*/
5766
- class TypeAlias (alias : Type ) extends AliasingBounds (alias) {
5767
- def derivedAlias (alias : Type )(using Context ): AliasingBounds =
5768
- if (alias eq this .alias) this else TypeAlias (alias)
5769
- }
5744
+ class TypeAlias (alias : Type ) extends AliasingBounds (alias)
5770
5745
5771
5746
/** = T where `T` is a `MatchType`
5772
5747
*
@@ -5775,10 +5750,7 @@ object Types extends TypeUtils {
5775
5750
* If we assumed full substitutivity, we would have to reject all recursive match
5776
5751
* aliases (or else take the jump and allow full recursive types).
5777
5752
*/
5778
- class MatchAlias (alias : Type ) extends AliasingBounds (alias) {
5779
- def derivedAlias (alias : Type )(using Context ): AliasingBounds =
5780
- if (alias eq this .alias) this else MatchAlias (alias)
5781
- }
5753
+ class MatchAlias (alias : Type ) extends AliasingBounds (alias)
5782
5754
5783
5755
object TypeBounds {
5784
5756
def apply (lo : Type , hi : Type )(using Context ): TypeBounds =
0 commit comments