Skip to content

Commit 7066ef7

Browse files
committed
Cache boxed versions of CapturingTypes
1 parent f6407e3 commit 7066ef7

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ case class CaptureAnnotation(refs: CaptureSet, boxed: Boolean)(cls: Symbol) exte
1616
import CaptureAnnotation.*
1717
import tpd.*
1818

19+
var boxedType: Type = NoType
20+
1921
override def tree(using Context) =
2022
val elems = refs.elems.toList.map {
2123
case cr: TermRef => ref(cr)

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

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

1313
private val Captures: Key[CaptureSet] = Key()
14+
private val Boxed: Key[Type] = Key()
1415

1516
def retainedElems(tree: Tree)(using Context): List[Tree] = tree match
1617
case Apply(_, Typed(SeqLiteral(elems, _), _) :: Nil) => elems
@@ -40,6 +41,22 @@ extension (tp: Type)
4041
if (parent eq p) && (refs eq r) then tp
4142
else CapturingType(parent, refs, tp.isBoxed)
4243

44+
def boxed(using Context): Type = tp.dealias match
45+
case tp @ CapturingType(parent, refs) =>
46+
def boxedTp = CapturingType(parent, refs, boxed = true)
47+
if tp.isBoxed || refs.isAlwaysEmpty then tp
48+
else tp.annot match
49+
case ann: CaptureAnnotation =>
50+
if !ann.boxedType.exists then ann.boxedType = boxedTp
51+
ann.boxedType
52+
case ann =>
53+
ann.tree.getAttachment(Boxed) match
54+
case None => ann.tree.putAttachment(Boxed, boxedTp)
55+
case _ =>
56+
ann.tree.attachment(Boxed)
57+
case _ =>
58+
tp
59+
4360
/** The boxed capture set of a type */
4461
def boxedCaptured(using Context): CaptureSet =
4562
def getBoxed(tp: Type): CaptureSet = tp match
@@ -105,3 +122,4 @@ extension (tp: AnnotatedType)
105122
def isBoxed(using Context): Boolean = tp.annot match
106123
case ann: CaptureAnnotation => ann.boxed
107124
case _ => false
125+

0 commit comments

Comments
 (0)