Skip to content

Commit 41c4ae3

Browse files
committed
Reduce visibility of opaque aliases
We need to go through denot when going from a Symbol to a SymDenotation. Hence, the fact that Symbol will extend SymDenotations is not allowed to be known even in Symbols.
1 parent 552fffa commit 41c4ae3

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed

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

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,21 @@ import annotation.targetName
3838

3939
object Symbols:
4040

41-
opaque type Symbol >: Null <: SymbolDecl = SymbolImpl
42-
opaque type ClassSymbol >: Null <: Symbol & ClassSymbolDecl = ClassSymbolImpl
43-
opaque type PackageSymbol >: Null <: ClassSymbol = PackageClassSymbolImpl
41+
object Vault:
42+
43+
opaque type Symbol >: Null <: SymbolDecl = SymbolImpl
44+
opaque type ClassSymbol >: Null <: Symbol & ClassSymbolDecl = ClassSymbolImpl
45+
opaque type PackageSymbol >: Null <: ClassSymbol = PackageClassSymbolImpl
46+
47+
extension (sym: Symbol) def toSymbolImpl: SymbolImpl = sym
48+
extension (sym: SymbolImpl) def fromSymbolImpl: Symbol = sym
49+
50+
end Vault
51+
export Vault._
4452

4553
type TermSymbol = Symbol { type ThisName = TermName }
4654
type TypeSymbol = Symbol { type ThisName = TypeName }
4755

48-
extension (sym: Symbol) def toSymbolImpl: SymbolImpl = sym
49-
extension (sym: SymbolImpl) def fromSymbolImpl: Symbol = sym
50-
5156
implicit def eqSymbol: CanEqual[Symbol, Symbol] = CanEqual.derived
5257

5358
/** Tree attachment containing the identifiers in a tree as a sorted array */
@@ -300,7 +305,7 @@ object Symbols:
300305
asInstanceOf[TypeSymbol]
301306
}
302307

303-
final def isClass: Boolean = isInstanceOf[ClassSymbol]
308+
final def isClass: Boolean = isInstanceOf[ClassSymbol @unchecked]
304309

305310
final def asClass: ClassSymbol = asInstanceOf[ClassSymbol]
306311

@@ -332,7 +337,7 @@ object Symbols:
332337
/** This symbol entered into owner's scope (owner must be a class). */
333338
final def entered(using Context): this.type = {
334339
if (this.owner.isClass) {
335-
this.owner.asClass.enter(this)
340+
this.owner.asClass.enter(this.fromSymbolImpl)
336341
if (this.is(Module)) this.owner.asClass.enter(this.moduleClass)
337342
}
338343
this
@@ -360,7 +365,7 @@ object Symbols:
360365

361366
/** Remove symbol from scope of owning class */
362367
final def drop()(using Context): Unit = {
363-
this.owner.asClass.delete(this)
368+
this.owner.asClass.delete(this.fromSymbolImpl)
364369
if (this.is(Module)) this.owner.asClass.delete(this.moduleClass)
365370
}
366371

@@ -379,7 +384,7 @@ object Symbols:
379384
}
380385

381386
/** If this symbol satisfies predicate `p` this symbol, otherwise `NoSymbol` */
382-
def filter(p: Symbol => Boolean): Symbol = if (p(this)) this else NoSymbol
387+
def filter(p: Symbol => Boolean): Symbol = if (p(this.fromSymbolImpl)) this.fromSymbolImpl else NoSymbol
383388

384389
/** The current name of this symbol */
385390
final def name(using Context): ThisName = denot.name.asInstanceOf[ThisName]
@@ -433,9 +438,9 @@ object Symbols:
433438
*
434439
* @see enclosingSourceSymbols
435440
*/
436-
final def sourceSymbol(using Context): SymbolImpl =
441+
final def sourceSymbol(using Context): Symbol =
437442
if (!denot.exists)
438-
this
443+
this.fromSymbolImpl
439444
else if (denot.is(ModuleVal))
440445
this.moduleClass.sourceSymbol // The module val always has a zero-extent position
441446
else if (denot.is(Synthetic)) {
@@ -447,7 +452,7 @@ object Symbols:
447452
}
448453
else if (denot.isPrimaryConstructor)
449454
denot.owner.sourceSymbol
450-
else this
455+
else this.fromSymbolImpl
451456

