Skip to content

Commit 1ef47da

Browse files
committed
Streamline NamedType creation methods
- drop withSym - rename withSymAndName to another apply - streamline select methods
1 parent 3ef98eb commit 1ef47da

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
@@ -177,7 +177,7 @@ class Definitions {
177177
lazy val RootClass: ClassSymbol = ctx.newPackageSymbol(
178178
NoSymbol, nme.ROOT, (root, rootcls) => ctx.rootLoader(root)).moduleClass.asClass
179179
lazy val RootPackage: TermSymbol = ctx.newSymbol(
180-
NoSymbol, nme.ROOTPKG, PackageCreationFlags, TypeRef.withSym(NoPrefix, RootClass))
180+
NoSymbol, nme.ROOTPKG, PackageCreationFlags, TypeRef(NoPrefix, RootClass))
181181

182182
lazy val EmptyPackageVal = ctx.newPackageSymbol(
183183
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)
@@ -440,19 +440,23 @@ object Symbols {
440440
asInstanceOf[TypeSymbol]
441441
}
442442

443-
444443
final def isClass: Boolean = isInstanceOf[ClassSymbol]
445444
final def asClass: ClassSymbol = asInstanceOf[ClassSymbol]
446445

447446
final def isReferencedSymbolically(implicit ctx: Context) = {
448-
val sym = lastDenot
449-
sym != null && (
450-
(sym is NonMember)
451-
|| sym.isClass && sym.is(Scala2x) && !sym.owner.is(Package)
452-
|| sym.isTerm && ctx.phase.symbolicRefs
447+
val d = lastDenot
448+
d != null && (
449+
(d is NonMember)
450+
|| d.isClass && d.is(Scala2x) && !d.owner.is(Package)
451+
|| d.isTerm && ctx.phase.symbolicRefs
453452
)
454453
}
455454

455+
/** The symbol's signature if it is completed, NotAMethod otherwise. */
456+
final def unforcedSignature(implicit ctx: Context) =
457+
if (lastDenot != null && lastDenot.isCompleted) lastDenot.signature
458+
else Signature.NotAMethod
459+
456460
/** Special cased here, because it may be used on naked symbols in substituters */
457461
final def isStatic(implicit ctx: Context): Boolean =
458462
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
@@ -1128,21 +1128,16 @@ object Types {
11281128
}
11291129

11301130
/** The type <this . name> , reduced if possible */
1131-
def select(name: Name)(implicit ctx: Context): Type = name match {
1132-
case name: TermName => TermRef(this, name)
1133-
case name: TypeName => TypeRef(this, name).reduceProjection
1134-
}
1131+
def select(name: Name)(implicit ctx: Context): Type =
1132+
NamedType(this, name).reduceProjection
11351133

11361134
/** The type <this . name> , reduced if possible, with given denotation if unreduced */
1137-
def select(name: Name, denot: Denotation)(implicit ctx: Context): Type = name match {
1138-
case name: TermName => TermRef(this, name, denot)
1139-
case name: TypeName => TypeRef(this, name, denot).reduceProjection
1140-
}
1135+
def select(name: Name, denot: Denotation)(implicit ctx: Context): Type =
1136+
NamedType(this, name, denot).reduceProjection
11411137

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

11471142
// ----- Access to parts --------------------------------------------
11481143

@@ -1766,10 +1761,12 @@ object Types {
17661761
* provided `U` does not refer with a RecThis to the
17671762
* refinement type `T { X = U; ... }`
17681763
*/
1769-
def reduceProjection(implicit ctx: Context): Type = {
1770-
val reduced = prefix.lookupRefined(name)
1771-
if (reduced.exists) reduced else this
1772-
}
1764+
def reduceProjection(implicit ctx: Context): Type =
1765+
if (isType) {
1766+
val reduced = prefix.lookupRefined(name)
1767+
if (reduced.exists) reduced else this
1768+
}
1769+
else this
17731770

17741771
def symbol(implicit ctx: Context): Symbol = {
17751772
val now = ctx.period
@@ -2038,103 +2035,82 @@ object Types {
20382035
def apply(prefix: Type, designator: Name, denot: Denotation)(implicit ctx: Context) =
20392036
if (designator.isTermName) TermRef(prefix, designator.asTermName, denot)
20402037
else TypeRef(prefix, designator.asTypeName, denot)
2041-
def withSymAndName(prefix: Type, sym: Symbol, name: Name)(implicit ctx: Context): NamedType =
2042-
if (sym.isType) TypeRef.withSymAndName(prefix, sym.asType, name.asTypeName)
2043-
else TermRef.withSymAndName(prefix, sym.asTerm, name.asTermName)
2038+
def apply(prefix: Type, sym: Symbol, name: Name)(implicit ctx: Context): NamedType =
2039+
if (sym.isType) TypeRef.apply(prefix, sym.asType, name.asTypeName)
2040+
else TermRef.apply(prefix, sym.asTerm, name.asTermName)
20442041
}
20452042

20462043
object TermRef {
20472044

2048-
private def symbolicRefs(implicit ctx: Context) = ctx.phase.symbolicRefs
2049-
20502045
/** Create term ref with given name, without specifying a signature.
20512046
* Its meaning is the (potentially multi-) denotation of the member(s)
20522047
* of prefix with given name.
20532048
*/
20542049
def apply(prefix: Type, designator: TermDesignator)(implicit ctx: Context): TermRef =
20552050
ctx.uniqueNamedTypes.enterIfNew(prefix, designator, isTerm = true).asInstanceOf[TermRef]
20562051

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

20642063
/** Create term ref to given initial denotation, taking the signature
20652064
* from the denotation if it is completed, or creating a term ref without
20662065
* signature, if denotation is not yet completed.
20672066
*/
20682067
def apply(prefix: Type, designator: TermName, denot: Denotation)(implicit ctx: Context): TermRef = {
2069-
if ((prefix eq NoPrefix) || denot.symbol.isReferencedSymbolically || symbolicRefs)
2070-
withSym(prefix, denot.symbol.asTerm)
2068+
if ((prefix eq NoPrefix) || denot.symbol.isReferencedSymbolically)
2069+
apply(prefix, denot.symbol.asTerm)
20712070
else denot match {
20722071
case denot: SymDenotation if denot.isCompleted => withSig(prefix, designator, denot.signature)
20732072
case _ => apply(prefix, designator)
20742073
}
20752074
} withDenot denot
20762075

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

21032083
/** Create a term ref with given prefix, name and signature */
2104-
def withSig(prefix: Type, designator: TermName, sig: Signature)(implicit ctx: Context): TermRef =
2105-
apply(prefix, designator.withSig(sig))
2084+
def withSig(prefix: Type, name: TermName, sig: Signature)(implicit ctx: Context): TermRef =
2085+
apply(prefix, name.withSig(sig))
21062086

21072087
/** Create a term ref with given prefix, name, signature, and initial denotation */
21082088
def withSigAndDenot(prefix: Type, name: TermName, sig: Signature, denot: Denotation)(implicit ctx: Context): TermRef = {
2109-
if ((prefix eq NoPrefix) || denot.symbol.isReferencedSymbolically || symbolicRefs)
2089+
if ((prefix eq NoPrefix) || denot.symbol.isReferencedSymbolically)
21102090
apply(prefix, denot.symbol.asTerm)
21112091
else
21122092
withSig(prefix, name, sig)
21132093
} withDenot denot
21142094
}
21152095

21162096
object TypeRef {
2097+
21172098
/** Create type ref with given prefix and name */
21182099
def apply(prefix: Type, desig: TypeDesignator)(implicit ctx: Context): TypeRef =
21192100
ctx.uniqueNamedTypes.enterIfNew(prefix, desig, isTerm = false).asInstanceOf[TypeRef]
21202101

2121-
/** Create type ref to given symbol */
2122-
def withSym(prefix: Type, sym: TypeSymbol)(implicit ctx: Context): TypeRef =
2123-
withSymAndName(prefix, sym, sym.name)
2124-
2125-
/** Create a type ref referring to given symbol with given name.
2126-
* This is very similar to TypeRef(Type, Symbol),
2127-
* except for two differences:
2128-
* (1) The symbol might not yet have a denotation, so the name needs to be given explicitly.
2129-
* (2) The name in the type ref need not be the same as the name of the Symbol.
2102+
/** Create a type ref referring to either a given symbol or its name.
2103+
* This is similar to TypeRef(prefix, sym), except:
2104+
* (1) the symbol might not yet have a denotation, so the name needs to be given explicitly.
2105+
* (2) the designator of the TypeRef is either the symbol or its name
21302106
*/
2131-
def withSymAndName(prefix: Type, sym: TypeSymbol, name: TypeName)(implicit ctx: Context): TypeRef =
2107+
def apply(prefix: Type, sym: TypeSymbol, name: TypeName)(implicit ctx: Context): TypeRef =
21322108
if ((prefix eq NoPrefix) || sym.isReferencedSymbolically) apply(prefix, sym)
21332109
else apply(prefix, name).withSym(sym)
21342110

21352111
/** Create a type ref with given name and initial denotation */
21362112
def apply(prefix: Type, name: TypeName, denot: Denotation)(implicit ctx: Context): TypeRef = {
2137-
if ((prefix eq NoPrefix) || denot.symbol.isReferencedSymbolically) withSym(prefix, denot.symbol.asType)
2113+
if ((prefix eq NoPrefix) || denot.symbol.isReferencedSymbolically) apply(prefix, denot.symbol.asType)
21382114
else apply(prefix, name)
21392115
} withDenot denot
21402116
}
@@ -3421,7 +3397,7 @@ object Types {
34213397
appliedRefCache
34223398
}
34233399

3424-
def symbolicTypeRef(implicit ctx: Context): TypeRef = TypeRef.withSym(prefix, cls)
3400+
def symbolicTypeRef(implicit ctx: Context): TypeRef = TypeRef(prefix, cls, cls.name)
34253401

34263402
// cached because baseType needs parents
34273403
private 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)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
989989
else if (!owner.isCompleted)
990990
(EmptyTree, errorType(em"$owner has return statement; needs result type", tree.pos))
991991
else {
992-
val from = Ident(TermRef.withSym(NoPrefix, owner.asTerm))
992+
val from = Ident(TermRef(NoPrefix, owner.asTerm))
993993
val proto = returnProto(owner, cx.scope)
994994
(from, proto)
995995
}

0 commit comments

Comments
 (0)