Skip to content

Commit bf91347

Browse files
committed
Refactor signature caching
Avoid duplication between MethodicType and NamedType
1 parent 9753483 commit bf91347

File tree

1 file changed

+16
-25
lines changed

1 file changed

+16
-25
lines changed

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

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,7 +1580,7 @@ object Types {
15801580

15811581
// --- NamedTypes ------------------------------------------------------------------
15821582

1583-
abstract class NamedType extends CachedProxyType with ValueType { self =>
1583+
abstract class NamedType extends CachedProxyType with ValueType with SignatureCachingType { self =>
15841584

15851585
type ThisType >: this.type <: NamedType
15861586
type ThisName <: Name
@@ -1592,8 +1592,6 @@ object Types {
15921592
assert(prefix.isValueType || (prefix eq NoPrefix), s"invalid prefix $prefix")
15931593

15941594
private[this] var myName: Name = null
1595-
private[this] var mySig: Signature = null
1596-
private[this] var mySigRunId: Int = NoRunId
15971595
private[this] var lastDenotation: Denotation = null
15981596
private[this] var lastSymbol: Symbol = null
15991597
private[this] var checkedPeriod: Period = Nowhere
@@ -1623,15 +1621,7 @@ object Types {
16231621
/** The signature of the last known denotation, or if there is none, the
16241622
* signature of the symbol
16251623
*/
1626-
final override def signature(implicit ctx: Context): Signature = {
1627-
if (ctx.runId != mySigRunId) {
1628-
mySig = computeSignature
1629-
if (!mySig.isUnderDefined) mySigRunId = ctx.runId
1630-
}
1631-
mySig
1632-
}
1633-
1634-
def computeSignature(implicit ctx: Context): Signature = {
1624+
protected def computeSignature(implicit ctx: Context): Signature = {
16351625
val lastd = lastDenotation
16361626
if (lastd != null) lastd.signature
16371627
else symbol.asSeenFrom(prefix).signature
@@ -1642,7 +1632,7 @@ object Types {
16421632
* Otherwise NotAMethod.
16431633
*/
16441634
private def currentSignature(implicit ctx: Context): Signature =
1645-
if (ctx.runId == mySigRunId) mySig
1635+
if (ctx.runId == mySignatureRunId) mySignature
16461636
else {
16471637
val lastd = lastDenotation
16481638
if (lastd != null) lastd.signature
@@ -2589,13 +2579,22 @@ object Types {
25892579
// and therefore two different poly types would never be equal.
25902580

25912581
/** A trait that mixes in functionality for signature caching */
2592-
trait MethodicType extends TermType {
2593-
2594-
private[this] var mySignature: Signature = _
2595-
private[this] var mySignatureRunId: Int = NoRunId
2582+
trait SignatureCachingType extends TermType {
2583+
protected[this] var mySignature: Signature = _
2584+
protected[this] var mySignatureRunId: Int = NoRunId
25962585

25972586
protected def computeSignature(implicit ctx: Context): Signature
25982587

2588+
final override def signature(implicit ctx: Context): Signature = {
2589+
if (ctx.runId != mySignatureRunId) {
2590+
mySignature = computeSignature
2591+
if (!mySignature.isUnderDefined) mySignatureRunId = ctx.runId
2592+
}
2593+
mySignature
2594+
}
2595+
}
2596+
2597+
trait MethodicType extends SignatureCachingType {
25992598
protected def resultSignature(implicit ctx: Context) = try resultType match {
26002599
case rtp: MethodicType => rtp.signature
26012600
case tp =>
@@ -2607,14 +2606,6 @@ object Types {
26072606
println(i"failure while taking result signature of $this: $resultType")
26082607
throw ex
26092608
}
2610-
2611-
final override def signature(implicit ctx: Context): Signature = {
2612-
if (ctx.runId != mySignatureRunId) {
2613-
mySignature = computeSignature
2614-
if (!mySignature.isUnderDefined) mySignatureRunId = ctx.runId
2615-
}
2616-
mySignature
2617-
}
26182609
}
26192610

26202611
/** A by-name parameter type of the form `=> T`, or the type of a method with no parameter list. */

0 commit comments

Comments
 (0)