Skip to content

Commit 71dab76

Browse files
committed
Refactor dealias
Refactor dealias to make it easier to add further keep conditions
1 parent 5863c07 commit 71dab76

File tree

1 file changed

+35
-26
lines changed

1 file changed

+35
-26
lines changed

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

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,48 +1473,48 @@ object Types extends TypeUtils {
14731473
case Atoms.Unknown => Atoms.Unknown
14741474
case _ => Atoms.Unknown
14751475

1476-
private def dealias1(keep: AnnotatedType => Context ?=> Boolean, keepOpaques: Boolean)(using Context): Type = this match {
1476+
def dealias(keeps: Keeps)(using Context): Type = this match
14771477
case tp: TypeRef =>
1478-
if (tp.symbol.isClass) tp
1479-
else tp.info match {
1480-
case TypeAlias(alias) if !(keepOpaques && tp.symbol.is(Opaque)) =>
1481-
alias.dealias1(keep, keepOpaques)
1478+
if tp.symbol.isClass then tp
1479+
else tp.info match
1480+
case TypeAlias(alias) if (keeps & KeepOpaques) == 0 || !tp.symbol.is(Opaque) =>
1481+
alias.dealias(keeps)
14821482
case _ => tp
1483-
}
14841483
case app @ AppliedType(tycon, _) =>
1485-
val tycon1 = tycon.dealias1(keep, keepOpaques)
1486-
if (tycon1 ne tycon) app.superType.dealias1(keep, keepOpaques)
1484+
val tycon1 = tycon.dealias(keeps)
1485+
if tycon1 ne tycon then app.superType.dealias(keeps)
14871486
else this
1488-
case tp: TypeVar =>
1487+
case tp: TypeVar if (keeps & KeepTypeVars) == 0 =>
14891488
val tp1 = tp.instanceOpt
1490-
if (tp1.exists) tp1.dealias1(keep, keepOpaques) else tp
1489+
if tp1.exists then tp1.dealias(keeps) else tp
14911490
case tp: AnnotatedType =>
1492-
val parent1 = tp.parent.dealias1(keep, keepOpaques)
1493-
if keep(tp) then tp.derivedAnnotatedType(parent1, tp.annot)
1491+
val parent1 = tp.parent.dealias(keeps)
1492+
if (keeps & KeepAnnots) != 0
1493+
|| (keeps & KeepRefiningAnnots) != 0 && tp.isRefining
1494+
then tp.derivedAnnotatedType(parent1, tp.annot)
14941495
else tp match
14951496
case tp @ CapturingType(parent, refs) =>
14961497
tp.derivedCapturingType(parent1, refs)
14971498
case _ =>
14981499
parent1
14991500
case tp: LazyRef =>
1500-
tp.ref.dealias1(keep, keepOpaques)
1501+
tp.ref.dealias(keeps)
15011502
case _ => this
1502-
}
15031503

15041504
/** Follow aliases and dereference LazyRefs, annotated types and instantiated
15051505
* TypeVars until type is no longer alias type, annotated type, LazyRef,
15061506
* or instantiated type variable.
15071507
*/
1508-
final def dealias(using Context): Type = dealias1(keepNever, keepOpaques = false)
1508+
final def dealias(using Context): Type = dealias(KeepNothing)
15091509

15101510
/** Follow aliases and dereference LazyRefs and instantiated TypeVars until type
15111511
* is no longer alias type, LazyRef, or instantiated type variable.
15121512
* Goes through annotated types and rewraps annotations on the result.
15131513
*/
1514-
final def dealiasKeepAnnots(using Context): Type = dealias1(keepAlways, keepOpaques = false)
1514+
final def dealiasKeepAnnots(using Context): Type = dealias(KeepAnnots)
15151515

15161516
/** Like `dealiasKeepAnnots`, but keeps only refining annotations */
1517-
final def dealiasKeepRefiningAnnots(using Context): Type = dealias1(keepIfRefining, keepOpaques = false)
1517+
final def dealiasKeepRefiningAnnots(using Context): Type = dealias(KeepRefiningAnnots)
15181518

15191519
/** Like dealias, but does not follow aliases if symbol is Opaque. This is
15201520
* necessary if we want to look at the info of a symbol containing opaque
@@ -1532,13 +1532,13 @@ object Types extends TypeUtils {
15321532
* Here, we dealias symbol infos at the start of capture checking in operation `fluidify`.
15331533
* We have to be careful not to accidentally reveal opaque aliases when doing so.
15341534
*/
1535-
final def dealiasKeepOpaques(using Context): Type = dealias1(keepNever, keepOpaques = true)
1535+
final def dealiasKeepOpaques(using Context): Type = dealias(KeepOpaques)
15361536

15371537
/** Like dealiasKeepAnnots, but does not follow opaque aliases. See `dealiasKeepOpaques`
15381538
* for why this is sometimes necessary.
15391539
*/
15401540
final def dealiasKeepAnnotsAndOpaques(using Context): Type =
1541-
dealias1(keepAlways, keepOpaques = true)
1541+
dealias(KeepAnnots | KeepOpaques)
15421542

15431543
/** Approximate this type with a type that does not contain skolem types. */
15441544
final def deskolemized(using Context): Type =
@@ -1570,19 +1570,18 @@ object Types extends TypeUtils {
15701570
case tp: AppliedType => tp.underlyingNormalizable
15711571
case _ => NoType
15721572

1573-
private def widenDealias1(keep: AnnotatedType => Context ?=> Boolean)(using Context): Type = {
1574-
val res = this.widen.dealias1(keep, keepOpaques = false)
1575-
if (res eq this) res else res.widenDealias1(keep)
1576-
}
1573+
private def widenDealias(keeps: Keeps)(using Context): Type =
1574+
val tp1 = widen.dealias(keeps)
1575+
if tp1 eq this then this else tp1.widenDealias(keeps)
15771576

15781577
/** Perform successive widenings and dealiasings until none can be applied anymore */
1579-
final def widenDealias(using Context): Type = widenDealias1(keepNever)
1578+
final def widenDealias(using Context): Type = widenDealias(KeepNothing)
15801579

15811580
/** Perform successive widenings and dealiasings while rewrapping annotations, until none can be applied anymore */
1582-
final def widenDealiasKeepAnnots(using Context): Type = widenDealias1(keepAlways)
1581+
final def widenDealiasKeepAnnots(using Context): Type = widenDealias(KeepAnnots)
15831582

15841583
/** Perform successive widenings and dealiasings while rewrapping refining annotations, until none can be applied anymore */
1585-
final def widenDealiasKeepRefiningAnnots(using Context): Type = widenDealias1(keepIfRefining)
1584+
final def widenDealiasKeepRefiningAnnots(using Context): Type = widenDealias(KeepRefiningAnnots)
15861585

15871586
/** Widen from constant type to its underlying non-constant
15881587
* base type.
@@ -7101,6 +7100,16 @@ object Types extends TypeUtils {
71017100
def isStable = true
71027101
}
71037102

7103+
// ----- Dealias keep flags --------------------------------------------
7104+
7105+
private type Keeps = Int
7106+
7107+
private val KeepNothing = 0
7108+
private val KeepAnnots = 1
7109+
private val KeepRefiningAnnots = 2
7110+
private val KeepOpaques = 4
7111+
private val KeepTypeVars = 8
7112+
71047113
// ----- Debug ---------------------------------------------------------
71057114

71067115
@sharable var debugTrace: Boolean = false

0 commit comments

Comments
 (0)