Skip to content

Commit 96843b8

Browse files
committed
refactor path aliasing constraints
1 parent a6f3a3a commit 96843b8

File tree

3 files changed

+22
-43
lines changed

3 files changed

+22
-43
lines changed

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

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,11 @@ sealed abstract class GadtConstraint extends Showable {
5959
/** Further constrain a path-dependent type already present in the constraint. */
6060
def addBound(p: PathType, sym: Symbol, bound: Type, isUpper: Boolean)(using Context): Boolean
6161

62-
/** Record the equality between two singleton types. */
63-
def addEquality(p: PathType, q: PathType)(using Context): Unit
62+
/** Record the aliasing relationship between two singleton types. */
63+
def recordPathAliasing(p: PathType, q: PathType)(using Context): Unit
6464

65-
/** Check whether two singleton types are equivalent. */
66-
def isEquivalent(p: PathType, q: PathType): Boolean
67-
68-
/** Query the representative member of a singleton type. */
69-
def reprOf(p: PathType): PathType | Null
65+
/** Check whether two paths are equivalent via path aliasing. */
66+
def isAliasingPath(p: PathType, q: PathType): Boolean
7067

7168
/** Scrutinee path of the current pattern matching. */
7269
def scrutineePath: TermRef | Null
@@ -124,7 +121,7 @@ final class ProperGadtConstraint private(
124121
private var pathDepReverseMapping: SimpleIdentityMap[TypeParamRef, TypeRef],
125122
private var wasConstrained: Boolean,
126123
private var myScrutineePath: TermRef | Null,
127-
private var myUnionFind: SimpleIdentityMap[PathType, PathType],
124+
private var pathAliasingMap: SimpleIdentityMap[PathType, PathType],
128125
private var myPatternSkolem: SkolemType | Null,
129126
) extends GadtConstraint with ConstraintHandling {
130127
import dotty.tools.dotc.config.Printers.{gadts, gadtsConstr}
@@ -137,7 +134,7 @@ final class ProperGadtConstraint private(
137134
pathDepReverseMapping = SimpleIdentityMap.empty,
138135
wasConstrained = false,
139136
myScrutineePath = null,
140-
myUnionFind = SimpleIdentityMap.empty,
137+
pathAliasingMap = SimpleIdentityMap.empty,
141138
myPatternSkolem = null,
142139
)
143140

@@ -392,9 +389,6 @@ final class ProperGadtConstraint private(
392389
buf ++= "}"
393390
buf.result
394391

395-
/** Get the representative member of the path in the union find. */
396-
override def reprOf(p: PathType): PathType | Null = lookupPath(p)
397-
398392
override def addToConstraint(params: List[Symbol])(using Context): Boolean = {
399393
import NameKinds.DepParamName
400394

@@ -512,15 +506,15 @@ final class ProperGadtConstraint private(
512506
}
513507

514508
private def lookupPath(p: PathType): PathType | Null =
515-
def recur(p: PathType): PathType | Null = myUnionFind(p) match
509+
def recur(p: PathType): PathType | Null = pathAliasingMap(p) match
516510
case null => null
517511
case q: PathType if q eq p => q
518512
case q: PathType =>
519513
recur(q)
520514

521515
recur(p)
522516

523-
override def addEquality(p: PathType, q: PathType)(using Context): Unit =
517+
override def recordPathAliasing(p: PathType, q: PathType)(using Context): Unit =
524518
val pRep: PathType | Null = lookupPath(p)
525519
val qRep: PathType | Null = lookupPath(q)
526520

@@ -529,13 +523,13 @@ final class ProperGadtConstraint private(
529523
case (null, r: PathType) => r
530524
case (r: PathType, null) => r
531525
case (r1: PathType, r2: PathType) =>
532-
myUnionFind = myUnionFind.updated(r2, r1)
526+
pathAliasingMap = pathAliasingMap.updated(r2, r1)
533527
r1
534528

535-
myUnionFind = myUnionFind.updated(p, newRep)
536-
myUnionFind = myUnionFind.updated(q, newRep)
529+
pathAliasingMap = pathAliasingMap.updated(p, newRep)
530+
pathAliasingMap = pathAliasingMap.updated(q, newRep)
537531

538-
override def isEquivalent(p: PathType, q: PathType): Boolean =
532+
override def isAliasingPath(p: PathType, q: PathType): Boolean =
539533
lookupPath(p) match
540534
case null => false
541535
case p0: PathType => lookupPath(q) match
@@ -637,7 +631,7 @@ final class ProperGadtConstraint private(
637631
pathDepReverseMapping,
638632
wasConstrained,
639633
myScrutineePath,
640-
myUnionFind,
634+
pathAliasingMap,
641635
myPatternSkolem,
642636
)
643637

@@ -650,7 +644,7 @@ final class ProperGadtConstraint private(
650644
this.pathDepReverseMapping = other.pathDepReverseMapping
651645
this.wasConstrained = other.wasConstrained
652646
this.myScrutineePath = other.myScrutineePath
653-
this.myUnionFind = other.myUnionFind
647+
this.pathAliasingMap = other.pathAliasingMap
654648
this.myPatternSkolem = other.myPatternSkolem
655649
case _ => ;
656650
}
@@ -675,10 +669,10 @@ final class ProperGadtConstraint private(
675669
}
676670

677671
def updateUnionFind() =
678-
myUnionFind(myPatternSkolem.nn) match {
672+
pathAliasingMap(myPatternSkolem.nn) match {
679673
case null =>
680674
case repr: PathType =>
681-
myUnionFind = myUnionFind.updated(path, repr)
675+
pathAliasingMap = pathAliasingMap.updated(path, repr)
682676
}
683677

684678
updateMappings()
@@ -784,7 +778,7 @@ final class ProperGadtConstraint private(
784778
}
785779
}
786780
sb ++= "\nSingleton equalities:\n"
787-
myUnionFind foreachBinding { case (path, _) =>
781+
pathAliasingMap foreachBinding { case (path, _) =>
788782
val repr = lookupPath(path)
789783
repr match
790784
case repr: PathType if repr ne path =>
@@ -805,8 +799,6 @@ final class ProperGadtConstraint private(
805799
override def bounds(tp: TypeRef)(using Context): TypeBounds | Null = null
806800
override def fullBounds(tp: TypeRef)(using Context): TypeBounds | Null = null
807801

808-
override def reprOf(p: PathType): PathType | Null = null
809-
810802
override def isLess(sym1: Symbol, sym2: Symbol)(using Context): Boolean = unsupported("EmptyGadtConstraint.isLess")
811803
override def isLess(tp1: NamedType, tp2: NamedType)(using Context): Boolean = unsupported("EmptyGadtConstraint.isLess")
812804

@@ -827,9 +819,9 @@ final class ProperGadtConstraint private(
827819
override def addBound(sym: Symbol, bound: Type, isUpper: Boolean)(using Context): Boolean = unsupported("EmptyGadtConstraint.addBound")
828820
override def addBound(path: PathType, sym: Symbol, bound: Type, isUpper: Boolean)(using Context): Boolean = unsupported("EmptyGadtConstraint.addBound")
829821

830-
override def addEquality(p: PathType, q: PathType)(using Context) = ()
822+
override def recordPathAliasing(p: PathType, q: PathType)(using Context) = ()
831823

832-
override def isEquivalent(p: PathType, q: PathType) = false
824+
override def isAliasingPath(p: PathType, q: PathType) = false
833825

834826
override def approximation(sym: Symbol, fromBelow: Boolean, maxLevel: Int)(using Context): Type = unsupported("EmptyGadtConstraint.approximation")
835827

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ trait PatternTypeConstrainer { self: TypeComparer =>
230230
/** Reconstruct subtype from the cohabitation between the scrutinee and the
231231
pattern. */
232232
def constrainPattern: Boolean = {
233-
ctx.gadt.addEquality(scrutineePath, patternPath)
233+
ctx.gadt.recordPathAliasing(scrutineePath, patternPath)
234234

235235
(!registerPattern || reconstructSubTypeFor(patternPath, scrutineePath))
236236
&& (!registerScrutinee || reconstructSubTypeFor(scrutineePath, patternPath))
@@ -253,7 +253,7 @@ trait PatternTypeConstrainer { self: TypeComparer =>
253253
(!registerPtPath || reconstructSubTypeFor(ptPath, scrutineePath))
254254
&& (!registerScrutinee || reconstructSubTypeFor(scrutineePath, ptPath))
255255

256-
ctx.gadt.addEquality(scrutineePath, ptPath)
256+
ctx.gadt.recordPathAliasing(scrutineePath, ptPath)
257257

258258
result
259259
case _ =>

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

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
593593
def compareSingletonGADT: Boolean =
594594
(tp1, tp2) match {
595595
case (tp1: TermRef, tp2: TermRef) =>
596-
ctx.gadt.isEquivalent(tp1, tp2) && { GADTused = true; true }
596+
ctx.gadt.isAliasingPath(tp1, tp2) && { GADTused = true; true }
597597
case _ => false
598598
}
599599

@@ -2360,19 +2360,6 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
23602360
case Atoms.Range(lo2, hi2) =>
23612361
if hi1.subsetOf(lo2) then return tp2
23622362
if hi2.subsetOf(lo1) then return tp1
2363-
2364-
def getReprSet(ps: Set[Type]): Set[Type] =
2365-
ps.map { x =>
2366-
x match
2367-
case p: PathType =>
2368-
val rep = ctx.gadt.reprOf(p)
2369-
if rep == null then p else rep
2370-
case t => t
2371-
}
2372-
val (repLo1, repHi1, repLo2, repHi2) = (getReprSet(lo1), getReprSet(hi1), getReprSet(lo2), getReprSet(hi2))
2373-
if repHi2.subsetOf(repLo1) then return tp1
2374-
if repHi1.subsetOf(repLo2) then return tp2
2375-
23762363
if (hi1 & hi2).isEmpty then return orType(tp1, tp2)
23772364
case none =>
23782365
case none =>

0 commit comments

Comments
 (0)