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