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