Skip to content

Commit f841cf6

Browse files
committed
Refactor dealias
Refactor dealias to make it easier to add further keep conditions
1 parent 03538a1 commit f841cf6

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
@@ -1471,48 +1471,48 @@ object Types extends TypeUtils {
14711471
case Atoms.Unknown => Atoms.Unknown
14721472
case _ => Atoms.Unknown
14731473

1474-
private def dealias1(keep: AnnotatedType => Context ?=> Boolean, keepOpaques: Boolean)(using Context): Type = this match {
1474+
def dealias(keeps: Keeps)(using Context): Type = this match
14751475
case tp: TypeRef =>
1476-
if (tp.symbol.isClass) tp
1477-
else tp.info match {
1478-
case TypeAlias(alias) if !(keepOpaques && tp.symbol.is(Opaque)) =>
1479-
alias.dealias1(keep, keepOpaques)
1476+
if tp.symbol.isClass then tp
1477+
else tp.info match
1478+
case TypeAlias(alias) if (keeps & KeepOpaques) == 0 || !tp.symbol.is(Opaque) =>
1479+
alias.dealias(keeps)
14801480
case _ => tp
1481-
}
14821481
case app @ AppliedType(tycon, _) =>
1483-
val tycon1 = tycon.dealias1(keep, keepOpaques)
1484-
if (tycon1 ne tycon) app.superType.dealias1(keep, keepOpaques)
1482+
val tycon1 = tycon.dealias(keeps)
1483+
if tycon1 ne tycon then app.superType.dealias(keeps)
14851484
else this
1486-
case tp: TypeVar =>
1485+
case tp: TypeVar if (keeps & KeepTypeVars) == 0 =>
14871486
val tp1 = tp.instanceOpt
1488-
if (tp1.exists) tp1.dealias1(keep, keepOpaques) else tp
1487+
if tp1.exists then tp1.dealias(keeps) else tp
14891488
case tp: AnnotatedType =>
1490-
val parent1 = tp.parent.dealias1(keep, keepOpaques)
1491-
if keep(tp) then tp.derivedAnnotatedType(parent1, tp.annot)
1489+
val parent1 = tp.parent.dealias(keeps)
1490+
if (keeps & KeepAnnots) != 0
1491+
|| (keeps & KeepRefiningAnnots) != 0 && tp.isRefining
1492+
then tp.derivedAnnotatedType(parent1, tp.annot)
14921493
else tp match
14931494
case tp @ CapturingType(parent, refs) =>
14941495
tp.derivedCapturingType(parent1, refs)
14951496
case _ =>
14961497
parent1
14971498
case tp: LazyRef =>
1498-
tp.ref.dealias1(keep, keepOpaques)
1499+
tp.ref.dealias(keeps)
14991500
case _ => this
1500-
}
15011501

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

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

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

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

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

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

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

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

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

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

15851584
/** Widen from constant type to its underlying non-constant
15861585
* base type.
@@ -7027,6 +7026,16 @@ object Types extends TypeUtils {
70277026
def isStable = true
70287027
}
70297028

7029+
// ----- Dealias keep flags --------------------------------------------
7030+
7031+
private type Keeps = Int
7032+
7033+
private val KeepNothing = 0
7034+
private val KeepAnnots = 1
7035+
private val KeepRefiningAnnots = 2
7036+
private val KeepOpaques = 4
7037+
private val KeepTypeVars = 8
7038+
70307039
// ----- Debug ---------------------------------------------------------
70317040

70327041
@sharable var debugTrace: Boolean = false

0 commit comments

Comments
 (0)