-
Notifications
You must be signed in to change notification settings - Fork 13.5k
make cfg_select
a builtin macro
#143461
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
make cfg_select
a builtin macro
#143461
Changes from all commits
Commits
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 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,63 @@ | ||
use rustc_ast::tokenstream::TokenStream; | ||
use rustc_attr_parsing as attr; | ||
use rustc_expand::base::{DummyResult, ExpandResult, ExtCtxt, MacroExpanderResult}; | ||
use rustc_parse::parser::cfg_select::{CfgSelectBranches, CfgSelectRule, parse_cfg_select}; | ||
use rustc_span::{Ident, Span, sym}; | ||
|
||
use crate::errors::{CfgSelectNoMatches, CfgSelectUnreachable}; | ||
|
||
/// Selects the first arm whose rule evaluates to true. | ||
fn select_arm(ecx: &ExtCtxt<'_>, branches: CfgSelectBranches) -> Option<(TokenStream, Span)> { | ||
for (cfg, tt, arm_span) in branches.reachable { | ||
if attr::cfg_matches( | ||
&cfg, | ||
&ecx.sess, | ||
ecx.current_expansion.lint_node_id, | ||
Some(ecx.ecfg.features), | ||
) { | ||
return Some((tt, arm_span)); | ||
} | ||
} | ||
|
||
branches.wildcard.map(|(_, tt, span)| (tt, span)) | ||
} | ||
|
||
pub(super) fn expand_cfg_select<'cx>( | ||
ecx: &'cx mut ExtCtxt<'_>, | ||
sp: Span, | ||
tts: TokenStream, | ||
) -> MacroExpanderResult<'cx> { | ||
ExpandResult::Ready(match parse_cfg_select(&mut ecx.new_parser_from_tts(tts)) { | ||
Ok(branches) => { | ||
if let Some((underscore, _, _)) = branches.wildcard { | ||
// Warn for every unreachable rule. We store the fully parsed branch for rustfmt. | ||
for (rule, _, _) in &branches.unreachable { | ||
let span = match rule { | ||
CfgSelectRule::Wildcard(underscore) => underscore.span, | ||
CfgSelectRule::Cfg(cfg) => cfg.span(), | ||
}; | ||
let err = CfgSelectUnreachable { span, wildcard_span: underscore.span }; | ||
ecx.dcx().emit_warn(err); | ||
petrochenkov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
if let Some((tts, arm_span)) = select_arm(ecx, branches) { | ||
return ExpandResult::from_tts( | ||
ecx, | ||
tts, | ||
sp, | ||
arm_span, | ||
Ident::with_dummy_span(sym::cfg_select), | ||
); | ||
} else { | ||
// Emit a compiler error when none of the rules matched. | ||
let guar = ecx.dcx().emit_err(CfgSelectNoMatches { span: sp }); | ||
DummyResult::any(sp, guar) | ||
} | ||
} | ||
Err(err) => { | ||
let guar = err.emit(); | ||
DummyResult::any(sp, guar) | ||
} | ||
}) | ||
} |
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
petrochenkov marked this conversation as resolved.
Show resolved
Hide resolved
|
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,73 @@ | ||
use rustc_ast::token::Token; | ||
use rustc_ast::tokenstream::{TokenStream, TokenTree}; | ||
use rustc_ast::{MetaItemInner, token}; | ||
use rustc_errors::PResult; | ||
use rustc_span::Span; | ||
|
||
use crate::exp; | ||
use crate::parser::Parser; | ||
|
||
pub enum CfgSelectRule { | ||
Cfg(MetaItemInner), | ||
Wildcard(Token), | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct CfgSelectBranches { | ||
/// All the conditional branches. | ||
pub reachable: Vec<(MetaItemInner, TokenStream, Span)>, | ||
/// The first wildcard `_ => { ... }` branch. | ||
pub wildcard: Option<(Token, TokenStream, Span)>, | ||
/// All branches after the first wildcard, including further wildcards. | ||
/// These branches are kept for formatting. | ||
pub unreachable: Vec<(CfgSelectRule, TokenStream, Span)>, | ||
} | ||
|
||
/// Parses a `TokenTree` that must be of the form `{ /* ... */ }`, and returns a `TokenStream` where | ||
/// the surrounding braces are stripped. | ||
fn parse_token_tree<'a>(p: &mut Parser<'a>) -> PResult<'a, TokenStream> { | ||
// Generate an error if the `=>` is not followed by `{`. | ||
if p.token != token::OpenBrace { | ||
p.expect(exp!(OpenBrace))?; | ||
} | ||
|
||
// Strip the outer '{' and '}'. | ||
match p.parse_token_tree() { | ||
TokenTree::Token(..) => unreachable!("because of the expect above"), | ||
TokenTree::Delimited(.., tts) => Ok(tts), | ||
} | ||
} | ||
|
||
pub fn parse_cfg_select<'a>(p: &mut Parser<'a>) -> PResult<'a, CfgSelectBranches> { | ||
let mut branches = CfgSelectBranches::default(); | ||
|
||
while p.token != token::Eof { | ||
if p.eat_keyword(exp!(Underscore)) { | ||
let underscore = p.prev_token; | ||
p.expect(exp!(FatArrow))?; | ||
|
||
let tts = parse_token_tree(p)?; | ||
let span = underscore.span.to(p.token.span); | ||
|
||
match branches.wildcard { | ||
None => branches.wildcard = Some((underscore, tts, span)), | ||
Some(_) => { | ||
branches.unreachable.push((CfgSelectRule::Wildcard(underscore), tts, span)) | ||
} | ||
} | ||
} else { | ||
let meta_item = p.parse_meta_item_inner()?; | ||
p.expect(exp!(FatArrow))?; | ||
|
||
let tts = parse_token_tree(p)?; | ||
let span = meta_item.span().to(p.token.span); | ||
|
||
match branches.wildcard { | ||
None => branches.reachable.push((meta_item, tts, span)), | ||
Some(_) => branches.unreachable.push((CfgSelectRule::Cfg(meta_item), tts, span)), | ||
} | ||
} | ||
} | ||
|
||
Ok(branches) | ||
} |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.