Skip to content
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

Keep annotation order #15063

Merged
merged 1 commit into from
Apr 28, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import Constants._
import Types._
import Decorators._

import scala.collection.mutable

class RepeatableAnnotations extends MiniPhase:

override def phaseName: String = RepeatableAnnotations.name
Expand All @@ -28,7 +30,7 @@ class RepeatableAnnotations extends MiniPhase:
tree

private def aggregateAnnotations(annotations: Seq[Annotation])(using Context): List[Annotation] =
val annsByType = annotations.groupBy(_.symbol)
val annsByType = stableGroupBy(annotations, _.symbol)
annsByType.flatMap {
case (_, a :: Nil) => a :: Nil
case (sym, anns) if sym.derivesFrom(defn.ClassfileAnnotationClass) =>
Expand All @@ -50,6 +52,14 @@ class RepeatableAnnotations extends MiniPhase:
case (_, anns) => anns
}.toList

private def stableGroupBy[A, K](ins: Seq[A], f: A => K): scala.collection.MapView[K, List[A]] =
val out = new mutable.LinkedHashMap[K, mutable.ListBuffer[A]]()
for (in <- ins) {
val buffer = out.getOrElseUpdate(f(in), new mutable.ListBuffer)
buffer += in
}
out.view.mapValues(_.toList)

object RepeatableAnnotations:
val name: String = "repeatableAnnotations"
val description: String = "aggregate repeatable annotations"
8 changes: 8 additions & 0 deletions tests/pos/Annotations.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package foo.bar

import jdk.jfr.Enabled

@Enabled
@Deprecated
Comment on lines +5 to +6
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the test involve using the same annotation multiple times to trigger use of RepeatableAnnotations, like in scala/scala@954c5d3#diff-8bd66281732306fbd83125f4c1bba7a81797b9ed31a0857afb0b805dd6e1fe6cR258-R262 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that would also be useful to test, though it wasn't the scenario I was aiming to fix: because the 'annotations.groupBy(_.symbol)` doesn't have a predictable order of keys, even regular 'unrepeated' annotations would not necessarily end up in the bytecode in the same order.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I didn't realize that this code wasn't simply bypassed when no repeated annotations exist. But yeah, if you could copy the tests that are in the scala 2 PRs that would be nice: to compile multiple files at once (the .java annotation definition and the scala usage) you can create a subdirectory in tests/pos.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha, added!

final class Annotations {
}
2 changes: 2 additions & 0 deletions tests/pos/annotations1/a.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Annot1(s: String) extends scala.annotation.StaticAnnotation
class Annot2(s: Class[_]) extends scala.annotation.StaticAnnotation
3 changes: 3 additions & 0 deletions tests/pos/annotations1/b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@Annot1("foo")
@Annot2(classOf[AnyRef])
class Test
5 changes: 5 additions & 0 deletions tests/pos/annotationsJava/Annot1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Inherited
@interface Annot1 { String value() default ""; }
5 changes: 5 additions & 0 deletions tests/pos/annotationsJava/Annot2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Inherited
@interface Annot2 { Class value(); }
1 change: 1 addition & 0 deletions tests/pos/annotationsJava/b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@Annot1("foo") @Annot2(classOf[AnyRef]) class Test
13 changes: 13 additions & 0 deletions tests/pos/annotationsJavaRepeatable/Annot1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import java.lang.annotation.*;

@Repeatable(Annot1.Container.class)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface Annot1 { String value() default "";

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public static @interface Container {
Annot1[] value();
}
}
6 changes: 6 additions & 0 deletions tests/pos/annotationsJavaRepeatable/Annot2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Inherited
@interface Annot2 { Class value(); }
1 change: 1 addition & 0 deletions tests/pos/annotationsJavaRepeatable/b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@Annot1("foo") @Annot2(classOf[String]) @Annot1("bar") class Test