@@ -372,18 +372,38 @@ object SymDenotations {
372372 case _ => unforcedDecls.openForMutations
373373 }
374374
375- /** If this is a synthetic opaque type alias, mark it as Deferred with empty bounds
375+ /** If this is a synthetic opaque type alias, mark it as Deferred with bounds
376+ * as given by the right hand side's `WithBounds` annotation, if one is present,
377+ * or with empty bounds of the right kind, otherwise.
378+ * At the same time, integrate the original alias as a refinement of the
379+ * self type of the enclosing class.
376380 */
377381 final def normalizeOpaque ()(implicit ctx : Context ) = {
378382 def abstractRHS (tp : Type ): Type = tp match {
379383 case tp : HKTypeLambda => tp.derivedLambdaType(resType = abstractRHS(tp.resType))
380384 case _ => defn.AnyType
381385 }
382- if (isOpaqueHelper ) {
386+ if (isOpaqueAlias ) {
383387 info match {
384388 case TypeAlias (alias) =>
385- info = TypeBounds (defn.NothingType , abstractRHS(alias))
389+ val (refiningAlias, bounds) = alias match {
390+ case AnnotatedType (alias1, Annotation .WithBounds (bounds)) =>
391+ (alias1, bounds)
392+ case _ =>
393+ (alias, TypeBounds (defn.NothingType , abstractRHS(alias)))
394+ }
395+ def refineSelfType (selfType : Type ) =
396+ RefinedType (selfType, name, TypeAlias (refiningAlias))
397+ val enclClassInfo = owner.asClass.classInfo
398+ enclClassInfo.selfInfo match {
399+ case self : Type =>
400+ owner.info = enclClassInfo.derivedClassInfo(selfInfo = refineSelfType(self))
401+ case self : Symbol =>
402+ self.info = refineSelfType(self.info)
403+ }
404+ info = bounds
386405 setFlag(Deferred )
406+ typeRef.recomputeDenot()
387407 case _ =>
388408 }
389409 }
@@ -553,18 +573,14 @@ object SymDenotations {
553573 final def isAbstractOrParamType (implicit ctx : Context ): Boolean = this is DeferredOrTypeParam
554574
555575 /** Is this symbol a user-defined opaque alias type? */
556- def isOpaqueAlias (implicit ctx : Context ): Boolean = is(Opaque , butNot = Synthetic )
557-
558- /** Is this symbol the companion of an opaque alias type? */
559- def isOpaqueCompanion (implicit ctx : Context ): Boolean = is(OpaqueModule )
576+ def isOpaqueAlias (implicit ctx : Context ): Boolean = is(Opaque ) && ! isClass
560577
561- /** Is this symbol a synthetic opaque type inside an opaque companion object ? */
562- def isOpaqueHelper (implicit ctx : Context ): Boolean = is(SyntheticOpaque , butNot = Module )
578+ /** Is this symbol a module that contains opaque aliases ? */
579+ def containsOpaques (implicit ctx : Context ): Boolean = is(Opaque ) && isClass
563580
564- /** Can this symbol have a companion module?
565- * This is the case if it is a class or an opaque type alias.
566- */
567- final def canHaveCompanion (implicit ctx : Context ) = isClass || isOpaqueAlias
581+ def seesOpaques (implicit ctx : Context ): Boolean =
582+ containsOpaques ||
583+ is(Module , butNot = Package ) && owner.containsOpaques
568584
569585 /** Is this the denotation of a self symbol of some class?
570586 * This is the case if one of two conditions holds:
@@ -789,7 +805,7 @@ object SymDenotations {
789805 */
790806 def membersNeedAsSeenFrom (pre : Type )(implicit ctx : Context ): Boolean =
791807 ! ( this .isTerm
792- || this .isStaticOwner && ! this .isOpaqueCompanion
808+ || this .isStaticOwner && ! this .seesOpaques
793809 || ctx.erasedTypes
794810 || (pre eq NoPrefix )
795811 || (pre eq thisType)
@@ -1029,16 +1045,6 @@ object SymDenotations {
10291045 */
10301046 final def companionModule (implicit ctx : Context ): Symbol =
10311047 if (is(Module )) sourceModule
1032- else if (isOpaqueAlias) {
1033- def reference (tp : Type ): Symbol = tp match {
1034- case TypeRef (prefix : TermRef , _) => prefix.termSymbol
1035- case tp : HKTypeLambda => reference(tp.resType)
1036- case tp : AppliedType => reference(tp.tycon)
1037- case tp : ErrorType => registeredCompanion.sourceModule
1038- }
1039- val TypeAlias (alias) = info
1040- reference(alias)
1041- }
10421048 else registeredCompanion.sourceModule
10431049
10441050 private def companionType (implicit ctx : Context ): Symbol =
@@ -1053,13 +1059,6 @@ object SymDenotations {
10531059 final def companionClass (implicit ctx : Context ): Symbol =
10541060 companionType.suchThat(_.isClass).symbol
10551061
1056- /** The opaque type with the same (type-) name as this module or module class,
1057- * and which is also defined in the same scope and compilation unit.
1058- * NoSymbol if this type does not exist.
1059- */
1060- final def companionOpaqueType (implicit ctx : Context ): Symbol =
1061- companionType.suchThat(_.isOpaqueAlias).symbol
1062-
10631062 final def scalacLinkedClass (implicit ctx : Context ): Symbol =
10641063 if (this is ModuleClass ) companionNamed(effectiveName.toTypeName)
10651064 else if (this .isClass) companionNamed(effectiveName.moduleClassName).sourceModule.moduleClass
@@ -1109,15 +1108,17 @@ object SymDenotations {
11091108 final def enclosingSubClass (implicit ctx : Context ): Symbol =
11101109 ctx.owner.ownersIterator.findSymbol(_.isSubClass(symbol))
11111110
1112- /** The alias of a synthetic opaque type that's stored in the self type of the
1111+ /** The alias of an opaque type alias that's stored in the self type of the
11131112 * containing object.
11141113 */
11151114 def opaqueAlias (implicit ctx : Context ): Type = {
1116- if (isOpaqueHelper)
1117- owner.asClass.classInfo.selfType match {
1118- case RefinedType (_, _, bounds) => bounds.extractOpaqueAlias
1119- }
1120- else NoType
1115+ def recur (tp : Type ): Type = tp match {
1116+ case RefinedType (parent, rname, TypeAlias (alias)) =>
1117+ if (rname == name) alias else recur(parent)
1118+ case _ =>
1119+ NoType
1120+ }
1121+ recur(owner.asClass.classInfo.selfType)
11211122 }
11221123
11231124 /** The non-private symbol whose name and type matches the type of this symbol
@@ -1974,7 +1975,7 @@ object SymDenotations {
19741975
19751976 /** Register companion class */
19761977 override def registerCompanion (companion : Symbol )(implicit ctx : Context ) =
1977- if (companion.canHaveCompanion && ! unforcedIsAbsent && ! companion.unforcedIsAbsent)
1978+ if (companion.isClass && ! unforcedIsAbsent && ! companion.unforcedIsAbsent)
19781979 myCompanion = companion
19791980
19801981 override def registeredCompanion (implicit ctx : Context ) = { ensureCompleted(); myCompanion }
0 commit comments