-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Declarative macro_rules!
attribute macros
#3697
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: master
Are you sure you want to change the base?
Declarative macro_rules!
attribute macros
#3697
Conversation
Nominated as a follow-up to recent lang discussions about this. |
how will this work in // ok
macro main {
attr() ($func:item) => { make_async_main!($func) },
attr(threads = $threads:literal) ($func:item) => { make_async_main!($func, $threads) },
}
// ?
macro stub attr() ($func:item) {
make_stub_func!($func)
} |
|
Nominated for consideration in a meeting to address any questions that arise. |
This RFC seems clear and straightforward to me, and the prior art in macro_rules_attribute and smol-macros makes it clear that this would be a useful feature out of the box. Thanks @joshtriplett for contributing this design. @rfcbot reviewed |
@rfcbot reviewed |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
@rfcbot reviewed Thanks @joshtriplett for putting this together and putting it forward. The design makes sense and is appealing in its details, and it's clearly been put together carefully.
For our future selves, on this point, see also: |
Moreover, this will allow macros to access other crates, and this is very good. This is not solved by rust-lang/rust#141996 |
@joshtriplett , you didn't answer my question #3697 (comment) In short: will declarative attribute macros be allowed on statements? (Procedural macros currently are not) |
My intention is exactly what's documented in the RFC: allowed everywhere an attribute is currently allowed. If, in the course of implementation, it becomes necessary to limit that further, we can do that and provide that update as part of the tracking issue. I've added a note to the unresolved questions section to evaluate that in the course of implementation. |
I'm aware of one such place: statements. Proc macro attributes are not allowed in stable Rust, see this playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=388113d8f930565ec153d4453e2786c8 . But builtin attributes are allowed in stable Rust: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=2455967bcc1ecd203f44d7a0532fd800 . Attributes on expressions always disallowed in stable Rust: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=68a4651e5619d11dbfa62ff5f84af066 |
nit: they're allowed in the input to an attribute macro, as long as the macro doesn't have them in its output. #[my_attr_macro]
pub fn f() {
let v = #[some_attr] S { a: 1 };
} I use that extensively in fayalite |
Why are proc macro atteibutes not allowed on statements? Would that same reason apply to declaritive attribute macros? |
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. This will be merged soon. |
Many crates provide attribute macros. Today, this requires defining proc
macros, in a separate crate, typically with several additional dependencies
adding substantial compilation time, and typically guarded by a feature that
users need to remember to enable.
However, many common cases of attribute macros don't require any more power
than an ordinary
macro_rules!
macro. Supporting these common cases wouldallow many crates to avoid defining proc macros, reduce dependencies and
compilation time, and provide these macros unconditionally without requiring a
the user to enable a feature.
I've reviewed several existing proc-macro-based attributes in the ecosystem,
and it appears that many would be able to use this feature to avoid needing
proc macros at all.
Rendered