Skip to content

Macros and java annotation parameter constant check #11183

Open
@yhpark

Description

@yhpark

I was trying to use macro for annotation parameter which I thought would be okay since macros are applied during compile time. However, the compiler throws an error annotation argument needs to be a constant.

Example:

def identity(a: String): String = macro identityMacro
def identityMacro(c: blackbox.Context)(a: c.Expr[String]): c.Expr[String] = a

...

case class Test(@annot(foo = identity("bar")) test: Int)

Here I defined a macro that returns without doing anything, so the Expr it's returning would be the same as it's given, which would be something of Literal(Constant(String)).
The @annot is just a plain annotation with one member String foo that I defined in Java

I found that the macro def is executed before the moment the arguments are checked whether constant. So, what I thought was: when the compiler is checking the arguments if they're constant, theidentity("bar") will have been converted to "bar" since the macro is already executed, which apparently is not what's happening.

Not sure if this is a bug, but I want to know if this is as intended by the design of the language.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions