Skip to content

Commit 760479b

Browse files
committed
Eliminate TermRef.withSig
To do this, we needed a more robust version of Symbol#signature.
1 parent 483a8f3 commit 760479b

File tree

8 files changed

+34
-40
lines changed

8 files changed

+34
-40
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import dotty.tools.dotc.transform.{ExplicitOuter, Erasure}
66
import dotty.tools.dotc.typer.ProtoTypes.FunProtoTyped
77
import transform.SymUtils._
88
import core._
9-
import util.Positions._, Types._, Contexts._, Constants._, Names._, Flags._
9+
import util.Positions._, Types._, Contexts._, Constants._, Names._, Flags._, NameOps._
1010
import SymDenotations._, Symbols._, StdNames._, Annotations._, Trees._, Symbols._
1111
import Denotations._, Decorators._, DenotTransformers._
1212
import collection.mutable
@@ -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.withSig(tycon, constr))
385+
.select(TermRef(tycon, constr, constr.name))
386386
.appliedToTypes(targs)
387387
.appliedToArgs(args)
388388
}
@@ -704,14 +704,13 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
704704
else
705705
TermRef.withSigAndDenot(tree.tpe, sym.name.asTermName,
706706
sym.signature, sym.denot.asSeenFrom(tree.tpe))
707-
untpd.Select(tree, sym.name)
708-
.withType(tp)
707+
untpd.Select(tree, sym.name).withType(tp)
709708
}
710709

711710
/** A select node with the given selector name and signature and a computed type */
712711
def selectWithSig(name: Name, sig: Signature)(implicit ctx: Context): Tree =
713712
untpd.SelectWithSig(tree, name, sig)
714-
.withType(TermRef.withSig(tree.tpe, name.asTermName, sig))
713+
.withType(TermRef(tree.tpe, name.asTermName.withSig(sig)))
715714

716715
/** A select node with selector name and signature taken from `sym`.
717716
* Note: Use this method instead of select(sym) if the referenced symbol
@@ -918,8 +917,9 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
918917
alternatives.head
919918
}
920919
else denot.asSingleDenotation.termRef
920+
val selectedSym = selected.termSymbol.asTerm
921921
val fun = receiver
922-
.select(TermRef.withSig(receiver.tpe, selected.termSymbol.asTerm))
922+
.select(TermRef(receiver.tpe, selectedSym, selectedSym.name))
923923
.appliedToTypes(targs)
924924

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

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -455,10 +455,12 @@ object Symbols {
455455
)
456456
}
457457

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
458+
/** The symbol's signature if it is completed or a method, NotAMethod otherwise. */
459+
final def signature(implicit ctx: Context) =
460+
if (lastDenot != null && (lastDenot.isCompleted || lastDenot.is(Method)))
461+
denot.signature
462+
else
463+
Signature.NotAMethod
462464

463465
/** Special cased here, because it may be used on naked symbols in substituters */
464466
final def isStatic(implicit ctx: Context): Boolean =

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

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,10 @@ 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 => TermRef.withSig(this, d.symbol.asTerm))
710+
.toList.map { d =>
711+
val mbr = d.symbol.asTerm
712+
TermRef(this, mbr, mbr.name)
713+
}
711714
}
712715

713716
/** The set of member classes of this type */
@@ -1729,7 +1732,7 @@ object Types {
17291732
}
17301733

17311734
private def withSig(sig: Signature)(implicit ctx: Context): NamedType =
1732-
TermRef.withSig(prefix, name.asTermName, sig)
1735+
TermRef(prefix, name.asTermName.withSig(sig))
17331736

17341737
protected def loadDenot(implicit ctx: Context): Denotation = {
17351738
val d = asMemberOf(prefix, allowPrivate = true)
@@ -1985,7 +1988,7 @@ object Types {
19851988
val candidate =
19861989
if (newSig ne curSig) {
19871990
core.println(i"sig change at ${ctx.phase} for $this, pre = $prefix, sig: $curSig --> $newSig")
1988-
TermRef.withSig(prefix, name, newSig)
1991+
TermRef(prefix, name.withSig(newSig))
19891992
}
19901993
else TermRef(prefix, designator)
19911994
fixDenot(candidate, prefix)
@@ -2050,41 +2053,30 @@ object Types {
20502053
* (2) the designator of the TermRef is either the symbol or its name & unforced signature.
20512054
*/
20522055
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)
2056+
if ((prefix eq NoPrefix) || sym.isReferencedSymbolically) apply(prefix, sym)
2057+
else apply(prefix, name.withSig(sym.signature)).withSym(sym)
20572058

20582059
/** Create term ref to given initial denotation, taking the signature
20592060
* from the denotation if it is completed, or creating a term ref without
20602061
* signature, if denotation is not yet completed.
20612062
*/
2062-
def apply(prefix: Type, designator: TermName, denot: Denotation)(implicit ctx: Context): TermRef = {
2063+
def apply(prefix: Type, name: TermName, denot: Denotation)(implicit ctx: Context): TermRef = {
20632064
if ((prefix eq NoPrefix) || denot.symbol.isReferencedSymbolically)
20642065
apply(prefix, denot.symbol.asTerm)
20652066
else denot match {
2066-
case denot: SymDenotation if denot.isCompleted => withSig(prefix, designator, denot.signature)
2067-
case _ => apply(prefix, designator)
2067+
case denot: SymDenotation if denot.isCompleted =>
2068+
apply(prefix, name.withSig(denot.signature))
2069+
case _ =>
2070+
apply(prefix, name)
20682071
}
20692072
} withDenot denot
20702073

2071-
/** Create a term ref to given symbol, taking the signature from the symbol
2072-
* (which must be completed).
2073-
*/
2074-
def withSig(prefix: Type, sym: TermSymbol)(implicit ctx: Context): TermRef =
2075-
if ((prefix eq NoPrefix) || sym.isReferencedSymbolically) apply(prefix, sym)
2076-
else withSig(prefix, sym.name, sym.signature).withSym(sym)
2077-
2078-
/** Create a term ref with given prefix, name and signature */
2079-
def withSig(prefix: Type, name: TermName, sig: Signature)(implicit ctx: Context): TermRef =
2080-
apply(prefix, name.withSig(sig))
2081-
20822074
/** Create a term ref with given prefix, name, signature, and initial denotation */
20832075
def withSigAndDenot(prefix: Type, name: TermName, sig: Signature, denot: Denotation)(implicit ctx: Context): TermRef = {
20842076
if ((prefix eq NoPrefix) || denot.symbol.isReferencedSymbolically)
20852077
apply(prefix, denot.symbol.asTerm)
20862078
else
2087-
withSig(prefix, name, sig)
2079+
apply(prefix, name.withSig(sig))
20882080
} withDenot denot
20892081
}
20902082

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ class TreeUnpickler(reader: TastyReader, nameAtRef: NameRef => TermName, posUnpi
710710
val cls = ctx.owner.asClass
711711
val assumedSelfType =
712712
if (cls.is(Module) && cls.owner.isClass)
713-
TermRef.withSig(cls.owner.thisType, cls.name.sourceModuleName, Signature.NotAMethod)
713+
TermRef(cls.owner.thisType, cls.name.sourceModuleName.withSig(Signature.NotAMethod))
714714
else NoType
715715
cls.info = new TempClassInfo(cls.owner.thisType, cls, cls.unforcedDecls, assumedSelfType)
716716
val localDummy = symbolAtCurrent()
@@ -871,7 +871,7 @@ class TreeUnpickler(reader: TastyReader, nameAtRef: NameRef => TermName, posUnpi
871871
untpd.Ident(readName().toTypeName).withType(readType())
872872
case SELECT =>
873873
def readRest(name: Name, sig: Signature) =
874-
completeSelect(name, TermRef.withSig(_, name.asTermName, sig))
874+
completeSelect(name, TermRef(_, name.asTermName.withSig(sig)))
875875
readName() match {
876876
case SignedName(name, sig) => readRest(name, sig)
877877
case name => readRest(name, Signature.NotAMethod)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -704,8 +704,8 @@ class Scala2Unpickler(bytes: Array[Byte], classRoot: ClassDenotation, moduleClas
704704
case SINGLEtpe =>
705705
val pre = readTypeRef()
706706
val sym = readDisambiguatedSymbolRef(_.info.isParameterless)
707-
if (isLocal(sym) || (pre == NoPrefix)) pre select sym
708-
else TermRef.withSig(pre, sym.name.asTermName, Signature.NotAMethod) // !!! should become redundant
707+
if (isLocal(sym) || (pre eq NoPrefix)) pre select sym
708+
else TermRef(pre, sym.name.asTermName.withSig(Signature.NotAMethod)) // !!! should become redundant
709709
case SUPERtpe =>
710710
val thistpe = readTypeRef()
711711
val supertpe = readTypeRef()

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package transform
44
import core._
55
import ast.Trees._
66
import Contexts._, Types._, Symbols._, Flags._, TypeUtils._, DenotTransformers._, StdNames._
7-
import Decorators._
7+
import Decorators._, NameOps._
88
import config.Printers.typr
99

1010
/** For all parameter accessors
@@ -96,7 +96,7 @@ class ParamForwarding(thisTransformer: DenotTransformer) {
9696
// It could be a param forwarder; adapt the signature
9797
val newSig = tpe.prefix.memberInfo(tpe.symbol).signature
9898
if (newSig == Signature.NotAMethod) tree
99-
else tree.withType(TermRef.withSig(tpe.prefix, tpe.name, newSig)).asInstanceOf[T]
99+
else tree.withType(TermRef(tpe.prefix, tpe.name.withSig(newSig))).asInstanceOf[T]
100100
case _ =>
101101
tree
102102
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisTran
8686
private def fixSignature[T <: Tree](tree: T)(implicit ctx: Context): T = tree.tpe match {
8787
case tpe: TermRef if tpe.signature.isUnderDefined =>
8888
typr.println(i"fixing $tree with type ${tree.tpe.widen.toString} with sig ${tpe.signature} to ${tpe.widen.signature}")
89-
tree.withType(TermRef.withSig(tpe.prefix, tpe.name, tpe.widen.signature)).asInstanceOf[T]
89+
tree.withType(TermRef(tpe.prefix, tpe.name.withSig(tpe.widen.signature))).asInstanceOf[T]
9090
case _ => tree
9191
}
9292

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ trait ImplicitRunInfo { self: RunInfo =>
428428
def addRef(companion: TermRef): Unit = {
429429
val compSym = companion.symbol
430430
if (compSym is Package)
431-
addRef(TermRef.withSig(companion, nme.PACKAGE, Signature.NotAMethod))
431+
addRef(TermRef(companion, nme.PACKAGE.withSig(Signature.NotAMethod)))
432432
else if (compSym.exists)
433433
comps += companion.asSeenFrom(pre, compSym.owner).asInstanceOf[TermRef]
434434
}

0 commit comments

Comments
 (0)