Skip to content

Commit d223e79

Browse files
committed
Drop RetainingType
We don't need the constructor anymore since `RetainingAnnotation` has equivalent functionality. And we don't need the deconstructor since there is already `CapturingOrRetainsType`.
1 parent effba5c commit d223e79

File tree

8 files changed

+34
-66
lines changed

8 files changed

+34
-66
lines changed

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -492,8 +492,7 @@ extension (cls: ClassSymbol)
492492
defn.pureBaseClasses.contains(bc)
493493
|| bc.is(CaptureChecked)
494494
&& bc.givenSelfType.dealiasKeepAnnots.match
495-
case CapturingType(_, refs) => refs.isAlwaysEmpty
496-
case RetainingType(_, refs) => refs.retainedElements.isEmpty
495+
case CapturingOrRetainsType(_, refs) => refs.isAlwaysEmpty
497496
case selfType =>
498497
isCaptureChecking // At Setup we have not processed self types yet, so
499498
// unless a self type is explicitly given, we can't tell
@@ -653,17 +652,16 @@ class PathSelectionProto(val select: Select, val pt: Type) extends typer.ProtoTy
653652
def selector(using Context): Symbol = select.symbol
654653

655654
/** Drop retains annotations in the inferred type if CC is not enabled
656-
* or transform them into RetainingTypes with Nothing as argument if CC is enabled
657-
* (we need to do that to keep by-name status).
655+
* or transform them into retains annotations with Nothing (i.e. empty set) as
656+
* argument if CC is enabled (we need to do that to keep by-name status).
658657
*/
659658
class CleanupRetains(using Context) extends TypeMap:
660659
def apply(tp: Type): Type = tp match
661-
case AnnotatedType(parent, annot: RetainingAnnotation) =>
660+
case tp @ AnnotatedType(parent, annot: RetainingAnnotation) =>
662661
if Feature.ccEnabled then
663-
if annot.symbol == defn.RetainsAnnot || annot.symbol == defn.RetainsByNameAnnot then
664-
RetainingType(parent, defn.NothingType, byName = annot.symbol == defn.RetainsByNameAnnot)
665-
else mapOver(tp)
666-
else apply(parent)
662+
if annot.symbol == defn.RetainsCapAnnot then tp
663+
else AnnotatedType(this(parent), RetainingAnnotation(annot.symbol.asClass, defn.NothingType))
664+
else this(parent)
667665
case _ => mapOver(tp)
668666

669667
/** A base class for extractors that match annotated types with a specific

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import config.Feature
1010
/** A class for annotations @retains, @retainsByName and @retainsCap */
1111
class RetainingAnnotation(tpe: Type) extends CompactAnnotation(tpe):
1212

