-
Notifications
You must be signed in to change notification settings - Fork 919
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
Extend cfg_if! support to cfg_match! #6522
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
There are no files selected for viewing
This file contains 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 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 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,80 @@ | ||
use std::panic::{AssertUnwindSafe, catch_unwind}; | ||
|
||
use rustc_ast::ast; | ||
use rustc_ast::token::{Delimiter, TokenKind}; | ||
use rustc_parse::exp; | ||
use rustc_parse::parser::ForceCollect; | ||
|
||
use crate::parse::macros::build_stream_parser; | ||
use crate::parse::session::ParseSess; | ||
|
||
pub(crate) fn parse_cfg_match<'a>( | ||
psess: &'a ParseSess, | ||
mac: &'a ast::MacCall, | ||
) -> Result<Vec<ast::Item>, &'static str> { | ||
match catch_unwind(AssertUnwindSafe(|| parse_cfg_match_inner(psess, mac))) { | ||
Ok(Ok(items)) => Ok(items), | ||
Ok(err @ Err(_)) => err, | ||
Err(..) => Err("failed to parse cfg_match!"), | ||
} | ||
} | ||
|
||
fn parse_cfg_match_inner<'a>( | ||
psess: &'a ParseSess, | ||
mac: &'a ast::MacCall, | ||
) -> Result<Vec<ast::Item>, &'static str> { | ||
let ts = mac.args.tokens.clone(); | ||
let mut parser = build_stream_parser(psess.inner(), ts); | ||
|
||
if parser.token == TokenKind::OpenDelim(Delimiter::Brace) { | ||
return Err("Expression position cfg_match! not yet supported"); | ||
} | ||
|
||
let mut items = vec![]; | ||
|
||
while parser.token.kind != TokenKind::Eof { | ||
if !parser.eat_keyword(exp!(Underscore)) { | ||
parser.parse_attr_item(ForceCollect::No).map_err(|e| { | ||
e.cancel(); | ||
"Failed to parse attr item" | ||
})?; | ||
} | ||
|
||
if !parser.eat(exp!(FatArrow)) { | ||
return Err("Expected a fat arrow"); | ||
} | ||
|
||
if !parser.eat(exp!(OpenBrace)) { | ||
return Err("Expected an opening brace"); | ||
} | ||
|
||
while parser.token != TokenKind::CloseDelim(Delimiter::Brace) | ||
&& parser.token.kind != TokenKind::Eof | ||
{ | ||
let item = match parser.parse_item(ForceCollect::No) { | ||
Ok(Some(item_ptr)) => item_ptr.into_inner(), | ||
Ok(None) => continue, | ||
Err(err) => { | ||
err.cancel(); | ||
parser.psess.dcx().reset_err_count(); | ||
return Err( | ||
"Expected item inside cfg_match block, but failed to parse it as an item", | ||
); | ||
} | ||
}; | ||
if let ast::ItemKind::Mod(..) = item.kind { | ||
items.push(item); | ||
} | ||
} | ||
|
||
if !parser.eat(exp!(CloseBrace)) { | ||
return Err("Expected a closing brace"); | ||
} | ||
|
||
if parser.eat(exp!(Eof)) { | ||
break; | ||
} | ||
} | ||
|
||
Ok(items) | ||
} |
This file contains 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 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 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,2 @@ | ||
|
||
pub fn format_me_please_1( ) { } |
This file contains 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,2 @@ | ||
|
||
pub fn format_me_please_2( ) { } |
This file contains 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,2 @@ | ||
|
||
pub fn format_me_please_3( ) { } |
This file contains 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,2 @@ | ||
|
||
pub fn format_me_please_4( ) { } |
This file contains 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,16 @@ | ||
#![feature(cfg_match)] | ||
|
||
std::cfg_match! { | ||
test => { | ||
mod format_me_please_1; | ||
} | ||
target_family = "unix" => { | ||
mod format_me_please_2; | ||
} | ||
cfg(target_pointer_width = "32") => { | ||
mod format_me_please_3; | ||
} | ||
_ => { | ||
mod format_me_please_4; | ||
} | ||
} |
This file contains 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,3 @@ | ||
its_a_macro! { | ||
// Contents | ||
} |
This file contains 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 @@ | ||
pub fn format_me_please_1() {} |
This file contains 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 @@ | ||
pub fn format_me_please_2() {} |
This file contains 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 @@ | ||
pub fn format_me_please_3() {} |
This file contains 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 @@ | ||
pub fn format_me_please_4() {} |
This file contains 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,16 @@ | ||
#![feature(cfg_match)] | ||
|
||
std::cfg_match! { | ||
test => { | ||
mod format_me_please_1; | ||
} | ||
target_family = "unix" => { | ||
mod format_me_please_2; | ||
} | ||
cfg(target_pointer_width = "32") => { | ||
mod format_me_please_3; | ||
} | ||
_ => { | ||
mod format_me_please_4; | ||
} | ||
} |
This file contains 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,3 @@ | ||
its_a_macro! { | ||
// Contents | ||
} |
Oops, something went wrong.
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.
I want to draw attention to this:
cfg_match!
is checking that the last path segment iscfg_match!
(i.e. tries to processany::path::cfg_match!
), whereascfg_if!
is checking that the first path segment iscfg_if!
(i.e. tries to processcfg_if::any::path!
). I think my behavior is more correct, and the worst case scenario is that some more files get discovered when a customcfg_match!
that looks similar enough tocore::cfg_match!
is used, but it bears pointing out.If factoring this behavior out to be the same between the two is desirable, I can do that, and would choose to check the macro-ident, not the path front. (The current
cfg_if!
processing will skip::cfg_if::cfg_if!
, since the path starts withkw::PathRoot
.)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.
I went ahead and wrote the patch to unify the predicate: a129548
The visitor could be further refactored into a single
KnownMacroVisitor
which processes bothcfg_if!
andcfg_match!
, but that starts to feel a bit excessive...I haven't included it in this PR since it's behavior changing for the existing
cfg_if!
support, to keep this PR scoped to just thecfg_match!
support.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.
... and I ended up doing that cleanup as well: 4ae9dff
But I'm actually done doing extended cleanup for this now 😅
To be fully explicit about it: the two linked commits in this and the prior comment are made available under the same license terms as if I had directly contributed them by placing them in a PR. Feel free to pull them (or ask me to PR them) if you believe them to be an improvement.