Skip to content

Implement declarative (macro_rules!) derive macros (RFC 3698) #145208

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

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

joshtriplett
Copy link
Member

This is a draft for review, and should not be merged yet.

This is layered atop #145153 , and has
only two additional commits atop that. The first handles parsing and provides a
test for various parse errors. The second implements expansion and handles
application.

This implements RFC 3698, "Declarative (macro_rules!) derive macros".
Tracking issue: #143549

This has one remaining issue, which I could use some help debugging: in
tests/ui/macros/macro-rules-derive-error.rs, the diagnostics for
derive(fn_only) (for a fn_only with no derive rules) and
derive(ForwardReferencedDerive) both get emitted twice, as a duplicate
diagnostic.

From what I can tell via adding some debugging code,
unresolved_macro_suggestions is getting called twice from
finalize_macro_resolutions for each of them, because
self.single_segment_macro_resolutions has two entries for the macro, with two
different parent_scope values. I'm not clear on why that happened; it doesn't
happen with the equivalent code using attrs.

I'd welcome any suggestions for fixing this.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Aug 10, 2025
@joshtriplett
Copy link
Member Author

r? @petrochenkov

@joshtriplett joshtriplett removed T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-clippy Relevant to the Clippy team. labels Aug 10, 2025
@joshtriplett
Copy link
Member Author

(Removing T-clippy and T-rustdoc, because the two commits specific to this PR don't touch them.)

@rust-log-analyzer

This comment has been minimized.

@joshtriplett joshtriplett added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 10, 2025
@joshtriplett
Copy link
Member Author

(Marking as "waiting on review" because I'm seeking help with the test failure here.)

@bors
Copy link
Collaborator

bors commented Aug 11, 2025

☔ The latest upstream changes (presumably #135846) made this pull request unmergeable. Please resolve the merge conflicts.

@petrochenkov
Copy link
Contributor

Blocked on #145153.
@rustbot blocked

@rustbot rustbot added S-blocked Status: Blocked on something else such as an RFC or other implementation work. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 11, 2025
@rustbot rustbot added the T-clippy Relevant to the Clippy team. label Aug 11, 2025
@joshtriplett joshtriplett removed the T-clippy Relevant to the Clippy team. label Aug 11, 2025
@rust-log-analyzer

This comment was marked as outdated.

@rustbot rustbot added the T-clippy Relevant to the Clippy team. label Aug 12, 2025
@joshtriplett joshtriplett removed the T-clippy Relevant to the Clippy team. label Aug 12, 2025
@rust-log-analyzer

This comment has been minimized.

…e kind

Review everything that uses `MacroKind`, and switch anything that could
refer to more than one kind to use `MacroKinds`.

Add a new `SyntaxExtensionKind::MacroRules` for `macro_rules!` macros,
using the concrete `MacroRulesMacroExpander` type, and have it track
which kinds it can handle. Eliminate the separate optional `attr_ext`,
now that a `SyntaxExtension` can handle multiple macro kinds.

This also avoids the need to downcast when calling methods on
`MacroRulesMacroExpander`, such as `get_unused_rule`.

Integrate macro kind checking into name resolution's
`sub_namespace_match`, so that we only find a macro if it's the right
type, and eliminate the special-case hack for attributes.
I discovered this via research through the git log, and I want to leave
additional guidance for future macro spelunkers.
This eliminates the case in `failed_to_match_macro` to check for a
function-like invocation of a macro with no function-like rules.

Instead, macro kind mismatches now result in an unresolved macro, and we
detect this case in `unresolved_macro_suggestions`, which now carefully
distinguishes between a kind mismatch and other errors.

This also handles cases of forward-referenced attributes and cyclic
attributes.

Expand test coverage to include all of these cases.
The use of `Not` to describe the `!` in `macro_rules!` reads
confusingly, and also results in search collisions with the diagnostic
structure `MacroRulesNot` elsewhere in the compiler. Rename it to use
the more conventional `Bang` for `!`.
This updates two clippy lints which had exceptions for `MacroKind::Bang`
macros to extend those exceptions to any macro, now that a macro_rules
macro can be any kind of macro.
This makes the minimal fixes necessary for rustdoc to compile and pass
existing tests with the switch to `MacroKinds`. It only works for macros
that don't actually have multiple kinds, and will panic (with a `todo!`)
if it encounters a macro with multiple kinds.

rustdoc needs further fixes to handle macros with multiple kinds, and to
handle attributes and derive macros that aren't proc macros.
@joshtriplett joshtriplett added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-blocked Status: Blocked on something else such as an RFC or other implementation work. labels Aug 12, 2025
@joshtriplett
Copy link
Member Author

joshtriplett commented Aug 12, 2025

(Marking for review now that 145153 is queued.)

@petrochenkov
Copy link
Contributor

(I'll wait until the rebase anyway.)

This handles various kinds of errors, but does not allow applying the
derive yet.

This adds the feature gate `macro_derive`.
Add infrastructure to apply a derive macro to arguments, consuming and
returning a `TokenTree` only.

Handle `SyntaxExtensionKind::MacroRules` when expanding a derive, if the
macro's kinds support derive.

Add tests covering various cases of `macro_rules` derives.
@rustbot rustbot added the T-clippy Relevant to the Clippy team. label Aug 12, 2025
@joshtriplett
Copy link
Member Author

joshtriplett commented Aug 12, 2025

@petrochenkov Rebased atop the latest version of 145153. (All but the last two commits will go away once 145153 is merged.)

@joshtriplett joshtriplett removed the T-clippy Relevant to the Clippy team. label Aug 12, 2025
@rust-log-analyzer
Copy link
Collaborator

The job aarch64-gnu-llvm-19-1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)

@petrochenkov
Copy link
Contributor

I meant after #145153 is merged, the github interface makes reviewing stacked PRs quite inconvenient, so I basically never look at them.

@petrochenkov petrochenkov added S-blocked Status: Blocked on something else such as an RFC or other implementation work. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-blocked Status: Blocked on something else such as an RFC or other implementation work. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants