-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[Proof of Concept] Code generation via rewriting errors in macro annotations #16545
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
base: main
Are you sure you want to change the base?
Conversation
a1c63e7
to
53716bf
Compare
case _ => | ||
report.error(s"Replace the underline code by:\n$expectedBody", rhs.pos) | ||
case _ => | ||
report.error("FIXME: Passing -Yretain-trees is currently needed for this macro to work") |
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.
@nicolasstucki I'm surprised -Yretain-trees
is needed since I'm accessing the tree of a symbol defined in the class being annotated, so the tree is accessible via traversing the tree
argument of the macro annotation transform
method, but it would be more convenient if I could access it through Symbol#tree too.
53716bf
to
9ad1732
Compare
@smarter I rebased and solved the conflicts. I will have a look at the implementation and maybe add some changes. |
cls.declaredMethod(withParam).find(o => | ||
val paramss = o.paramSymss | ||
paramss.size == 1 && paramss(0).size == 1 | ||
cdef.body.find(stat => | ||
val paramss = stat.symbol.paramSymss | ||
stat.symbol.name == withParam | ||
&& paramss.size == 1 && paramss(0).size == 1 |
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.
@nicolasstucki I understand that we can look into the input tree directly, but in my opinion we should also be able to use Symbol#tree for this usecase, unless there is something that prevents us from using it?
…tations This commit prototypes the proposal in https://contributors.scala-lang.org/t/scala-3-macro-annotations-and-code-generation/6035 to use a macro annotation to do code generation. Since the -rewrite mechanism isn't currently accessible to macros, we just print an error indicating what part of the source code needs to be rewritten. We implement support for a subset of `@data`: only the `withField` methods are added (note that equals/hashCode/toString wouldn't need to be code-generated since they are already defined in `AnyRef`, so a macro annotation can override them as demonstrated in the existing `tests/run-macros/annot-mod-class-data` test). This is only one possible path forward. Alternatively, we could recommend using scalafix for this usecase, and then the compiler itself wouldn't have to grow code generation abilities, but libraries defining macro annotation might not like to be tied to an external tool.
We can't just pattern match on it because of scala#16543.
This PR prototypes the proposal in
https://contributors.scala-lang.org/t/scala-3-macro-annotations-and-code-generation/6035
to use a macro annotation to do code generation. Since the -rewrite mechanism
isn't currently accessible to macros, this proof of concept just prints an error indicating what
part of the source code needs to be rewritten.
We implement support for a subset of
@data
: only thewithField
methods areadded (note that equals/hashCode/toString wouldn't need to be code-generated
since they are already defined in
AnyRef
, so a macro annotation can overridethem as demonstrated in the existing
tests/run-macros/annot-mod-class-data
test).
This is only one possible path forward. Alternatively, we could recommend using
scalafix for this usecase, and then the compiler itself wouldn't have to grow
code generation abilities, but libraries defining macro annotation might not
like to be tied to an external tool.