Skip to content

Commit 2f9d66c

Browse files
committed
Go back to withSym creation methods for NamedTypes
It allows us to drop the additional name field in most cases and the methods become easier to discover.
1 parent 4241475 commit 2f9d66c

File tree

7 files changed

+36
-35
lines changed

7 files changed

+36
-35
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
382382
val targs = tp.argTypes
383383
val tycon = tp.typeConstructor
384384
New(tycon)
385-
.select(TermRef(tycon, constr, constr.name))
385+
.select(TermRef.withSym(tycon, constr))
386386
.appliedToTypes(targs)
387387
.appliedToArgs(args)
388388
}
@@ -916,9 +916,8 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
916916
alternatives.head
917917
}
918918
else denot.asSingleDenotation.termRef
919-
val selectedSym = selected.termSymbol.asTerm
920919
val fun = receiver
921-
.select(TermRef(receiver.tpe, selectedSym, selectedSym.name))
920+
.select(TermRef.withSym(receiver.tpe, selected.termSymbol.asTerm))
922921
.appliedToTypes(targs)
923922

924923
def adaptLastArg(lastParam: Tree, expectedType: Type) = {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1395,7 +1395,7 @@ object SymDenotations {
13951395
private def computeThisType(implicit ctx: Context): Type = {
13961396
val cls = symbol.asType
13971397
val pre = if (this is Package) NoPrefix else owner.thisType
1398-
ThisType.raw(TypeRef(pre, cls, cls.name))
1398+
ThisType.raw(TypeRef.withSym(pre, cls))
13991399
}
14001400

14011401
private[this] var myTypeRef: TypeRef = null

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

Lines changed: 3 additions & 3 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(owner.thisType, modcls, modclsName)
157+
if (cdenot.isCompleted) TypeRef.withSym(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(owner.thisType, module, name)),
182+
owner.thisType, modcls, parents, decls, TermRef.withSym(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(owner.thisType, tparam, name)
290+
trefBuf += TypeRef.withSym(owner.thisType, tparam, name)
291291
}
292292
val tparams = tparamBuf.toList
293293
val bounds = boundsFn(trefBuf.toList)

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

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -707,10 +707,7 @@ object Types {
707707
final def implicitMembers(implicit ctx: Context): List[TermRef] = track("implicitMembers") {
708708
memberDenots(implicitFilter,
709709
(name, buf) => buf ++= member(name).altsWith(_ is Implicit))
710-
.toList.map { d =>
711-
val mbr = d.symbol.asTerm
712-
TermRef(this, mbr, mbr.name)
713-
}
710+
.toList.map(d => TermRef.withSym(this, d.symbol.asTerm))
714711
}
715712

716713
/** The set of member classes of this type */
@@ -1136,7 +1133,7 @@ object Types {
11361133

11371134
/** The type <this . name> with either `sym` or its signed name as designator, reduced if possible */
11381135
def select(sym: Symbol)(implicit ctx: Context): Type =
1139-
NamedType(this, sym, sym.name).reduceProjection
1136+
NamedType.withSym(this, sym).reduceProjection
11401137

11411138
// ----- Access to parts --------------------------------------------
11421139

@@ -2034,9 +2031,9 @@ object Types {
20342031
def apply(prefix: Type, designator: Name, denot: Denotation)(implicit ctx: Context) =
20352032
if (designator.isTermName) TermRef(prefix, designator.asTermName, denot)
20362033
else TypeRef(prefix, designator.asTypeName, denot)
2037-
def apply(prefix: Type, sym: Symbol, name: Name)(implicit ctx: Context): NamedType =
2038-
if (sym.isType) TypeRef.apply(prefix, sym.asType, name.asTypeName)
2039-
else TermRef.apply(prefix, sym.asTerm, name.asTermName)
2034+
def withSym(prefix: Type, sym: Symbol)(implicit ctx: Context): NamedType =
2035+
if (sym.isType) TypeRef.withSym(prefix, sym.asType)
2036+
else TermRef.withSym(prefix, sym.asTerm)
20402037
}
20412038

20422039
object TermRef {
@@ -2048,15 +2045,6 @@ object Types {
20482045
def apply(prefix: Type, designator: TermDesignator)(implicit ctx: Context): TermRef =
20492046
ctx.uniqueNamedTypes.enterIfNew(prefix, designator, isTerm = true).asInstanceOf[TermRef]
20502047

2051-
/** Create a term ref referring to given symbol with given name.
2052-
* This is similar to TermRef(Type, Symbol), except:
2053-
* (1) the symbol might not yet have a denotation, so the name needs to be given explicitly.
2054-
* (2) the designator of the TermRef is either the symbol or its name & unforced signature.
2055-
*/
2056-
def apply(prefix: Type, sym: TermSymbol, name: TermName)(implicit ctx: Context): TermRef =
2057-
if ((prefix eq NoPrefix) || sym.isReferencedSymbolically) apply(prefix, sym)
2058-
else apply(prefix, name.withSig(sym.signature)).withSym(sym)
2059-
20602048
/** Create term ref to given initial denotation, taking the signature
20612049
* from the denotation if it is completed, or creating a term ref without
20622050
* signature, if denotation is not yet completed.
@@ -2068,6 +2056,18 @@ object Types {
20682056
case _ => apply(prefix, name)
20692057
}
20702058
} withDenot denot
2059+
2060+
/** Create a term ref referring to given symbol with given name.
2061+
* This is similar to TermRef(Type, Symbol), except:
2062+
* (1) the symbol might not yet have a denotation, so the name needs to be given explicitly.
2063+
* (2) the designator of the TermRef is either the symbol or its name & unforced signature.
2064+
*/
2065+
def withSym(prefix: Type, sym: TermSymbol, name: TermName)(implicit ctx: Context): TermRef =
2066+
if ((prefix eq NoPrefix) || sym.isReferencedSymbolically) apply(prefix, sym)
2067+
else apply(prefix, name.withSig(sym.signature)).withSym(sym)
2068+
2069+
def withSym(prefix: Type, sym: TermSymbol)(implicit ctx: Context): TermRef =
2070+
withSym(prefix, sym, sym.name)
20712071
}
20722072

20732073
object TypeRef {
@@ -2076,20 +2076,23 @@ object Types {
20762076
def apply(prefix: Type, desig: TypeDesignator)(implicit ctx: Context): TypeRef =
20772077
ctx.uniqueNamedTypes.enterIfNew(prefix, desig, isTerm = false).asInstanceOf[TypeRef]
20782078

2079+
/** Create a type ref with given name and initial denotation */
2080+
def apply(prefix: Type, name: TypeName, denot: Denotation)(implicit ctx: Context): TypeRef = {
2081+
if ((prefix eq NoPrefix) || denot.symbol.isReferencedSymbolically) apply(prefix, denot.symbol.asType)
2082+
else apply(prefix, name)
2083+
} withDenot denot
2084+
20792085
/** Create a type ref referring to either a given symbol or its name.
20802086
* This is similar to TypeRef(prefix, sym), except:
20812087
* (1) the symbol might not yet have a denotation, so the name needs to be given explicitly.
20822088
* (2) the designator of the TypeRef is either the symbol or its name
20832089
*/
2084-
def apply(prefix: Type, sym: TypeSymbol, name: TypeName)(implicit ctx: Context): TypeRef =
2090+
def withSym(prefix: Type, sym: TypeSymbol, name: TypeName)(implicit ctx: Context): TypeRef =
20852091
if ((prefix eq NoPrefix) || sym.isReferencedSymbolically) apply(prefix, sym)
20862092
else apply(prefix, name).withSym(sym)
20872093

2088-
/** Create a type ref with given name and initial denotation */
2089-
def apply(prefix: Type, name: TypeName, denot: Denotation)(implicit ctx: Context): TypeRef = {
2090-
if ((prefix eq NoPrefix) || denot.symbol.isReferencedSymbolically) apply(prefix, denot.symbol.asType)
2091-
else apply(prefix, name)
2092-
} withDenot denot
2094+
def withSym(prefix: Type, sym: TypeSymbol)(implicit ctx: Context): TypeRef =
2095+
withSym(prefix, sym, sym.name)
20932096
}
20942097

20952098
// --- Other SingletonTypes: ThisType/SuperType/ConstantType ---------------------------
@@ -3374,7 +3377,7 @@ object Types {
33743377
appliedRefCache
33753378
}
33763379

3377-
def symbolicTypeRef(implicit ctx: Context): TypeRef = TypeRef(prefix, cls, cls.name)
3380+
def symbolicTypeRef(implicit ctx: Context): TypeRef = TypeRef.withSym(prefix, cls)
33783381

33793382
// cached because baseType needs parents
33803383
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(prefix, sym, sym.name)
333+
val res = NamedType.withSym(prefix, sym)
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/typer/Applications.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,7 @@ 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)
388-
selectGetter(ref(TermRef(prefix, companion.asTerm, companion.name.asTermName)))
387+
if (prefix.exists) selectGetter(ref(TermRef.withSym(prefix, companion.asTerm)))
389388
else EmptyTree
390389
}
391390
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(pre, sym, sym.name))
1011+
f(TermRef.withSym(pre, sym))
10121012
}
10131013

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

0 commit comments

Comments
 (0)