Skip to content

Commit d4dc78c

Browse files
committed
New method on types: givenSelfType
The self type as given (or implied for module classes) in the source. Also defined and updated for normal types, not just ClassInfo types. Common functionality between this and baseTypeWithArgs has been pulled into RefinedType#wrapIfMember.
1 parent dcfebd8 commit d4dc78c

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,7 @@ class TypeApplications(val self: Type) extends AnyVal {
268268
case _ => default
269269
}
270270
case tp @ RefinedType(parent, name) if !tp.member(name).symbol.is(ExpandedTypeParam) =>
271-
val pbase = parent.baseTypeWithArgs(base)
272-
if (pbase.member(name).exists) RefinedType(pbase, name, tp.refinedInfo)
273-
else pbase
271+
tp.wrapIfMember(parent.baseTypeWithArgs(base))
274272
case tp: TermRef =>
275273
tp.underlying.baseTypeWithArgs(base)
276274
case AndType(tp1, tp2) =>
@@ -281,7 +279,7 @@ class TypeApplications(val self: Type) extends AnyVal {
281279
default
282280
}
283281
}
284-
282+
285283
/** Translate a type of the form From[T] to To[T], keep other types as they are.
286284
* `from` and `to` must be static classes, both with one type parameter, and the same variance.
287285
*/

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

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,13 @@ object Types {
858858
case _ => defn.AnyClass.typeRef
859859
}
860860

861+
/** the self type of the underlying classtype */
862+
def givenSelfType(implicit ctx: Context): Type = this match {
863+
case tp @ RefinedType(parent, name) => tp.wrapIfMember(parent.givenSelfType)
864+
case tp: TypeProxy => tp.underlying.givenSelfType
865+
case _ => NoType
866+
}
867+
861868
/** The parameter types of a PolyType or MethodType, Empty list for others */
862869
final def paramTypess(implicit ctx: Context): List[List[Type]] = this match {
863870
case mt: MethodType => mt.paramTypes :: mt.resultType.paramTypess
@@ -1781,7 +1788,12 @@ object Types {
17811788
if (false) RefinedType(parent, refinedName, refinedInfo)
17821789
else RefinedType(parent, refinedName, rt => refinedInfo.substSkolem(this, SkolemType(rt)))
17831790
}
1784-
1791+
1792+
/** Add this refinement to `parent`, provided If `refinedName` is a member of `parent`. */
1793+
def wrapIfMember(parent: Type)(implicit ctx: Context): Type =
1794+
if (parent.member(refinedName).exists) derivedRefinedType(parent, refinedName, refinedInfo)
1795+
else parent
1796+
17851797
override def equals(that: Any) = that match {
17861798
case that: RefinedType =>
17871799
this.parent == that.parent &&
@@ -2398,22 +2410,27 @@ object Types {
23982410
* - the fully applied reference to the class itself.
23992411
*/
24002412
def selfType(implicit ctx: Context): Type = {
2401-
if (selfTypeCache == null) {
2402-
def fullRef = fullyAppliedRef(cls.typeRef, cls.typeParams)
2403-
def withFullRef(tp: Type): Type =
2404-
if (ctx.erasedTypes) fullRef else AndType(tp, fullRef)
2405-
selfTypeCache = selfInfo match {
2406-
case NoType =>
2407-
fullRef
2408-
case tp: Type =>
2409-
if (cls is Module) tp else withFullRef(tp)
2410-
case self: Symbol =>
2411-
assert(!(cls is Module))
2412-
withFullRef(self.info)
2413+
if (selfTypeCache == null)
2414+
selfTypeCache = {
2415+
def fullRef = fullyAppliedRef(cls.typeRef, cls.typeParams)
2416+
val given = givenSelfType
2417+
val raw =
2418+
if (!given.exists) fullRef
2419+
else if (cls is Module) given
2420+
else if (ctx.erasedTypes) fullRef
2421+
else AndType(given, fullRef)
2422+
raw//.asSeenFrom(prefix, cls.owner)
24132423
}
2414-
}
24152424
selfTypeCache
24162425
}
2426+
2427+
/** The explicitly given self type (self types of modules are assumed to be
2428+
* explcitly given here).
2429+
*/
2430+
override def givenSelfType(implicit ctx: Context): Type = selfInfo match {
2431+
case tp: Type => tp
2432+
case self: Symbol => self.info
2433+
}
24172434

24182435
private var selfTypeCache: Type = null
24192436

0 commit comments

Comments
 (0)