452457
/** The position of this symbol, or NoSpan if the symbol was not loaded
453458
* from source or from TASTY. This is always a zero-extent position.
@@ -468,7 +473,7 @@ object Symbols:
468473
def isTypeParam(using Context): Boolean = denot.is(TypeParam)
469474
def paramName(using Context): ThisName = name.asInstanceOf[ThisName]
470475
def paramInfo(using Context): Type = denot.info
471-
def paramInfoAsSeenFrom(pre: Type)(using Context): Type = pre.memberInfo(this)
476+
def paramInfoAsSeenFrom(pre: Type)(using Context): Type = pre.memberInfo(this.fromSymbolImpl)
472477
def paramInfoOrCompleter(using Context): Type = denot.infoOrCompleter
473478
def paramVariance(using Context): Variance = denot.variance
474479
def paramRef(using Context): TypeRef = denot.typeRef
@@ -482,14 +487,14 @@ object Symbols:
482487
if (lastDenot == null) s"Naked$prefixString#$id"
483488
else lastDenot.toString// + "#" + id // !!! DEBUG
484489

485-
def toText(printer: Printer): Text = printer.toText(this)
490+
def toText(printer: Printer): Text = printer.toText(this.fromSymbolImpl)
486491

487-
def showLocated(using Context): String = ctx.printer.locatedText(this).show
488-
def showExtendedLocation(using Context): String = ctx.printer.extendedLocationText(this).show
489-
def showDcl(using Context): String = ctx.printer.dclText(this).show
490-
def showKind(using Context): String = ctx.printer.kindString(this)
491-
def showName(using Context): String = ctx.printer.nameString(this)
492-
def showFullName(using Context): String = ctx.printer.fullNameString(this)
492+
def showLocated(using Context): String = ctx.printer.locatedText(this.fromSymbolImpl).show
493+
def showExtendedLocation(using Context): String = ctx.printer.extendedLocationText(this.fromSymbolImpl).show
494+
def showDcl(using Context): String = ctx.printer.dclText(this.fromSymbolImpl).show
495+
def showKind(using Context): String = ctx.printer.kindString(this.fromSymbolImpl)
496+
def showName(using Context): String = ctx.printer.nameString(this.fromSymbolImpl)
497+
def showFullName(using Context): String = ctx.printer.fullNameString(this.fromSymbolImpl)
493498

494499
override def hashCode(): Int = id // for debugging.
495500
}
@@ -567,7 +572,7 @@ object Symbols:
567572
if file != null && file.extension != "class" then
568573
mySource = ctx.getSource(file)
569574
else
570-
mySource = defn.patchSource(this)
575+
mySource = defn.patchSource(this.fromSymbolImpl)
571576
if !mySource.exists then
572577
mySource = atPhaseNoLater(flattenPhase) {
573578
denot.topLevelClass.unforcedAnnotation(defn.SourceFileAnnot) match
@@ -592,7 +597,7 @@ object Symbols:
592597
val NoSymbol: Symbol = new SymbolImpl(NoCoord, 0) {
593598
override def associatedFile(using Context): AbstractFile = NoSource.file
594599
override def recomputeDenot(lastd: SymDenotation)(using Context): SymDenotation = NoDenotation
595-
}
600+
}.fromSymbolImpl
596601
NoDenotation // force it in order to set `denot` field of NoSymbol
597602

598603
extension [N <: Name](sym: Symbol { type ThisName = N })(using Context) {
@@ -621,9 +626,11 @@ object Symbols:
621626

622627
/** Makes all denotation operations available on symbols */
623628
implicit def toDenot(sym: Symbol)(using Context): SymDenotation = sym.denot
629+
implicit def toDenot2(sym: SymbolImpl)(using Context): SymDenotation = sym.denot
624630

625631
/** Makes all class denotation operations available on class symbols */
626632
implicit def toClassDenot(cls: ClassSymbol)(using Context): ClassDenotation = cls.classDenot
633+
implicit def toClassDenot2(cls: ClassSymbolImpl)(using Context): ClassDenotation = cls.classDenot
627634

628635
/** The Definitions object */
629636
def defn(using Context): Definitions = ctx.definitions
@@ -665,11 +672,12 @@ object Symbols:
665672
coord: Coord = NoCoord,
666673
assocFile: AbstractFile = null)(using Context): ClassSymbol
667674
= {
668-
val cls =
675+
val cls = (
669676
if flags.is(Package) then
670677
new PackageClassSymbolImpl(ctx.base.nextSymId)
671678
else
672679
new ClassSymbolImpl(coord, assocFile, ctx.base.nextSymId)
680+
).fromSymbolImpl.asClass
673681
val denot = SymDenotation(cls, owner, name, flags, infoFn(cls), privateWithin)
674682
cls.denot = denot
675683
cls

0 commit comments

Comments
 (0)