-
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
Merged
joshtriplett
merged 33 commits into
rust-lang:master
from
joshtriplett:declarative-attribute-macros.md
Jul 6, 2025
Merged
Changes from 4 commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
661912e
Declarative `macro_rules!` attribute macros
joshtriplett 6c9944b
RFC 3697
joshtriplett 6fd82e8
Attribute macros are active
joshtriplett 2b4989b
Fix typo
joshtriplett 258608e
Remove the extra `=>`
joshtriplett fa7820f
Abbreviate to `attr`, with `cfg_attr` as precedent
joshtriplett 0e4e46c
Mention `$crate`
joshtriplett 0fea475
Caching
joshtriplett b11f474
Add an alternative way of marking and handling attr/non-attr macros
joshtriplett d13e2ba
Clarify justification for having a single macro support attr and non-…
joshtriplett 0900f9f
Add notes about backwards compatibility of adding `attr` rules
joshtriplett 26e6969
Note that both `MacroMatcher`s share the same namespace
joshtriplett 5cfb620
Document the cases of no arguments and empty arguments
joshtriplett 10f011d
Future possibilities: better error reporting
joshtriplett 1cfec3d
Add drawbacks section mentioning impact on crate maintainers
joshtriplett b7d4e06
Expand future work
joshtriplett 0110363
Future possibilities: consider the case of multiple attributes in any…
joshtriplett 23cd82f
Recursion
joshtriplett 9fbf852
Clarify recursive invocation
joshtriplett 95838c4
Fix typo
joshtriplett ff26c82
Add unresolved question to make sure we don't have awful error messages
joshtriplett 25ab000
Word-wrapping
joshtriplett 66ac59f
Support unsafe attributes
joshtriplett 5fdd0d8
Copy a drawback to the unresolved questions section
joshtriplett f2eb3ed
Fix missing backquote
joshtriplett 27efc29
Example
joshtriplett e43905e
Document that an attribute macro may invoke another, or itself
joshtriplett 31f4eb7
Document how the compiler should handle using a non-attr macro as an …
joshtriplett cd2c9f5
Future possibility: attribute macros that don't modify the original t…
joshtriplett a7dde80
Copy motivating examples from prior art to motivation
joshtriplett be3c823
Fix typo
joshtriplett 7594f62
Document an additional unresolved question for evaluation during impl…
joshtriplett f4bfd55
RFC 3697
joshtriplett File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
- Feature Name: `declarative_attribute_macros` | ||
- Start Date: 2024-09-20 | ||
- RFC PR: [rust-lang/rfcs#3697](https://github.com/rust-lang/rfcs/pull/3697) | ||
- Rust Issue: [rust-lang/rust#0000](https://github.com/rust-lang/rust/issues/0000) | ||
|
||
# Summary | ||
[summary]: #summary | ||
|
||
Support defining `macro_rules!` macros that work as attribute macros. | ||
|
||
# Motivation | ||
[motivation]: #motivation | ||
|
||
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 would | ||
allow many crates to avoid defining proc macros, reduce dependencies and | ||
compilation time, and provide these macros unconditionally without requiring | ||
the user to enable a feature. | ||
|
||
# Guide-level explanation | ||
joshtriplett marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[guide-level-explanation]: #guide-level-explanation | ||
|
||
When defining a `macro_rules!` macro, you can prefix some of the macro's rules | ||
with `attribute(...) =>` to allow using the macro as an attribute. The | ||
arguments to the attribute, if any, are parsed by the *MacroMatcher* in the | ||
first set of parentheses; the second *MacroMatcher* parses the entire construct | ||
the attribute was applied to. The resulting macro will work anywhere an | ||
joshtriplett marked this conversation as resolved.
Show resolved
Hide resolved
|
||
attribute currently works. | ||
|
||
```rust | ||
macro_rules! main { | ||
attribute() => ($func:item) => { make_async_main!($func) }; | ||
joshtriplett marked this conversation as resolved.
Show resolved
Hide resolved
|
||
attribute(threads = $threads:literal) => ($func:item) => { make_async_main!($threads, $func) }; | ||
} | ||
|
||
#[main] | ||
async fn main() { ... } | ||
|
||
#[main(threads = 42)] | ||
async fn main() { ... } | ||
``` | ||
|
||
Attribute macros defined using `macro_rules!` follow the same scoping rules as | ||
any other macro, and may be invoked by any path that resolves to them. | ||
joshtriplett marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
An attribute macro must not require itself for resolution, either directly or | ||
indirectly (e.g. applied to a containing module or item). | ||
|
||
Note that a single macro can have both attribute and non-attribute rules. | ||
joshtriplett marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Attribute invocations can only match the attribute rules, and non-attribute | ||
invocations can only match the non-attribute rules. | ||
|
||
For simplicity, an attribute macro may not recursively invoke its attribute | ||
rules; to recurse, invoke a non-attribute rule or another macro. | ||
|
||
# Reference-level explanation | ||
[reference-level-explanation]: #reference-level-explanation | ||
joshtriplett marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
The grammar for macros is extended as follows: | ||
|
||
> _MacroRule_ :\ | ||
> ( `attribute` _MacroMatcher_ `=>` )<sup>?</sup> _MacroMatcher_ `=>` _MacroTranscriber_ | ||
|
||
The first _MacroMatcher_ matches the attribute's arguments, which will be an | ||
empty token tree if not present. The second _MacroMatcher_ matches the entire | ||
construct the attribute was applied to, receiving precisely what a | ||
proc-macro-based attribute would in the same place. | ||
|
||
This grammar addition is backwards compatible: previously, a _MacroRule_ could | ||
only start with `(`, `[`, or `{`, so the parser can easily distinguish the | ||
identifier `attribute`. | ||
|
||
Attribute macros declared using `macro_rules!` are | ||
[active](https://doc.rust-lang.org/reference/attributes.html#active-and-inert-attributes), | ||
just like those declared using proc macros. | ||
joshtriplett marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Rationale and alternatives | ||
[rationale-and-alternatives]: #rationale-and-alternatives | ||
joshtriplett marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Adding this feature will allow many crates in the ecosystem to drop their proc | ||
macro crates and corresponding dependencies, and decrease their build times. | ||
|
||
Crates could instead define `macro_rules!` macros and encourage users to invoke | ||
them using existing syntax like `macroname! { ... }`. This would provide the | ||
same functionality, but would not support the same syntax people are accustomed | ||
to, and could not maintain semver compatibility with an existing | ||
proc-macro-based attribute. | ||
|
||
We could require the `!` in attribute macros (`#[myattr!]` or similar). | ||
However, proc-macro-based attribute macros do not require this, and this would | ||
not allow declarative attribute macros to be fully compatible with | ||
proc-macro-based attribute macros. | ||
|
||
Many macros will want to parse their arguments and separately parse the | ||
construct they're applied to, rather than a combinatorial explosion of both. | ||
This problem is not unique to attribute macros. In both cases, the standard | ||
solution is to parse one while carrying along the other. | ||
|
||
We could use `attr` rather than `attribute`. Rust usually avoids abbreviating | ||
except for the most common constructs; however, this can occur repeatedly in | ||
multiple rules, so it may make sense to abbreviate it. | ||
|
||
# Prior art | ||
[prior-art]: #prior-art | ||
|
||
We have had proc-macro-based attribute macros for a long time, and the | ||
ecosystem makes extensive use of them. | ||
|
||
The [`macro_rules_attribute`](https://crates.io/crates/macro_rules_attribute) | ||
crate defines proc macros that allow invoking declarative macros as attributes, | ||
demonstrating a demand for this. This feature would allow defining such | ||
attributes without requiring proc macros at all, and would support the same | ||
invocation syntax as a proc macro. | ||
|
||
# Future possibilities | ||
[future-possibilities]: #future-possibilities | ||
|
||
We should provide a way to define `derive` macros declaratively, as well. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.