13+
def this(cls: ClassSymbol, args: Type*)(using Context) = this(cls.typeRef.appliedTo(args.toList))
14+
1315
/** Sanitize @retains arguments to approximate illegal types that could cause a compilation
1416
* time blowup before they are dropped ot detected. This means mapping all all skolems
1517
* (?n: T) to (?n: Any), and mapping all recursive captures that are not on CapSet to `^`.
@@ -20,9 +22,9 @@ class RetainingAnnotation(tpe: Type) extends CompactAnnotation(tpe):
2022
override protected def sanitize(tp: Type)(using Context): Type = tp match
2123
case SkolemType(_) =>
2224
SkolemType(defn.AnyType)
23-
case tp @ AnnotatedType(parent, ann)
24-
if ann.symbol.isRetainsLike && parent.typeSymbol != defn.Caps_CapSet =>
25-
tp.derivedAnnotatedType(parent, ann.derivedClassAnnotation(defn.RetainsCapAnnot))
25+
case tp @ AnnotatedType(parent, ann: RetainingAnnotation)
26+
if parent.typeSymbol != defn.Caps_CapSet && ann.symbol != defn.RetainsCapAnnot =>
27+
AnnotatedType(parent, RetainingAnnotation(defn.RetainsCapAnnot))
2628
case tp @ OrType(tp1, tp2) =>
2729
tp.derivedOrType(sanitize(tp1), sanitize(tp2))
2830
case _ =>

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

Lines changed: 0 additions & 20 deletions
This file was deleted.

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -813,10 +813,8 @@ class Setup extends PreRecheck, SymTransformer, SetupAPI:
813813
*/
814814
private def instanceCanBeImpure(tp: Type)(using Context): Boolean = {
815815
tp.dealiasKeepAnnots match
816-
case CapturingType(_, refs) =>
816+
case CapturingOrRetainsType(_, refs) =>
817817
!refs.isAlwaysEmpty
818-
case RetainingType(parent, refs) =>
819-
!refs.retainedElements.isEmpty
820818
case tp: (TypeRef | AppliedType) =>
821819
val sym = tp.typeSymbol
822820
if sym.isClass
@@ -856,15 +854,10 @@ class Setup extends PreRecheck, SymTransformer, SetupAPI:
856854
needsVariable(tp.tp1) && needsVariable(tp.tp2)
857855
case tp: OrType =>
858856
needsVariable(tp.tp1) || needsVariable(tp.tp2)
859-
case CapturingType(parent, refs) =>
857+
case CapturingOrRetainsType(parent, refs) =>
860858
needsVariable(parent)
861859
&& refs.isConst // if refs is a variable, no need to add another
862860
&& !refs.isUniversal // if refs is {cap}, an added variable would not change anything
863-
case RetainingType(parent, refs) =>
864-
needsVariable(parent)
865-
&& !refs.retainedElements.exists:
866-
case ref: TermRef => ref.isCapRef
867-
case _ => false
868861
case AnnotatedType(parent, _) =>
869862
needsVariable(parent)
870863
case _ =>

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ object Annotations {
3333
def derivedAnnotation(tree: Tree)(using Context): Annotation =
3434
if (tree eq this.tree) this else Annotation(tree)
3535

36-
def derivedClassAnnotation(cls: ClassSymbol)(using Context) =
37-
Annotation(cls, tree.span)
38-
3936
/** All term arguments of this annotation in a single flat list */
4037
def arguments(using Context): List[Tree] = tpd.allTermArguments(tree)
4138

@@ -140,9 +137,6 @@ object Annotations {
140137
override def derivedAnnotation(tree: Tree)(using Context): Annotation =
141138
derivedAnnotation(tree.tpe)
142139

143-
override def derivedClassAnnotation(cls: ClassSymbol)(using Context) =
144-
derivedAnnotation(cls.typeRef)
145-
146140
def derivedAnnotation(tp: Type)(using Context): Annotation =
147141
if tp eq this.tpe then this else CompactAnnotation(tp)
148142

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import Comments.{Comment, docCtx}
1515
import util.Spans.NoSpan
1616
import config.Feature
1717
import Symbols.requiredModuleRef
18-
import cc.{CaptureSet, RetainingType}
18+
import cc.{CaptureSet, RetainingAnnotation}
1919
import ast.tpd.ref
2020

2121
import scala.annotation.tailrec
@@ -122,8 +122,9 @@ class Definitions {
122122
denot.info = TypeAlias(
123123
HKTypeLambda(argParamNames :+ "R".toTypeName, argVariances :+ Covariant)(
124124
tl => List.fill(arity + 1)(TypeBounds.empty),
125-
tl => RetainingType(underlyingClass.typeRef.appliedTo(tl.paramRefs),
126-
captureRoot.termRef)
125+
tl => AnnotatedType(
126+
underlyingClass.typeRef.appliedTo(tl.paramRefs),
127+
RetainingAnnotation(defn.RetainsCapAnnot))
127128
))
128129
else
129130
val cls = denot.asClass.classSymbol
@@ -1347,8 +1348,8 @@ class Definitions {
13471348
*/
13481349
object ByNameFunction:
13491350
def apply(tp: Type)(using Context): Type = tp match
1350-
case tp @ RetainingType(tp1, refSet) if tp.annot.symbol == RetainsByNameAnnot =>
1351-
RetainingType(apply(tp1), refSet)
1351+
case tp @ AnnotatedType(tp1, ann: RetainingAnnotation) if ann.symbol == RetainsByNameAnnot =>
1352+
AnnotatedType(apply(tp1), RetainingAnnotation(defn.RetainsAnnot, ann.argumentTypes*))
13521353
case _ =>
13531354
defn.ContextFunction0.typeRef.appliedTo(tp :: Nil)
13541355
def unapply(tp: Type)(using Context): Option[Type] = tp match

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4227,10 +4227,8 @@ object Types extends TypeUtils {
42274227
val parent1 = mapOver(parent)
42284228
if ann.symbol.isRetainsLike then
42294229
range(
4230-
AnnotatedType(parent1,
4231-
RetainingAnnotation(defn.RetainsAnnot.typeRef.appliedTo(defn.NothingType))),
4232-
AnnotatedType(parent1,
4233-
RetainingAnnotation(defn.RetainsCapAnnot.appliedRef)))
4230+
AnnotatedType(parent1, RetainingAnnotation(defn.RetainsAnnot, defn.NothingType)),
4231+
AnnotatedType(parent1, RetainingAnnotation(defn.RetainsCapAnnot)))
42344232
else
42354233
parent1
42364234
case _ => mapOver(tp)

compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,6 @@ class PlainPrinter(_ctx: Context) extends Printer {
291291
&& (!parent.derivesFromStateful || refs.isReadOnly)
292292
then toText(parent)
293293
else toTextCapturing(parent, refs, boxText)
294-
case tp @ RetainingType(parent, refSet) =>
295-
if Feature.ccEnabledSomewhere then
296-
toTextCapturing(parent, refSet.retainedElementsRaw, "") ~ Str("R").provided(printDebug)
297-
else toText(parent)
298294
case tp: PreviousErrorType if ctx.settings.XprintTypes.value =>
299295
"<error>" // do not print previously reported error message because they may try to print this error type again recursively
300296
case tp: ErrorType =>
@@ -335,12 +331,18 @@ class PlainPrinter(_ctx: Context) extends Printer {
335331
toTextGlobal(tp.resultType)
336332
}
337333
case AnnotatedType(tpe, annot) =>
338-
if defn.SilentAnnots.contains(annot.symbol) && !printDebug then
339-
toText(tpe)
340-
else if annot.isInstanceOf[CaptureAnnotation] then
341-
toTextLocal(tpe) ~ "^" ~ toText(annot)
342-
else
343-
toTextLocal(tpe) ~ " " ~ toText(annot)
334+
annot match
335+
case annot: RetainingAnnotation =>
336+
if Feature.ccEnabledSomewhere then
337+
toTextCapturing(tpe, annot.retainedType.retainedElementsRaw, "")
338+
~ Str("R").provided(printDebug)
339+
else toText(tpe)
340+
case annot: CaptureAnnotation =>
341+
toTextLocal(tpe) ~ "^" ~ toText(annot)
342+
case _ if defn.SilentAnnots.contains(annot.symbol) && !printDebug =>
343+
toText(tpe)
344+
case _ =>
345+
toTextLocal(tpe) ~ " " ~ toText(annot)
344346
case FlexibleType(_, tpe) =>
345347
"(" ~ toText(tpe) ~ ")?"
346348
case tp: TypeVar =>

0 commit comments

Comments
 (0)