Skip to content

Some annotations are not visible in macros since 2.13.17 #13140

@adamw

Description

@adamw

I've got a macro which inspects the annotations on the fields of a case class. Starting with 2.13.17, annotations which extend a class with a parameter are no longer visible in the macro.

Given the following annotations:

import scala.annotation.StaticAnnotation

sealed trait InputAnnotation extends StaticAnnotation

// works
class path extends InputAnnotation
class query(val name: String = "") extends InputAnnotation

// problematic
class body(val s: String) extends InputAnnotation
class jsonbody extends body("x")

and the given class:

case class Product(
    @jsonbody id: String,
    @path name: String,
    @query("age") age: Int
)

The path and query annotations are visible in the macro (through the field's Symbol.annotations), while the jsonbody is not.

This used to work fine in 2.13.16. Specifically, I found this bug through a failing tapir dependency update. This continues to work fine in Scala 2.12 and 3.

Here's a macro to reproduce the problem: https://github.com/adamw/scala2-ann-bug

When you compile the code, it will output the found annotations:

sbt:root> tc
[info] compiling 2 Scala sources to /Users/adamw/projects/temp/scala2-ann-bug/macros/target/scala-2.13/classes ...
[info] compiling 1 Scala source to /Users/adamw/projects/temp/scala2-ann-bug/test/target/scala-2.13/classes ...
[info] /Users/adamw/projects/temp/scala2-ann-bug/test/src/main/scala/test/TestMacro.scala:14:26: Inspecting case class: test.Product
[info]     printFieldAnnotations[Product]
[info]                          ^
[info] /Users/adamw/projects/temp/scala2-ann-bug/test/src/main/scala/test/TestMacro.scala:14:26: Field: id : String
[info]     printFieldAnnotations[Product]
[info]                          ^
[info] /Users/adamw/projects/temp/scala2-ann-bug/test/src/main/scala/test/TestMacro.scala:14:26:   - No annotations
[info]     printFieldAnnotations[Product]
[info]                          ^
[info] /Users/adamw/projects/temp/scala2-ann-bug/test/src/main/scala/test/TestMacro.scala:14:26: Field: name : String
[info]     printFieldAnnotations[Product]
[info]                          ^
[info] /Users/adamw/projects/temp/scala2-ann-bug/test/src/main/scala/test/TestMacro.scala:14:26:   - Annotation: macros.path = new macros.path()
[info]     printFieldAnnotations[Product]
[info]                          ^
[info] /Users/adamw/projects/temp/scala2-ann-bug/test/src/main/scala/test/TestMacro.scala:14:26: Field: age : Int
[info]     printFieldAnnotations[Product]
[info]                          ^
[info] /Users/adamw/projects/temp/scala2-ann-bug/test/src/main/scala/test/TestMacro.scala:14:26:   - Annotation: macros.query = new macros.query("age")
[info]     printFieldAnnotations[Product]
[info]                          ^

Note that there are no annotations found on the id field. If you change the Scala version in the build to 2.13.16, clean and recompile, the annotation appears:

[info] /Users/adamw/projects/temp/scala2-ann-bug/test/src/main/scala/test/TestMacro.scala:14:26: Field: id : String
[info]     printFieldAnnotations[Product]
[info]                          ^
[info] /Users/adamw/projects/temp/scala2-ann-bug/test/src/main/scala/test/TestMacro.scala:14:26:   - Annotation: macros.jsonbody = new macros.jsonbody()

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions