rustc_builtin_macros/
cfg_select.rs

1use rustc_ast::tokenstream::TokenStream;
2use rustc_attr_parsing as attr;
3use rustc_attr_parsing::{
4    CfgSelectBranches, CfgSelectPredicate, EvalConfigResult, ShouldEmit, parse_cfg_select,
5};
6use rustc_expand::base::{DummyResult, ExpandResult, ExtCtxt, MacroExpanderResult};
7use rustc_span::{Ident, Span, sym};
8
9use crate::errors::{CfgSelectNoMatches, CfgSelectUnreachable};
10
11/// Selects the first arm whose predicate evaluates to true.
12fn select_arm(ecx: &ExtCtxt<'_>, branches: CfgSelectBranches) -> Option<(TokenStream, Span)> {
13    let mut result = None;
14    for (cfg, tt, arm_span) in branches.reachable {
15        if let EvalConfigResult::True = attr::eval_config_entry(
16            &ecx.sess,
17            &cfg,
18            ecx.current_expansion.lint_node_id,
19            ShouldEmit::ErrorsAndLints,
20        ) {
21            // FIXME(#149215) Ideally we should short-circuit here, but `eval_config_entry` currently emits lints so we cannot do this yet.
22            result.get_or_insert((tt, arm_span));
23        }
24    }
25
26    let wildcard = branches.wildcard.map(|(_, tt, span)| (tt, span));
27    result.or(wildcard)
28}
29
30pub(super) fn expand_cfg_select<'cx>(
31    ecx: &'cx mut ExtCtxt<'_>,
32    sp: Span,
33    tts: TokenStream,
34) -> MacroExpanderResult<'cx> {
35    ExpandResult::Ready(
36        match parse_cfg_select(
37            &mut ecx.new_parser_from_tts(tts),
38            ecx.sess,
39            Some(ecx.ecfg.features),
40            ecx.current_expansion.lint_node_id,
41        ) {
42            Ok(branches) => {
43                if let Some((underscore, _, _)) = branches.wildcard {
44                    // Warn for every unreachable predicate. We store the fully parsed branch for rustfmt.
45                    for (predicate, _, _) in &branches.unreachable {
46                        let span = match predicate {
47                            CfgSelectPredicate::Wildcard(underscore) => underscore.span,
48                            CfgSelectPredicate::Cfg(cfg) => cfg.span(),
49                        };
50                        let err = CfgSelectUnreachable { span, wildcard_span: underscore.span };
51                        ecx.dcx().emit_warn(err);
52                    }
53                }
54
55                if let Some((tts, arm_span)) = select_arm(ecx, branches) {
56                    return ExpandResult::from_tts(
57                        ecx,
58                        tts,
59                        sp,
60                        arm_span,
61                        Ident::with_dummy_span(sym::cfg_select),
62                    );
63                } else {
64                    // Emit a compiler error when none of the predicates matched.
65                    let guar = ecx.dcx().emit_err(CfgSelectNoMatches { span: sp });
66                    DummyResult::any(sp, guar)
67                }
68            }
69            Err(guar) => DummyResult::any(sp, guar),
70        },
71    )
72}