Skip to content

Commit fe696a9

Browse files
Merge pull request #10643 from dotty-staging/update-annotations-api
Rework reflect annotations API
2 parents b7d4fc6 + 44a2b27 commit fe696a9

File tree

6 files changed

+25
-17
lines changed

6 files changed

+25
-17
lines changed

compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1807,7 +1807,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
18071807
given AnnotatedTypeMethods: AnnotatedTypeMethods with
18081808
extension (self: AnnotatedType):
18091809
def underlying: TypeRepr = self.underlying.stripTypeVar
1810-
def annot: Term = self.annot.tree
1810+
def annotation: Term = self.annot.tree
18111811
end extension
18121812
end AnnotatedTypeMethods
18131813

@@ -2304,8 +2304,14 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
23042304

23052305
def tree: Tree = FromSymbol.definitionFromSym(self)
23062306

2307-
def annots: List[Term] =
2308-
self.annotations.flatMap {
2307+
def hasAnnotation(annotSym: Symbol): Boolean =
2308+
self.denot.hasAnnotation(annotSym)
2309+
2310+
def getAnnotation(annotSym: Symbol): Option[Term] =
2311+
self.denot.getAnnotation(annotSym).map(_.tree)
2312+
2313+
def annotations: List[Term] =
2314+
self.denot.annotations.flatMap {
23092315
case _: dotc.core.Annotations.BodyAnnotation => Nil
23102316
case annot => annot.tree :: Nil
23112317
}

compiler/src/scala/quoted/runtime/impl/printers/SourceCode.scala

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ object SourceCode {
370370
this += "throw "
371371
printTree(expr)
372372

373-
case Apply(fn, args) if fn.symbol == Symbol.requiredMethod("scala.internal.Quoted.exprQuote") =>
373+
case Apply(fn, args) if fn.symbol == Symbol.requiredMethod("scala.quoted.runtime.quote") =>
374374
args.head match {
375375
case Block(stats, expr) =>
376376
this += "'{"
@@ -385,12 +385,7 @@ object SourceCode {
385385
this += "}"
386386
}
387387

388-
case TypeApply(fn, args) if fn.symbol == Symbol.requiredMethod("scala.internal.Quoted.typeQuote") =>
389-
this += "'["
390-
printTypeTree(args.head)
391-
this += "]"
392-
393-
case Apply(fn, arg :: Nil) if fn.symbol == Symbol.requiredMethod("scala.internal.Quoted.exprSplice") =>
388+
case Apply(fn, arg :: Nil) if fn.symbol == Symbol.requiredMethod("scala.quoted.runtime.splice") =>
394389
this += "${"
395390
printTree(arg)
396391
this += "}"
@@ -594,8 +589,9 @@ object SourceCode {
594589

595590
private def printFlatBlock(stats: List[Statement], expr: Term)(using elideThis: Option[Symbol]): this.type = {
596591
val (stats1, expr1) = flatBlock(stats, expr)
592+
val splicedTypeAnnot = Symbol.requiredClass("scala.quoted.runtime.SplicedType").primaryConstructor
597593
val stats2 = stats1.filter {
598-
case tree: TypeDef => !tree.symbol.annots.exists(_.symbol.maybeOwner == Symbol.requiredClass("scala.internal.Quoted.quoteTypeTag"))
594+
case tree: TypeDef => !tree.symbol.hasAnnotation(splicedTypeAnnot)
599595
case _ => true
600596
}
601597
if (stats2.isEmpty) {
@@ -1271,7 +1267,7 @@ object SourceCode {
12711267
}
12721268

12731269
private def printDefAnnotations(definition: Definition)(using elideThis: Option[Symbol]): this.type = {
1274-
val annots = definition.symbol.annots.filter {
1270+
val annots = definition.symbol.annotations.filter {
12751271
case Annotation(annot, _) =>
12761272
val sym = annot.tpe.typeSymbol
12771273
sym != Symbol.requiredClass("scala.forceInline") &&

library/src/scala/quoted/Quotes.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2356,7 +2356,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
23562356
trait AnnotatedTypeMethods:
23572357
extension (self: AnnotatedType):
23582358
def underlying: TypeRepr
2359-
def annot: Term
2359+
def annotation: Term
23602360
end extension
23612361
end AnnotatedTypeMethods
23622362

@@ -3083,8 +3083,14 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
30833083
*/
30843084
def tree: Tree
30853085

3086+
/** Is the annotation defined with `annotSym` attached to this symbol? */
3087+
def hasAnnotation(annotSym: Symbol): Boolean
3088+
3089+
/** Get the annotation defined with `annotSym` attached to this symbol */
3090+
def getAnnotation(annotSym: Symbol): Option[Term]
3091+
30863092
/** Annotations attached to this symbol */
3087-
def annots: List[Term]
3093+
def annotations: List[Term]
30883094

30893095
/** Does this symbol come from a currently compiled source file? */
30903096
def isDefinedInCurrentRun: Boolean

scala3doc/src/dotty/dokka/tasty/BasicSupport.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ trait BasicSupport:
4545
path.map(TastyDocumentableSource(_, sym.pos.startLine))
4646

4747
def getAnnotations(): List[Annotation] =
48-
sym.annots.filterNot(_.symbol.packageName.startsWith("scala.annotation.internal")).map(parseAnnotation).reverse
48+
sym.annotations.filterNot(_.symbol.packageName.startsWith("scala.annotation.internal")).map(parseAnnotation).reverse
4949

5050

0 commit comments

Comments
 (0)