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 = parser
144                        .parse_stmt_without_recovery(false, ForceCollect::Yes, false)?
145                        .unwrap();
146                    Annotatable::Stmt(P(self.flat_map_stmt(stmt).pop().unwrap()))
147                }
148                Annotatable::Expr(_) => {
149                    let mut expr = parser.parse_expr_force_collect()?;
150                    self.visit_expr(&mut expr);
151                    Annotatable::Expr(expr)
152                }
153                _ => unreachable!(),
154            }
155        };
156
157        match res {
158            Ok(ann) => ann,
159            Err(err) => {
160                err.emit();
161                annotatable
162            }
163        }
164    }
165}
166
167impl MutVisitor for CfgEval<'_> {
168    #[instrument(level = "trace", skip(self))]
169    fn visit_expr(&mut self, expr: &mut P<ast::Expr>) {
170        self.0.configure_expr(expr, false);
171        mut_visit::walk_expr(self, expr);
172    }
173
174    #[instrument(level = "trace", skip(self))]
175    fn visit_method_receiver_expr(&mut self, expr: &mut P<ast::Expr>) {
176        self.0.configure_expr(expr, true);
177        mut_visit::walk_expr(self, expr);
178    }
179
180    fn filter_map_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
181        let mut expr = configure!(self, expr);
182        mut_visit::walk_expr(self, &mut expr);
183        Some(expr)
184    }
185
186    fn flat_map_generic_param(
187        &mut self,
188        param: ast::GenericParam,
189    ) -> SmallVec<[ast::GenericParam; 1]> {
190        let param = configure!(self, param);
191        mut_visit::walk_flat_map_generic_param(self, param)
192    }
193
194    fn flat_map_stmt(&mut self, stmt: ast::Stmt) -> SmallVec<[ast::Stmt; 1]> {
195        let stmt = configure!(self, stmt);
196        mut_visit::walk_flat_map_stmt(self, stmt)
197    }
198
199    fn flat_map_item(&mut self, item: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
200        let item = configure!(self, item);
201        mut_visit::walk_flat_map_item(self, item)
202    }
203
204    fn flat_map_assoc_item(
205        &mut self,
206        item: P<ast::AssocItem>,
207        ctxt: AssocCtxt,
208    ) -> SmallVec<[P<ast::AssocItem>; 1]> {
209        let item = configure!(self, item);
210        mut_visit::walk_flat_map_assoc_item(self, item, ctxt)
211    }
212
213    fn flat_map_foreign_item(
214        &mut self,
215        foreign_item: P<ast::ForeignItem>,
216    ) -> SmallVec<[P<ast::ForeignItem>; 1]> {
217        let foreign_item = configure!(self, foreign_item);
218        mut_visit::walk_flat_map_foreign_item(self, foreign_item)
219    }
220
221    fn flat_map_arm(&mut self, arm: ast::Arm) -> SmallVec<[ast::Arm; 1]> {
222        let arm = configure!(self, arm);
223        mut_visit::walk_flat_map_arm(self, arm)
224    }
225
226    fn flat_map_expr_field(&mut self, field: ast::ExprField) -> SmallVec<[ast::ExprField; 1]> {
227        let field = configure!(self, field);
228        mut_visit::walk_flat_map_expr_field(self, field)
229    }
230
231    fn flat_map_pat_field(&mut self, fp: ast::PatField) -> SmallVec<[ast::PatField; 1]> {
232        let fp = configure!(self, fp);
233        mut_visit::walk_flat_map_pat_field(self, fp)
234    }
235
236    fn flat_map_param(&mut self, p: ast::Param) -> SmallVec<[ast::Param; 1]> {
237        let p = configure!(self, p);
238        mut_visit::walk_flat_map_param(self, p)
239    }
240
241    fn flat_map_field_def(&mut self, sf: ast::FieldDef) -> SmallVec<[ast::FieldDef; 1]> {
242        let sf = configure!(self, sf);
243        mut_visit::walk_flat_map_field_def(self, sf)
244    }
245
246    fn flat_map_variant(&mut self, variant: ast::Variant) -> SmallVec<[ast::Variant; 1]> {
247        let variant = configure!(self, variant);
248        mut_visit::walk_flat_map_variant(self, variant)
249    }
250}