Skip to content

Commit 291aeca

Browse files
committed
Methods for filtering annotations carrying meta information.
1 parent 8191c86 commit 291aeca

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

src/dotty/tools/dotc/core/SymDenotations.scala

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ object SymDenotations {
215215
final def transformAnnotations(f: Annotation => Annotation)(implicit ctx: Context): Unit =
216216
annotations = annotations.mapConserve(f)
217217

218+
/** Keep only those annotations that satisfy `p` */
219+
final def filterAnnotations(p: Annotation => Boolean)(implicit ctx: Context): Unit =
220+
annotations = annotations.filterConserve(p)
221+
218222
/** Optionally, the annotation matching the given class symbol */
219223
final def getAnnotation(cls: Symbol)(implicit ctx: Context): Option[Annotation] =
220224
dropOtherAnnotations(annotations, cls) match {
@@ -230,9 +234,9 @@ object SymDenotations {
230234
final def removeAnnotation(cls: Symbol)(implicit ctx: Context): Unit =
231235
annotations = myAnnotations.filterNot(_ matches cls)
232236

233-
/** Copy all annotations from given symbol by adding them to this symbol */
234-
final def addAnnotations(from: Symbol)(implicit ctx: Context): Unit =
235-
from.annotations.foreach(addAnnotation)
237+
/** Add all given annotations to this symbol */
238+
final def addAnnotations(annots: TraversableOnce[Annotation])(implicit ctx: Context): Unit =
239+
annots.foreach(addAnnotation)
236240

237241
@tailrec
238242
private def dropOtherAnnotations(anns: List[Annotation], cls: Symbol)(implicit ctx: Context): List[Annotation] = anns match {

src/dotty/tools/dotc/transform/ExtensionMethods.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ class ExtensionMethods extends MiniPhaseTransform with DenotTransformer with Ful
117117
imeth.flags | Final &~ (Override | Protected | AbsOverride),
118118
fullyParameterizedType(imeth.info, imeth.owner.asClass),
119119
privateWithin = imeth.privateWithin, coord = imeth.coord)
120-
extensionMeth.addAnnotations(from = imeth)(ctx.withPhase(thisTransformer))
120+
extensionMeth.addAnnotations(imeth.annotations)(ctx.withPhase(thisTransformer))
121121
// need to change phase to add tailrec annotation which gets removed from original method in the same phase.
122122
extensionMeth
123123
}

src/dotty/tools/dotc/transform/MixinOps.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class MixinOps(cls: ClassSymbol, thisTransform: DenotTransformer)(implicit ctx:
1919
name = member.name.stripScala2LocalSuffix,
2020
flags = member.flags &~ Deferred,
2121
info = cls.thisType.memberInfo(member)).enteredAfter(thisTransform).asTerm
22-
res.addAnnotations(member)
22+
res.addAnnotations(member.annotations)
2323
res
2424
}
2525

src/dotty/tools/dotc/transform/SymUtils.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Names._
1111
import StdNames._
1212
import NameOps._
1313
import Flags._
14+
import Annotations._
1415
import language.implicitConversions
1516

1617
object SymUtils {
@@ -90,4 +91,12 @@ class SymUtils(val self: Symbol) extends AnyVal {
9091

9192
def implClass(implicit ctx: Context): Symbol =
9293
self.owner.info.decl(self.name.implClassName).symbol
94+
95+
def annotationsCarrying(meta: ClassSymbol)(implicit ctx: Context): List[Annotation] =
96+
self.annotations.filter(_.symbol.hasAnnotation(meta))
97+
98+
def withAnnotationsCarrying(from: Symbol, meta: ClassSymbol)(implicit ctx: Context): self.type = {
99+
self.addAnnotations(from.annotationsCarrying(meta))
100+
self
101+
}
93102
}

0 commit comments

Comments
 (0)