]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_builtin_macros/src/cfg_eval.rs
Rollup merge of #95365 - mkroening:hermit-alloc-error-handler, r=joshtriplett
[rust.git] / compiler / rustc_builtin_macros / src / cfg_eval.rs
1 use crate::util::{check_builtin_macro_attribute, warn_on_duplicate_attribute};
2
3 use rustc_ast as ast;
4 use rustc_ast::mut_visit::MutVisitor;
5 use rustc_ast::ptr::P;
6 use rustc_ast::tokenstream::CanSynthesizeMissingTokens;
7 use rustc_ast::visit::Visitor;
8 use rustc_ast::NodeId;
9 use rustc_ast::{mut_visit, visit};
10 use rustc_ast::{Attribute, HasAttrs, HasTokens};
11 use rustc_expand::base::{Annotatable, ExtCtxt};
12 use rustc_expand::config::StripUnconfigured;
13 use rustc_expand::configure;
14 use rustc_feature::Features;
15 use rustc_parse::parser::{ForceCollect, Parser};
16 use rustc_session::utils::FlattenNonterminals;
17 use rustc_session::Session;
18 use rustc_span::symbol::sym;
19 use rustc_span::Span;
20 use smallvec::SmallVec;
21
22 crate fn expand(
23     ecx: &mut ExtCtxt<'_>,
24     _span: Span,
25     meta_item: &ast::MetaItem,
26     annotatable: Annotatable,
27 ) -> Vec<Annotatable> {
28     check_builtin_macro_attribute(ecx, meta_item, sym::cfg_eval);
29     warn_on_duplicate_attribute(&ecx, &annotatable, sym::cfg_eval);
30     vec![cfg_eval(ecx.sess, ecx.ecfg.features, annotatable, ecx.current_expansion.lint_node_id)]
31 }
32
33 crate fn cfg_eval(
34     sess: &Session,
35     features: Option<&Features>,
36     annotatable: Annotatable,
37     lint_node_id: NodeId,
38 ) -> Annotatable {
39     CfgEval { cfg: &mut StripUnconfigured { sess, features, config_tokens: true, lint_node_id } }
40         .configure_annotatable(annotatable)
41         // Since the item itself has already been configured by the `InvocationCollector`,
42         // we know that fold result vector will contain exactly one element.
43         .unwrap()
44 }
45
46 struct CfgEval<'a, 'b> {
47     cfg: &'a mut StripUnconfigured<'b>,
48 }
49
50 fn flat_map_annotatable(
51     vis: &mut impl MutVisitor,
52     annotatable: Annotatable,
53 ) -> Option<Annotatable> {
54     match annotatable {
55         Annotatable::Item(item) => vis.flat_map_item(item).pop().map(Annotatable::Item),
56         Annotatable::TraitItem(item) => {
57             vis.flat_map_trait_item(item).pop().map(Annotatable::TraitItem)
58         }
59         Annotatable::ImplItem(item) => {
60             vis.flat_map_impl_item(item).pop().map(Annotatable::ImplItem)
61         }
62         Annotatable::ForeignItem(item) => {
63             vis.flat_map_foreign_item(item).pop().map(Annotatable::ForeignItem)
64         }
65         Annotatable::Stmt(stmt) => {
66             vis.flat_map_stmt(stmt.into_inner()).pop().map(P).map(Annotatable::Stmt)
67         }
68         Annotatable::Expr(mut expr) => {
69             vis.visit_expr(&mut expr);
70             Some(Annotatable::Expr(expr))
71         }
72         Annotatable::Arm(arm) => vis.flat_map_arm(arm).pop().map(Annotatable::Arm),
73         Annotatable::ExprField(field) => {
74             vis.flat_map_expr_field(field).pop().map(Annotatable::ExprField)
75         }
76         Annotatable::PatField(fp) => vis.flat_map_pat_field(fp).pop().map(Annotatable::PatField),
77         Annotatable::GenericParam(param) => {
78             vis.flat_map_generic_param(param).pop().map(Annotatable::GenericParam)
79         }
80         Annotatable::Param(param) => vis.flat_map_param(param).pop().map(Annotatable::Param),
81         Annotatable::FieldDef(sf) => vis.flat_map_field_def(sf).pop().map(Annotatable::FieldDef),
82         Annotatable::Variant(v) => vis.flat_map_variant(v).pop().map(Annotatable::Variant),
83         Annotatable::Crate(mut krate) => {
84             vis.visit_crate(&mut krate);
85             Some(Annotatable::Crate(krate))
86         }
87     }
88 }
89
90 struct CfgFinder {
91     has_cfg_or_cfg_attr: bool,
92 }
93
94 impl CfgFinder {
95     fn has_cfg_or_cfg_attr(annotatable: &Annotatable) -> bool {
96         let mut finder = CfgFinder { has_cfg_or_cfg_attr: false };
97         match annotatable {
98             Annotatable::Item(item) => finder.visit_item(&item),
99             Annotatable::TraitItem(item) => finder.visit_assoc_item(&item, visit::AssocCtxt::Trait),
100             Annotatable::ImplItem(item) => finder.visit_assoc_item(&item, visit::AssocCtxt::Impl),
101             Annotatable::ForeignItem(item) => finder.visit_foreign_item(&item),
102             Annotatable::Stmt(stmt) => finder.visit_stmt(&stmt),
103             Annotatable::Expr(expr) => finder.visit_expr(&expr),
104             Annotatable::Arm(arm) => finder.visit_arm(&arm),
105             Annotatable::ExprField(field) => finder.visit_expr_field(&field),
106             Annotatable::PatField(field) => finder.visit_pat_field(&field),
107             Annotatable::GenericParam(param) => finder.visit_generic_param(&param),
108             Annotatable::Param(param) => finder.visit_param(&param),
109             Annotatable::FieldDef(field) => finder.visit_field_def(&field),
110             Annotatable::Variant(variant) => finder.visit_variant(&variant),
111             Annotatable::Crate(krate) => finder.visit_crate(krate),
112         };
113         finder.has_cfg_or_cfg_attr
114     }
115 }
116
117 impl<'ast> visit::Visitor<'ast> for CfgFinder {
118     fn visit_attribute(&mut self, attr: &'ast Attribute) {
119         // We want short-circuiting behavior, so don't use the '|=' operator.
120         self.has_cfg_or_cfg_attr = self.has_cfg_or_cfg_attr
121             || attr
122                 .ident()
123                 .map_or(false, |ident| ident.name == sym::cfg || ident.name == sym::cfg_attr);
124     }
125 }
126
127 impl CfgEval<'_, '_> {
128     fn configure<T: HasAttrs + HasTokens>(&mut self, node: T) -> Option<T> {
129         self.cfg.configure(node)
130     }
131
132     fn configure_annotatable(&mut self, mut annotatable: Annotatable) -> Option<Annotatable> {
133         // Tokenizing and re-parsing the `Annotatable` can have a significant
134         // performance impact, so try to avoid it if possible
135         if !CfgFinder::has_cfg_or_cfg_attr(&annotatable) {
136             return Some(annotatable);
137         }
138
139         // The majority of parsed attribute targets will never need to have early cfg-expansion
140         // run (e.g. they are not part of a `#[derive]` or `#[cfg_eval]` macro input).
141         // Therefore, we normally do not capture the necessary information about `#[cfg]`
142         // and `#[cfg_attr]` attributes during parsing.
143         //
144         // Therefore, when we actually *do* run early cfg-expansion, we need to tokenize
145         // and re-parse the attribute target, this time capturing information about
146         // the location of `#[cfg]` and `#[cfg_attr]` in the token stream. The tokenization
147         // process is lossless, so this process is invisible to proc-macros.
148
149         let parse_annotatable_with: fn(&mut Parser<'_>) -> _ = match annotatable {
150             Annotatable::Item(_) => {
151                 |parser| Annotatable::Item(parser.parse_item(ForceCollect::Yes).unwrap().unwrap())
152             }
153             Annotatable::TraitItem(_) => |parser| {
154                 Annotatable::TraitItem(
155                     parser.parse_trait_item(ForceCollect::Yes).unwrap().unwrap().unwrap(),
156                 )
157             },
158             Annotatable::ImplItem(_) => |parser| {
159                 Annotatable::ImplItem(
160                     parser.parse_impl_item(ForceCollect::Yes).unwrap().unwrap().unwrap(),
161                 )
162             },
163             Annotatable::ForeignItem(_) => |parser| {
164                 Annotatable::ForeignItem(
165                     parser.parse_foreign_item(ForceCollect::Yes).unwrap().unwrap().unwrap(),
166                 )
167             },
168             Annotatable::Stmt(_) => |parser| {
169                 Annotatable::Stmt(P(parser.parse_stmt(ForceCollect::Yes).unwrap().unwrap()))
170             },
171             Annotatable::Expr(_) => {
172                 |parser| Annotatable::Expr(parser.parse_expr_force_collect().unwrap())
173             }
174             _ => unreachable!(),
175         };
176
177         let mut orig_tokens = annotatable.to_tokens(&self.cfg.sess.parse_sess);
178
179         // 'Flatten' all nonterminals (i.e. `TokenKind::Interpolated`)
180         // to `None`-delimited groups containing the corresponding tokens. This
181         // is normally delayed until the proc-macro server actually needs to
182         // provide a `TokenKind::Interpolated` to a proc-macro. We do this earlier,
183         // so that we can handle cases like:
184         //
185         // ```rust
186         // #[cfg_eval] #[cfg] $item
187         //```
188         //
189         // where `$item` is `#[cfg_attr] struct Foo {}`. We want to make
190         // sure to evaluate *all* `#[cfg]` and `#[cfg_attr]` attributes - the simplest
191         // way to do this is to do a single parse of a stream without any nonterminals.
192         let mut flatten = FlattenNonterminals {
193             nt_to_tokenstream: rustc_parse::nt_to_tokenstream,
194             parse_sess: &self.cfg.sess.parse_sess,
195             synthesize_tokens: CanSynthesizeMissingTokens::No,
196         };
197         orig_tokens = flatten.process_token_stream(orig_tokens);
198
199         // Re-parse the tokens, setting the `capture_cfg` flag to save extra information
200         // to the captured `AttrAnnotatedTokenStream` (specifically, we capture
201         // `AttrAnnotatedTokenTree::AttributesData` for all occurrences of `#[cfg]` and `#[cfg_attr]`)
202         let mut parser =
203             rustc_parse::stream_to_parser(&self.cfg.sess.parse_sess, orig_tokens, None);
204         parser.capture_cfg = true;
205         annotatable = parse_annotatable_with(&mut parser);
206
207         // Now that we have our re-parsed `AttrAnnotatedTokenStream`, recursively configuring
208         // our attribute target will correctly the tokens as well.
209         flat_map_annotatable(self, annotatable)
210     }
211 }
212
213 impl MutVisitor for CfgEval<'_, '_> {
214     fn visit_expr(&mut self, expr: &mut P<ast::Expr>) {
215         self.cfg.configure_expr(expr);
216         mut_visit::noop_visit_expr(expr, self);
217     }
218
219     fn filter_map_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
220         let mut expr = configure!(self, expr);
221         mut_visit::noop_visit_expr(&mut expr, self);
222         Some(expr)
223     }
224
225     fn flat_map_generic_param(
226         &mut self,
227         param: ast::GenericParam,
228     ) -> SmallVec<[ast::GenericParam; 1]> {
229         mut_visit::noop_flat_map_generic_param(configure!(self, param), self)
230     }
231
232     fn flat_map_stmt(&mut self, stmt: ast::Stmt) -> SmallVec<[ast::Stmt; 1]> {
233         mut_visit::noop_flat_map_stmt(configure!(self, stmt), self)
234     }
235
236     fn flat_map_item(&mut self, item: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
237         mut_visit::noop_flat_map_item(configure!(self, item), self)
238     }
239
240     fn flat_map_impl_item(&mut self, item: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
241         mut_visit::noop_flat_map_assoc_item(configure!(self, item), self)
242     }
243
244     fn flat_map_trait_item(&mut self, item: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
245         mut_visit::noop_flat_map_assoc_item(configure!(self, item), self)
246     }
247
248     fn flat_map_foreign_item(
249         &mut self,
250         foreign_item: P<ast::ForeignItem>,
251     ) -> SmallVec<[P<ast::ForeignItem>; 1]> {
252         mut_visit::noop_flat_map_foreign_item(configure!(self, foreign_item), self)
253     }
254
255     fn flat_map_arm(&mut self, arm: ast::Arm) -> SmallVec<[ast::Arm; 1]> {
256         mut_visit::noop_flat_map_arm(configure!(self, arm), self)
257     }
258
259     fn flat_map_expr_field(&mut self, field: ast::ExprField) -> SmallVec<[ast::ExprField; 1]> {
260         mut_visit::noop_flat_map_expr_field(configure!(self, field), self)
261     }
262
263     fn flat_map_pat_field(&mut self, fp: ast::PatField) -> SmallVec<[ast::PatField; 1]> {
264         mut_visit::noop_flat_map_pat_field(configure!(self, fp), self)
265     }
266
267     fn flat_map_param(&mut self, p: ast::Param) -> SmallVec<[ast::Param; 1]> {
268         mut_visit::noop_flat_map_param(configure!(self, p), self)
269     }
270
271     fn flat_map_field_def(&mut self, sf: ast::FieldDef) -> SmallVec<[ast::FieldDef; 1]> {
272         mut_visit::noop_flat_map_field_def(configure!(self, sf), self)
273     }
274
275     fn flat_map_variant(&mut self, variant: ast::Variant) -> SmallVec<[ast::Variant; 1]> {
276         mut_visit::noop_flat_map_variant(configure!(self, variant), self)
277     }
278 }