-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Declarative macro_rules!
derive macros
#3698
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
Open
joshtriplett
wants to merge
40
commits into
rust-lang:master
Choose a base branch
from
joshtriplett:declarative-derive-macros
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+303
−0
Open
Changes from 2 commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
a3cd084
Declarative `macro_rules!` derive macros
joshtriplett 567411b
Give a more realistic example
joshtriplett f3f5de9
Add alternative about allowing direct invocation
joshtriplett 65b1053
Mention `$crate`
joshtriplett 923325f
Caching
joshtriplett ba77548
Clarify text about helper attributes
joshtriplett 8881b68
Future possibilities: Better error reporting
joshtriplett f195edf
Mention automatically_derived
joshtriplett c4b185b
Future possibilities: Error recovery
joshtriplett 7bcdf3b
Add drawbacks section mentioning impact on crate maintainers
joshtriplett 7e1d517
Expand future work
joshtriplett bf67d21
Future work: Namespacing helper attributes
joshtriplett 32d91b6
Future work: helpers for `where` bounds
joshtriplett 9faa8f4
Add unresolved question about avoid cascading errors due to missing i…
joshtriplett 5ee8fe0
Add unresolved question to make sure we don't have awful error messages
joshtriplett 8352b1c
Future work: helpers for higher-level concepts like struct fields
joshtriplett f797596
Add further steps about averting pressure on crate maintainers
joshtriplett 3526d7f
Copy a drawback to the unresolved questions section
joshtriplett ba7effc
Fix typo
joshtriplett d4702b8
Future work: unsafe derives
joshtriplett aaf9860
Future possibilities: parameters
joshtriplett c6a2d35
Example
joshtriplett a71fbf7
Elaborate on an alternative
joshtriplett cf8e13e
Add more future possibilities
joshtriplett d50dbff
Further discussion on future possibility of derives with parameters
joshtriplett c323473
Discuss syntax alternative: `derive(...)` rules, like RFC 3697's `att…
joshtriplett e088d55
Unresolved question: helper attribute namespacing and hygiene
joshtriplett 319ca10
Elaborate a future work item
joshtriplett 5be851f
Future possibilities: const Trait and similar
joshtriplett 07f297d
Updates from design meeting
joshtriplett 0bb3ea4
Future work: talk about namespaced, hygienic helper attributes
joshtriplett d38892f
Future work: suggest a possible naming lint
joshtriplett cae86ff
Add unsafe derive rules and corresponding syntax
joshtriplett 98aca06
Add reference-level explanation with grammar additions
joshtriplett d448d19
Remove future possibility of unsafe, now that it's in the RFC
joshtriplett 504fa8c
Update to use only `derive(unsafe(DangerousDeriveMacro))` to match RF…
joshtriplett 27215e7
Update prior art section
joshtriplett ded1ced
Expand the motivation section
joshtriplett 1a2c1a3
Move mention of `derive_alias` to future work
joshtriplett 6f674fa
Add additional motivation
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,117 @@ | ||||||||||||||||||||||||||||||
- Feature Name: `declarative_derive_macros` | ||||||||||||||||||||||||||||||
- Start Date: 2024-09-20 | ||||||||||||||||||||||||||||||
- RFC PR: [rust-lang/rfcs#3698](https://github.com/rust-lang/rfcs/pull/3698) | ||||||||||||||||||||||||||||||
- Rust Issue: [rust-lang/rust#0000](https://github.com/rust-lang/rust/issues/0000) | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
# Summary | ||||||||||||||||||||||||||||||
[summary]: #summary | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
Support implementing `derive(Trait)` via a `macro_rules!` macro. | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
# Motivation | ||||||||||||||||||||||||||||||
[motivation]: #motivation | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
Many crates support deriving their traits with `derive(Trait)`. 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 derives 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 | ||||||||||||||||||||||||||||||
[guide-level-explanation]: #guide-level-explanation | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
You can define a macro to implement `derive(MyTrait)` by defining a | ||||||||||||||||||||||||||||||
`macro_rules!` macro with the `#[macro_derive]` attribute. Such a macro can | ||||||||||||||||||||||||||||||
create new items based on a struct, enum, or union. Note that the macro can | ||||||||||||||||||||||||||||||
only append new items; it cannot modify the item it was applied to. | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
For example: | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
```rust | ||||||||||||||||||||||||||||||
trait Answer { fn answer(&self) -> u32; } | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
#[macro_derive] | ||||||||||||||||||||||||||||||
macro_rules! Answer { | ||||||||||||||||||||||||||||||
// Simplified for this example | ||||||||||||||||||||||||||||||
(struct $n:ident $_:tt) => { | ||||||||||||||||||||||||||||||
joshtriplett marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||
impl Answer for $n { | ||||||||||||||||||||||||||||||
fn answer(&self) -> u32 { 42 } | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
#[derive(Answer)] | ||||||||||||||||||||||||||||||
struct Struct; | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
fn main() { | ||||||||||||||||||||||||||||||
let s = Struct; | ||||||||||||||||||||||||||||||
assert_eq!(42, s.answer()); | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
Comment on lines
+65
to
+71
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: There seems to be a missed opportunity to have
Suggested change
|
||||||||||||||||||||||||||||||
``` | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
Derive 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. | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
A derive macro may share the same path as a trait of the same name. For | ||||||||||||||||||||||||||||||
instance, the name `mycrate::MyTrait` can refer to both the `MyTrait` trait and | ||||||||||||||||||||||||||||||
the macro for `derive(MyTrait)`. This is consistent with existing derive | ||||||||||||||||||||||||||||||
macros. | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
A derive macro may also define *helper attributes*. These attributes are | ||||||||||||||||||||||||||||||
[inert](https://doc.rust-lang.org/reference/attributes.html#active-and-inert-attributes), | ||||||||||||||||||||||||||||||
and their only purpose is to be fed into the derive macro that defined them. | ||||||||||||||||||||||||||||||
That said, they can be seen by all macros. | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
To define helper attributes, put an attributes key in the `macro_derive` | ||||||||||||||||||||||||||||||
attribute, with a comma-separated list of identifiers for helper attributes: | ||||||||||||||||||||||||||||||
`#[macro_derive(attributes(helper))]`. The derive macro can process the | ||||||||||||||||||||||||||||||
`#[helper]` attribute, along with any arguments to it, as part of the item the | ||||||||||||||||||||||||||||||
derive macro was applied to. | ||||||||||||||||||||||||||||||
joshtriplett marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
If a derive macro mistakenly emits the token stream it was applied to | ||||||||||||||||||||||||||||||
(resulting in a duplicate item definition), the error the compiler emits for | ||||||||||||||||||||||||||||||
the duplicate item should hint to the user that the macro was defined | ||||||||||||||||||||||||||||||
incorrectly, and remind the user that derive macros only append new items. | ||||||||||||||||||||||||||||||
joshtriplett marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
# Rationale and alternatives | ||||||||||||||||||||||||||||||
joshtriplett marked this conversation as resolved.
Show resolved
Hide resolved
joshtriplett marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||
[rationale-and-alternatives]: #rationale-and-alternatives | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
Adding this feature will allow many crates in the ecosystem to drop their proc | ||||||||||||||||||||||||||||||
macro crates and corresponding dependencies, and decrease their build times. | ||||||||||||||||||||||||||||||
joshtriplett marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
Crates could instead define `macro_rules!` macros and encourage users to invoke | ||||||||||||||||||||||||||||||
them using existing syntax like `macroname! { ... }`, rather than using | ||||||||||||||||||||||||||||||
derives. 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 derive. In addition, this would | ||||||||||||||||||||||||||||||
not preserve the property derive macros normally have that they cannot change | ||||||||||||||||||||||||||||||
the item they are applied to. | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
A mechanism to define attribute macros would let people write attributes like | ||||||||||||||||||||||||||||||
`#[derive_mytrait]`, but that would not provide compatibility with existing | ||||||||||||||||||||||||||||||
derive syntax. | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
We could allow `macro_rules!` derive macros to emit a replacement token stream; | ||||||||||||||||||||||||||||||
however, that would be inconsistent with the restriction preventing proc macros | ||||||||||||||||||||||||||||||
from doing the same. | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
# Prior art | ||||||||||||||||||||||||||||||
[prior-art]: #prior-art | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
We have had proc-macro-based derive 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 derives, | ||||||||||||||||||||||||||||||
demonstrating a demand for this. This feature would allow defining such derives | ||||||||||||||||||||||||||||||
without requiring proc macros at all, and would support the same invocation | ||||||||||||||||||||||||||||||
syntax as a proc macro. | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
The `macro_derive` attribute and its `attributes` syntax are based on the | ||||||||||||||||||||||||||||||
[existing `proc_macro_derive` attribute for proc | ||||||||||||||||||||||||||||||
macros](https://doc.rust-lang.org/reference/procedural-macros.html#derive-macros). | ||||||||||||||||||||||||||||||
joshtriplett marked this conversation as resolved.
Show resolved
Hide resolved
joshtriplett marked this conversation as resolved.
Show resolved
Hide resolved
|
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.