Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upNew RFC: proc-macro-attribute-recursion #2628
Conversation
llogiq
force-pushed the
llogiq:proc-macro-attribute-recursion
branch
from
8b017e6
to
91ac6d3
Jan 23, 2019
oli-obk
reviewed
Jan 23, 2019
llogiq
force-pushed the
llogiq:proc-macro-attribute-recursion
branch
from
ae7e822
to
665feb4
Jan 23, 2019
This comment has been minimized.
This comment has been minimized.
ExpHP
commented
Jan 23, 2019
•
|
So.... what currently happens if a |
This comment has been minimized.
This comment has been minimized.
|
It just gets ignored. |
Centril
reviewed
Jan 24, 2019
| # Reference-level explanation | ||
| [reference-level-explanation]: #reference-level-explanation | ||
|
|
||
| The expander is extended to search the expansion of `proc_macro` and `proc_macro_attributes` for other macro invocations. Those are then expanded until there are no more attributes or macro invocations left or the macro expansion limit is reached, whichever comes first. |
This comment has been minimized.
This comment has been minimized.
Centril
Jan 24, 2019
Contributor
This whole section (and the RFC in general) seems rather underspecified; I'd like to see examples of proc_macro added to the previous section at least. This also doesn't seem like a full description of behavior since #[flame] gets applied to macro expansions. This also doesn't say what happens if you write #[flame(alpha, beta)]. This should be specified including with examples.
This comment has been minimized.
This comment has been minimized.
llogiq
Jan 24, 2019
Contributor
I didn't detail what #[flame(alpha, beta)] does (currently it should give you an error), because this RFC does not change this part of functionality. As for bang-macros, it doesn't matter if they are defined via macro, macro_rules! or as a proc macro. Again the functionality of expansion is unchanged.
The only two things this RFC defines: That the output of proc_macro_attributes get expanded and that the order of expansion follows the order of appearance (so that things from the original code get expanded before things added by the expansion).
This comment has been minimized.
This comment has been minimized.
Centril
Jan 24, 2019
Contributor
Well, from what I can tell, the reference says nothing about how #[flame] gets attached to this_is_fun.
That the output of proc_macro_attributes get expanded and that the order of expansion follows the order of appearance (so that things from the original code get expanded before things added by the expansion).
That's not what the text says; it says "The expander is extended to search the expansion of proc_macro and proc_macro_attributes for other macro invocations." -- you've added examples for the latter but not the former.
| Implementors will have to make sure to order the expansions within expanded output by their origin: macros which are in the `proc_macro_attribute`s' input need to be expanded before expanding macros that have been added by the `proc_macro_attribute`s themselves. This can easily be done by examining the `Span`s of the expansion and ordering them by `SyntaxContext`. | ||
|
|
||
| # Drawbacks | ||
| [drawbacks]: #drawbacks |
This comment has been minimized.
This comment has been minimized.
Centril
Jan 24, 2019
Contributor
The behavior of attaching #[flame] to expansion of macros is, as far as I can see, theoretically a breaking change if attaching the attribute has an effect on static or dynamic semantics of the expansion. I'm surprised that this behavior is not opt-in. It seems the proc macro author should request this behavior.
This comment has been minimized.
This comment has been minimized.
llogiq
Jan 24, 2019
Contributor
I agree that in theory this is a breaking change. I' would however be surprised if anyone relied on the current behavior, as it doesn't do anything useful. As I commented on #2320, this was the thing I tried because it felt natural and just might have worked – so in effect by adding the attribute, I am opting in to the expansion. What else would a proc macro author expect when adding an attribute that is expanded by a proc macro?
This comment has been minimized.
This comment has been minimized.
Centril
Jan 24, 2019
Contributor
I' would however be surprised if anyone relied on the current behavior, as it doesn't do anything useful.
We should at least crater run it and the theoretical breakage should be added to the text with a reasoning about why you would be surprised and why it doesn't do anything useful.
As I commented on #2320, this was the thing I tried because it felt natural and just might have worked – so in effect by adding the attribute, I am opting in to the expansion. What else would a proc macro author expect when adding an attribute that is expanded by a proc macro?
It could either expand, as per your RFC, to:
mod fun {
#[flame]
fn this_is_fun(x: u64) -> u64 { x + 1 }
}or alternatively:
mod fun {
fn this_is_fun(x: u64) -> u64 { x + 1 } // This is what I'd expect.
}this might make a world of difference if #[flame] does transformations to this_is_fun or not.
This comment has been minimized.
This comment has been minimized.
llogiq
Jan 24, 2019
Contributor
OK, I added some text to this effect. Please be aware that this is a very language-lawyerly perspective (not that there's anything wrong with that) – as a proc macro author I certainly wouldn't write code to add an attribute that does nothing. In fact, I wrote this RFC because this was what I tried to get macros expanded in flamer and I don't want to wait for a more general solution that may take far more effort.
Anyway, a crater run certainly won't hurt.
Centril
added
T-lang
A-macros
A-proc-macros
labels
Jan 24, 2019
Centril
requested a review
from
petrochenkov
Jan 24, 2019
llogiq
force-pushed the
llogiq:proc-macro-attribute-recursion
branch
from
5555c94
to
a08b6cd
Jan 24, 2019
This comment has been minimized.
This comment has been minimized.
|
For things to work as described three somewhat independent changes are needed:
TLDR: This is probably not a small and simple change, and it may have implications of the "Small and simple change" would be to provide |
This comment has been minimized.
This comment has been minimized.
|
Good point; while this RFC is conceptually simple, the implementation has some subtle details.
Also your proposal is incomplete – we'd need multiple functions, for macros in item, expr, pat and ty positions (and I'm not sure if I forgot one). Given this, it is far from simple, too. While I agree that it would indeed be somewhat usable, I'd rather not create such a strong dependency on resolve from expansion. Better to keep the API boundary small. |
This comment has been minimized.
This comment has been minimized.
|
@llogiq
, is (The proper solution should probably be a position agnostic |
This comment has been minimized.
This comment has been minimized.
|
True. Fair enough, if I get to pull this trick, you may too. Apart from that, I outlined how, given a support library, this can be quite usable from |
Centril
self-assigned this
Jan 31, 2019
This comment has been minimized.
This comment has been minimized.
|
cc @nrc |
llogiq commentedJan 23, 2019
•
edited
This breaks out a small part of #2320 that is simple to implement and reason about and will give us a simple workable solution to macro expansion in proc macros while we wait for the more complete solution to emerge.
Rendered