Skip to content

Commit 5b587b5

Browse files
committed
Refactor CleanupRetains to handle retains annotations based on CC feature status
1 parent e8fb67b commit 5b587b5

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,8 @@ object Capabilities:
898898
case t @ AnnotatedType(parent, ann) =>
899899
val parent1 = this(parent)
900900
if ann.symbol.isRetains && ann.tree.toCaptureSet.containsCap then
901+
// TODO: this can cause infinite recursion in some cases during printing
902+
// scalac -Xprint:all tests/pos/i23885/S_1.scala tests/pos/i23885/S_2.scala
901903
this(CapturingType(parent1, ann.tree.toCaptureSet))
902904
else
903905
t.derivedAnnotatedType(parent1, ann)

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import Annotations.Annotation
1414
import CaptureSet.VarState
1515
import Capabilities.*
1616
import StdNames.nme
17+
import config.Feature
1718

1819
/** Attachment key for capturing type trees */
1920
private val Captures: Key[CaptureSet] = Key()
@@ -634,13 +635,18 @@ extension (tp: AnnotatedType)
634635
case ann: CaptureAnnotation => ann.boxed
635636
case _ => false
636637

637-
/** Drop retains annotations in the type. */
638+
/** Drop retains annotations in the inferred type if CC is not enabled
639+
* or transform them into RetainingTypes if CC is enabled.
640+
*/
638641
class CleanupRetains(using Context) extends TypeMap:
639-
def apply(tp: Type): Type =
640-
tp match
641-
case AnnotatedType(tp, annot) if annot.symbol == defn.RetainsAnnot || annot.symbol == defn.RetainsByNameAnnot =>
642-
RetainingType(tp, defn.NothingType, byName = annot.symbol == defn.RetainsByNameAnnot)
643-
case _ => mapOver(tp)
642+
def apply(tp: Type): Type = tp match
643+
case AnnotatedType(parent, annot) if annot.symbol.isRetainsLike =>
644+
if Feature.ccEnabled then
645+
if annot.symbol == defn.RetainsAnnot || annot.symbol == defn.RetainsByNameAnnot then
646+
RetainingType(parent, defn.NothingType, byName = annot.symbol == defn.RetainsByNameAnnot)
647+
else mapOver(tp)
648+
else apply(parent)
649+
case _ => mapOver(tp)
644650

645651
/** A base class for extractors that match annotated types with a specific
646652
* Capability annotation.

0 commit comments

Comments
 (0)