@@ -195,12 +195,12 @@ extension (tree: Tree)
195195 * throw IllegalCaptureRef otherwise
196196 */
197197 def toCaptureRefs (using Context ): List [CaptureRef ] = tree match
198- case ReachCapabilityApply (arg) =>
199- arg.toCaptureRefs.map(_.reach)
200- case ReadOnlyCapabilityApply (arg) =>
201- arg.toCaptureRefs.map(_.readOnly)
202- case CapsOfApply (arg) =>
203- arg.toCaptureRefs
198+ // case ReachCapabilityApply(arg) =>
199+ // arg.toCaptureRefs.map(_.reach)
200+ // case ReadOnlyCapabilityApply(arg) =>
201+ // arg.toCaptureRefs.map(_.readOnly)
202+ // case CapsOfApply(arg) =>
203+ // arg.toCaptureRefs
204204 case _ => tree.tpe.dealiasKeepAnnots match
205205 case ref : CaptureRef if ref.isTrackableRef =>
206206 ref :: Nil
@@ -220,28 +220,20 @@ extension (tree: Tree)
220220 val refs =
221221 tree match
222222 case Apply (_ : TypeApply , _) =>
223- CaptureSet (tree.retainedElemsFromType * )
223+ CaptureSet (tree.retainedSet.retainedElements * )
224224 case _ =>
225225 CaptureSet (tree.retainedElems.flatMap(_.toCaptureRefs)* )
226226 // println(s"toCaptureSet: $tree -> $refs")
227227 tree.putAttachment(Captures , refs)
228228 refs
229229
230- def retainedElemsFromType (using Context ): List [CaptureRef ] =
231- def collectRefs (tp : Type ): List [CaptureRef ] = tp match
232- case tp : CaptureRef if tp.isTrackableRef =>
233- tp :: Nil
234- case tp : TypeRef if tp.symbol.isType && tp.derivesFrom(defn.Caps_CapSet ) =>
235- tp :: Nil
236- case OrType (tp1, tp2) =>
237- collectRefs(tp1) ++ collectRefs(tp2)
238- case _ =>
239- Nil
230+ def retainedSet (using Context ): Type =
240231 tree match
241- case Apply (TypeApply (_, refs :: Nil ), _) =>
242- collectRefs(refs.tpe)
232+ case Apply (TypeApply (_, refs :: Nil ), _) => refs.tpe
243233 case _ =>
244- Nil
234+ if tree.symbol.maybeOwner == defn.RetainsCapAnnot
235+ then root.cap
236+ else NoType
245237
246238 /** The arguments of a @retains, @retainsCap or @retainsByName annotation */
247239 def retainedElems (using Context ): List [Tree ] = tree match
@@ -254,6 +246,21 @@ extension (tree: Tree)
254246
255247extension (tp : Type )
256248
249+ def retainedElements (using Context ): List [CaptureRef ] = tp match
250+ case ReachCapability (tp1) =>
251+ tp1.reach :: Nil
252+ case ReadOnlyCapability (tp1) =>
253+ tp1.readOnly :: Nil
254+ case tp : CaptureRef if tp.isTrackableRef =>
255+ tp :: Nil
256+ case tp : TypeRef if tp.symbol.isType && tp.derivesFrom(defn.Caps_CapSet ) =>
257+ tp :: Nil
258+ case OrType (tp1, tp2) =>
259+ tp1.retainedElements ++ tp2.retainedElements
260+ case _ =>
261+ if tp.isNothingType then Nil
262+ else throw IllegalCaptureRef (tp)
263+
257264 /** Is this type a CaptureRef that can be tracked?
258265 * This is true for
259266 * - all ThisTypes and all TermParamRef,
@@ -676,7 +683,7 @@ extension (cls: ClassSymbol)
676683 || bc.is(CaptureChecked )
677684 && bc.givenSelfType.dealiasKeepAnnots.match
678685 case CapturingType (_, refs) => refs.isAlwaysEmpty
679- case RetainingType (_, refs) => refs.isEmpty
686+ case RetainingType (_, refs) => refs.retainedElements. isEmpty
680687 case selfType =>
681688 isCaptureChecking // At Setup we have not processed self types yet, so
682689 // unless a self type is explicitly given, we can't tell
@@ -794,7 +801,7 @@ class CleanupRetains(using Context) extends TypeMap:
794801 def apply (tp : Type ): Type =
795802 tp match
796803 case AnnotatedType (tp, annot) if annot.symbol == defn.RetainsAnnot || annot.symbol == defn.RetainsByNameAnnot =>
797- RetainingType (tp, Nil , byName = annot.symbol == defn.RetainsByNameAnnot )
804+ RetainingType (tp, defn. NothingType , byName = annot.symbol == defn.RetainsByNameAnnot )
798805 case _ => mapOver(tp)
799806
800807/** A typemap that follows aliases and keeps their transformed results if
@@ -813,18 +820,18 @@ trait FollowAliasesMap(using Context) extends TypeMap:
813820/** An extractor for `caps.reachCapability(ref)`, which is used to express a reach
814821 * capability as a tree in a @retains annotation.
815822 */
816- object ReachCapabilityApply :
817- def unapply (tree : Apply )(using Context ): Option [Tree ] = tree match
818- case Apply (reach, arg :: Nil ) if reach.symbol == defn.Caps_reachCapability => Some (arg)
819- case _ => None
823+ // object ReachCapabilityApply:
824+ // def unapply(tree: Apply)(using Context): Option[Tree] = tree match
825+ // case Apply(reach, arg :: Nil) if reach.symbol == defn.Caps_reachCapability => Some(arg)
826+ // case _ => None
820827
821828/** An extractor for `caps.readOnlyCapability(ref)`, which is used to express a read-only
822829 * capability as a tree in a @retains annotation.
823830 */
824- object ReadOnlyCapabilityApply :
825- def unapply (tree : Apply )(using Context ): Option [Tree ] = tree match
826- case Apply (ro, arg :: Nil ) if ro.symbol == defn.Caps_readOnlyCapability => Some (arg)
827- case _ => None
831+ // object ReadOnlyCapabilityApply:
832+ // def unapply(tree: Apply)(using Context): Option[Tree] = tree match
833+ // case Apply(ro, arg :: Nil) if ro.symbol == defn.Caps_readOnlyCapability => Some(arg)
834+ // case _ => None
828835
829836/** An extractor for `caps.capsOf[X]`, which is used to express a generic capture set
830837 * as a tree in a @retains annotation.
0 commit comments