rustc_builtin_macros/
cfg_eval.rs

1use core::ops::ControlFlow;
2
3use rustc_ast as ast;
4use rustc_ast::mut_visit::MutVisitor;
5use rustc_ast::visit::{AssocCtxt, Visitor};
6use rustc_ast::{Attribute, HasAttrs, HasTokens, NodeId, mut_visit, visit};
7use rustc_errors::PResult;
8use rustc_expand::base::{Annotatable, ExtCtxt};
9use rustc_expand::config::StripUnconfigured;
10use rustc_expand::configure;
11use rustc_feature::Features;
12use rustc_parse::parser::{ForceCollect, Parser};
13use rustc_session::Session;
14use rustc_span::{Span, sym};
15use smallvec::SmallVec;
16use tracing::instrument;
17
18use crate::util::{check_builtin_macro_attribute, warn_on_duplicate_attribute};
19
20pub(crate) fn expand(
21    ecx: &mut ExtCtxt<'_>,
22    _span: Span,
23    meta_item: &ast::MetaItem,
24    annotatable: Annotatable,
25) -> Vec<Annotatable> {
26    check_builtin_macro_attribute(ecx, meta_item, sym::cfg_eval);
27    warn_on_duplicate_attribute(ecx, &annotatable, sym::cfg_eval);
28    vec![cfg_eval(ecx.sess, ecx.ecfg.features, annotatable, ecx.current_expansion.lint_node_id)]
29}
30
31pub(crate) fn cfg_eval(
32    sess: &Session,
33    features: &Features,
34    annotatable: Annotatable,
35    lint_node_id: NodeId,
36) -> Annotatable {
37    let features = Some(features);
38    CfgEval(StripUnconfigured { sess, features, config_tokens: true, lint_node_id })
39        .configure_annotatable(annotatable)
40}
41
42struct CfgEval<'a>(StripUnconfigured<'a>);
43
44fn has_cfg_or_cfg_attr(annotatable: &Annotatable) -> bool {
45    struct CfgFinder;
46
47    impl<'ast> visit::Visitor<'ast> for CfgFinder {
48        type Result = ControlFlow<()>;
49        fn visit_attribute(&mut self, attr: &'ast Attribute) -> ControlFlow<()> {
50            if attr.name().is_some_and(|name| name == sym::cfg || name == sym::cfg_attr) {
51                ControlFlow::Break(())
52            } else {
53                ControlFlow::Continue(())
54            }
55        }
56    }
57
58    let res = match annotatable {
59        Annotatable::Item(item) => CfgFinder.visit_item(item),
60        Annotatable::AssocItem(item, ctxt) => CfgFinder.visit_assoc_item(item, *ctxt),
61        Annotatable::ForeignItem(item) => CfgFinder.visit_foreign_item(item),
62        Annotatable::Stmt(stmt) => CfgFinder.visit_stmt(stmt),
63        Annotatable::Expr(expr) => CfgFinder.visit_expr(expr),
64        _ => unreachable!(),
65    };
66    res.is_break()
67}
68
69impl CfgEval<'_> {
70    fn configure<T: HasAttrs + HasTokens>(&mut self, node: T) -> Option<T> {
71        self.0.configure(node)
72    }
73
74    fn configure_annotatable(mut self, annotatable: Annotatable) -> Annotatable {
75        // Tokenizing and re-parsing the `Annotatable` can have a significant
76        // performance impact, so try to avoid it if possible
77        if !has_cfg_or_cfg_attr(&annotatable) {
78            return annotatable;
79        }
80
81        // The majority of parsed attribute targets will never need to have early cfg-expansion
82        // run (e.g. they are not part of a `#[derive]` or `#[cfg_eval]` macro input).
83        // Therefore, we normally do not capture the necessary information about `#[cfg]`
84        // and `#[cfg_attr]` attributes during parsing.
85        //
86        // Therefore, when we actually *do* run early cfg-expansion, we need to tokenize
87        // and re-parse the attribute target, this time capturing information about
88        // the location of `#[cfg]` and `#[cfg_attr]` in the token stream. The tokenization
89        // process is lossless, so this process is invisible to proc-macros.
90
91        // Interesting cases:
92        //
93        // ```rust
94        // #[cfg_eval] #[cfg] $item
95        //```
96        //
97        // where `$item` is `#[cfg_attr] struct Foo {}`. We want to make
98        // sure to evaluate *all* `#[cfg]` and `#[cfg_attr]` attributes - the simplest
99        // way to do this is to do a single parse of the token stream.
100        let orig_tokens = annotatable.to_tokens();
101
102        // Re-parse the tokens, setting the `capture_cfg` flag to save extra information
103        // to the captured `AttrTokenStream` (specifically, we capture
104        // `AttrTokenTree::AttrsTarget` for all occurrences of `#[cfg]` and `#[cfg_attr]`)
105        //
106        // After that we have our re-parsed `AttrTokenStream`, recursively configuring
107        // our attribute target will correctly configure the tokens as well.
108        let mut parser = Parser::new(&self.0.sess.psess, orig_tokens, None);
109        parser.capture_cfg = true;
110        let res: PResult<'_, Annotatable> = try {
111            match annotatable {
112                Annotatable::Item(_) => {
113                    let item = parser.parse_item(ForceCollect::Yes)?.unwrap();
114                    Annotatable::Item(self.flat_map_item(item).pop().unwrap())
115                }
116                Annotatable::AssocItem(_, ctxt) => {
117                    let item = parser.parse_trait_item(ForceCollect::Yes)?.unwrap().unwrap();
118                    Annotatable::AssocItem(
119                        self.flat_map_assoc_item(item, ctxt).pop().unwrap(),
120                        ctxt,
121                    )
122                }
123                Annotatable::ForeignItem(_) => {
124                    let item = parser.parse_foreign_item(ForceCollect::Yes)?.unwrap().unwrap();
125                    Annotatable::ForeignItem(self.flat_map_foreign_item(item).pop().unwrap())
126                }
127                Annotatable::Stmt(_) => {
128                    let stmt = parser
129                        .parse_stmt_without_recovery(false, ForceCollect::Yes, false)?
130                        .unwrap();
131                    Annotatable::Stmt(Box::new(self.flat_map_stmt(stmt).pop().unwrap()))
132                }
133                Annotatable::Expr(_) => {
134                    let mut expr = parser.parse_expr_force_collect()?;
135                    self.visit_expr(&mut expr);
136                    Annotatable::Expr(expr)
137                }
138                _ => unreachable!(),
139            }
140        };
141
142        match res {
143            Ok(ann) => ann,
144            Err(err) => {
145                err.emit();
146                annotatable
147            }
148        }
149    }
150}
151
152impl MutVisitor for CfgEval<'_> {
153    #[instrument(level = "trace", skip(self))]
154    fn visit_expr(&mut self, expr: &mut ast::Expr) {
155        self.0.configure_expr(expr, false);
156        mut_visit::walk_expr(self, expr);
157    }
158
159    #[instrument(level = "trace", skip(self))]
160    fn visit_method_receiver_expr(&mut self, expr: &mut ast::Expr) {
161        self.0.configure_expr(expr, true);
162        mut_visit::walk_expr(self, expr);
163    }
164
165    fn filter_map_expr(&mut self, expr: Box<ast::Expr>) -> Option<Box<ast::Expr>> {
166        let mut expr = configure!(self, expr);
167        mut_visit::walk_expr(self, &mut expr);
168        Some(expr)
169    }
170
171    fn flat_map_generic_param(
172        &mut self,
173        param: ast::GenericParam,
174    ) -> SmallVec<[ast::GenericParam; 1]> {
175        let param = configure!(self, param);
176        mut_visit::walk_flat_map_generic_param(self, param)
177    }
178
179    fn flat_map_stmt(&mut self, stmt: ast::Stmt) -> SmallVec<[ast::Stmt; 1]> {
180        let stmt = configure!(self, stmt);
181        mut_visit::walk_flat_map_stmt(self, stmt)
182    }
183
184    fn flat_map_item(&mut self, item: Box<ast::Item>) -> SmallVec<[Box<ast::Item>; 1]> {
185        let item = configure!(self, item);
186        mut_visit::walk_flat_map_item(self, item)
187    }
188
189    fn flat_map_assoc_item(
190        &mut self,
191        item: Box<ast::AssocItem>,
192        ctxt: AssocCtxt,
193    ) -> SmallVec<[Box<ast::AssocItem>; 1]> {
194        let item = configure!(self, item);
195        mut_visit::walk_flat_map_assoc_item(self, item, ctxt)
196    }
197
198    fn flat_map_foreign_item(
199        &mut self,
200        foreign_item: Box<ast::ForeignItem>,
201    ) -> SmallVec<[Box<ast::ForeignItem>; 1]> {
202        let foreign_item = configure!(self, foreign_item);
203        mut_visit::walk_flat_map_foreign_item(self, foreign_item)
204    }
205
206    fn flat_map_arm(&mut self, arm: ast::Arm) -> SmallVec<[ast::Arm; 1]> {
207        let arm = configure!(self, arm);
208        mut_visit::walk_flat_map_arm(self, arm)
209    }
210
211    fn flat_map_expr_field(&mut self, field: ast::ExprField) -> SmallVec<[ast::ExprField; 1]> {
212        let field = configure!(self, field);
213        mut_visit::walk_flat_map_expr_field(self, field)
214    }
215
216    fn flat_map_pat_field(&mut self, fp: ast::PatField) -> SmallVec<[ast::PatField; 1]> {
217        let fp = configure!(self, fp);
218        mut_visit::walk_flat_map_pat_field(self, fp)
219    }
220
221    fn flat_map_param(&mut self, p: ast::Param) -> SmallVec<[ast::Param; 1]> {
222        let p = configure!(self, p);
223        mut_visit::walk_flat_map_param(self, p)
224    }
225
226    fn flat_map_field_def(&mut self, sf: ast::FieldDef) -> SmallVec<[ast::FieldDef; 1]> {
227        let sf = configure!(self, sf);
228        mut_visit::walk_flat_map_field_def(self, sf)
229    }
230
231    fn flat_map_variant(&mut self, variant: ast::Variant) -> SmallVec<[ast::Variant; 1]> {
232        let variant = configure!(self, variant);
233        mut_visit::walk_flat_map_variant(self, variant)
234    }
235}