Skip to content

Commit 09f00bb

Browse files
committed
Introduce isCaptureChecking test
1 parent 0fbd6b8 commit 09f00bb

File tree

9 files changed

+31
-20
lines changed

9 files changed

+31
-20
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,24 @@ object ccConfig:
3030
*/
3131
private[cc] val constrainRootsWhenMapping = true
3232

33-
val oldRefiningRoots = false
33+
/** Use old scheme for refining vars, which should be no longer necessary
34+
*/
35+
val oldRefiningVars = false
3436
end ccConfig
3537

3638
def allowUniversalInBoxed(using Context) =
3739
Feature.sourceVersion.isAtLeast(SourceVersion.`3.3`)
3840

41+
/** Are we at checkCaptures phase? */
42+
def isCaptureChecking(using Context): Boolean =
43+
ctx.phaseId == Phases.checkCapturesPhase.id
44+
45+
/** Are we at checkCaptures or Setup phase? */
46+
def isCaptureCheckingOrSetup(using Context): Boolean =
47+
val ccId = Phases.checkCapturesPhase.id
48+
val ctxId = ctx.phaseId
49+
ctxId == ccId || ctxId == ccId - 1
50+
3951
/** A dependent function type with given arguments and result type
4052
* TODO Move somewhere else where we treat all function type related ops together.
4153
*/

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ object CapturingType:
5656
/** Decompose `tp` as a capturing type without taking IgnoreCaptures into account */
5757
def decomposeCapturingType(tp: Type)(using Context): Option[(Type, CaptureSet)] = tp match
5858
case AnnotatedType(parent, ann: CaptureAnnotation)
59-
if ctx.phaseId <= Phases.checkCapturesPhase.id =>
59+
if isCaptureCheckingOrSetup =>
6060
Some((parent, ann.refs))
6161
case AnnotatedType(parent, ann)
62-
if ann.symbol == defn.RetainsAnnot && ctx.phase == Phases.checkCapturesPhase =>
62+
if ann.symbol == defn.RetainsAnnot && isCaptureChecking =>
6363
// There are some circumstances where we cannot map annotated types
6464
// with retains annotations to capturing types, so this second recognizer
6565
// path still has to exist. One example is when checking capture sets

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import Symbols.*, SymDenotations.*, Contexts.*, Flags.*, Types.*, Decorators.*
77
import StdNames.nme
88
import Names.Name
99
import NameKinds.DefaultGetterName
10-
import Phases.checkCapturesPhase
1110
import config.Printers.capt
1211

1312
/** Classification and transformation methods for function methods and
@@ -75,7 +74,7 @@ object Synthetics:
7574
*/
7675
def addCaptureDeps(info: Type): Type = info match
7776
case info: MethodType =>
78-
val trackedParams = info.paramRefs.filter(atPhase(checkCapturesPhase)(_.isTracked))
77+
val trackedParams = info.paramRefs.filter(atPhase(Phases.checkCapturesPhase)(_.isTracked))
7978
def augmentResult(tp: Type): Type = tp match
8079
case tp: MethodOrPoly =>
8180
tp.derivedLambdaType(resType = augmentResult(tp.resType))

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
657657
def compareRefined: Boolean =
658658
val tp1w = tp1.widen
659659

660-
if ctx.phase == Phases.checkCapturesPhase || ctx.phase == Phases.checkCapturesPhase.prev then
660+
if isCaptureCheckingOrSetup then
661661

662662
// A relaxed version of subtyping for dependent functions where method types
663663
// are treated as contravariant.
@@ -1013,10 +1013,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
10131013
def tp1widened =
10141014
val tp1w = tp1.underlying.widenExpr
10151015
tp1 match
1016-
case tp1: CaptureRef
1017-
if (ctx.phase == Phases.checkCapturesPhase || ctx.phase == Phases.checkCapturesPhase.prev)
1018-
&& tp1.isTracked
1019-
=>
1016+
case tp1: CaptureRef if isCaptureCheckingOrSetup && tp1.isTracked =>
10201017
CapturingType(tp1w.stripCapturing, tp1.singletonCaptureSet)
10211018
case _ =>
10221019
tp1w
@@ -2117,7 +2114,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
21172114
ExprType(info1.resType)
21182115
case info1 => info1
21192116

2120-
if ccConfig.oldRefiningRoots && ctx.phase == Phases.checkCapturesPhase || ctx.phase == Phases.checkCapturesPhase.prev then
2117+
if ccConfig.oldRefiningVars && isCaptureCheckingOrSetup then
21212118
// When comparing against a RefiningVar refinement, map the
21222119
// localRoot of the corresponding class in `tp1` to the owner of the
21232120
// refining capture set.
@@ -2262,7 +2259,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
22622259
val paramsMatch =
22632260
if precise then
22642261
isSameTypeWhenFrozen(formal1, formal2a)
2265-
else if ctx.phase == Phases.checkCapturesPhase || ctx.phase == Phases.checkCapturesPhase.prev then
2262+
else if isCaptureCheckingOrSetup then
22662263
// allow to constrain capture set variables
22672264
isSubType(formal2a, formal1)
22682265
else

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import config.Printers.{core, typr, matchTypes}
3636
import reporting.{trace, Message}
3737
import java.lang.ref.WeakReference
3838
import compiletime.uninitialized
39-
import cc.{CapturingType, CaptureSet, derivedCapturingType, isBoxedCapturing, RetainingType, CaptureRoot}
39+
import cc.{CapturingType, CaptureSet, derivedCapturingType, isBoxedCapturing, RetainingType, CaptureRoot, isCaptureChecking}
4040
import CaptureSet.{CompareResult, IdempotentCaptRefMap, IdentityCaptRefMap}
4141

