-
Notifications
You must be signed in to change notification settings - Fork 13.8k
:vis matcher for macro_rules #41012
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
:vis matcher for macro_rules #41012
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a248949
Implementation of the `vis` macro matcher.
DanielKeep d53e413
update :vis implementation to current rust
durka 06411c4
update print_visibility for new pub(restricted) syntax
durka 16010c2
parse interpolated visibility tokens
durka 06d3233
move NtVis enum variant to stave off comment rot
durka 37459e1
widen :vis follow set
durka 1d46805
document :vis in unstable book
durka e0cd766
feature gate :vis matcher
durka 51c8af5
reduce macro rule duplication in test
durka cfa51f2
satisfy completely useless tidy check
durka 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
Next
Next commit
Implementation of the
vis
macro matcher.
- Loading branch information
commit a2489495d909c43cfbefaeb79db6a77b13908257
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
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
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
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
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
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,119 @@ | ||
#![allow(dead_code, unused_imports)] | ||
#![feature(pub_restricted)] | ||
|
||
/** | ||
Ensure that `:vis` matches can be captured in existing positions, and passed | ||
through without the need for reparse tricks. | ||
*/ | ||
macro_rules! vis_passthru { | ||
($vis:vis const $name:ident: $ty:ty = $e:expr;) => { $vis const $name: $ty = $e; }; | ||
($vis:vis enum $name:ident {}) => { $vis struct $name {} }; | ||
($vis:vis extern "C" fn $name:ident() {}) => { $vis extern "C" fn $name() {} }; | ||
($vis:vis fn $name:ident() {}) => { $vis fn $name() {} }; | ||
($vis:vis mod $name:ident {}) => { $vis mod $name {} }; | ||
($vis:vis static $name:ident: $ty:ty = $e:expr;) => { $vis static $name: $ty = $e; }; | ||
($vis:vis struct $name:ident;) => { $vis struct $name; }; | ||
($vis:vis trait $name:ident {}) => { $vis trait $name {} }; | ||
($vis:vis type $name:ident = $ty:ty;) => { $vis type $name = $ty; }; | ||
($vis:vis use $path:ident as $name:ident;) => { $vis use self::$path as $name; }; | ||
} | ||
|
||
mod with_pub { | ||
vis_passthru! { pub const A: i32 = 0; } | ||
vis_passthru! { pub enum B {} } | ||
vis_passthru! { pub extern "C" fn c() {} } | ||
vis_passthru! { pub mod d {} } | ||
vis_passthru! { pub static E: i32 = 0; } | ||
vis_passthru! { pub struct F; } | ||
vis_passthru! { pub trait G {} } | ||
vis_passthru! { pub type H = i32; } | ||
vis_passthru! { pub use A as I; } | ||
} | ||
|
||
mod without_pub { | ||
vis_passthru! { const A: i32 = 0; } | ||
vis_passthru! { enum B {} } | ||
vis_passthru! { extern "C" fn c() {} } | ||
vis_passthru! { mod d {} } | ||
vis_passthru! { static E: i32 = 0; } | ||
vis_passthru! { struct F; } | ||
vis_passthru! { trait G {} } | ||
vis_passthru! { type H = i32; } | ||
vis_passthru! { use A as I; } | ||
} | ||
|
||
mod with_pub_restricted { | ||
vis_passthru! { pub(crate) const A: i32 = 0; } | ||
vis_passthru! { pub(crate) enum B {} } | ||
vis_passthru! { pub(crate) extern "C" fn c() {} } | ||
vis_passthru! { pub(crate) mod d {} } | ||
vis_passthru! { pub(crate) static E: i32 = 0; } | ||
vis_passthru! { pub(crate) struct F; } | ||
vis_passthru! { pub(crate) trait G {} } | ||
vis_passthru! { pub(crate) type H = i32; } | ||
vis_passthru! { pub(crate) use A as I; } | ||
} | ||
|
||
mod garden { | ||
mod with_pub_restricted_path { | ||
vis_passthru! { pub(::garden) const A: i32 = 0; } | ||
vis_passthru! { pub(::garden) enum B {} } | ||
vis_passthru! { pub(::garden) extern "C" fn c() {} } | ||
vis_passthru! { pub(::garden) mod d {} } | ||
vis_passthru! { pub(::garden) static E: i32 = 0; } | ||
vis_passthru! { pub(::garden) struct F; } | ||
vis_passthru! { pub(::garden) trait G {} } | ||
vis_passthru! { pub(::garden) type H = i32; } | ||
vis_passthru! { pub(::garden) use A as I; } | ||
} | ||
} | ||
|
||
/* | ||
Ensure that the `:vis` matcher works in a more complex situation: parsing a | ||
struct definition. | ||
*/ | ||
macro_rules! vis_parse_struct { | ||
/* | ||
The rule duplication is currently unavoidable due to the leading attribute | ||
matching. | ||
*/ | ||
|
||
($(#[$($attrs:tt)*])* pub($($vis:tt)*) struct $name:ident {$($body:tt)*}) => { | ||
vis_parse_struct! { @parse_fields $(#[$($attrs)*])*, pub($($vis)*), $name, $($body)* } | ||
}; | ||
($(#[$($attrs:tt)*])* pub struct $name:ident {$($body:tt)*}) => { | ||
vis_parse_struct! { @parse_fields $(#[$($attrs)*])*, pub, $name, $($body)* } | ||
}; | ||
($(#[$($attrs:tt)*])* struct $name:ident {$($body:tt)*}) => { | ||
vis_parse_struct! { @parse_fields $(#[$($attrs)*])*, , $name, $($body)* } | ||
}; | ||
|
||
($(#[$($attrs:tt)*])* pub($($vis:tt)*) struct $name:ident ($($body:tt)*);) => { | ||
vis_parse_struct! { @parse_tuple $(#[$($attrs)*])*, pub($($vis)*), $name, $($body)* } | ||
}; | ||
($(#[$($attrs:tt)*])* pub struct $name:ident ($($body:tt)*);) => { | ||
vis_parse_struct! { @parse_tuple $(#[$($attrs)*])*, pub, $name, $($body)* } | ||
}; | ||
($(#[$($attrs:tt)*])* struct $name:ident ($($body:tt)*);) => { | ||
vis_parse_struct! { @parse_tuple $(#[$($attrs)*])*, , $name, $($body)* } | ||
}; | ||
|
||
(@parse_fields $(#[$attrs:meta])*, $vis:vis, $name:ident, $($fvis:vis $fname:ident: $fty:ty),* $(,)*) => { | ||
$(#[$attrs])* $vis struct $name { $($fvis $fname: $fty,)* } | ||
}; | ||
|
||
(@parse_tuple $(#[$attrs:meta])*, $vis:vis, $name:ident, $($fvis:vis $fty:ty),* $(,)*) => { | ||
$(#[$attrs])* $vis struct $name ( $($fvis $fty,)* ); | ||
}; | ||
} | ||
|
||
mod test_struct { | ||
vis_parse_struct! { pub(crate) struct A { pub a: i32, b: i32, pub(crate) c: i32 } } | ||
vis_parse_struct! { pub struct B { a: i32, pub(crate) b: i32, pub c: i32 } } | ||
vis_parse_struct! { struct C { pub(crate) a: i32, pub b: i32, c: i32 } } | ||
|
||
vis_parse_struct! { pub(crate) struct D (pub i32, i32, pub(crate) i32); } | ||
vis_parse_struct! { pub struct E (i32, pub(crate) i32, pub i32); } | ||
vis_parse_struct! { struct F (pub(crate) i32, pub i32, i32); } | ||
} | ||
|
||
fn main() {} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now the comment
// These are not exposed to macros ....
applies toNtVis
, it's better moved higher in the list.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.