Skip to content

Commit c0dcf97

Browse files
committed
less eager dealiasing of type aliases
1 parent d53cf4b commit c0dcf97

31 files changed

+203
-55
lines changed

compiler/src/dotty/tools/dotc/config/Config.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,10 @@ object Config {
174174

175175
/** If this flag is on, always rewrite an application `S[Ts]` where `S` is an alias for
176176
* `[Xs] -> U` to `[Xs := Ts]U`.
177-
* Turning this flag on was observed to give a ~6% speedup on the JUnit test suite.
177+
* Turning this flag on was observed to give a ~6% speedup on the JUnit test suite
178+
* but over-eagerly dealiases type aliases.
178179
*/
179-
inline val simplifyApplications = true
180+
inline val simplifyApplications = false
180181

181182
/** Assume -indent by default */
182183
inline val defaultIndent = true

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ class OrderingConstraint(private val boundsMap: ParamBounds,
436436
* of the parameter elsewhere in the constraint by type `tp`.
437437
*/
438438
def replace(param: TypeParamRef, tp: Type)(using Context): OrderingConstraint =
439-
val replacement = tp.dealiasKeepAnnots.stripTypeVar
439+
val replacement = tp.stripTypeVar
440440
if param == replacement then this.checkNonCyclic()
441441
else
442442
assert(replacement.isValueTypeOrLambda)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ class TypeApplications(val self: Type) extends AnyVal {
325325
case dealiased: HKTypeLambda =>
326326
def tryReduce =
327327
if (!args.exists(isBounds)) {
328-
val followAlias = Config.simplifyApplications && {
328+
val followAlias = (Config.simplifyApplications || self.typeSymbol.isPrivate) && {
329329
dealiased.resType match {
330330
case AppliedType(tyconBody, dealiasedArgs) =>
331331
// Reduction should not affect type inference when it's

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,25 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
403403
case tp1: NamedType =>
404404
tp1.info match {
405405
case info1: TypeAlias =>
406-
if (recur(info1.alias, tp2)) return true
406+
def realiasConstraint() = tp2 match {
407+
case tp2: TypeParamRef =>
408+
constraint.entry(tp2) match {
409+
case TypeBounds(lo, hi) =>
410+
val aliasLo = tp1 != lo && info1.alias == lo
411+
val aliasHi = tp1 != hi && info1.alias == hi
412+
if aliasLo || aliasHi then
413+
constraint = constraint.updateEntry(tp2, TypeBounds(
414+
if aliasLo then tp1 else lo,
415+
if aliasHi then tp1 else hi))
416+
case tp =>
417+
if tp1 != tp && info1.alias == tp then
418+
constraint = constraint.updateEntry(tp2, tp1)
419+
}
420+
case _ =>
421+
}
422+
val res = recur(info1.alias, tp2)
423+
if (tp1.symbol.isStatic) realiasConstraint()
424+
if (res) return true
407425
if (tp1.prefix.isStable) return tryLiftedToThis1
408426
case _ =>
409427
if (tp1 eq NothingType) || isBottom(tp1) then return true

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ object TypeOps:
423423
override def apply(tp: Type): Type = tp match
424424
case tp: TermRef
425425
if toAvoid(tp) =>
426-
tp.info.widenExpr.dealias match {
426+
tp.info.widenExpr match {
427427
case info: SingletonType => apply(info)
428428
case info => range(defn.NothingType, apply(info))
429429
}

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

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,11 +1300,11 @@ object Types {
13001300
case tp =>
13011301
tp
13021302

1303-
/** Widen all top-level singletons reachable by dealiasing
1304-
* and going to the operands of & and |.
1303+
/** Widen all top-level singletons reachable
1304+
* by going to the operands of & and |.
13051305
* Overridden and cached in OrType.
13061306
*/
1307-
def widenSingletons(using Context): Type = dealias match {
1307+
def widenSingletons(using Context): Type = this match {
13081308
case tp: SingletonType =>
13091309
tp.widen
13101310
case tp: OrType =>
@@ -1846,11 +1846,11 @@ object Types {
18461846
case _ => this
18471847
}
18481848

1849-
/** The set of distinct symbols referred to by this type, after all aliases are expanded */
1849+
/** The set of distinct symbols referred to by this type, after */
18501850
def coveringSet(using Context): Set[Symbol] =
18511851
(new CoveringSetAccumulator).apply(Set.empty[Symbol], this)
18521852

1853-
/** The number of applications and refinements in this type, after all aliases are expanded */
1853+
/** The number of applications and refinements in this type, after */
18541854
def typeSize(using Context): Int =
18551855
(new TypeSizeAccumulator).apply(0, this)
18561856

@@ -6137,11 +6137,12 @@ object Types {
61376137

61386138
class TypeSizeAccumulator(using Context) extends TypeAccumulator[Int] {
61396139
var seen = util.HashSet[Type](initialCapacity = 8)
6140-
def apply(n: Int, tp: Type): Int =
6141-
if seen.contains(tp) then n
6140+
def apply(n: Int, tp1: Type): Int =
6141+
val tp0 = tp1.dealias
6142+
if seen.contains(tp0) then n
61426143
else {
6143-
seen += tp
6144-
tp match {
6144+
seen += tp0
6145+
tp0 match {
61456146
case tp: AppliedType =>
61466147
foldOver(n + 1, tp)
61476148
case tp: RefinedType =>
@@ -6151,23 +6152,24 @@ object Types {
61516152
case tp: TypeParamRef =>
61526153
apply(n, TypeComparer.bounds(tp))
61536154
case _ =>
6154-
foldOver(n, tp)
6155+
foldOver(n, tp0)
61556156
}
61566157
}
61576158
}
61586159

61596160
class CoveringSetAccumulator(using Context) extends TypeAccumulator[Set[Symbol]] {
61606161
var seen = util.HashSet[Type](initialCapacity = 8)
6161-
def apply(cs: Set[Symbol], tp: Type): Set[Symbol] =
6162-
if seen.contains(tp) then cs
6162+
def apply(cs: Set[Symbol], tp1: Type): Set[Symbol] =
6163+
val tp0 = tp1.dealias
6164+
if seen.contains(tp0) then cs
61636165
else {
6164-
seen += tp
6165-
tp match {
6166+
seen += tp0
6167+
tp0 match {
61666168
case tp if tp.isExactlyAny || tp.isExactlyNothing =>
61676169
cs
6168-
case tp: AppliedType =>
6170+
case tp: AppliedType if !tp.typeSymbol.isAliasType =>
61696171
foldOver(cs + tp.typeSymbol, tp)
6170-
case tp: RefinedType =>
6172+
case tp: RefinedType if !tp.typeSymbol.isAliasType =>
61716173
foldOver(cs + tp.typeSymbol, tp)
61726174
case tp: TypeRef if tp.info.isTypeAlias =>
61736175
apply(cs, tp.superType)
@@ -6179,7 +6181,7 @@ object Types {
61796181
case tp: TypeParamRef =>
61806182
apply(cs, TypeComparer.bounds(tp))
61816183
case other =>
6182-
foldOver(cs, tp)
6184+
foldOver(cs, tp0)
61836185
}
61846186
}
61856187
}

compiler/test-resources/repl/i5177

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
scala> class A[T]
2+
// defined class A
3+
scala> type B[T] = A[T]
4+
// defined alias type B[T] = A[T]
5+
scala> def b: B[String] = ???
6+
def b: B[String]
7+
scala> class C
8+
// defined class C
9+
scala> type D = C
10+
// defined alias type D = C
11+
scala> def d: D = ???
12+
def d: D

scaladoc/test/dotty/tools/scaladoc/ExternalLocationProviderIntegrationTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class Scaladoc3ExternalLocationProviderIntegrationTest extends ExternalLocationP
4747
".*externalStubs.*::scaladoc3::https://external.stubs/api/"
4848
),
4949
List(
50-
"https://dotty.epfl.ch/api/scala/collection/immutable/Map.html",
50+
"https://dotty.epfl.ch/api/scala/Predef$.html#Map-0",
5151
"https://dotty.epfl.ch/api/scala/Predef$.html#String-0",
5252
"https://dotty.epfl.ch/api/scala/util/matching/Regex$$Match.html",
5353
"https://external.stubs/api/tests/externalStubs/$div$bslash$.html",
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
trait Iterable[T]
2+
3+
@deprecated type Traversable[T] = Iterable[T]
4+
5+
def test: Traversable[Int] = ??? // error

tests/neg-macros/i6997.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ import scala.quoted.*
33
class Foo {
44
def mcrImpl(body: Expr[Any])(using t: Type[_ <: Any])(using ctx: Quotes): Expr[Any] = '{
55
val tmp = ???.asInstanceOf[t.Underlying] // error // error
6-
tmp
6+
tmp // error
77
}
88
}

0 commit comments

Comments
 (0)