Skip to content

Commit 28ab1af

Browse files
committed
Get rid of WithFixedSym
1 parent 33af34c commit 28ab1af

File tree

10 files changed

+67
-92
lines changed

10 files changed

+67
-92
lines changed

compiler/src/dotty/tools/dotc/ast/Trees.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ object Trees {
8686
* where we overwrite with a simplified version of the type itself.
8787
*/
8888
private[dotc] def overwriteType(tpe: T) = {
89-
if (this.isInstanceOf[Template[_]]) assert(tpe.isInstanceOf[WithFixedSym], s"$this <--- $tpe")
89+
if (this.isInstanceOf[Template[_]])
90+
tpe match {
91+
case tpe: TermRef => assert(tpe.hasFixedSym , s"$this <--- $tpe")
92+
}
9093
myTpe = tpe
9194
}
9295

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ trait Substituters { this: Context =>
130130
var ts = to
131131
while (fs.nonEmpty) {
132132
if (fs.head eq sym)
133-
return tp match {
134-
case tp: WithFixedSym => NamedType.withFixedSym(tp.prefix, ts.head)
135-
case _ => substSym(tp.prefix, from, to, theMap) select ts.head
133+
return {
134+
if (tp.hasFixedSym) NamedType.withFixedSym(tp.prefix, ts.head) // ### why the different treatement of prefix?
135+
else substSym(tp.prefix, from, to, theMap) select ts.head
136136
}
137137
fs = fs.tail
138138
ts = ts.tail

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,7 @@ object SymDenotations {
11361136
TermRef.withSigAndDenot(owner.thisType, name.asTermName, signature, this)
11371137

11381138
def nonMemberTermRef(implicit ctx: Context): TermRef =
1139-
TermRef.withFixedSym(owner.thisType, name.asTermName, symbol.asTerm)
1139+
TermRef.withFixedSym(owner.thisType, symbol.asTerm)
11401140

11411141
/** The variance of this type parameter or type member as an Int, with
11421142
* +1 = Covariant, -1 = Contravariant, 0 = Nonvariant, or not a type parameter

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ class TypeComparer(initctx: Context) extends DotClass with ConstraintHandling {
198198
( (tp1.name eq tp2.name)
199199
&& isSubType(tp1.prefix, tp2.prefix)
200200
&& tp1.signature == tp2.signature
201-
&& !tp1.isInstanceOf[WithFixedSym]
202-
&& !tp2.isInstanceOf[WithFixedSym]
201+
&& !tp1.hasFixedSym
202+
&& !tp2.hasFixedSym
203203
) ||
204204
thirdTryNamed(tp1, tp2)
205205
case _ =>

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

Lines changed: 37 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,6 +1502,7 @@ object Types {
15021502

15031503
def isType = isInstanceOf[TypeRef]
15041504
def isTerm = isInstanceOf[TermRef]
1505+
def hasFixedSym = designator.isInstanceOf[Symbol]
15051506

15061507
private[this] var myName: ThisName = _
15071508
private[this] var mySig: Signature = null
@@ -1610,7 +1611,7 @@ object Types {
16101611
val sym = lastSymbol
16111612
if (sym == null) loadDenot else denotOfSym(sym)
16121613
case d: SymDenotation =>
1613-
if (this.isInstanceOf[WithFixedSym]) d.current
1614+
if (hasFixedSym) d.current
16141615
else if (d.validFor.runId == ctx.runId || ctx.stillValid(d))
16151616
if (d.exists && prefix.isTightPrefix(d.owner) || d.isConstructor) d.current
16161617
else recomputeMember(d) // symbol could have been overridden, recompute membership
@@ -1696,7 +1697,9 @@ object Types {
16961697
}
16971698

16981699
private[dotc] def withDenot(denot: Denotation)(implicit ctx: Context): ThisType =
1699-
if (signature != denot.signature && denot.signature.ne(Signature.OverloadedSignature))
1700+
if (!hasFixedSym &&
1701+
signature != denot.signature &&
1702+
denot.signature.ne(Signature.OverloadedSignature))
17001703
withSig(denot.signature).withDenot(denot).asInstanceOf[ThisType]
17011704
else {
17021705
setDenot(denot)
@@ -1975,22 +1978,25 @@ object Types {
19751978
}
19761979
else candidate
19771980

1978-
def newLikeThis(prefix: Type)(implicit ctx: Context): NamedType = {
1979-
// If symbol exists, the new signature is the symbol's signature as seen
1980-
// from the new prefix, modulo consistency
1981-
val curSig = signature
1982-
val newSig =
1983-
if (curSig == Signature.NotAMethod || !symbol.exists)
1984-
curSig
1985-
else
1986-
curSig.updateWith(symbol.info.asSeenFrom(prefix, symbol.owner).signature)
1987-
val candidate =
1988-
if (newSig ne curSig) {
1989-
core.println(i"sig change at ${ctx.phase} for $this, pre = $prefix, sig: $curSig --> $newSig")
1990-
TermRef.withSig(prefix, name, newSig)
1991-
}
1992-
else TermRef(prefix, designatorName.asTermName) // ###
1993-
fixDenot(candidate, prefix)
1981+
def newLikeThis(prefix: Type)(implicit ctx: Context): NamedType = designator match {
1982+
case designator: TermSymbol =>
1983+
TermRef.withFixedSym(prefix, designator)
1984+
case designator: TermName =>
1985+
// If symbol exists, the new signature is the symbol's signature as seen
1986+
// from the new prefix, modulo consistency
1987+
val curSig = signature
1988+
val newSig =
1989+
if (curSig == Signature.NotAMethod || !symbol.exists)
1990+
curSig
1991+
else
1992+
curSig.updateWith(symbol.info.asSeenFrom(prefix, symbol.owner).signature)
1993+
val candidate =
1994+
if (newSig ne curSig) {
1995+
core.println(i"sig change at ${ctx.phase} for $this, pre = $prefix, sig: $curSig --> $newSig")
1996+
TermRef.withSig(prefix, name, newSig)
1997+
}
1998+
else TermRef(prefix, designator)
1999+
fixDenot(candidate, prefix)
19942000
}
19952001

19962002
override def shadowed(implicit ctx: Context): NamedType =
@@ -2005,41 +2011,16 @@ object Types {
20052011
override def underlying(implicit ctx: Context): Type = info
20062012

20072013
def newLikeThis(prefix: Type)(implicit ctx: Context): NamedType =
2008-
TypeRef(prefix, name) // ###
2009-
}
2010-
2011-
2012-
trait WithFixedSym extends NamedType {
2013-
def fixedSym: Symbol = designator.asInstanceOf[Symbol]
2014-
2015-
override def withDenot(denot: Denotation)(implicit ctx: Context): ThisType = {
2016-
assert(denot.symbol eq fixedSym)
2017-
setDenot(denot)
2018-
this
2019-
}
2020-
2021-
override def withSym(sym: Symbol)(implicit ctx: Context): this.type =
2022-
unsupported("withSym")
2023-
2024-
override def newLikeThis(prefix: Type)(implicit ctx: Context): NamedType =
2025-
NamedType.withFixedSym(prefix, fixedSym)
2014+
TypeRef(prefix, designator)
20262015
}
20272016

20282017
final class CachedTermRef(prefix: Type, designator: TermDesignator, hc: Int) extends TermRef(prefix, designator) {
2029-
assert(prefix ne NoPrefix)
2018+
assert((prefix ne NoPrefix) || hasFixedSym)
20302019
myHash = hc
20312020
}
20322021

20332022
final class CachedTypeRef(prefix: Type, designator: TypeDesignator, hc: Int) extends TypeRef(prefix, designator) {
2034-
assert(prefix ne NoPrefix)
2035-
myHash = hc
2036-
}
2037-
2038-
// Those classes are non final as Linker extends them.
2039-
class TermRefWithFixedSym(prefix: Type, designator: TermSymbol, hc: Int) extends TermRef(prefix, designator) with WithFixedSym {
2040-
myHash = hc
2041-
}
2042-
class TypeRefWithFixedSym(prefix: Type, designator: TypeSymbol, hc: Int) extends TypeRef(prefix, designator) with WithFixedSym {
2023+
assert((prefix ne NoPrefix) || hasFixedSym)
20432024
myHash = hc
20442025
}
20452026

@@ -2055,8 +2036,8 @@ object Types {
20552036
if (designator.isTermName) TermRef(prefix, designator.asTermName, denot)
20562037
else TypeRef(prefix, designator.asTypeName, denot)
20572038
def withFixedSym(prefix: Type, sym: Symbol)(implicit ctx: Context) =
2058-
if (sym.isType) TypeRef.withFixedSym(prefix, sym.name.asTypeName, sym.asType)
2059-
else TermRef.withFixedSym(prefix, sym.name.asTermName, sym.asTerm)
2039+
if (sym.isType) TypeRef.withFixedSym(prefix, sym.asType)
2040+
else TermRef.withFixedSym(prefix, sym.asTerm)
20602041
def withSymAndName(prefix: Type, sym: Symbol, name: Name)(implicit ctx: Context): NamedType =
20612042
if (sym.isType) TypeRef.withSymAndName(prefix, sym.asType, name.asTypeName)
20622043
else TermRef.withSymAndName(prefix, sym.asTerm, name.asTermName)
@@ -2096,7 +2077,7 @@ object Types {
20962077
/** Create a non-member term ref (which cannot be reloaded using `member`),
20972078
* with given prefix, name, and signature
20982079
*/
2099-
def withFixedSym(prefix: Type, name: TermName, sym: TermSymbol)(implicit ctx: Context): TermRef =
2080+
def withFixedSym(prefix: Type, sym: TermSymbol)(implicit ctx: Context): TermRef =
21002081
ctx.uniqueNamedTypes.enterIfNew(prefix, sym, isTerm = true).asInstanceOf[TermRef]
21012082

21022083
/** Create a term ref referring to given symbol with given name, taking the signature
@@ -2108,7 +2089,7 @@ object Types {
21082089
*/
21092090
def withSymAndName(prefix: Type, sym: TermSymbol, name: TermName)(implicit ctx: Context): TermRef =
21102091
if ((prefix eq NoPrefix) || sym.isFresh || symbolicRefs)
2111-
withFixedSym(prefix, name, sym)
2092+
withFixedSym(prefix, sym)
21122093
else if (sym.defRunId != NoRunId && sym.isCompleted)
21132094
withSig(prefix, name, sym.signature).withSym(sym)
21142095
// Linker note:
@@ -2122,7 +2103,7 @@ object Types {
21222103
* (which must be completed).
21232104
*/
21242105
def withSig(prefix: Type, sym: TermSymbol)(implicit ctx: Context): TermRef =
2125-
if ((prefix eq NoPrefix) || sym.isFresh || symbolicRefs) withFixedSym(prefix, sym.name, sym)
2106+
if ((prefix eq NoPrefix) || sym.isFresh || symbolicRefs) withFixedSym(prefix, sym)
21262107
else withSig(prefix, sym.name, sym.signature).withSym(sym)
21272108

21282109
/** Create a term ref with given prefix, name and signature */
@@ -2132,16 +2113,16 @@ object Types {
21322113
/** Create a term ref with given prefix, name, signature, and initial denotation */
21332114
def withSigAndDenot(prefix: Type, name: TermName, sig: Signature, denot: Denotation)(implicit ctx: Context): TermRef = {
21342115
if ((prefix eq NoPrefix) || denot.symbol.isFresh || symbolicRefs)
2135-
withFixedSym(prefix, denot.symbol.asTerm.name, denot.symbol.asTerm)
2116+
withFixedSym(prefix, denot.symbol.asTerm)
21362117
else
21372118
withSig(prefix, name, sig)
21382119
} withDenot denot
21392120
}
21402121

21412122
object TypeRef {
21422123
/** Create type ref with given prefix and name */
2143-
def apply(prefix: Type, name: TypeName)(implicit ctx: Context): TypeRef =
2144-
ctx.uniqueNamedTypes.enterIfNew(prefix, name, isTerm = false).asInstanceOf[TypeRef]
2124+
def apply(prefix: Type, desig: TypeDesignator)(implicit ctx: Context): TypeRef =
2125+
ctx.uniqueNamedTypes.enterIfNew(prefix, desig, isTerm = false).asInstanceOf[TypeRef]
21452126

21462127
/** Create type ref to given symbol */
21472128
def apply(prefix: Type, sym: TypeSymbol)(implicit ctx: Context): TypeRef =
@@ -2150,7 +2131,7 @@ object Types {
21502131
/** Create a non-member type ref (which cannot be reloaded using `member`),
21512132
* with given prefix, name, and symbol.
21522133
*/
2153-
def withFixedSym(prefix: Type, name: TypeName, sym: TypeSymbol)(implicit ctx: Context): TypeRef =
2134+
def withFixedSym(prefix: Type, sym: TypeSymbol)(implicit ctx: Context): TypeRef =
21542135
ctx.uniqueNamedTypes.enterIfNew(prefix, sym, isTerm = false).asInstanceOf[TypeRef]
21552136

21562137
/** Create a type ref referring to given symbol with given name.
@@ -2160,7 +2141,7 @@ object Types {
21602141
* (2) The name in the type ref need not be the same as the name of the Symbol.
21612142
*/
21622143
def withSymAndName(prefix: Type, sym: TypeSymbol, name: TypeName)(implicit ctx: Context): TypeRef =
2163-
if ((prefix eq NoPrefix) || sym.isFresh) withFixedSym(prefix, name, sym)
2144+
if ((prefix eq NoPrefix) || sym.isFresh) withFixedSym(prefix, sym)
21642145
else apply(prefix, name).withSym(sym)
21652146

21662147
/** Create a type ref with given name and initial denotation */

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,8 @@ object Uniques {
5656
val h = doHash(designator, prefix)
5757
if (monitored) recordCaching(h, classOf[NamedType])
5858
def newType = {
59-
if (isTerm)
60-
designator match {
61-
case designator: TermSymbol => new TermRefWithFixedSym(prefix, designator, h)
62-
case _ => new CachedTermRef(prefix, designator.asTerm, h)
63-
}
64-
else
65-
designator match {
66-
case designator: TypeSymbol => new TypeRefWithFixedSym(prefix, designator, h)
67-
case _ => new CachedTypeRef(prefix, designator.asType, h)
68-
}
59+
if (isTerm) new CachedTermRef(prefix, designator.asInstanceOf[TermDesignator], h)
60+
else new CachedTypeRef(prefix, designator.asInstanceOf[TypeDesignator], h)
6961
}.init()
7062
if (h == NotCached) newType
7163
else {

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

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ class TreePickler(pickler: TastyPickler) {
142142
withLength { pickleType(tycon); args.foreach(pickleType(_)) }
143143
case ConstantType(value) =>
144144
pickleConstant(value)
145-
case tpe: WithFixedSym =>
145+
case tpe: NamedType =>
146146
val sym = tpe.symbol
147147
def pickleRef() =
148148
if (tpe.prefix == NoPrefix) {
@@ -169,21 +169,21 @@ class TreePickler(pickler: TastyPickler) {
169169
pickleRef()
170170
}
171171
}
172-
else pickleRef()
173-
case tpe: NamedType =>
174-
val sym = tpe.symbol
175-
if (sym.is(Flags.Package) && tpe.isTerm)
176-
picklePackageRef(sym)
177-
else if (isLocallyDefined(tpe.symbol) && tpe.signature.eq(Signature.NotAMethod)) {
172+
else if (tpe.hasFixedSym) {
173+
pickleRef()
174+
}
175+
else if (isLocallyDefined(sym) && tpe.signature.eq(Signature.NotAMethod)) {
178176
writeByte(if (tpe.isType) TYPEREFsymbol else TERMREFsymbol)
179-
pickleSymRef(tpe.symbol); pickleType(tpe.prefix)
177+
pickleSymRef(sym); pickleType(tpe.prefix)
180178
} else {
181179
writeByte(if (tpe.isType) TYPEREF else TERMREF)
182180
pickleName(tpe.designatorName); pickleType(tpe.prefix)
183181
}
184182
case tpe: ThisType =>
185-
if (tpe.cls.is(Flags.Package) && !tpe.cls.isEffectiveRoot)
186-
picklePackageRef(tpe.cls)
183+
if (tpe.cls.is(Flags.Package) && !tpe.cls.isEffectiveRoot) {
184+
writeByte(TERMREFpkg)
185+
pickleName(tpe.cls.fullName)
186+
}
187187
else {
188188
writeByte(THIS)
189189
pickleType(tpe.tref)
@@ -238,11 +238,6 @@ class TreePickler(pickler: TastyPickler) {
238238
pickleType(tpe.ref)
239239
}
240240

241-
def picklePackageRef(pkg: Symbol)(implicit ctx: Context): Unit = {
242-
writeByte(TERMREFpkg)
243-
pickleName(pkg.fullName)
244-
}
245-
246241
def pickleMethodic(tag: Int, tpe: LambdaType)(implicit ctx: Context) = {
247242
writeByte(tag)
248243
withLength {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ class TreeUnpickler(reader: TastyReader, nameAtRef: NameRef => TermName, posUnpi
246246
val sym = ctx.newSymbol(ctx.owner, readName().toTypeName, BindDefinedType, readType())
247247
registerSym(start, sym)
248248
if (currentAddr != end) readType()
249-
TypeRef.withFixedSym(NoPrefix, sym.name, sym)
249+
TypeRef.withFixedSym(NoPrefix, sym)
250250
case POLYtype =>
251251
readMethodic(PolyType, _.toTypeName)
252252
case METHODtype =>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ class Scala2Unpickler(bytes: Array[Byte], classRoot: ClassDenotation, moduleClas
735735
val tycon =
736736
if (sym.isClass && sym.is(Scala2x) && !sym.owner.is(Package))
737737
// used fixed sym for Scala 2 inner classes, because they might be shadowed
738-
TypeRef.withFixedSym(pre, sym.name.asTypeName, sym.asType)
738+
TypeRef.withFixedSym(pre, sym.asType)
739739
else if (isLocal(sym) || pre == NoPrefix) {
740740
val pre1 = if ((pre eq NoPrefix) && (sym is TypeParam)) sym.owner.thisType else pre
741741
pre1 select sym

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,12 @@ class TreeChecker extends Phase with SymTransformer {
323323
assert(tree.isTerm || !ctx.isAfterTyper, tree.show + " at " + ctx.phase)
324324
val tpe = tree.typeOpt
325325
val sym = tree.symbol
326-
if (!tpe.isInstanceOf[WithFixedSym] &&
327-
sym.exists && !sym.is(Private) &&
326+
val symIsFixed = tpe match {
327+
case tpe: TermRef => tpe.hasFixedSym
328+
case _ => false
329+
}
330+
if (sym.exists && !sym.is(Private) &&
331+
!symIsFixed &&
328332
!tree.name.is(OuterSelectName) // outer selects have effectively fixed symbols
329333
) {
330334
val qualTpe = tree.qualifier.typeOpt

0 commit comments

Comments
 (0)