-
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
base: master
Are you sure you want to change the base?
Conversation
rustbot has assigned @petrochenkov. Use |
The Miri subtree was changed cc @rust-lang/miri |
This handles various kinds of errors, but does not allow applying the attributes yet.
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.
Test macros via path and local macros.
Now that `macro_rules` macros can define attribute rules, make sure error messages account for that.
Add a corresponding test.
…ttribute Avoid saying "a declarative macro cannot be used as an attribute macro"; instead, say that the macro has no `attr` rules.
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 |
The job Click to see the possible cause of the failure (guessed by this bot)
|
`failed_to_match_macro` and `failed_to_match_macro_attr` had a fair bit in common. Unify the two, and handle the differences with conditionals.
…cursively This allows a macro attribute to implement default arguments by reapplying itself with the defaults filled in, for instance.
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.) |
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.