4242
import scala.annotation.internal.sharable
@@ -2211,7 +2211,7 @@ object Types {
22112211
else
22122212
myCaptureSet = CaptureSet.Pending
22132213
val computed = CaptureSet.ofInfo(this)
2214-
if ctx.phase != Phases.checkCapturesPhase || underlying.isProvisional then
2214+
if !isCaptureChecking || underlying.isProvisional then
22152215
myCaptureSet = null
22162216
else
22172217
myCaptureSet = computed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import NameKinds.DefaultGetterName
88
import NullOpsDecorator._
99
import collection.immutable.BitSet
1010
import scala.annotation.tailrec
11+
import cc.isCaptureChecking
1112

1213
/** A module that can produce a kind of iterator (`Cursor`),
1314
* which yields all pairs of overriding/overridden symbols
@@ -31,7 +32,7 @@ object OverridingPairs:
3132
*/
3233
protected def exclude(sym: Symbol): Boolean =
3334
!sym.memberCanMatchInheritedSymbols
34-
|| ctx.phase == Phases.checkCapturesPhase && sym.is(Recheck.ResetPrivate)
35+
|| isCaptureChecking && sym.is(Recheck.ResetPrivate)
3536

3637
/** The parents of base that are checked when deciding whether an overriding
3738
* pair has already been treated in a parent class.

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import config.Feature.sourceVersion
3939
import config.SourceVersion._
4040
import printing.Formatting.hlAsKeyword
4141
import transform.TypeUtils.*
42+
import cc.isCaptureChecking
4243

4344
import collection.mutable
4445
import reporting._
@@ -67,7 +68,7 @@ object Checking {
6768
*/
6869
def checkBounds(args: List[tpd.Tree], boundss: List[TypeBounds],
6970
instantiate: (Type, List[Type]) => Type, app: Type = NoType, tpt: Tree = EmptyTree)(using Context): Unit =
70-
if ctx.phase != Phases.checkCapturesPhase then
71+
if !isCaptureChecking then
7172
args.lazyZip(boundss).foreach { (arg, bound) =>
7273
if !bound.isLambdaSub && !arg.tpe.hasSimpleKind then
7374
errorTree(arg,
@@ -152,7 +153,7 @@ object Checking {
152153
// if we attempt to check bounds of F-bounded mutually recursive Java interfaces.
153154
// Do check all bounds in Scala units and those bounds in Java units that
154155
// occur in applications of Scala type constructors.
155-
&& !(ctx.phase == Phases.checkCapturesPhase && !tycon.typeSymbol.is(CaptureChecked))
156+
&& !isCaptureChecking || tycon.typeSymbol.is(CaptureChecked)
156157
// Don't check bounds when capture checking type constructors that were not
157158
// themselves capture checked. Since the type constructor could not foresee
158159
// possible capture sets, it's better to be lenient for backwards compatibility.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import Implicits.{hasExtMethod, Candidate}
1414
import java.util.{Timer, TimerTask}
1515
import collection.mutable
1616
import scala.util.control.NonFatal
17+
import cc.isCaptureChecking
1718

1819
/** This trait defines the method `importSuggestionAddendum` that adds an addendum
1920
* to error messages suggesting additional imports.
@@ -319,7 +320,7 @@ trait ImportSuggestions:
319320
* If there's nothing to suggest, an empty string is returned.
320321
*/
321322
override def importSuggestionAddendum(pt: Type)(using Context): String =
322-
if ctx.phase == Phases.checkCapturesPhase then
323+
if isCaptureChecking then
323324
return "" // it's too late then to look for implicits
324325
val (fullMatches, headMatches) =
325326
importSuggestions(pt)(using ctx.fresh.setExploreTyperState())

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import config.SourceVersion.{`3.0`, `future`}
2020
import config.Printers.refcheck
2121
import reporting._
2222
import Constants.Constant
23-
import cc.{mapRoots, localRoot}
23+
import cc.{mapRoots, localRoot, isCaptureChecking}
2424

2525
object RefChecks {
2626
import tpd._
@@ -105,7 +105,7 @@ object RefChecks {
105105

106106
def checkSelfConforms(other: ClassSymbol) =
107107
var otherSelf = other.declaredSelfTypeAsSeenFrom(cls.thisType)
108-
if ctx.phase == Phases.checkCapturesPhase then
108+
if isCaptureChecking then
109109
otherSelf = mapRoots(other.localRoot.termRef, cls.localRoot.termRef)(otherSelf)
110110
.showing(i"map self $otherSelf = $result", capt)
111111
if otherSelf.exists then

0 commit comments

Comments
 (0)