Skip to content

Commit 483a8f3

Browse files
committed
Streamline NamedType creation methods
- drop withSym - rename withSymAndName to another apply - streamline select methods
1 parent 1ec7fe5 commit 483a8f3

File tree

11 files changed

+66
-83
lines changed

11 files changed

+66
-83
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
9797
def Closure(meth: TermSymbol, rhsFn: List[List[Tree]] => Tree, targs: List[Tree] = Nil, targetType: Type = NoType)(implicit ctx: Context): Block = {
9898
val targetTpt = if (targetType.exists) TypeTree(targetType) else EmptyTree
9999
val call =
100-
if (targs.isEmpty) Ident(TermRef.withSym(NoPrefix, meth))
101-
else TypeApply(Ident(TermRef.withSym(NoPrefix, meth)), targs)
100+
if (targs.isEmpty) Ident(TermRef(NoPrefix, meth))
101+
else TypeApply(Ident(TermRef(NoPrefix, meth)), targs)
102102
Block(
103103
DefDef(meth, rhsFn) :: Nil,
104104
Closure(Nil, call, targetTpt))

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ class Definitions {
174174
lazy val RootClass: ClassSymbol = ctx.newPackageSymbol(
175175
NoSymbol, nme.ROOT, (root, rootcls) => ctx.rootLoader(root)).moduleClass.asClass
176176
lazy val RootPackage: TermSymbol = ctx.newSymbol(
177-
NoSymbol, nme.ROOTPKG, PackageCreationFlags, TypeRef.withSym(NoPrefix, RootClass))
177+
NoSymbol, nme.ROOTPKG, PackageCreationFlags, TypeRef(NoPrefix, RootClass))
178178

179179
lazy val EmptyPackageVal = ctx.newPackageSymbol(
180180
RootClass, nme.EMPTY_PACKAGE, (emptypkg, emptycls) => ctx.rootLoader(emptypkg)).entered

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,9 +1398,11 @@ object SymDenotations {
13981398
myThisType
13991399
}
14001400

1401-
private def computeThisType(implicit ctx: Context): Type =
1402-
ThisType.raw(TypeRef.withSym(
1403-
if (this is Package) NoPrefix else owner.thisType, symbol.asType))
1401+
private def computeThisType(implicit ctx: Context): Type = {
1402+
val cls = symbol.asType
1403+
val pre = if (this is Package) NoPrefix else owner.thisType
1404+
ThisType.raw(TypeRef(pre, cls, cls.name))
1405+
}
14041406

14051407
private[this] var myTypeRef: TypeRef = null
14061408

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ trait Symbols { this: Context =>
154154
infoFn(module, modcls), privateWithin)
155155
val mdenot = SymDenotation(
156156
module, owner, name, modFlags | ModuleCreationFlags,
157-
if (cdenot.isCompleted) TypeRef.withSymAndName(owner.thisType, modcls, modclsName)
157+
if (cdenot.isCompleted) TypeRef(owner.thisType, modcls, modclsName)
158158
else new ModuleCompleter(modcls))
159159
module.denot = mdenot
160160
modcls.denot = cdenot
@@ -179,7 +179,7 @@ trait Symbols { this: Context =>
179179
newModuleSymbol(
180180
owner, name, modFlags, clsFlags,
181181
(module, modcls) => ClassInfo(
182-
owner.thisType, modcls, parents, decls, TermRef.withSymAndName(owner.thisType, module, name)),
182+
owner.thisType, modcls, parents, decls, TermRef(owner.thisType, module, name)),
183183
privateWithin, coord, assocFile)
184184

185185
val companionMethodFlags = Flags.Synthetic | Flags.Private | Flags.Method
@@ -287,7 +287,7 @@ trait Symbols { this: Context =>
287287
for (name <- names) {
288288
val tparam = newNakedSymbol[TypeName](NoCoord)
289289
tparamBuf += tparam
290-
trefBuf += TypeRef.withSymAndName(owner.thisType, tparam, name)
290+
trefBuf += TypeRef(owner.thisType, tparam, name)
291291
}
292292
val tparams = tparamBuf.toList
293293
val bounds = boundsFn(trefBuf.toList)
@@ -443,19 +443,23 @@ object Symbols {
443443
asInstanceOf[TypeSymbol]
444444
}
445445

446-
447446
final def isClass: Boolean = isInstanceOf[ClassSymbol]
448447
final def asClass: ClassSymbol = asInstanceOf[ClassSymbol]
449448

450449
final def isReferencedSymbolically(implicit ctx: Context) = {
451-
val sym = lastDenot
452-
sym != null && (
453-
(sym is NonMember)
454-
|| sym.isClass && sym.is(Scala2x) && !sym.owner.is(Package)
455-
|| sym.isTerm && ctx.phase.symbolicRefs
450+
val d = lastDenot
451+
d != null && (
452+
(d is NonMember)
453+
|| d.isClass && d.is(Scala2x) && !d.owner.is(Package)
454+
|| d.isTerm && ctx.phase.symbolicRefs
456455
)
457456
}
458457

458+
/** The symbol's signature if it is completed, NotAMethod otherwise. */
459+
final def unforcedSignature(implicit ctx: Context) =
460+
if (lastDenot != null && lastDenot.isCompleted) lastDenot.signature
461+
else Signature.NotAMethod
462+
459463
/** Special cased here, because it may be used on naked symbols in substituters */
460464
final def isStatic(implicit ctx: Context): Boolean =
461465
lastDenot != null && denot.isStatic

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ object TypeErasure {
150150
assert(tp.symbol.exists, tp)
151151
val tp1 = ctx.makePackageObjPrefixExplicit(tp)
152152
if (tp1 ne tp) erasedRef(tp1)
153-
else TermRef.withSym(erasedRef(tp.prefix), tp.symbol.asTerm)
153+
else TermRef(erasedRef(tp.prefix), tp.symbol.asTerm)
154154
case tp: ThisType =>
155155
tp
156156
case tp =>

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

Lines changed: 38 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,21 +1124,16 @@ object Types {
11241124
}
11251125

11261126
/** The type <this . name> , reduced if possible */
1127-
def select(name: Name)(implicit ctx: Context): Type = name match {
1128-
case name: TermName => TermRef(this, name)
1129-
case name: TypeName => TypeRef(this, name).reduceProjection
1130-
}
1127+
def select(name: Name)(implicit ctx: Context): Type =
1128+
NamedType(this, name).reduceProjection
11311129

11321130
/** The type <this . name> , reduced if possible, with given denotation if unreduced */
1133-
def select(name: Name, denot: Denotation)(implicit ctx: Context): Type = name match {
1134-
case name: TermName => TermRef(this, name, denot)
1135-
case name: TypeName => TypeRef(this, name, denot).reduceProjection
1136-
}
1131+
def select(name: Name, denot: Denotation)(implicit ctx: Context): Type =
1132+
NamedType(this, name, denot).reduceProjection
11371133

1138-
/** The type <this . name> with given symbol, reduced if possible */
1134+
/** The type <this . name> with either `sym` or its signed name as designator, reduced if possible */
11391135
def select(sym: Symbol)(implicit ctx: Context): Type =
1140-
if (sym.isTerm) TermRef.withSym(this, sym.asTerm)
1141-
else TypeRef.withSym(this, sym.asType).reduceProjection
1136+
NamedType(this, sym, sym.name).reduceProjection
11421137

11431138
// ----- Access to parts --------------------------------------------
11441139

@@ -1762,10 +1757,12 @@ object Types {
17621757
* provided `U` does not refer with a RecThis to the
17631758
* refinement type `T { X = U; ... }`
17641759
*/
1765-
def reduceProjection(implicit ctx: Context): Type = {
1766-
val reduced = prefix.lookupRefined(name)
1767-
if (reduced.exists) reduced else this
1768-
}
1760+
def reduceProjection(implicit ctx: Context): Type =
1761+
if (isType) {
1762+
val reduced = prefix.lookupRefined(name)
1763+
if (reduced.exists) reduced else this
1764+
}
1765+
else this
17691766

17701767
def symbol(implicit ctx: Context): Symbol =
17711768
if (checkedPeriod == ctx.period ||
@@ -2033,103 +2030,82 @@ object Types {
20332030
def apply(prefix: Type, designator: Name, denot: Denotation)(implicit ctx: Context) =
20342031
if (designator.isTermName) TermRef(prefix, designator.asTermName, denot)
20352032
else TypeRef(prefix, designator.asTypeName, denot)
2036-
def withSymAndName(prefix: Type, sym: Symbol, name: Name)(implicit ctx: Context): NamedType =
2037-
if (sym.isType) TypeRef.withSymAndName(prefix, sym.asType, name.asTypeName)
2038-
else TermRef.withSymAndName(prefix, sym.asTerm, name.asTermName)
2033+
def apply(prefix: Type, sym: Symbol, name: Name)(implicit ctx: Context): NamedType =
2034+
if (sym.isType) TypeRef.apply(prefix, sym.asType, name.asTypeName)
2035+
else TermRef.apply(prefix, sym.asTerm, name.asTermName)
20392036
}
20402037

20412038
object TermRef {
20422039

2043-
private def symbolicRefs(implicit ctx: Context) = ctx.phase.symbolicRefs
2044-
20452040
/** Create term ref with given name, without specifying a signature.
20462041
* Its meaning is the (potentially multi-) denotation of the member(s)
20472042
* of prefix with given name.
20482043
*/
20492044
def apply(prefix: Type, designator: TermDesignator)(implicit ctx: Context): TermRef =
20502045
ctx.uniqueNamedTypes.enterIfNew(prefix, designator, isTerm = true).asInstanceOf[TermRef]
20512046

2052-
/** Create term ref referring to given symbol, taking the signature
2053-
* from the symbol if it is completed, or creating a term ref without
2054-
* signature, if symbol is not yet completed.
2047+
/** Create a term ref referring to given symbol with given name.
2048+
* This is similar to TermRef(Type, Symbol), except:
2049+
* (1) the symbol might not yet have a denotation, so the name needs to be given explicitly.
2050+
* (2) the designator of the TermRef is either the symbol or its name & unforced signature.
20552051
*/
2056-
def withSym(prefix: Type, sym: TermSymbol)(implicit ctx: Context): TermRef =
2057-
withSymAndName(prefix, sym, sym.name)
2052+
def apply(prefix: Type, sym: TermSymbol, name: TermName)(implicit ctx: Context): TermRef =
2053+
if ((prefix eq NoPrefix) || sym.isReferencedSymbolically)
2054+
apply(prefix, sym)
2055+
else
2056+
withSig(prefix, name.asTermName, sym.unforcedSignature).withSym(sym)
20582057

20592058
/** Create term ref to given initial denotation, taking the signature
20602059
* from the denotation if it is completed, or creating a term ref without
20612060
* signature, if denotation is not yet completed.
20622061
*/
20632062
def apply(prefix: Type, designator: TermName, denot: Denotation)(implicit ctx: Context): TermRef = {
2064-
if ((prefix eq NoPrefix) || denot.symbol.isReferencedSymbolically || symbolicRefs)
2065-
withSym(prefix, denot.symbol.asTerm)
2063+
if ((prefix eq NoPrefix) || denot.symbol.isReferencedSymbolically)
2064+
apply(prefix, denot.symbol.asTerm)
20662065
else denot match {
20672066
case denot: SymDenotation if denot.isCompleted => withSig(prefix, designator, denot.signature)
20682067
case _ => apply(prefix, designator)
20692068
}
20702069
} withDenot denot
20712070

2072-
/** Create a term ref referring to given symbol with given name, taking the signature
2073-
* from the symbol if it is completed, or creating a term ref without
2074-
* signature, if symbol is not yet completed. This is very similar to TermRef(Type, Symbol),
2075-
* except for two differences:
2076-
* (1) The symbol might not yet have a denotation, so the name needs to be given explicitly.
2077-
* (2) The name in the term ref need not be the same as the name of the Symbol.
2078-
*/
2079-
def withSymAndName(prefix: Type, sym: TermSymbol, name: TermName)(implicit ctx: Context): TermRef =
2080-
if ((prefix eq NoPrefix) || sym.isReferencedSymbolically|| symbolicRefs)
2081-
apply(prefix, sym)
2082-
else if (sym.defRunId != NoRunId && sym.isCompleted)
2083-
withSig(prefix, name, sym.signature).withSym(sym)
2084-
// Linker note:
2085-
// this is problematic, as withSig method could return a hash-consed refference
2086-
// that could have symbol already set making withSym trigger a double-binding error
2087-
// ./tests/run/absoverride.scala demonstates this
2088-
else
2089-
withSig(prefix, name, Signature.NotAMethod).withSym(sym)
2090-
20912071
/** Create a term ref to given symbol, taking the signature from the symbol
20922072
* (which must be completed).
20932073
*/
20942074
def withSig(prefix: Type, sym: TermSymbol)(implicit ctx: Context): TermRef =
2095-
if ((prefix eq NoPrefix) || sym.isReferencedSymbolically || symbolicRefs) apply(prefix, sym)
2075+
if ((prefix eq NoPrefix) || sym.isReferencedSymbolically) apply(prefix, sym)
20962076
else withSig(prefix, sym.name, sym.signature).withSym(sym)
20972077

20982078
/** Create a term ref with given prefix, name and signature */
2099-
def withSig(prefix: Type, designator: TermName, sig: Signature)(implicit ctx: Context): TermRef =
2100-
apply(prefix, designator.withSig(sig))
2079+
def withSig(prefix: Type, name: TermName, sig: Signature)(implicit ctx: Context): TermRef =
2080+
apply(prefix, name.withSig(sig))
21012081

21022082
/** Create a term ref with given prefix, name, signature, and initial denotation */
21032083
def withSigAndDenot(prefix: Type, name: TermName, sig: Signature, denot: Denotation)(implicit ctx: Context): TermRef = {
2104-
if ((prefix eq NoPrefix) || denot.symbol.isReferencedSymbolically || symbolicRefs)
2084+
if ((prefix eq NoPrefix) || denot.symbol.isReferencedSymbolically)
21052085
apply(prefix, denot.symbol.asTerm)
21062086
else
21072087
withSig(prefix, name, sig)
21082088
} withDenot denot
21092089
}
21102090

21112091
object TypeRef {
2092+
21122093
/** Create type ref with given prefix and name */
21132094
def apply(prefix: Type, desig: TypeDesignator)(implicit ctx: Context): TypeRef =
21142095
ctx.uniqueNamedTypes.enterIfNew(prefix, desig, isTerm = false).asInstanceOf[TypeRef]
21152096

2116-
/** Create type ref to given symbol */
2117-
def withSym(prefix: Type, sym: TypeSymbol)(implicit ctx: Context): TypeRef =
2118-
withSymAndName(prefix, sym, sym.name)
2119-
2120-
/** Create a type ref referring to given symbol with given name.
2121-
* This is very similar to TypeRef(Type, Symbol),
2122-
* except for two differences:
2123-
* (1) The symbol might not yet have a denotation, so the name needs to be given explicitly.
2124-
* (2) The name in the type ref need not be the same as the name of the Symbol.
2097+
/** Create a type ref referring to either a given symbol or its name.
2098+
* This is similar to TypeRef(prefix, sym), except:
2099+
* (1) the symbol might not yet have a denotation, so the name needs to be given explicitly.
2100+
* (2) the designator of the TypeRef is either the symbol or its name
21252101
*/
2126-
def withSymAndName(prefix: Type, sym: TypeSymbol, name: TypeName)(implicit ctx: Context): TypeRef =
2102+
def apply(prefix: Type, sym: TypeSymbol, name: TypeName)(implicit ctx: Context): TypeRef =
21272103
if ((prefix eq NoPrefix) || sym.isReferencedSymbolically) apply(prefix, sym)
21282104
else apply(prefix, name).withSym(sym)
21292105

21302106
/** Create a type ref with given name and initial denotation */
21312107
def apply(prefix: Type, name: TypeName, denot: Denotation)(implicit ctx: Context): TypeRef = {
2132-
if ((prefix eq NoPrefix) || denot.symbol.isReferencedSymbolically) withSym(prefix, denot.symbol.asType)
2108+
if ((prefix eq NoPrefix) || denot.symbol.isReferencedSymbolically) apply(prefix, denot.symbol.asType)
21332109
else apply(prefix, name)
21342110
} withDenot denot
21352111
}
@@ -3416,7 +3392,7 @@ object Types {
34163392
appliedRefCache
34173393
}
34183394

3419-
def symbolicTypeRef(implicit ctx: Context): TypeRef = TypeRef.withSym(prefix, cls)
3395+
def symbolicTypeRef(implicit ctx: Context): TypeRef = TypeRef(prefix, cls, cls.name)
34203396

34213397
// cached because baseType needs parents
34223398
private[this] var parentsCache: List[Type] = null

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ class TreeUnpickler(reader: TastyReader, nameAtRef: NameRef => TermName, posUnpi
330330
private def readSymNameRef()(implicit ctx: Context): Type = {
331331
val sym = readSymRef()
332332
val prefix = readType()
333-
val res = NamedType.withSymAndName(prefix, sym, sym.name)
333+
val res = NamedType(prefix, sym, sym.name)
334334
prefix match {
335335
case prefix: ThisType if prefix.cls eq sym.owner => res.withDenot(sym.denot)
336336
// without this precaution we get an infinite cycle when unpickling pos/extmethods.scala

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ object ExplicitOuter {
321321
*/
322322
private def fixThis(tpe: Type)(implicit ctx: Context): Type = tpe match {
323323
case tpe: ThisType if tpe.cls.is(Module) && !ctx.owner.isContainedIn(tpe.cls) =>
324-
fixThis(TermRef.withSym(tpe.cls.owner.thisType, tpe.cls.sourceModule.asTerm))
324+
fixThis(tpe.cls.owner.thisType.select(tpe.cls.sourceModule.asTerm))
325325
case tpe: TermRef =>
326326
tpe.derivedSelect(fixThis(tpe.prefix))
327327
case _ =>

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,8 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
384384
val companion = cls.companionModule
385385
if (companion.isTerm) {
386386
val prefix = receiver.tpe.baseType(cls).normalizedPrefix
387-
if (prefix.exists) selectGetter(ref(TermRef.withSym(prefix, companion.asTerm)))
387+
if (prefix.exists)
388+
selectGetter(ref(TermRef(prefix, companion.asTerm, companion.name.asTermName)))
388389
else EmptyTree
389390
}
390391
else EmptyTree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,7 @@ class TermRefSet(implicit ctx: Context) extends mutable.Traversable[TermRef] {
10081008
override def foreach[U](f: TermRef => U): Unit =
10091009
for (sym <- elems.keysIterator)
10101010
for (pre <- elems(sym))
1011-
f(TermRef.withSym(pre, sym))
1011+
f(TermRef(pre, sym, sym.name))
10121012
}
10131013

10141014
@sharable object EmptyTermRefSet extends TermRefSet()(NoContext)

0 commit comments

Comments
 (0)