Skip to content

Commit d868f33

Browse files
committed
Get rid of WithFixedSym
1 parent a3c69a5 commit d868f33

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
@@ -1498,6 +1498,7 @@ object Types {
14981498

14991499
def isType = isInstanceOf[TypeRef]
15001500
def isTerm = isInstanceOf[TermRef]
1501+
def hasFixedSym = designator.isInstanceOf[Symbol]
15011502

15021503
private[this] var myName: ThisName = _
15031504
private[this] var mySig: Signature = null
@@ -1606,7 +1607,7 @@ object Types {
16061607
val sym = lastSymbol
16071608
if (sym != null && sym.isValidInCurrentRun) denotOfSym(sym) else loadDenot
16081609
case d: SymDenotation =>
1609-
if (this.isInstanceOf[WithFixedSym]) d.current
1610+
if (hasFixedSym) d.current
16101611
else if (d.validFor.runId == ctx.runId || ctx.stillValid(d))
16111612
if (d.exists && prefix.isTightPrefix(d.owner) || d.isConstructor) d.current
16121613
else recomputeMember(d) // symbol could have been overridden, recompute membership
@@ -1692,7 +1693,9 @@ object Types {
16921693
}
16931694

16941695
private[dotc] def withDenot(denot: Denotation)(implicit ctx: Context): ThisType =
1695-
if (signature != denot.signature && denot.signature.ne(Signature.OverloadedSignature))
1696+
if (!hasFixedSym &&
1697+
signature != denot.signature &&
1698+
denot.signature.ne(Signature.OverloadedSignature))
16961699
withSig(denot.signature).withDenot(denot).asInstanceOf[ThisType]
16971700
else {
16981701
setDenot(denot)
@@ -1970,22 +1973,25 @@ object Types {
19701973
}
19711974
else candidate
19721975

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

19911997
override def shadowed(implicit ctx: Context): NamedType =
@@ -2000,41 +2006,16 @@ object Types {
20002006
override def underlying(implicit ctx: Context): Type = info
20012007

20022008
def newLikeThis(prefix: Type)(implicit ctx: Context): NamedType =
2003-
TypeRef(prefix, name) // ###
2004-
}
2005-
2006-
2007-
trait WithFixedSym extends NamedType {
2008-
def fixedSym: Symbol = designator.asInstanceOf[Symbol]
2009-
2010-
override def withDenot(denot: Denotation)(implicit ctx: Context): ThisType = {
2011-
assert(denot.symbol eq fixedSym)
2012-
setDenot(denot)
2013-
this
2014-
}
2015-
2016-
override def withSym(sym: Symbol)(implicit ctx: Context): this.type =
2017-
unsupported("withSym")
2018-
2019-
override def newLikeThis(prefix: Type)(implicit ctx: Context): NamedType =
2020-
NamedType.withFixedSym(prefix, fixedSym)
2009+
TypeRef(prefix, designator)
20212010
}
20222011

20232012
final class CachedTermRef(prefix: Type, designator: TermDesignator, hc: Int) extends TermRef(prefix, designator) {
2024-
assert(prefix ne NoPrefix)
2013+
assert((prefix ne NoPrefix) || hasFixedSym)
20252014
myHash = hc
20262015
}
20272016

20282017
final class CachedTypeRef(prefix: Type, designator: TypeDesignator, hc: Int) extends TypeRef(prefix, designator) {
2029-
assert(prefix ne NoPrefix)
2030-
myHash = hc
2031-
}
2032-
2033-
// Those classes are non final as Linker extends them.
2034-
class TermRefWithFixedSym(prefix: Type, designator: TermSymbol, hc: Int) extends TermRef(prefix, designator) with WithFixedSym {
2035-
myHash = hc
2036-
}
2037-
class TypeRefWithFixedSym(prefix: Type, designator: TypeSymbol, hc: Int) extends TypeRef(prefix, designator) with WithFixedSym {
2018+
assert((prefix ne NoPrefix) || hasFixedSym)
20382019
myHash = hc
20392020
}
20402021

@@ -2050,8 +2031,8 @@ object Types {
20502031
if (designator.isTermName) TermRef(prefix, designator.asTermName, denot)
20512032
else TypeRef(prefix, designator.asTypeName, denot)
20522033
def withFixedSym(prefix: Type, sym: Symbol)(implicit ctx: Context) =
2053-
if (sym.isType) TypeRef.withFixedSym(prefix, sym.name.asTypeName, sym.asType)
2054-
else TermRef.withFixedSym(prefix, sym.name.asTermName, sym.asTerm)
2034+
if (sym.isType) TypeRef.withFixedSym(prefix, sym.asType)
2035+
else TermRef.withFixedSym(prefix, sym.asTerm)
20552036
def withSymAndName(prefix: Type, sym: Symbol, name: Name)(implicit ctx: Context): NamedType =
20562037
if (sym.isType) TypeRef.withSymAndName(prefix, sym.asType, name.asTypeName)
20572038
else TermRef.withSymAndName(prefix, sym.asTerm, name.asTermName)
@@ -2091,7 +2072,7 @@ object Types {
20912072
/** Create a non-member term ref (which cannot be reloaded using `member`),
20922073
* with given prefix, name, and signature
20932074
*/
2094-
def withFixedSym(prefix: Type, name: TermName, sym: TermSymbol)(implicit ctx: Context): TermRef =
2075+
def withFixedSym(prefix: Type, sym: TermSymbol)(implicit ctx: Context): TermRef =
20952076
ctx.uniqueNamedTypes.enterIfNew(prefix, sym, isTerm = true).asInstanceOf[TermRef]
20962077

20972078
/** Create a term ref referring to given symbol with given name, taking the signature
@@ -2103,7 +2084,7 @@ object Types {
21032084
*/
21042085
def withSymAndName(prefix: Type, sym: TermSymbol, name: TermName)(implicit ctx: Context): TermRef =
21052086
if ((prefix eq NoPrefix) || sym.isFresh || symbolicRefs)
2106-
withFixedSym(prefix, name, sym)
2087+
withFixedSym(prefix, sym)
21072088
else if (sym.defRunId != NoRunId && sym.isCompleted)
21082089
withSig(prefix, name, sym.signature).withSym(sym)
21092090
// Linker note:
@@ -2117,7 +2098,7 @@ object Types {
21172098
* (which must be completed).
21182099
*/
21192100
def withSig(prefix: Type, sym: TermSymbol)(implicit ctx: Context): TermRef =
2120-
if ((prefix eq NoPrefix) || sym.isFresh || symbolicRefs) withFixedSym(prefix, sym.name, sym)
2101+
if ((prefix eq NoPrefix) || sym.isFresh || symbolicRefs) withFixedSym(prefix, sym)
21212102
else withSig(prefix, sym.name, sym.signature).withSym(sym)
21222103

21232104
/** Create a term ref with given prefix, name and signature */
@@ -2127,16 +2108,16 @@ object Types {
21272108
/** Create a term ref with given prefix, name, signature, and initial denotation */
21282109
def withSigAndDenot(prefix: Type, name: TermName, sig: Signature, denot: Denotation)(implicit ctx: Context): TermRef = {
21292110
if ((prefix eq NoPrefix) || denot.symbol.isFresh || symbolicRefs)
2130-
withFixedSym(prefix, denot.symbol.asTerm.name, denot.symbol.asTerm)
2111+
withFixedSym(prefix, denot.symbol.asTerm)
21312112
else
21322113
withSig(prefix, name, sig)
21332114
} withDenot denot
21342115
}
21352116

21362117
object TypeRef {
21372118
/** Create type ref with given prefix and name */
2138-
def apply(prefix: Type, name: TypeName)(implicit ctx: Context): TypeRef =
2139-
ctx.uniqueNamedTypes.enterIfNew(prefix, name, isTerm = false).asInstanceOf[TypeRef]
2119+
def apply(prefix: Type, desig: TypeDesignator)(implicit ctx: Context): TypeRef =
2120+
ctx.uniqueNamedTypes.enterIfNew(prefix, desig, isTerm = false).asInstanceOf[TypeRef]
21402121

21412122
/** Create type ref to given symbol */
21422123
def apply(prefix: Type, sym: TypeSymbol)(implicit ctx: Context): TypeRef =
@@ -2145,7 +2126,7 @@ object Types {
21452126
/** Create a non-member type ref (which cannot be reloaded using `member`),
21462127
* with given prefix, name, and symbol.
21472128
*/
2148-
def withFixedSym(prefix: Type, name: TypeName, sym: TypeSymbol)(implicit ctx: Context): TypeRef =
2129+
def withFixedSym(prefix: Type, sym: TypeSymbol)(implicit ctx: Context): TypeRef =
21492130
ctx.uniqueNamedTypes.enterIfNew(prefix, sym, isTerm = false).asInstanceOf[TypeRef]
21502131

21512132
/** Create a type ref referring to given symbol with given name.
@@ -2155,7 +2136,7 @@ object Types {
21552136
* (2) The name in the type ref need not be the same as the name of the Symbol.
21562137
*/
21572138
def withSymAndName(prefix: Type, sym: TypeSymbol, name: TypeName)(implicit ctx: Context): TypeRef =
2158-
if ((prefix eq NoPrefix) || sym.isFresh) withFixedSym(prefix, name, sym)
2139+
if ((prefix eq NoPrefix) || sym.isFresh) withFixedSym(prefix, sym)
21592140
else apply(prefix, name).withSym(sym)
21602141

21612142
/** 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
@@ -320,8 +320,12 @@ class TreeChecker extends Phase with SymTransformer {
320320
assert(tree.isTerm || !ctx.isAfterTyper, tree.show + " at " + ctx.phase)
321321
val tpe = tree.typeOpt
322322
val sym = tree.symbol
323-
if (!tpe.isInstanceOf[WithFixedSym] &&
324-
sym.exists && !sym.is(Private) &&
323+
val symIsFixed = tpe match {
324+
case tpe: TermRef => tpe.hasFixedSym
325+
case _ => false
326+
}
327+
if (sym.exists && !sym.is(Private) &&
328+
!symIsFixed &&
325329
!tree.name.is(OuterSelectName) // outer selects have effectively fixed symbols
326330
) {
327331
val qualTpe = tree.qualifier.typeOpt

0 commit comments

Comments
 (0)