- Notifications
You must be signed in to change notification settings - Fork 14k
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
12 commits Select commit Hold shift + click to select a range
2054a0c mbe: In error messages, don't assume attributes are always proc macros
joshtriplett bad0d45 mbe: Parse macro attribute rules
joshtriplett f0a5e70 mbe: Fix error message for using a macro with no `attr` rules as an a…
joshtriplett 0cc0b11 mbe: Emit an error if a macro call has no function-like rules
joshtriplett 34be8ab mbe: Handle applying attribute rules with paths
joshtriplett 549c2fe mbe: Handle local `macro_rules` attr resolution
joshtriplett 4f999f7 mbe: Add test for `macro_rules` attributes
joshtriplett bd5206e mbe: Add test for attribute expansion with `compile_error!`
joshtriplett 1500195 mbe: Add a test for calling a macro with no function-like rules
joshtriplett 9a9ccc0 mbe: Add parser test for macro attribute recovery
joshtriplett 489734c mbe: Add a test confirming that a macro attribute can apply itself re…
joshtriplett f88839d mbe: Add a test checking for infinite recursion in macro attributes
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
mbe: Add test for
macro_rules attributes Test macros via path and local macros.
- Loading branch information
commit 4f999f72dee5e7085a2a5ed11cd917dcad5143be
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,90 @@ | ||
| //@ run-pass | ||
| //@ check-run-results | ||
| #![feature(macro_attr)] | ||
| #![warn(unused)] | ||
| | ||
| #[macro_export] | ||
| macro_rules! exported_attr { | ||
| attr($($args:tt)*) { $($body:tt)* } => { | ||
| println!( | ||
| "exported_attr: args={:?}, body={:?}", | ||
| stringify!($($args)*), | ||
| stringify!($($body)*), | ||
| ); | ||
| }; | ||
| { $($args:tt)* } => { | ||
| println!("exported_attr!({:?})", stringify!($($args)*)); | ||
| }; | ||
| attr() {} => { | ||
| unused_rule(); | ||
| }; | ||
| attr() {} => { | ||
| compile_error!(); | ||
| }; | ||
| {} => { | ||
| unused_rule(); | ||
| }; | ||
| {} => { | ||
| compile_error!(); | ||
| }; | ||
| } | ||
| | ||
| macro_rules! local_attr { | ||
| attr($($args:tt)*) { $($body:tt)* } => { | ||
| println!( | ||
| "local_attr: args={:?}, body={:?}", | ||
| stringify!($($args)*), | ||
| stringify!($($body)*), | ||
| ); | ||
| }; | ||
| { $($args:tt)* } => { | ||
| println!("local_attr!({:?})", stringify!($($args)*)); | ||
| }; | ||
| attr() {} => { //~ WARN: never used | ||
| unused_rule(); | ||
| }; | ||
| attr() {} => { | ||
| compile_error!(); | ||
| }; | ||
| {} => { //~ WARN: never used | ||
| unused_rule(); | ||
| }; | ||
| {} => { | ||
| compile_error!(); | ||
| }; | ||
| } | ||
| | ||
| fn main() { | ||
| #[crate::exported_attr] | ||
| struct S; | ||
| #[::exported_attr(arguments, key = "value")] | ||
| fn func(_arg: u32) {} | ||
| #[self::exported_attr(1)] | ||
| #[self::exported_attr(2)] | ||
| struct Twice; | ||
| | ||
| crate::exported_attr!(); | ||
| crate::exported_attr!(invoked, arguments); | ||
| | ||
| #[exported_attr] | ||
| struct S; | ||
| #[exported_attr(arguments, key = "value")] | ||
| fn func(_arg: u32) {} | ||
| #[exported_attr(1)] | ||
| #[exported_attr(2)] | ||
| struct Twice; | ||
| | ||
| exported_attr!(); | ||
| exported_attr!(invoked, arguments); | ||
| | ||
| #[local_attr] | ||
| struct S; | ||
| #[local_attr(arguments, key = "value")] | ||
| fn func(_arg: u32) {} | ||
| #[local_attr(1)] | ||
| #[local_attr(2)] | ||
| struct Twice; | ||
| | ||
| local_attr!(); | ||
| local_attr!(invoked, arguments); | ||
| } | ||
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,15 @@ | ||
| exported_attr: args="", body="struct S;" | ||
| exported_attr: args="arguments, key = \"value\"", body="fn func(_arg: u32) {}" | ||
| exported_attr: args="1", body="#[self::exported_attr(2)] struct Twice;" | ||
| exported_attr!("") | ||
| exported_attr!("invoked, arguments") | ||
| exported_attr: args="", body="struct S;" | ||
| exported_attr: args="arguments, key = \"value\"", body="fn func(_arg: u32) {}" | ||
| exported_attr: args="1", body="#[exported_attr(2)] struct Twice;" | ||
| exported_attr!("") | ||
| exported_attr!("invoked, arguments") | ||
| local_attr: args="", body="struct S;" | ||
| local_attr: args="arguments, key = \"value\"", body="fn func(_arg: u32) {}" | ||
| local_attr: args="1", body="#[local_attr(2)] struct Twice;" | ||
| local_attr!("") | ||
| local_attr!("invoked, arguments") |
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,21 @@ | ||
| warning: rule #3 of macro `local_attr` is never used | ||
| --> $DIR/macro-rules-attr.rs:43:9 | ||
| | | ||
| LL | attr() {} => { | ||
| | ^^ ^^ | ||
| | | ||
| note: the lint level is defined here | ||
| --> $DIR/macro-rules-attr.rs:4:9 | ||
| | | ||
| LL | #![warn(unused)] | ||
| | ^^^^^^ | ||
| = note: `#[warn(unused_macro_rules)]` implied by `#[warn(unused)]` | ||
| | ||
| warning: rule #5 of macro `local_attr` is never used | ||
| --> $DIR/macro-rules-attr.rs:49:5 | ||
| | | ||
| LL | {} => { | ||
| | ^^ | ||
| | ||
| warning: 2 warnings emitted | ||
| |
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.