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