Skip to content

Commit 7116033

Browse files
committed
Make Symbol and ClassSymbol opaque type aliases
1 parent c819cb1 commit 7116033

File tree

5 files changed

+36
-53
lines changed

5 files changed

+36
-53
lines changed

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,8 @@ object Denotations {
235235
private var myValidFor: Period = Nowhere
236236

237237
final def validFor: Period = myValidFor
238-
final def validFor_=(p: Period): Unit = {
238+
final def validFor_=(p: Period): Unit =
239239
myValidFor = p
240-
if symbol.initialDenot ne null then
241-
symbol.initialDenot.checkedPeriod = Nowhere
242-
}
243240

244241
/** Is this denotation different from NoDenotation or an ErrorDenotation? */
245242
def exists: Boolean = true

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ object Periods {
6363
//
6464
// Let's compute:
6565
//
66-
// lastDiff = X * 2^5 + (l1 - l2) mod 2^5
66+
// lastDiff = X * 2^7 + (l1 - l2) mod 2^7
6767
// where X >= 0, X == 0 iff r1 == r2 & l1 - l2 >= 0
6868
// result = lastDiff + d2 <= d1
6969
// We have:
@@ -103,6 +103,13 @@ object Periods {
103103
def !=(that: Period): Boolean = this.code != that.code
104104
}
105105

106+
/** Same as new Period(p1).contains(new Period(p2)), assuming that p2 is the
107+
* code of a single-phase period.
108+
*/
109+
extension (p1: Int)
110+
transparent inline def containsSinglePhasePeriod(p2: Int): Boolean =
111+
((p1 - p2) >>> PhaseWidth) <= (p1 & PhaseMask)
112+
106113
object Period {
107114

108115
/** The single-phase period consisting of given run id and phase id */

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ object SymDenotations {
8080
initPrivateWithin: Symbol = NoSymbol)
8181
extends SingleDenotation(symbolHint, initInfo, name.isTypeName), ParamInfo, SrcPos, Named {
8282

83-
//assert(symbol.id != 4940, name)
83+
//assert(common.id != 9141, name)
8484

8585
override def hasUniqueSym: Boolean = exists
8686

@@ -1810,7 +1810,6 @@ object SymDenotations {
18101810

18111811
/** The last denotation of this symbol */
18121812
var lastDenot: SymDenotation = _
1813-
var checkedPeriod: Period = Nowhere
18141813

18151814
/** Overridden in NoSymbol */
18161815
//private[SymDenotations]
@@ -2699,7 +2698,6 @@ object SymDenotations {
26992698
override def filterWithFlags(required: FlagSet, excluded: FlagSet)(using Context): SingleDenotation = this
27002699
override def associatedFile(using Context): AbstractFile | Null = NoSource.file
27012700

2702-
NoSymbol.initialDenot = this
27032701
NoSymbol.denot_=(this)
27042702
validFor = Period.allInRun(NoRunId)
27052703
}

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

Lines changed: 25 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -37,34 +37,32 @@ import scala.reflect.TypeTest
3737

3838
object Symbols {
3939

40-
implicit def eqSymbol: CanEqual[Symbol, Symbol] = CanEqual.derived
41-
4240
/** Tree attachment containing the identifiers in a tree as a sorted array */
4341
val Ids: Property.Key[Array[String]] = new Property.Key
4442

45-
opaque type Symbl <: ParamInfo & SrcPos & Named & printing.Showable
43+
opaque type Symbol <: ParamInfo & SrcPos & Named & printing.Showable
4644
= SymDenotation
47-
48-
opaque type ClassSymbl <: Symbl
45+
opaque type ClassSymbol <: TypeSymbol
4946
= ClassDenotation
5047

5148
object TypeTests:
5249

53-
given SymTest: TypeTest[AnyRef, Symbol] with
54-
def unapply(x: AnyRef): Option[x.type & Symbol] = x match
55-
case sd: SymDenotation => Some(sd.asInstanceOf)
50+
given SymTest: TypeTest[Any, Symbol] with
51+
def unapply(x: Any): Option[x.type & Symbol] = x match
52+
case sd: SymDenotation => Some(sd.asInstanceOf[x.type & Symbol])
5653
case _ => None
5754

58-
given ClsTest: TypeTest[AnyRef, ClassSymbol] with
59-
def unapply(x: AnyRef): Option[x.type & ClassSymbol] = x match
60-
case cd: ClassDenotation => Some(cd.asInstanceOf)
55+
given ClsTest: TypeTest[Any, ClassSymbol] with
56+
def unapply(x: Any): Option[x.type & ClassSymbol] = x match
57+
case cd: ClassDenotation => Some(cd.asInstanceOf[x.type & ClassSymbol])
6158
case _ => None
6259
end TypeTests
6360

61+
implicit def eqSymbol: CanEqual[Symbol, Symbol] = CanEqual.derived
62+
6463
/** A Symbol represents a Scala definition/declaration or a package.
6564
* @param coord The coordinates of the symbol (a position or an index)
6665
* @param id A unique identifier of the symbol (unique per ContextBase)
67-
*/
6866
class Symbol private[Symbols] ()
6967
extends ParamInfo, SrcPos, Named, printing.Showable {
7068
@@ -95,6 +93,7 @@ object Symbols {
9593
9694
override def hashCode(): Int = Symbols.id(this) // for debugging.
9795
}
96+
*/
9897

9998
type TermSymbol = Symbol { type ThisName = TermName }
10099
type TypeSymbol = Symbol { type ThisName = TypeName }
@@ -104,7 +103,7 @@ object Symbols {
104103
inline def asSymbol: Symbol = x.asInstanceOf[Symbol]
105104

106105
extension (_self: Symbol)
107-
def self = _self.initialDenot
106+
def self: SymDenotation = _self
108107

109108
private def lastDenot: SymDenotation = self.lastDenot
110109
private def lastDenot_=(d: SymDenotation): Unit = self.lastDenot = d
@@ -154,20 +153,14 @@ object Symbols {
154153
private[core] def denot_=(d: SymDenotation): Unit =
155154
util.Stats.record("Symbol.denot_=")
156155
lastDenot = d
157-
self.checkedPeriod = Nowhere
158156

159157
/** The current denotation of this symbol */
160158
def denot(using Context): SymDenotation =
161159
util.Stats.record("Symbol.denot")
162160
val lastd = lastDenot
163-
if self.checkedPeriod.code == ctx.period.code then lastd
164-
else computeDenot(lastd)
165-
166-
def computeDenot(lastd: SymDenotation)(using Context): SymDenotation =
167-
util.Stats.record("Symbol.computeDenot")
168-
val now = ctx.period
169-
self.checkedPeriod = now
170-
if lastd.validFor contains now then lastd else recomputeDenot(lastd)
161+
if lastd.validFor.code.containsSinglePhasePeriod(ctx.period.code)
162+
then lastd
163+
else recomputeDenot(lastd)
171164

172165
private def recomputeDenot(lastd: SymDenotation)(using Context): SymDenotation =
173166
util.Stats.record("Symbol.recomputeDenot")
@@ -454,16 +447,16 @@ object Symbols {
454447
end extension
455448

456449
type TreeOrProvider = TreeProvider | Tree
457-
450+
/*
458451
class ClassSymbol private[Symbols] extends Symbol {
459452
460453
util.Stats.record("ClassSymbol")
461454
462455
type ThisName = TypeName
463456
}
464-
457+
*/
465458
extension (_self: ClassSymbol)
466-
def self = _self.initialDenot
459+
def self: ClassDenotation = _self
467460

468461
/** If this is a top-level class and `-Yretain-trees` (or `-from-tasty`) is set.
469462
* Returns the TypeDef tree (possibly wrapped inside PackageDefs) for this class, otherwise EmptyTree.
@@ -535,16 +528,7 @@ object Symbols {
535528

536529
end extension
537530

538-
@sharable object NoSymbol extends Symbol {
539-
//override def coord = NoCoord
540-
//override def id = 0
541-
//override def nestingLevel = 0
542-
//override def defTree = tpd.EmptyTree
543-
//override def associatedFile(using Context): AbstractFile | Null = NoSource.file
544-
//override def recomputeDenot(lastd: SymDenotation)(using Context): SymDenotation = NoDenotation
545-
}
546-
547-
NoDenotation // force it in order to set `denot` field of NoSymbol
531+
def NoSymbol: Symbol = NoDenotation
548532

549533
/** Makes all denotation operations available on symbols */
550534
implicit def toDenot(sym: Symbol)(using Context): SymDenotation = sym.denot
@@ -579,11 +563,9 @@ object Symbols {
579563
privateWithin: Symbol = NoSymbol,
580564
coord: Coord = NoCoord,
581565
nestingLevel: Int = ctx.nestingLevel): Symbol { type ThisName = N } = {
582-
val sym = new Symbol().asInstanceOf[Symbol { type ThisName = N }]
583-
val denot = SymDenotation(sym, SymCommon(coord, ctx.base.nextSymId, nestingLevel), owner, name, flags, info, privateWithin)
584-
sym.initialDenot = denot
585-
sym.denot_=(denot)
586-
sym
566+
val sym = SymDenotation(null, SymCommon(coord, ctx.base.nextSymId, nestingLevel), owner, name, flags, info, privateWithin)
567+
sym.denot_=(sym)
568+
sym.asInstanceOf[Symbol { type ThisName = N }]
587569
}
588570

589571
/** Create a class symbol from its non-info fields and a function
@@ -598,11 +580,9 @@ object Symbols {
598580
coord: Coord = NoCoord,
599581
assocFile: AbstractFile | Null = null)(using Context): ClassSymbol
600582
= {
601-
val cls = new ClassSymbol()
602-
val denot = SymDenotation(cls, ClassCommon(coord, ctx.base.nextSymId, ctx.nestingLevel, assocFile), owner, name, flags, NoType, privateWithin)
603-
cls.initialDenot = denot
604-
cls.denot_=(denot)
605-
denot.info = infoFn(cls)
583+
val cls = SymDenotation(null, ClassCommon(coord, ctx.base.nextSymId, ctx.nestingLevel, assocFile), owner, name, flags, NoType, privateWithin).asClass
584+
cls.denot_=(cls)
585+
cls.info = infoFn(cls)
606586
cls
607587
}
608588

scaladoc/src/dotty/tools/scaladoc/snippets/SnippetCompilerDataCollector.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import scala.quoted._
55
import dotty.tools.scaladoc.tasty.SymOps._
66
import dotty.tools.dotc.core._
77
import dotty.tools.dotc.util.{ SourceFile => CSourceFile, NoSource }
8+
import Symbols.TypeTests.given
89

910
class SnippetCompilerDataCollector[Q <: Quotes](val qctx: Q):
1011
import qctx.reflect._

0 commit comments

Comments
 (0)