@@ -4850,7 +4850,7 @@ object Types {
4850
4850
// ----- TypeMaps --------------------------------------------------------------------
4851
4851
4852
4852
/** Common base class of TypeMap and TypeAccumulator */
4853
- abstract class VariantTraversal {
4853
+ abstract class VariantTraversal :
4854
4854
protected [core] var variance : Int = 1
4855
4855
4856
4856
inline protected def atVariance [T ](v : Int )(op : => T ): T = {
@@ -4860,13 +4860,22 @@ object Types {
4860
4860
variance = saved
4861
4861
res
4862
4862
}
4863
- }
4863
+
4864
+ protected def stopAtStatic : Boolean = true
4865
+
4866
+ /** Can the prefix of this static reference be omitted if the reference
4867
+ * itself can be omitted? Overridden in TypeOps#avoid.
4868
+ */
4869
+ protected def isStaticPrefix (pre : Type )(using Context ): Boolean = true
4870
+
4871
+ protected def stopBecauseStaticOrLocal (tp : NamedType )(using Context ): Boolean =
4872
+ (tp.prefix eq NoPrefix )
4873
+ || stopAtStatic && tp.currentSymbol.isStatic && isStaticPrefix(tp.prefix)
4874
+ end VariantTraversal
4864
4875
4865
4876
abstract class TypeMap (implicit protected var mapCtx : Context )
4866
4877
extends VariantTraversal with (Type => Type ) { thisMap =>
4867
4878
4868
- protected def stopAtStatic : Boolean = true
4869
-
4870
4879
def apply (tp : Type ): Type
4871
4880
4872
4881
protected def derivedSelect (tp : NamedType , pre : Type ): Type =
@@ -4912,8 +4921,8 @@ object Types {
4912
4921
implicit val ctx = this .mapCtx
4913
4922
tp match {
4914
4923
case tp : NamedType =>
4915
- if (stopAtStatic && tp.symbol.isStatic || (tp.prefix `eq` NoPrefix )) tp
4916
- else {
4924
+ if stopBecauseStaticOrLocal(tp) then tp
4925
+ else
4917
4926
val prefix1 = atVariance(variance max 0 )(this (tp.prefix))
4918
4927
// A prefix is never contravariant. Even if say `p.A` is used in a contravariant
4919
4928
// context, we cannot assume contravariance for `p` because `p`'s lower
@@ -4922,7 +4931,6 @@ object Types {
4922
4931
// if `p <: q` then `p.A <: q.A`, and well-formedness requires that `A` is a member
4923
4932
// of `p`'s upper bound.
4924
4933
derivedSelect(tp, prefix1)
4925
- }
4926
4934
case _ : ThisType
4927
4935
| _ : BoundType
4928
4936
| NoPrefix => tp
@@ -5077,7 +5085,6 @@ object Types {
5077
5085
}
5078
5086
5079
5087
@ sharable object IdentityTypeMap extends TypeMap ()(NoContext ) {
5080
- override def stopAtStatic : Boolean = true
5081
5088
def apply (tp : Type ): Type = tp
5082
5089
}
5083
5090
@@ -5334,8 +5341,6 @@ object Types {
5334
5341
abstract class TypeAccumulator [T ](implicit protected val accCtx : Context )
5335
5342
extends VariantTraversal with ((T , Type ) => T ) {
5336
5343
5337
- protected def stopAtStatic : Boolean = true
5338
-
5339
5344
def apply (x : T , tp : Type ): T
5340
5345
5341
5346
protected def applyToAnnot (x : T , annot : Annotation ): T = x // don't go into annotations
@@ -5348,11 +5353,10 @@ object Types {
5348
5353
record(s " foldOver total " )
5349
5354
tp match {
5350
5355
case tp : TypeRef =>
5351
- if (stopAtStatic && tp.symbol.isStatic || (tp.prefix `eq` NoPrefix )) x
5352
- else {
5356
+ if stopBecauseStaticOrLocal(tp) then x
5357
+ else
5353
5358
val tp1 = tp.prefix.lookupRefined(tp.name)
5354
5359
if (tp1.exists) this (x, tp1) else applyToPrefix(x, tp)
5355
- }
5356
5360
5357
5361
case tp @ AppliedType (tycon, args) =>
5358
5362
@ tailrec def foldArgs (x : T , tparams : List [ParamInfo ], args : List [Type ]): T =
@@ -5378,8 +5382,7 @@ object Types {
5378
5382
this (y, restpe)
5379
5383
5380
5384
case tp : TermRef =>
5381
- if (stopAtStatic && tp.currentSymbol.isStatic || (tp.prefix `eq` NoPrefix )) x
5382
- else applyToPrefix(x, tp)
5385
+ if stopBecauseStaticOrLocal(tp) then x else applyToPrefix(x, tp)
5383
5386
5384
5387
case tp : TypeVar =>
5385
5388
this (x, tp.underlying)
0 commit comments