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