@@ -85,12 +85,8 @@ object Annotations {
8585 EmptyAnnotation // strip retains-like annotations unless capture checking is enabled
8686 else
8787 val mappedType = sanitize(tm(arg.tpe))
88- if mappedType `eql` arg.tpe then
89- this
90- else if cc.ccConfig.newScheme then
91- CompactAnnotation (symbol.typeRef.appliedTo(mappedType))
92- else
93- derivedAnnotation(rebuild(tree, mappedType))
88+ if mappedType `eql` arg.tpe then this
89+ else CompactAnnotation (symbol.typeRef.appliedTo(mappedType))
9490
9591 case args =>
9692 // Checks if `tm` would result in any change by applying it to types
@@ -185,16 +181,16 @@ object Annotations {
185181 case _ => None
186182
187183 override def mapWith (tm : TypeMap )(using Context ): Annotation =
188- def derived (tp : Type ) =
189- if tm.isRange(tp) then EmptyAnnotation else derivedAnnotation(tp)
190- def sanitizeArg (tp : Type ) = tp match
191- case tp @ AppliedType (tycon, args) =>
192- tp.derivedAppliedType(tycon, args.mapConserve(sanitize))
184+ val isRetains = symbol.isRetainsLike
185+ if isRetains && ! Feature .ccEnabledSomewhere then EmptyAnnotation
186+ else tm(tp) match
187+ case tp1 @ AppliedType (tycon, args) =>
188+ val args1 = if isRetains then args.mapConserve(sanitize) else args
189+ derivedAnnotation(tp1.derivedAppliedType(tycon, args1))
190+ case tp1 : TypeRef =>
191+ derivedAnnotation(tp1)
193192 case _ =>
194- tp
195- if ! symbol.isRetainsLike then derived(tm(tp))
196- else if Feature .ccEnabledSomewhere then derived(sanitizeArg(tm(tp)))
197- else EmptyAnnotation // strip retains-like annotations unless capture checking is enabled
193+ EmptyAnnotation
198194
199195 override def refersToParamOf (tl : TermLambda )(using Context ): Boolean =
200196 refersToLambdaParam(tp, tl)
@@ -203,6 +199,12 @@ object Annotations {
203199 override def eql (that : Annotation ) = that match
204200 case that : CompactAnnotation => this .tp `eql` that.tp
205201 case _ => false
202+
203+ object CompactAnnotation :
204+ def apply (tree : Tree )(using Context ): CompactAnnotation =
205+ val argTypes = tpd.allArguments(tree).map(_.tpe)
206+ apply(annotClass(tree).typeRef.appliedTo(argTypes))
207+
206208 end CompactAnnotation
207209
208210 /** Sanitize @retains arguments to approximate illegal types that could cause a compilation
@@ -313,7 +315,18 @@ object Annotations {
313315
314316 def apply (tree : Tree ): Annotation = tree match
315317 case tree : TypeTree => CompactAnnotation (tree.tpe)
316- case _ => ConcreteAnnotation (tree)
318+ case _ =>
319+ def assertNoRetains (t : Tree ): Unit = t match
320+ case Apply (fn, _) => assertNoRetains(fn)
321+ case TypeApply (fn, _) => assertNoRetains(fn)
322+ case Select (t, StdNames .nme.CONSTRUCTOR ) => assertNoRetains(t)
323+ case New (t) => assertNoRetains(t)
324+ case t : RefTree =>
325+ if t.name.toString == " retains" || t.name.toString == " retainsByName" then
326+ new Error ().printStackTrace()
327+ case _ =>
328+ // assertNoRetains(tree)
329+ ConcreteAnnotation (tree)
317330
318331 def apply (cls : ClassSymbol , span : Span )(using Context ): Annotation =
319332 apply(cls, Nil , span)
@@ -328,7 +341,9 @@ object Annotations {
328341 apply(atp, arg :: Nil , span)
329342
330343 def apply (atp : Type , args : List [Tree ], span : Span )(using Context ): Annotation =
331- apply(New (atp, args).withSpan(span))
344+ if atp.typeSymbol.isRetainsLike && args.isEmpty
345+ then CompactAnnotation (atp)
346+ else apply(New (atp, args).withSpan(span))
332347
333348 /** Create an annotation where the tree is computed lazily. */
334349 def deferred (sym : Symbol )(treeFn : Context ?=> Tree ): Annotation =
0 commit comments