Skip to content

Rework reflect annotations API #10643

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1807,7 +1807,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
given AnnotatedTypeMethods: AnnotatedTypeMethods with
extension (self: AnnotatedType):
def underlying: TypeRepr = self.underlying.stripTypeVar
def annot: Term = self.annot.tree
def annotation: Term = self.annot.tree
end extension
end AnnotatedTypeMethods

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

def tree: Tree = FromSymbol.definitionFromSym(self)

def annots: List[Term] =
self.annotations.flatMap {
def hasAnnotation(annotSym: Symbol): Boolean =
self.denot.hasAnnotation(annotSym)

def getAnnotation(annotSym: Symbol): Option[Term] =
self.denot.getAnnotation(annotSym).map(_.tree)

def annotations: List[Term] =
self.denot.annotations.flatMap {
case _: dotc.core.Annotations.BodyAnnotation => Nil
case annot => annot.tree :: Nil
}
Expand Down
14 changes: 5 additions & 9 deletions compiler/src/scala/quoted/runtime/impl/printers/SourceCode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ object SourceCode {
this += "throw "
printTree(expr)

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

case TypeApply(fn, args) if fn.symbol == Symbol.requiredMethod("scala.internal.Quoted.typeQuote") =>
this += "'["
printTypeTree(args.head)
this += "]"

case Apply(fn, arg :: Nil) if fn.symbol == Symbol.requiredMethod("scala.internal.Quoted.exprSplice") =>
case Apply(fn, arg :: Nil) if fn.symbol == Symbol.requiredMethod("scala.quoted.runtime.splice") =>
this += "${"
printTree(arg)
this += "}"
Expand Down Expand Up @@ -594,8 +589,9 @@ object SourceCode {

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

private def printDefAnnotations(definition: Definition)(using elideThis: Option[Symbol]): this.type = {
val annots = definition.symbol.annots.filter {
val annots = definition.symbol.annotations.filter {
case Annotation(annot, _) =>
val sym = annot.tpe.typeSymbol
sym != Symbol.requiredClass("scala.forceInline") &&
Expand Down
10 changes: 8 additions & 2 deletions library/src/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2356,7 +2356,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
trait AnnotatedTypeMethods:
extension (self: AnnotatedType):
def underlying: TypeRepr
def annot: Term
def annotation: Term
end extension
end AnnotatedTypeMethods

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

/** Is the annotation defined with `annotSym` attached to this symbol? */
def hasAnnotation(annotSym: Symbol): Boolean

/** Get the annotation defined with `annotSym` attached to this symbol */
def getAnnotation(annotSym: Symbol): Option[Term]

/** Annotations attached to this symbol */
def annots: List[Term]
def annotations: List[Term]

/** Does this symbol come from a currently compiled source file? */
def isDefinedInCurrentRun: Boolean
Expand Down
2 changes: 1 addition & 1 deletion scala3doc/src/dotty/dokka/tasty/BasicSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ trait BasicSupport:
path.map(TastyDocumentableSource(_, sym.pos.startLine))

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