Skip to content

Commit fa37358

Browse files
committed
Stabilize boxedType cache
Make sure we cache the correct type.
1 parent 5659aaf commit fa37358

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ case class CaptureAnnotation(refs: CaptureSet, boxed: Boolean)(cls: Symbol) exte
1616
import CaptureAnnotation.*
1717
import tpd.*
1818

19-
var boxedType: Type = NoType
19+
val boxedType = BoxedTypeCache()
2020

2121
override def tree(using Context) =
2222
val elems = refs.elems.toList.map {
@@ -66,3 +66,16 @@ case class CaptureAnnotation(refs: CaptureSet, boxed: Boolean)(cls: Symbol) exte
6666
case _ => false
6767

6868
end CaptureAnnotation
69+
70+
/** A one-element cache for the boxed version of an unboxed capturing type */
71+
class BoxedTypeCache:
72+
private var boxed: Type = compiletime.uninitialized
73+
private var unboxed: Type = NoType
74+
75+
def apply(tp: AnnotatedType)(using Context): Type =
76+
if tp ne unboxed then
77+
unboxed = tp
78+
val CapturingType(parent, refs) = tp: @unchecked
79+
boxed = CapturingType(parent, refs, boxed = true)
80+
boxed
81+
end BoxedTypeCache

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import util.Property.Key
1111
import tpd.*
1212

1313
private val Captures: Key[CaptureSet] = Key()
14-
private val Boxed: Key[Type] = Key()
14+
private val BoxedType: Key[BoxedTypeCache] = Key()
1515

1616
def retainedElems(tree: Tree)(using Context): List[Tree] = tree match
1717
case Apply(_, Typed(SeqLiteral(elems, _), _) :: Nil) => elems
@@ -51,13 +51,12 @@ extension (tp: Type)
5151
if tp.isBoxed || refs.isAlwaysEmpty then tp
5252
else tp.annot match
5353
case ann: CaptureAnnotation =>
54-
if !ann.boxedType.exists then ann.boxedType = boxedTp
55-
ann.boxedType
54+
ann.boxedType(tp)
5655
case ann =>
57-
ann.tree.getAttachment(Boxed) match
58-
case None => ann.tree.putAttachment(Boxed, boxedTp)
56+
ann.tree.getAttachment(BoxedType) match
57+
case None => ann.tree.putAttachment(BoxedType, BoxedTypeCache())
5958
case _ =>
60-
ann.tree.attachment(Boxed)
59+
ann.tree.attachment(BoxedType)(tp)
6160
case _ =>
6261
tp
6362

0 commit comments

Comments
 (0)