diff --git a/compiler/src/dotty/tools/dotc/core/Types.scala b/compiler/src/dotty/tools/dotc/core/Types.scala index 5d630cad6368..86223cde31c9 100644 --- a/compiler/src/dotty/tools/dotc/core/Types.scala +++ b/compiler/src/dotty/tools/dotc/core/Types.scala @@ -30,6 +30,7 @@ import Hashable.* import Uniques.* import collection.mutable import config.Config +import config.Feature import config.Feature.sourceVersion import config.SourceVersion import annotation.{tailrec, constructorOnly} @@ -6279,12 +6280,12 @@ object Types extends TypeUtils { end BiTypeMap - /** A typemap that follows aliases and keeps their transformed results if - * there is a change. - */ + /** A typemap that follows non-opaque aliases and keeps their transformed + * results if there is a change. + */ trait FollowAliasesMap(using Context) extends TypeMap: def mapFollowingAliases(t: Type): Type = - val t1 = t.dealiasKeepAnnots + val t1 = t.dealiasKeepAnnotsAndOpaques if t1 ne t then val t2 = apply(t1) if t2 ne t1 then t2 @@ -6483,10 +6484,13 @@ object Types extends TypeUtils { mapCapturingType(tp, parent, refs, variance) case tp @ AnnotatedType(underlying, annot) => - val underlying1 = this(underlying) - val annot1 = annot.mapWith(this) - if annot1 eq EmptyAnnotation then underlying1 - else derivedAnnotatedType(tp, underlying1, annot1) + if annot.symbol.isRetainsLike && !Feature.ccEnabledSomewhere then + this(underlying) // strip retains like annotations unless capture checking is enabled + else + val underlying1 = this(underlying) + val annot1 = annot.mapWith(this) + if annot1 eq EmptyAnnotation then underlying1 + else derivedAnnotatedType(tp, underlying1, annot1) case _: ThisType | _: BoundType