Skip to content

Commit 9b1085f

Browse files
committed
Divergence check uses HashMap
1 parent dea58c1 commit 9b1085f

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

compiler/src/dotty/tools/dotc/core/TypeComparer.scala

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3596,7 +3596,6 @@ object MatchReducer:
35963596
case Stuck => "Stuck"
35973597
case NoInstance(fails) => "NoInstance(" ~ Text(fails.map(p.toText(_) ~ p.toText(_)), ", ") ~ ")"
35983598

3599-
val Reduction = new Property.Key[Stack[Type]]
36003599

36013600
/** A type comparer for reducing match types.
36023601
* TODO: Not sure this needs to be a type comparer. Can we make it a
@@ -3944,23 +3943,7 @@ class MatchReducer(initctx: Context) extends TypeComparer(initctx) {
39443943
MatchTypeTrace.noInstance(scrut, cas, fails)
39453944
NoType
39463945
case MatchResult.Reduced(tp) =>
3947-
val reductionStack = ctx.property(Reduction) match
3948-
case Some(stack) => stack
3949-
case None => Stack[Type]()
3950-
3951-
println(s"Scrutinee ${scrut.show}")
3952-
println(s"Reduction stack: ${reductionStack.map(_.show)}")
3953-
3954-
if reductionStack.contains(tp) then
3955-
ErrorType(em"Cyclic match type reduction detected")
3956-
else if !reductionStack.isEmpty && tp.typeSize(false) > reductionStack.top.typeSize(false) then
3957-
ErrorType(em"Non-decresing match type reduction detected: ${tp.show}")
3958-
else
3959-
val bodyType =
3960-
reductionStack.push(tp)
3961-
given Context = ctx.fresh.setProperty(Reduction, reductionStack)
3962-
tp.simplified
3963-
bodyType
3946+
tp.simplified
39643947

39653948
case MatchResult.ReducedAndDisjoint =>
39663949
// Empty types break the basic assumption that if a scrutinee and a

compiler/src/dotty/tools/dotc/core/Types.scala

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,16 @@ import cc.*
4343
import CaptureSet.IdentityCaptRefMap
4444
import Capabilities.*
4545
import transform.Recheck.currentRechecker
46+
import scala.collection.immutable.HashMap
47+
import dotty.tools.dotc.util.Property
4648

4749
import scala.annotation.internal.sharable
4850
import scala.annotation.threadUnsafe
4951

5052
object 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

Comments
 (0)