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