Skip to content

Commit 9956808

Browse files
committed
Rework reflect annotations API
Also update SourceCode implementation
1 parent 222fe0b commit 9956808

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
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
object AnnotatedTypeMethodsImpl extends AnnotatedTypeMethods:
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 AnnotatedTypeMethodsImpl
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
@@ -2731,7 +2731,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
27312731
trait AnnotatedTypeMethods:
27322732
extension (self: AnnotatedType):
27332733
def underlying: TypeRepr
2734-
def annot: Term
2734+
def annotation: Term
27352735
end extension
27362736
end AnnotatedTypeMethods
27372737

@@ -3560,8 +3560,14 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
35603560
*/
35613561
def tree: Tree
35623562

3563+
/** Is the annotation defined with `annotSym` attached to this symbol? */
3564+
def hasAnnotation(annotSym: Symbol): Boolean
3565+
3566+
/** Get the annotation defined with `annotSym` attached to this symbol */
3567+
def getAnnotation(annotSym: Symbol): Option[Term]
3568+
35633569
/** Annotations attached to this symbol */
3564-
def annots: List[Term]
3570+
def annotations: List[Term]
35653571

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

0 commit comments

Comments
 (0)