rustc_builtin_macros/
cfg_select.rs

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