Skip to content

Commit 9e3ce5e

Browse files
authored
Also reduce term projections (#19389)
We already reduce `R { type A = T } # A` to `T` in most situations when we create types. We now also reduce `R { val x: S } # x` to `S` if `S` is a singleton type. This will simplify types as we go to more term-dependent typing. As a concrete benefit, it will avoid several test-pickling failures due to pickling differences when using dependent types.
2 parents 5bd93b1 + daa29e6 commit 9e3ce5e

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

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

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,6 +1645,8 @@ object Types extends TypeUtils {
16451645
pre.refinedInfo match {
16461646
case tp: AliasingBounds =>
16471647
if (pre.refinedName ne name) loop(pre.parent) else tp.alias
1648+
case tp: SingletonType =>
1649+
if pre.refinedName ne name then loop(pre.parent) else tp
16481650
case _ =>
16491651
loop(pre.parent)
16501652
}
@@ -2676,11 +2678,8 @@ object Types extends TypeUtils {
26762678
* refinement type `T { X = U; ... }`
26772679
*/
26782680
def reduceProjection(using Context): Type =
2679-
if (isType) {
2680-
val reduced = prefix.lookupRefined(name)
2681-
if (reduced.exists) reduced else this
2682-
}
2683-
else this
2681+
val reduced = prefix.lookupRefined(name)
2682+
if reduced.exists then reduced else this
26842683

26852684
/** Guard against cycles that can arise if given `op`
26862685
* follows info. The problematic cases are a type alias to itself or
@@ -2765,14 +2764,14 @@ object Types extends TypeUtils {
27652764
* (S | T)#A --> S#A | T#A
27662765
*/
27672766
def derivedSelect(prefix: Type)(using Context): Type =
2768-
if (prefix eq this.prefix) this
2769-
else if (prefix.isExactlyNothing) prefix
2767+
if prefix eq this.prefix then this
2768+
else if prefix.isExactlyNothing then prefix
27702769
else {
2770+
val res =
2771+
if (isType && currentValidSymbol.isAllOf(ClassTypeParam)) argForParam(prefix)
2772+
else prefix.lookupRefined(name)
2773+
if (res.exists) return res
27712774
if (isType) {
2772-
val res =
2773-
if (currentValidSymbol.isAllOf(ClassTypeParam)) argForParam(prefix)
2774-
else prefix.lookupRefined(name)
2775-
if (res.exists) return res
27762775
if (Config.splitProjections)
27772776
prefix match {
27782777
case prefix: AndType =>
@@ -6563,7 +6562,7 @@ object Types extends TypeUtils {
65636562
record(s"foldOver $getClass")
65646563
record(s"foldOver total")
65656564
tp match {
6566-
case tp: TypeRef =>
6565+
case tp: NamedType =>
65676566
if stopBecauseStaticOrLocal(tp) then x
65686567
else
65696568
val tp1 = tp.prefix.lookupRefined(tp.name)
@@ -6592,9 +6591,6 @@ object Types extends TypeUtils {
65926591
variance = saved
65936592
this(y, restpe)
65946593

6595-
case tp: TermRef =>
6596-
if stopBecauseStaticOrLocal(tp) then x else applyToPrefix(x, tp)
6597-
65986594
case tp: TypeVar =>
65996595
this(x, tp.underlying)
66006596

0 commit comments

Comments
 (0)