-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add support for repeatable annotations #10751
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
compiler/src/dotty/tools/dotc/transform/RepeatableAnnotations.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package dotty.tools.dotc | ||
package transform | ||
|
||
import core._ | ||
import ast.tpd._ | ||
import Contexts._ | ||
import MegaPhase._ | ||
import Annotations._ | ||
import Symbols.defn | ||
import Constants._ | ||
import Types._ | ||
import Decorators._ | ||
|
||
class RepeatableAnnotations extends MiniPhase: | ||
override def phaseName = "repeatableAnnotations" | ||
|
||
override def transformTypeDef(tree: TypeDef)(using Context): Tree = transformDef(tree) | ||
override def transformValDef(tree: ValDef)(using Context): Tree = transformDef(tree) | ||
override def transformDefDef(tree: DefDef)(using Context): Tree = transformDef(tree) | ||
|
||
private def transformDef(tree: DefTree)(using Context) = | ||
val annotations = tree.symbol.annotations | ||
if (!annotations.isEmpty) then | ||
tree.symbol.annotations = aggregateAnnotations(tree.symbol.annotations) | ||
tree | ||
|
||
private def aggregateAnnotations(annotations: Seq[Annotation])(using Context): List[Annotation] = | ||
val annsByType = annotations.groupBy(_.symbol) | ||
annsByType.flatMap { | ||
case (_, a :: Nil) => a :: Nil | ||
case (sym, anns) if sym.derivesFrom(defn.ClassfileAnnotationClass) => | ||
sym.getAnnotation(defn.JavaRepeatableAnnot).flatMap(_.argumentConstant(0)) match | ||
case Some(Constant(containerTpe: Type)) => | ||
val clashingAnns = annsByType.getOrElse(containerTpe.classSymbol, Nil) | ||
if clashingAnns.nonEmpty then | ||
// this is the same error javac would raise in this case | ||
val pos = clashingAnns.head.tree.srcPos | ||
report.error("Container must not be present at the same time as the element it contains", pos) | ||
Nil | ||
else | ||
val aggregated = JavaSeqLiteral(anns.map(_.tree).toList, TypeTree(sym.typeRef)) | ||
Annotation(containerTpe, NamedArg("value".toTermName, aggregated)) :: Nil | ||
case _ => | ||
val pos = anns.head.tree.srcPos | ||
report.error("Not repeatable annotation repeated", pos) | ||
Nil | ||
case (_, anns) => anns | ||
}.toList |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package repeatable; | ||
|
||
import java.lang.annotation.*; | ||
|
||
@Repeatable(SecondLevel_0.class) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface FirstLevel_0 { | ||
Plain_0[] value(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package repeatable; | ||
|
||
import java.lang.annotation.*; | ||
|
||
@Repeatable(FirstLevel_0.class) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface Plain_0 { | ||
public int value(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package repeatable; | ||
|
||
import java.lang.annotation.*; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface SecondLevel_0 { | ||
FirstLevel_0[] value(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import repeatable._ | ||
|
||
@Plain_0(1) | ||
@Plain_0(2) | ||
@Plain_0(3) | ||
@FirstLevel_0(Array()) // error | ||
trait U | ||
|
||
@FirstLevel_0(Array(Plain_0(4), Plain_0(5))) | ||
@FirstLevel_0(Array(Plain_0(6), Plain_0(7))) | ||
@SecondLevel_0(Array()) // error | ||
trait T | ||
|
||
@SecondLevel_0(Array()) | ||
@SecondLevel_0(Array()) // error | ||
trait S |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
1 | ||
2 | ||
3 | ||
|
||
List(4, 5) | ||
List(6, 7) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package repeatable; | ||
|
||
import java.lang.annotation.*; | ||
|
||
@Repeatable(SecondLevel_0.class) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface FirstLevel_0 { | ||
Plain_0[] value(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package repeatable; | ||
|
||
import java.lang.annotation.*; | ||
|
||
@Repeatable(FirstLevel_0.class) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface Plain_0 { | ||
public int value(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package repeatable; | ||
|
||
import java.lang.annotation.*; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface SecondLevel_0 { | ||
FirstLevel_0[] value(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import repeatable._ | ||
|
||
@Plain_0(1) | ||
@Plain_0(2) | ||
@Plain_0(3) | ||
trait U | ||
|
||
@FirstLevel_0(Array(Plain_0(4), Plain_0(5))) | ||
@FirstLevel_0(Array(Plain_0(6), Plain_0(7))) | ||
trait T | ||
|
||
object Test: | ||
def main(args: Array[String]) = | ||
val annValuesU = classOf[U].getAnnotation(classOf[FirstLevel_0]).value.toList.map(_.value).sorted | ||
annValuesU.foreach(println) | ||
|
||
println() | ||
|
||
val annValuesT = classOf[T].getAnnotation(classOf[SecondLevel_0]).value.toList.map(_.value.toList.map(_.value).sorted).sorted | ||
annValuesT.foreach(println) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Usually, we use
SymTransformer
to change symbol denotations. I think here it's fine to do so as the change is not supposed to be visible in a separately compiled module.