-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Allow macro annotation to transform companion #19677
Allow macro annotation to transform companion #19677
Conversation
It has been decided to be included in the 3.5.0 release. |
ef74f4d
to
d2c3db0
Compare
d711455
to
e81ef44
Compare
I will add support for stoping macro expansion in a second PR (this one is already contained and has many changes in the tests) |
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.
I still have not looked into compiler/src/dotty/tools/dotc/transform/MacroAnnotations.scala
} | ||
|
||
// Interpret macro annotation instantiation `new myAnnot(..)` | ||
// TODO: Make this error handling stronger (no error handling at the moment) |
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.
What kind of error handling?
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.
What if the interpretation of the annotation failed and returned None
? We might add a better handling here and avoid a possible compiler crash in the future
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.
Then we should fix this now. Maybe this would be enough. Just check that the interpretation error has been reported.
val annotInstance = interpreter.interpret[MacroAnnotation](annot.tree) match
case Some(annotInstance) => annotInstance
case None => return (List(tree), companion)
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.
We agreed to investigate this later
2cbc56f
to
1c26cda
Compare
compiler/src/dotty/tools/dotc/ast/TreeMapWithTrackedStats.scala
Outdated
Show resolved
Hide resolved
compiler/src/dotty/tools/dotc/ast/TreeMapWithTrackedStats.scala
Outdated
Show resolved
Hide resolved
compiler/src/dotty/tools/dotc/ast/TreeMapWithTrackedStats.scala
Outdated
Show resolved
Hide resolved
compiler/src/dotty/tools/dotc/ast/TreeMapWithTrackedStats.scala
Outdated
Show resolved
Hide resolved
compiler/src/dotty/tools/dotc/ast/TreeMapWithTrackedStats.scala
Outdated
Show resolved
Hide resolved
tests/pos-macros/annot-dependency-between-modules/Macro_1.scala
Outdated
Show resolved
Hide resolved
1c26cda
to
58fe2ac
Compare
compiler/src/dotty/tools/dotc/ast/TreeMapWithTrackedStats.scala
Outdated
Show resolved
Hide resolved
} | ||
|
||
// Interpret macro annotation instantiation `new myAnnot(..)` | ||
// TODO: Make this error handling stronger (no error handling at the moment) |
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.
Then we should fix this now. Maybe this would be enough. Just check that the interpretation error has been reported.
val annotInstance = interpreter.interpret[MacroAnnotation](annot.tree) match
case Some(annotInstance) => annotInstance
case None => return (List(tree), companion)
58fe2ac
to
4694b3b
Compare
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.
Looks good to me, I cannot find anything wrong here. Just one semi-related question. I can't find where do we check if the generated tree contains a macro annotation. Is that in TreeChecker?
Generated trees cannot have annotations. We do a second run to inline code in the new defintions if necessary (last paragraph in the description). |
Allow MacroAnnotations to update the companion of a definition
We extend the MacroAnnotation api to allow to modify the companion of a class or an object.
Specification
In the following example, we expand the annotations in this order:
a1
,a2
,a3
.We always expand the latest available tree. If an annotation defined on
class Foo
changes its companion (object Foo
) and theclass
is defined beforeobject
, the expansion of the annotations on theobject
will be performed on the result of the expansion ofclass
.We maintain the program order in the definitions that were expanded.
Example:
If the
@a2
annotation changes the definitions inclass Foo
, we will rerun the algorithm on the result of this new expansion. Please note that we don't allow to generate code with MacroAnnotations, the reason for rerunning the algorithm is to expand and inline possible macros that we generated.Closes #19676