@@ -43,12 +43,16 @@ import cc.*
4343import CaptureSet .IdentityCaptRefMap
4444import Capabilities .*
4545import transform .Recheck .currentRechecker
46+ import scala .collection .immutable .HashMap
47+ import dotty .tools .dotc .util .Property
4648
4749import scala .annotation .internal .sharable
4850import scala .annotation .threadUnsafe
4951
5052object Types extends TypeUtils {
5153
54+ val Reduction = new Property .Key [HashMap [Type , List [Type ]]]
55+
5256 @ sharable private var nextId = 0
5357
5458 implicit def eqType : CanEqual [Type , Type ] = CanEqual .derived
@@ -1628,7 +1632,27 @@ object Types extends TypeUtils {
16281632 * then the result after applying all toplevel normalizations, otherwise NoType.
16291633 */
16301634 def tryNormalize (using Context ): Type = underlyingNormalizable match
1631- case mt : MatchType => mt.reduced.normalized
1635+ case mt : MatchType =>
1636+ this match
1637+ case self : AppliedType =>
1638+ println(i " Applied match type alias: ${self.tycon} with args ${self.args}" )
1639+ println(i " Arguments Size: ${self.args.map(_.typeSize()).sum}" )
1640+ // Check that if self.args is smaller than previous size for
1641+ // self.tycon if already set in the map
1642+ val reductionMap = ctx.property(Reduction ) match
1643+ case Some (map) => map
1644+ case None => HashMap [Type , List [Type ]]()
1645+
1646+
1647+ if reductionMap.contains(self.tycon) && self.args.map(_.typeSize()).sum >= reductionMap(self.tycon).map(_.typeSize()).sum then
1648+ ErrorType (em " Non-decresing match type reduction detected: ${self.show}" )
1649+ else
1650+ val result =
1651+ val newMap = reductionMap.updated(self.tycon, self.args)
1652+ given Context = ctx.fresh.setProperty(Reduction , newMap)
1653+ mt.reduced.normalized
1654+ result
1655+ case _ => mt.reduced.normalized
16321656 case tp : AppliedType => tp.tryCompiletimeConstantFold
16331657 case _ => NoType
16341658
0 commit comments