-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Implement declarative (macro_rules!
) attribute macros (RFC 3697)
#144579
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
Conversation
rustbot has assigned @petrochenkov. Use |
The Miri subtree was changed cc @rust-lang/miri |
This comment was marked as outdated.
This comment was marked as outdated.
Here's an edge case that probably needs a test. Code and error#![feature(macro_attr)]
#[foo]
macro_rules! foo {
attr() { $($body:tt)* } => {
$($body)*
}
}
use foo;
|
This comment was marked as outdated.
This comment was marked as outdated.
@theemathas I can't reproduce that here. I get a different error instead:
This isn't the right error message, but the error is getting caught. (Suggestions welcome for how to catch and handle this case better.) |
This comment was marked as outdated.
This comment was marked as outdated.
@theemathas I looked into your test case more. I managed to find a way to reproduce the error you got; it only reproduces if there aren't other errors. So, I can reproduce it with the following test: #![crate_type = "lib"]
#![feature(macro_attr)]
#[attr]
macro_rules! attr {
attr() {} => {}
}
#[attr]
struct S; This gives:
The second error seems reasonable for attempting to invoke an attribute on itself. The first error also largely seems fine, except that it shouldn't say Note that "cannot find" is the same error that you get if you try this: mac! {
macro_rules! mac {
{} => {}
}
}
mac! {
struct S;
} This gives:
So, I think "cannot find ... in this scope" is an acceptable error, albeit one we might be able to improve. I think the main problem here is the spurious |
This comment has been minimized.
This comment has been minimized.
I think this is ready for review. The error messages could always be better, and there are additional cases that need handling (notably glob imports), but I think it would help to have some review before tackling those. |
This comment was marked as resolved.
This comment was marked as resolved.
(I started looking today, but will continue only on ~Monday.) |
cc @Muscraft |
This handles various kinds of errors, but does not allow applying the attributes yet. This adds the feature gate `macro_attr`.
…ttribute Avoid saying "a declarative macro cannot be used as an attribute macro"; instead, say that the macro has no `attr` rules.
Add a FIXME for moving this error earlier.
Add infrastructure to apply an attribute macro given argument tokens and body tokens. Teach the resolver to consider `macro_rules` macros when looking for an attribute via a path. This does not yet handle local `macro_rules` attributes.
Teach the resolver to consider `macro_rules` macros when looking for a local attribute. When looking for an attribute and considering a `macro_rules` macro, load the macro in order to see if it has attribute rules. Include a FIXME about tracking multiple macro kinds for a Def instead.
Test macros via path and local macros.
…cursively This allows a macro attribute to implement default arguments by reapplying itself with the defaults filled in, for instance.
I've rebased the series to eliminate all cases of a commit later in the series fixing code introduced earlier in the series. I've confirmed that the result is identical to the final result after the previous series, and that The commits now follow a logical progression:
@bors r=petrochenkov rollup |
🌲 The tree is currently closed for pull requests below priority 100. This pull request will be tested once the tree is reopened. |
Rollup of 8 pull requests Successful merges: - #139451 (Add `target_env = "macabi"` and `target_env = "sim"`) - #144039 (Use `tcx.short_string()` in more diagnostics) - #144192 (atomicrmw on pointers: move integer-pointer cast hacks into backend) - #144545 (In rustc_pattern_analysis, put `true` witnesses before `false` witnesses) - #144579 (Implement declarative (`macro_rules!`) attribute macros (RFC 3697)) - #144649 (Account for bare tuples and `Pin` methods in field searching logic) - #144775 (more strongly dissuade use of `skip_binder`) - #144987 (Enable f16 and f128 on targets that were fixed in LLVM21) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of #144579 - joshtriplett:mbe-attr, r=petrochenkov Implement declarative (`macro_rules!`) attribute macros (RFC 3697) This implements [RFC 3697](#143547), "Declarative (`macro_rules!`) attribute macros". I would suggest reading this commit-by-commit. This first introduces the feature gate, then adds parsing for attribute rules (doing nothing with them), then adds the ability to look up and apply `macro_rules!` attributes by path, then adds support for local attributes, then adds a test, and finally makes various improvements to errors.
Somehow this seems to have slightly improved tokentree muching performance :) #145126 (comment) |
Fun! That's comforting to hear, I had worried it might have a performance impact. |
Rollup of 8 pull requests Successful merges: - rust-lang/rust#139451 (Add `target_env = "macabi"` and `target_env = "sim"`) - rust-lang/rust#144039 (Use `tcx.short_string()` in more diagnostics) - rust-lang/rust#144192 (atomicrmw on pointers: move integer-pointer cast hacks into backend) - rust-lang/rust#144545 (In rustc_pattern_analysis, put `true` witnesses before `false` witnesses) - rust-lang/rust#144579 (Implement declarative (`macro_rules!`) attribute macros (RFC 3697)) - rust-lang/rust#144649 (Account for bare tuples and `Pin` methods in field searching logic) - rust-lang/rust#144775 (more strongly dissuade use of `skip_binder`) - rust-lang/rust#144987 (Enable f16 and f128 on targets that were fixed in LLVM21) r? `@ghost` `@rustbot` modify labels: rollup
@theemathas Turns out your cyclic attr test case is also getting fixed by @petrochenkov's suggestion to handle multiple macro kinds up front. That led to fixing the diagnostics to better distinguish between various error cases. |
@petrochenkov #145153 implements the change to support a macro with multiple kinds. Turned out very nicely, and led naturally to fixing and cleaning up various things. Thanks! |
Rollup of 8 pull requests Successful merges: - rust-lang#139451 (Add `target_env = "macabi"` and `target_env = "sim"`) - rust-lang#144039 (Use `tcx.short_string()` in more diagnostics) - rust-lang#144192 (atomicrmw on pointers: move integer-pointer cast hacks into backend) - rust-lang#144545 (In rustc_pattern_analysis, put `true` witnesses before `false` witnesses) - rust-lang#144579 (Implement declarative (`macro_rules!`) attribute macros (RFC 3697)) - rust-lang#144649 (Account for bare tuples and `Pin` methods in field searching logic) - rust-lang#144775 (more strongly dissuade use of `skip_binder`) - rust-lang#144987 (Enable f16 and f128 on targets that were fixed in LLVM21) r? `@ghost` `@rustbot` modify labels: rollup
This implements RFC 3697, "Declarative (
macro_rules!
) attribute macros".I would suggest reading this commit-by-commit. This first introduces the
feature gate, then adds parsing for attribute rules (doing nothing with them),
then adds the ability to look up and apply
macro_rules!
attributes by path,then adds support for local attributes, then adds a test, and finally makes
various improvements to errors.