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

Macros and java annotation parameter constant check #11183

Open
yhpark opened this issue Oct 1, 2018 · 0 comments
Open

Macros and java annotation parameter constant check #11183

yhpark opened this issue Oct 1, 2018 · 0 comments
Labels

Comments

@yhpark
Copy link

yhpark commented Oct 1, 2018

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.

@xuwei-k xuwei-k added the macros label Oct 1, 2018
@yhpark yhpark changed the title Macros and annotation parameter constant check Macros and java annotation parameter constant check Oct 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants