Skip to content

Commit 7e74341

Browse files
committed
fix baseType caching during capture checking
- disable caching during Setup - never cache capturing types
1 parent ac5d92e commit 7e74341

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ extension (tp: Type)
166166
case CapturingType(_, _) => true
167167
case _ => false
168168

169+
def isEventuallyCapturingType(using Context): Boolean =
170+
tp match
171+
case EventuallyCapturingType(_, _) => true
172+
case _ => false
173+
169174
/** Is type known to be always pure by its class structure,
170175
* so that adding a capture set to it would not make sense?
171176
*/

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -856,8 +856,7 @@ class CheckCaptures extends Recheck, SymTransformer:
856856
traverseChildren(t)
857857

858858
override def checkUnit(unit: CompilationUnit)(using Context): Unit =
859-
Setup(preRecheckPhase, thisPhase, recheckDef)
860-
.traverse(ctx.compilationUnit.tpdTree)
859+
Setup(preRecheckPhase, thisPhase, recheckDef)(ctx.compilationUnit.tpdTree)
861860
//println(i"SETUP:\n${Recheck.addRecheckedTypes.transform(ctx.compilationUnit.tpdTree)}")
862861
withCaptureSetsExplained {
863862
super.checkUnit(unit)

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import ast.tpd
1111
import transform.Recheck.*
1212
import CaptureSet.IdentityCaptRefMap
1313
import Synthetics.isExcluded
14+
import util.Property
1415

1516
/** A tree traverser that prepares a compilation unit to be capture checked.
1617
* It does the following:
@@ -484,4 +485,14 @@ extends tpd.TreeTraverser:
484485
capt.println(i"update info of ${tree.symbol} from $info to $newInfo")
485486
case _ =>
486487
end traverse
488+
489+
def apply(tree: Tree)(using Context): Unit =
490+
traverse(tree)(using ctx.withProperty(Setup.IsDuringSetupKey, Some(())))
487491
end Setup
492+
493+
object Setup:
494+
val IsDuringSetupKey = new Property.Key[Unit]
495+
496+
def isDuringSetup(using Context): Boolean =
497+
ctx.property(IsDuringSetupKey).isDefined
498+

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import config.Config
2424
import reporting._
2525
import collection.mutable
2626
import transform.TypeUtils._
27-
import cc.{CapturingType, derivedCapturingType}
27+
import cc.{CapturingType, derivedCapturingType, Setup, EventuallyCapturingType, isEventuallyCapturingType}
2828

2929
import scala.annotation.internal.sharable
3030

@@ -2140,14 +2140,15 @@ object SymDenotations {
21402140
final def baseTypeOf(tp: Type)(using Context): Type = {
21412141
val btrCache = baseTypeCache
21422142
def inCache(tp: Type) = tp match
2143+
case EventuallyCapturingType(_, _) => false
21432144
case tp: CachedType => btrCache.contains(tp)
21442145
case _ => false
21452146
def record(tp: CachedType, baseTp: Type) = {
21462147
if (Stats.monitored) {
21472148
Stats.record("basetype cache entries")
21482149
if (!baseTp.exists) Stats.record("basetype cache NoTypes")
21492150
}
2150-
if (!tp.isProvisional)
2151+
if (!tp.isProvisional && !tp.isEventuallyCapturingType && !Setup.isDuringSetup)
21512152
btrCache(tp) = baseTp
21522153
else
21532154
btrCache.remove(tp) // Remove any potential sentinel value
@@ -2161,8 +2162,9 @@ object SymDenotations {
21612162
def recur(tp: Type): Type = try {
21622163
tp match {
21632164
case tp: CachedType =>
2164-
val baseTp = btrCache.lookup(tp)
2165-
if (baseTp != null) return ensureAcyclic(baseTp)
2165+
val baseTp: Type | Null = btrCache.lookup(tp)
2166+
if (baseTp != null)
2167+
return ensureAcyclic(baseTp)
21662168
case _ =>
21672169
}
21682170
if (Stats.monitored) {

0 commit comments

Comments
 (0)