Skip to content

Commit 2b3007a

Browse files
committed
Cleanup SubstParamsMap
1 parent 9fa253d commit 2b3007a

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

compiler/src/dotty/tools/dotc/cc/CheckCaptures.scala

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ object CheckCaptures:
7777
final class SubstParamsMap(from: BindingType, to: List[Type])(using Context)
7878
extends ApproximatingTypeMap, IdempotentCaptRefMap:
7979
/** This SubstParamsMap is exact if `to` only contains `CaptureRef`s. */
80-
private val isExactSubstitution: Boolean = to.forall(_.isInstanceOf[CaptureRef])
80+
private val isExactSubstitution: Boolean = to.forall(_.isTrackableRef)
8181

8282
/** As long as this substitution is exact, there is no need to create `Range`s when mapping invariant positions. */
8383
override protected def needsRangeIfInvariant(refs: CaptureSet): Boolean = !isExactSubstitution
@@ -136,7 +136,7 @@ object CheckCaptures:
136136
for elem <- retainedElems(ann) do
137137
elem.tpe match
138138
case ref: CaptureRef =>
139-
if !ref.canBeTracked then
139+
if !ref.isTrackableRef then
140140
report.error(em"$elem cannot be tracked since it is not a parameter or local value", elem.srcPos)
141141
case tpe =>
142142
report.error(em"$elem: $tpe is not a legal element of a capture set", elem.srcPos)
@@ -470,10 +470,6 @@ class CheckCaptures extends Recheck, SymTransformer:
470470
case x :: xs1 => xs1.isEmpty || !xs1.contains(x) && isDistinct(xs1)
471471
case Nil => true
472472

473-
private def isTrackable(tp: Type)(using Context) = tp match
474-
case tp: CaptureRef => tp.canBeTracked
475-
case _ => false
476-
477473
/** Handle an application of method `sym` with type `mt` to arguments of types `argTypes`.
478474
* This means:
479475
* - Instantiate result type with actual arguments
@@ -490,7 +486,7 @@ class CheckCaptures extends Recheck, SymTransformer:
490486
val ownType =
491487
if !mt.isResultDependent then
492488
mt.resType
493-
else if argTypes.forall(isTrackable) && isDistinct(argTypes) then
489+
else if argTypes.forall(_.isTrackableRef) && isDistinct(argTypes) then
494490
SubstParamsBiMap(mt, argTypes)(mt.resType)
495491
else
496492
SubstParamsMap(mt, argTypes)(mt.resType)

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,11 @@ object Types {
478478
*/
479479
def isDeclaredVarianceLambda: Boolean = false
480480

481+
/** Is this type a CaptureRef that can be tracked?
482+
* This is true for all ThisTypes or ParamRefs but only for some NamedTypes.
483+
*/
484+
def isTrackableRef(using Context): Boolean = false
485+
481486
/** Does this type contain wildcard types? */
482487
final def containsWildcardTypes(using Context) =
483488
existsPart(_.isInstanceOf[WildcardType], StopAt.Static, forceLazy = false)
@@ -2149,15 +2154,10 @@ object Types {
21492154
private var myCaptureSetRunId: Int = NoRunId
21502155
private var mySingletonCaptureSet: CaptureSet.Const | Null = null
21512156

2152-
/** Can the reference be tracked? This is true for all ThisTypes or ParamRefs
2153-
* but only for some NamedTypes.
2154-
*/
2155-
def canBeTracked(using Context): Boolean
2156-
21572157
/** Is the reference tracked? This is true if it can be tracked and the capture
21582158
* set of the underlying type is not always empty.
21592159
*/
2160-
final def isTracked(using Context): Boolean = canBeTracked && !captureSetOfInfo.isAlwaysEmpty
2160+
final def isTracked(using Context): Boolean = isTrackableRef && !captureSetOfInfo.isAlwaysEmpty
21612161

21622162
/** Is this reference the root capability `cap` ? */
21632163
def isRootCapability(using Context): Boolean = false
@@ -2190,7 +2190,7 @@ object Types {
21902190

21912191
override def captureSet(using Context): CaptureSet =
21922192
val cs = captureSetOfInfo
2193-
if canBeTracked && !cs.isAlwaysEmpty then singletonCaptureSet else cs
2193+
if isTrackableRef && !cs.isAlwaysEmpty then singletonCaptureSet else cs
21942194
end CaptureRef
21952195

21962196
/** A trait for types that bind other types that refer to them.
@@ -2887,7 +2887,7 @@ object Types {
28872887
* They are subsumed in the capture sets of the enclosing class.
28882888
* TODO: ^^^ What about call-by-name?
28892889
*/
2890-
def canBeTracked(using Context) =
2890+
override def isTrackableRef(using Context) =
28912891
((prefix eq NoPrefix)
28922892
|| symbol.is(ParamAccessor) && (prefix eq symbol.owner.thisType)
28932893
|| isRootCapability
@@ -2897,7 +2897,7 @@ object Types {
28972897
name == nme.CAPTURE_ROOT && symbol == defn.captureRoot
28982898

28992899
override def normalizedRef(using Context): CaptureRef =
2900-
if canBeTracked then symbol.termRef else this
2900+
if isTrackableRef then symbol.termRef else this
29012901
}
29022902

29032903
abstract case class TypeRef(override val prefix: Type,
@@ -3050,7 +3050,7 @@ object Types {
30503050
// can happen in IDE if `cls` is stale
30513051
}
30523052

3053-
def canBeTracked(using Context) = true
3053+
override def isTrackableRef(using Context) = true
30543054

30553055
override def computeHash(bs: Binders): Int = doHash(bs, tref)
30563056

@@ -4661,9 +4661,9 @@ object Types {
46614661
*/
46624662
abstract case class TermParamRef(binder: TermLambda, paramNum: Int) extends ParamRef, CaptureRef {
46634663
type BT = TermLambda
4664-
def canBeTracked(using Context) = true
46654664
def kindString: String = "Term"
46664665
def copyBoundType(bt: BT): Type = bt.paramRefs(paramNum)
4666+
override def isTrackableRef(using Context) = true
46674667
}
46684668

46694669
private final class TermParamRefImpl(binder: TermLambda, paramNum: Int) extends TermParamRef(binder, paramNum)
@@ -5728,11 +5728,11 @@ object Types {
57285728

57295729
/** A restriction of this map to a function on tracked CaptureRefs */
57305730
def forward(ref: CaptureRef): CaptureRef = this(ref) match
5731-
case result: CaptureRef if result.canBeTracked => result
5731+
case result: CaptureRef if result.isTrackableRef => result
57325732

57335733
/** A restriction of the inverse to a function on tracked CaptureRefs */
57345734
def backward(ref: CaptureRef): CaptureRef = inverse(ref) match
5735-
case result: CaptureRef if result.canBeTracked => result
5735+
case result: CaptureRef if result.isTrackableRef => result
57365736
end BiTypeMap
57375737

57385738
abstract class TypeMap(implicit protected var mapCtx: Context)

0 commit comments

Comments
 (0)