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, 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    for (cfg, tt, arm_span) in branches.reachable {
14        if let EvalConfigResult::True = attr::eval_config_entry(&ecx.sess, &cfg) {
15            return Some((tt, arm_span));
16        }
17    }
18
19    branches.wildcard.map(|(_, tt, span)| (tt, span))
20}
21
22pub(super) fn expand_cfg_select<'cx>(
23    ecx: &'cx mut ExtCtxt<'_>,
24    sp: Span,
25    tts: TokenStream,
26) -> MacroExpanderResult<'cx> {
27    ExpandResult::Ready(
28        match parse_cfg_select(
29            &mut ecx.new_parser_from_tts(tts),
30            ecx.sess,
31            Some(ecx.ecfg.features),
32            ecx.current_expansion.lint_node_id,
33        ) {
34            Ok(branches) => {
35                if let Some((underscore, _, _)) = branches.wildcard {
36                    // Warn for every unreachable predicate. We store the fully parsed branch for rustfmt.
37                    for (predicate, _, _) in &branches.unreachable {
38                        let span = match predicate {
39                            CfgSelectPredicate::Wildcard(underscore) => underscore.span,
40                            CfgSelectPredicate::Cfg(cfg) => cfg.span(),
41                        };
42                        let err = CfgSelectUnreachable { span, wildcard_span: underscore.span };
43                        ecx.dcx().emit_warn(err);
44                    }
45                }
46
47                if let Some((tts, arm_span)) = select_arm(ecx, branches) {
48                    return ExpandResult::from_tts(
49                        ecx,
50                        tts,
51                        sp,
52                        arm_span,
53                        Ident::with_dummy_span(sym::cfg_select),
54                    );
55                } else {
56                    // Emit a compiler error when none of the predicates matched.
57                    let guar = ecx.dcx().emit_err(CfgSelectNoMatches { span: sp });
58                    DummyResult::any(sp, guar)
59                }
60            }
61            Err(guar) => DummyResult::any(sp, guar),
62        },
63    )
64}