@@ -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