Skip to content

Commit e499246

Browse files
committed
Turn Symbol and ClassSymbol into opaque types
1 parent b53346d commit e499246

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ import annotation.targetName
3838

3939
object Symbols {
4040

41+
opaque type Symbol >: Null <: SymbolDecl = SymbolImpl
42+
opaque type ClassSymbol >: Null <: Symbol & ClassSymbolDecl = ClassSymbolImpl
43+
44+
type TermSymbol = Symbol { type ThisName = TermName }
45+
type TypeSymbol = Symbol { type ThisName = TypeName }
46+
4147
implicit def eqSymbol: CanEqual[Symbol, Symbol] = CanEqual.derived
4248

4349
/** Tree attachment containing the identifiers in a tree as a sorted array */
@@ -100,6 +106,7 @@ object Symbols {
100106

101107
def span: Span
102108
def sourcePos(using Context): SourcePosition
109+
def srcPos: SrcPos
103110

104111
def associatedFile(using Context): AbstractFile
105112
def binaryFile(using Context): AbstractFile
@@ -113,7 +120,10 @@ object Symbols {
113120
def drop()(using Context): Unit
114121
def dropAfter(phase: DenotTransformer)(using Context): Unit
115122

116-
inline def orElse(inline that: Symbol)(using Context): Symbol
123+
/** This symbol, if it exists, otherwise the result of evaluating `that` */
124+
inline def orElse(inline that: Symbol)(using Context): Symbol =
125+
if denot.exists then this.asInstanceOf[Symbol] else that
126+
117127
def filter(p: Symbol => Boolean): Symbol
118128

119129
// ParamInfo types and methods
@@ -155,7 +165,7 @@ object Symbols {
155165
* @param coord The coordinates of the symbol (a position or an index)
156166
* @param id A unique identifier of the symbol (unique per ContextBase)
157167
*/
158-
class Symbol private[Symbols] (private var myCoord: Coord, val id: Int)
168+
class SymbolImpl private[Symbols] (private var myCoord: Coord, val id: Int)
159169
extends SymbolDecl {
160170

161171
type ThisName <: Name
@@ -354,10 +364,6 @@ object Symbols {
354364
drop()
355365
}
356366

357-
/** This symbol, if it exists, otherwise the result of evaluating `that` */
358-
inline def orElse(inline that: Symbol)(using Context): Symbol =
359-
if (this.exists) this else that
360-
361367
/** If this symbol satisfies predicate `p` this symbol, otherwise `NoSymbol` */
362368
def filter(p: Symbol => Boolean): Symbol = if (p(this)) this else NoSymbol
363369

@@ -413,7 +419,7 @@ object Symbols {
413419
*
414420
* @see enclosingSourceSymbols
415421
*/
416-
@annotation.tailrec final def sourceSymbol(using Context): Symbol =
422+
final def sourceSymbol(using Context): Symbol =
417423
if (!denot.exists)
418424
this
419425
else if (denot.is(ModuleVal))
@@ -474,11 +480,8 @@ object Symbols {
474480
override def hashCode(): Int = id // for debugging.
475481
}
476482

477-
type TermSymbol = Symbol { type ThisName = TermName }
478-
type TypeSymbol = Symbol { type ThisName = TypeName }
479-
480-
class ClassSymbol private[Symbols] (coord: Coord, val assocFile: AbstractFile, id: Int)
481-
extends Symbol(coord, id), ClassSymbolDecl {
483+
class ClassSymbolImpl private[Symbols] (coord: Coord, val assocFile: AbstractFile, id: Int)
484+
extends SymbolImpl(coord, id), ClassSymbolDecl {
482485

483486
type ThisName = TypeName
484487

@@ -569,7 +572,7 @@ object Symbols {
569572
}
570573

571574
@sharable
572-
val NoSymbol: Symbol = new Symbol(NoCoord, 0) {
575+
val NoSymbol: Symbol = new SymbolImpl(NoCoord, 0) {
573576
override def associatedFile(using Context): AbstractFile = NoSource.file
574577
override def recomputeDenot(lastd: SymDenotation)(using Context): SymDenotation = NoDenotation
575578
}

compiler/src/dotty/tools/dotc/transform/LazyVals.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ class LazyVals extends MiniPhase with IdentityDenotTransformer {
380380
val thizClass = Literal(Constant(claz.info))
381381
val helperModule = requiredModule("scala.runtime.LazyVals")
382382
val getOffset = Select(ref(helperModule), lazyNme.RLazyVals.getOffset)
383-
var offsetSymbol: TermSymbol = null
383+
var offsetSymbol: Symbol = null
384384
var flag: Tree = EmptyTree
385385
var ord = 0
386386

@@ -404,7 +404,7 @@ class LazyVals extends MiniPhase with IdentityDenotTransformer {
404404
val flagName = LazyBitMapName.fresh(id.toString.toTermName)
405405
val flagSymbol = newSymbol(claz, flagName, containerFlags, defn.LongType).enteredAfter(this)
406406
flag = ValDef(flagSymbol, Literal(Constant(0L)))
407-
val offsetTree = ValDef(offsetSymbol, getOffset.appliedTo(thizClass, Literal(Constant(flagName.toString))))
407+
val offsetTree = ValDef(offsetSymbol.asTerm, getOffset.appliedTo(thizClass, Literal(Constant(flagName.toString))))
408408
info.defs = offsetTree :: info.defs
409409
}
410410

@@ -414,7 +414,7 @@ class LazyVals extends MiniPhase with IdentityDenotTransformer {
414414
val flagName = LazyBitMapName.fresh("0".toTermName)
415415
val flagSymbol = newSymbol(claz, flagName, containerFlags, defn.LongType).enteredAfter(this)
416416
flag = ValDef(flagSymbol, Literal(Constant(0L)))
417-
val offsetTree = ValDef(offsetSymbol, getOffset.appliedTo(thizClass, Literal(Constant(flagName.toString))))
417+
val offsetTree = ValDef(offsetSymbol.asTerm, getOffset.appliedTo(thizClass, Literal(Constant(flagName.toString))))
418418
appendOffsetDefs += (claz -> new OffsetInfo(List(offsetTree), ord))
419419
}
420420

0 commit comments

Comments
 (0)