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

Open
wants to merge 2 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

This comment was marked as resolved.

@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.

@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.)

@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
@joshtriplett
Copy link
Member Author

@rustbot ready

@rustbot rustbot 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 14, 2025
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@petrochenkov
Copy link
Contributor

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.

Probably because of this FIXME in expand.rs

                                    // FIXME: Consider using the derive resolutions (`_exts`)
                                    // instead of enqueuing the derives to be resolved again later.

I don't think it's too important to fix, identically looking diagnostics are not shown to users anyway (UI tests suite specifically disables that deduplication).

It's suspicious that the parent_scope values are different though.

@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 14, 2025
@joshtriplett
Copy link
Member Author

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.

Probably because of this FIXME in expand.rs

                                    // FIXME: Consider using the derive resolutions (`_exts`)
                                    // instead of enqueuing the derives to be resolved again later.

Oh. Yeah, that would do it; thanks.

I don't think it's too important to fix, identically looking diagnostics are not shown to users anyway (UI tests suite specifically disables that deduplication).

Fair enough. It does seem worth fixing for performance reasons if nothing else, but then, the FIXME is there for a reason. I'll ignore it for now, thank you.

It's suspicious that the parent_scope values are different though.

That code appears to generate fresh expansions for the enqueuing.

I poked pretty extensively at test cases, though, and couldn't find any case where it happened more than twice. So, I think this is worst case 2N, not M*N or N^2.

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.

Note that due to a pre-existing FIXME in `expand.rs`, derives are
re-queued and some errors get emitted twice. Duplicate diagnostic
suppression makes them not visible, but the FIXME should still get
fixed.
@joshtriplett
Copy link
Member Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 14, 2025
@joshtriplett joshtriplett marked this pull request as ready for review August 15, 2025 00:13
@petrochenkov
Copy link
Contributor

@bors r+

@bors
Copy link
Collaborator

bors commented Aug 15, 2025

📌 Commit 354fcf2 has been approved by petrochenkov

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 15, 2025
jhpratt added a commit to jhpratt/rust that referenced this pull request Aug 16, 2025
…enkov

Implement declarative (`macro_rules!`) derive macros (RFC 3698)

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

This is layered atop rust-lang#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: rust-lang#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.
Zalathar added a commit to Zalathar/rust that referenced this pull request Aug 16, 2025
…enkov

Implement declarative (`macro_rules!`) derive macros (RFC 3698)

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

This is layered atop rust-lang#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: rust-lang#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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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