Skip to content

Commit 7f5e8a4

Browse files
committed
Simplify companion scheme
Use a field in ClassDenotation instead of an annotation
1 parent e09bb5d commit 7f5e8a4

File tree

9 files changed

+31
-49
lines changed

9 files changed

+31
-49
lines changed

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

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -173,21 +173,6 @@ object Annotations {
173173
else None
174174
}
175175

176-
/** A generic extractor for annotations carrying types */
177-
class TypeHintExtractor(cls: Context => ClassSymbol) {
178-
def apply(tp: Type)(implicit ctx: Context): Annotation =
179-
Annotation(TypeTree(cls(ctx).typeRef.appliedTo(tp)))
180-
def unapply(ann: Annotation)(implicit ctx: Context): Option[Type] =
181-
if (ann.symbol == cls(ctx)) {
182-
val AppliedType(tycon, arg :: Nil) = ann.tree.tpe
183-
Some(arg)
184-
}
185-
else None
186-
}
187-
188-
/** Extractor for linked type annotations */
189-
object LinkedType extends TypeHintExtractor(implicit ctx => defn.LinkedTypeAnnot)
190-
191176
def makeSourceFile(path: String)(implicit ctx: Context) =
192177
apply(defn.SourceFileAnnot, Literal(Constant(path)))
193178
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,6 @@ class Definitions {
530530
lazy val EqualsPatternClass = enterSpecialPolyClass(tpnme.EQUALS_PATTERN, EmptyFlags, Seq(AnyType))
531531

532532
lazy val RepeatedParamClass = enterSpecialPolyClass(tpnme.REPEATED_PARAM_CLASS, Covariant, Seq(ObjectType, SeqType))
533-
lazy val LinkedTypeAnnot = enterSpecialPolyClass(tpnme.LINKED_TYPE, EmptyFlags, Seq(AnyType))
534533
lazy val OpaqueAliasAnnot = enterSpecialPolyClass(tpnme.OPAQUE_ALIAS, EmptyFlags, Seq(AnyType))
535534

536535
// fundamental classes
@@ -1159,7 +1158,6 @@ class Definitions {
11591158
AnyRefAlias,
11601159
RepeatedParamClass,
11611160
ByNameParamClass2x,
1162-
LinkedTypeAnnot,
11631161
OpaqueAliasAnnot,
11641162
AnyValClass,
11651163
NullClass,

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ object Flags {
257257
/** An opqaue type */
258258
final val Opaque = typeFlag(12, "opaque")
259259

260+
final val MutableOrOpaque = Mutable.toCommonFlags
261+
260262
/** Symbol is local to current class (i.e. private[this] or protected[this]
261263
* pre: Private or Protected are also set
262264
*/
@@ -460,7 +462,7 @@ object Flags {
460462
/** Flags guaranteed to be set upon symbol creation */
461463
final val FromStartFlags =
462464
Module | Package | Deferred | MethodOrHKCommon | Param | ParamAccessor.toCommonFlags |
463-
Scala2ExistentialCommon | Mutable.toCommonFlags | Touched | JavaStatic |
465+
Scala2ExistentialCommon | MutableOrOpaque | Touched | JavaStatic |
464466
CovariantOrOuter | ContravariantOrLabel | CaseAccessor.toCommonFlags |
465467
NonMember | Erroneous | ImplicitCommon | Permanent | Synthetic |
466468
SuperAccessorOrScala2x | Inline

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

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -960,11 +960,12 @@ object SymDenotations {
960960
final def enclosingPackageClass(implicit ctx: Context): Symbol =
961961
if (this is PackageClass) symbol else owner.enclosingPackageClass
962962

963-
/** Register target as a companion */
964-
def registerCompanion(target: Symbol)(implicit ctx: Context) =
965-
if (exists && target.exists && !unforcedIsAbsent && !target.unforcedIsAbsent &&
966-
!myAnnotations.exists(_.symbol == defn.LinkedTypeAnnot))
967-
addAnnotation(Annotation.LinkedType(target.typeRef))
963+
/** Register target as a companion; overridden in ClassDenotation */
964+
def registerCompanion(target: Symbol)(implicit ctx: Context) = ()
965+
966+
/** The registered companion; overridden in ClassDenotation */
967+
def registeredCompanion(implicit ctx: Context): Symbol = NoSymbol
968+
def registeredCompanion_=(c: Symbol): Unit = ()
968969

969970
/** The module object with the same (term-) name as this class or module class,
970971
* and which is also defined in the same scope and compilation unit.
@@ -982,20 +983,12 @@ object SymDenotations {
982983
}
983984
case None => NoSymbol
984985
}
985-
else
986-
getAnnotation(defn.LinkedTypeAnnot) match {
987-
case Some(Annotation.LinkedType(linked: TypeRef)) =>
988-
linked.symbol.sourceModule
989-
case _ => NoSymbol
990-
}
986+
else registeredCompanion.sourceModule
991987

992988
private def companionType(implicit ctx: Context): Symbol =
993989
if (is(Package)) NoSymbol
994990
else if (is(ModuleVal)) moduleClass.denot.companionType
995-
else getAnnotation(defn.LinkedTypeAnnot) match {
996-
case Some(Annotation.LinkedType(linked: TypeRef)) => linked.symbol
997-
case _ => NoSymbol
998-
}
991+
else registeredCompanion
999992

1000993
/** The class with the same (type-) name as this module or module class,
1001994
* and which is also defined in the same scope and compilation unit.
@@ -1287,6 +1280,7 @@ object SymDenotations {
12871280
val annotations1 = if (annotations != null) annotations else this.annotations
12881281
val d = ctx.SymDenotation(symbol, owner, name, initFlags1, info1, privateWithin1)
12891282
d.annotations = annotations1
1283+
d.registeredCompanion = registeredCompanion
12901284
d
12911285
}
12921286

@@ -1853,6 +1847,16 @@ object SymDenotations {
18531847
.copyCaches(this, phase.next)
18541848
.installAfter(phase)
18551849
}
1850+
1851+
private[this] var myCompanion: Symbol = NoSymbol
1852+
1853+
/** Register companion class */
1854+
override def registerCompanion(companion: Symbol)(implicit ctx: Context) =
1855+
if (companion.canHaveCompanion && !unforcedIsAbsent && !companion.unforcedIsAbsent)
1856+
myCompanion = companion
1857+
1858+
override def registeredCompanion(implicit ctx: Context) = { ensureCompleted(); myCompanion }
1859+
override def registeredCompanion_=(c: Symbol) = { myCompanion = c }
18561860
}
18571861

18581862
/** The denotation of a package class.

compiler/src/dotty/tools/dotc/core/tasty/TreePickler.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ class TreePickler(pickler: TastyPickler) {
619619
// See tests/pickling/i3149.scala
620620
case _ =>
621621
val sym = ann.symbol
622-
sym == defn.BodyAnnot || sym == defn.OpaqueAliasAnnot || sym == defn.LinkedTypeAnnot
622+
sym == defn.BodyAnnot || sym == defn.OpaqueAliasAnnot
623623
// these are reconstituted automatically when unpickling
624624
}
625625

compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ class TreeUnpickler(reader: TastyReader,
742742
// be missing from the Tasty file. So we check explicitly for that.
743743
def isCodefined = roots.contains(companion.denot) == seenRoots.contains(companion)
744744

745-
if (companion.canHaveCompanion && isCodefined) sym.registerCompanion(companion)
745+
if (companion.exists && isCodefined) sym.registerCompanion(companion)
746746
TypeDef(readTemplate(localCtx))
747747
} else {
748748
sym.info = TypeBounds.empty // needed to avoid cyclic references when unpicklin rhs, see i3816.scala

compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,13 @@ object Scala2Unpickler {
121121

122122
def registerCompanionPair(module: Symbol, claz: Symbol) = {
123123
module.registerCompanion(claz)
124-
if (claz.isClass) claz.registerCompanion(module)
124+
claz.registerCompanion(module)
125125
}
126126

127-
if (denot.flagsUNSAFE is Module) {
127+
if (denot.flagsUNSAFE is Module)
128128
registerCompanionPair(denot.classSymbol, scalacCompanion)
129-
} else {
129+
else
130130
registerCompanionPair(scalacCompanion, denot.classSymbol)
131-
}
132131

133132
tempInfo.finalize(denot, normalizedParents) // install final info, except possibly for typeparams ordering
134133
denot.ensureTypeParamsInCorrectOrder()

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -694,11 +694,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
694694
Text(annotations.map(annotText), " ") ~~ flagsText ~~ (Str(kw) provided !suppressKw)
695695
}
696696

697-
protected def filterModTextAnnots(annots: List[untpd.Tree]): List[untpd.Tree] =
698-
annots filter {
699-
case tpt: TypeTree[_] => tpt.typeOpt.typeSymbol != defn.LinkedTypeAnnot
700-
case _ => true
701-
}
697+
protected def filterModTextAnnots(annots: List[untpd.Tree]): List[untpd.Tree] = annots
702698

703699
def optText(name: Name)(encl: Text => Text): Text =
704700
if (name.isEmpty) "" else encl(toText(name))

compiler/src/dotty/tools/dotc/typer/Namer.scala

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -620,11 +620,9 @@ class Namer { typer: Typer =>
620620
def createLinks(classTree: TypeDef, moduleTree: TypeDef)(implicit ctx: Context) = {
621621
val claz = ctx.effectiveScope.lookup(classTree.name)
622622
val modl = ctx.effectiveScope.lookup(moduleTree.name)
623-
if (modl.isClass && claz.canHaveCompanion) {
624-
modl.registerCompanion(claz)
625-
claz.registerCompanion(modl)
626-
}
627-
}
623+
modl.registerCompanion(claz)
624+
claz.registerCompanion(modl)
625+
}
628626

629627
def createCompanionLinks(implicit ctx: Context): Unit = {
630628
val classDef = mutable.Map[TypeName, TypeDef]()

0 commit comments

Comments
 (0)