Skip to content

Commit e9f60a8

Browse files
committed
Introduce and use isSymbol/asSymbol extension methods
1 parent b857e14 commit e9f60a8

File tree

3 files changed

+24
-19
lines changed

3 files changed

+24
-19
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ object Symbols {
8484
type TermSymbol = Symbol { type ThisName = TermName }
8585
type TypeSymbol = Symbol { type ThisName = TypeName }
8686

87+
extension (x: AnyRef)
88+
inline def isSymbol: Boolean = x.isInstanceOf[Symbol]
89+
inline def asSymbol: Symbol = x.asInstanceOf[Symbol]
90+
8791
extension (_self: Symbol)
8892
def self = _self.initialDenot
8993

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

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2221,7 +2221,7 @@ object Types {
22212221

22222222
private def computeName: Name = designator match {
22232223
case name: Name => name
2224-
case sym: Symbol => sym.originDenotation.name
2224+
case sym => sym.asSymbol.originDenotation.name
22252225
}
22262226

22272227
final override def signature(using Context): Signature =
@@ -2272,10 +2272,12 @@ object Types {
22722272
else computeSymbol
22732273

22742274
private def computeSymbol(using Context): Symbol =
2275-
val result = designator match
2276-
case sym: Symbol =>
2277-
if (sym.isValidInCurrentRun) sym else denot.symbol
2278-
case name =>
2275+
val desig = designator
2276+
val result =
2277+
if desig.isSymbol then
2278+
val sym = desig.asSymbol
2279+
if sym.isValidInCurrentRun then sym else denot.symbol
2280+
else
22792281
(if (denotationIsCurrent) lastDenotation.asInstanceOf[Denotation] else denot).symbol
22802282
if checkedPeriod.code != NowhereCode then checkedPeriod = ctx.period
22812283
result
@@ -2295,10 +2297,11 @@ object Types {
22952297
* type accumulators, as well as to be safe in diagnostic printing.
22962298
* Normally, it's better to use `symbol`, not `currentSymbol`.
22972299
*/
2298-
final def currentSymbol(using Context): Symbol = designator match {
2299-
case sym: Symbol => sym
2300-
case _ => if (denotationIsCurrent) lastDenotation.nn.symbol else NoSymbol
2301-
}
2300+
final def currentSymbol(using Context): Symbol =
2301+
val desig = designator
2302+
if desig.isSymbol then desig.asSymbol
2303+
else if denotationIsCurrent then lastDenotation.nn.symbol
2304+
else NoSymbol
23022305

23032306
/** Retrieves currently valid symbol without necessarily updating denotation.
23042307
* Assumes that symbols do not change between periods in the same run.
@@ -2340,7 +2343,8 @@ object Types {
23402343
val sym = lastSymbol
23412344
val allowPrivate = sym == null || (sym == NoSymbol) || sym.lastKnownDenotation.flagsUNSAFE.is(Private)
23422345
finish(memberDenot(name, allowPrivate))
2343-
case sym: Symbol =>
2346+
case desig =>
2347+
val sym = desig.asSymbol
23442348
val symd = sym.lastKnownDenotation
23452349
if (symd.validFor.runId != ctx.runId && !stillValid(symd))
23462350
finish(memberDenot(symd.initial.name, allowPrivate = false))
@@ -2452,11 +2456,8 @@ object Types {
24522456
val lastSym = denot.symbol.asInstanceOf[Symbol]
24532457
lastSymbol = lastSym
24542458
checkedPeriod = if (prefix.isProvisional) Nowhere else ctx.period
2455-
designator match {
2456-
case sym: Symbol if designator ne lastSym =>
2457-
designator = lastSym
2458-
case _ =>
2459-
}
2459+
if designator.isSymbol && (designator ne lastSym) then
2460+
designator = lastSym
24602461
checkDenot()
24612462
}
24622463

@@ -2675,7 +2676,7 @@ object Types {
26752676
val adapted = withSym(denot.symbol)
26762677
val result =
26772678
if (adapted.eq(this)
2678-
|| designator.isInstanceOf[Symbol]
2679+
|| designator.isSymbol
26792680
|| !adapted.denotationIsCurrent
26802681
|| adapted.info.eq(denot.info))
26812682
adapted
@@ -2864,12 +2865,12 @@ object Types {
28642865
}
28652866

28662867
final class CachedTermRef(prefix: Type, designator: Designator, hc: Int) extends TermRef(prefix, designator) {
2867-
assert((prefix ne NoPrefix) || designator.isInstanceOf[Symbol])
2868+
assert((prefix ne NoPrefix) || designator.isSymbol)
28682869
myHash = hc
28692870
}
28702871

28712872
final class CachedTypeRef(prefix: Type, designator: Designator, hc: Int) extends TypeRef(prefix, designator) {
2872-
assert((prefix ne NoPrefix) || designator.isInstanceOf[Symbol])
2873+
assert((prefix ne NoPrefix) || designator.isSymbol)
28732874
myHash = hc
28742875
}
28752876

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2088,7 +2088,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
20882088
def tparamBounds =
20892089
val bounds =
20902090
tparam.paramInfoAsSeenFrom(tpt1.tpe.appliedTo(tparams.map(_ => TypeBounds.empty)))
2091-
if tparam.isInstanceOf[Symbol] then bounds
2091+
if tparam.isSymbol then bounds
20922092
else sanitizeBounds(bounds, tpt1.tpe)
20932093
val (desugaredArg, argPt) =
20942094
if ctx.mode.is(Mode.Pattern) then

0 commit comments

Comments
 (0)