make cfg_select a builtin macro
This commit is contained in:
63
compiler/rustc_builtin_macros/src/cfg_select.rs
Normal file
63
compiler/rustc_builtin_macros/src/cfg_select.rs
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -954,3 +954,21 @@ pub(crate) struct AsmExpectedOther {
|
||||
pub(crate) span: Span,
|
||||
pub(crate) is_inline_asm: bool,
|
||||
}
|
||||
|
||||
#[derive(Diagnostic)]
|
||||
#[diag(builtin_macros_cfg_select_no_matches)]
|
||||
pub(crate) struct CfgSelectNoMatches {
|
||||
#[primary_span]
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
#[derive(Diagnostic)]
|
||||
#[diag(builtin_macros_cfg_select_unreachable)]
|
||||
pub(crate) struct CfgSelectUnreachable {
|
||||
#[primary_span]
|
||||
#[label(builtin_macros_label2)]
|
||||
pub span: Span,
|
||||
|
||||
#[label]
|
||||
pub wildcard_span: Span,
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ mod autodiff;
|
||||
mod cfg;
|
||||
mod cfg_accessible;
|
||||
mod cfg_eval;
|
||||
mod cfg_select;
|
||||
mod compile_error;
|
||||
mod concat;
|
||||
mod concat_bytes;
|
||||
@@ -79,6 +80,7 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
|
||||
asm: asm::expand_asm,
|
||||
assert: assert::expand_assert,
|
||||
cfg: cfg::expand_cfg,
|
||||
cfg_select: cfg_select::expand_cfg_select,
|
||||
column: source_util::expand_column,
|
||||
compile_error: compile_error::expand_compile_error,
|
||||
concat: concat::expand_concat,
|
||||
|
||||
Reference in New Issue
Block a user