Skip to content

[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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

smarter
Copy link
Member

@smarter smarter commented Dec 16, 2022

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 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.

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")
Copy link
Member Author

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.

@nicolasstucki
Copy link
Contributor

@smarter I rebased and solved the conflicts. I will have a look at the implementation and maybe add some changes.

Comment on lines 22 to 31
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
Copy link
Member Author

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?

smarter and others added 4 commits January 5, 2023 15:12
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants