]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_builtin_macros/src/cfg_eval.rs
New upstream version 1.75.0+dfsg1
[rustc.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_errors::PResult;
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::Session;
17 use rustc_span::symbol::sym;
18 use rustc_span::Span;
19 use smallvec::SmallVec;
20
21 pub(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
32 pub(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 { 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 .is_some_and(|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: for<'a> fn(&mut Parser<'a>) -> PResult<'a, _> =
150 match annotatable {
151 Annotatable::Item(_) => {
152 |parser| Ok(Annotatable::Item(parser.parse_item(ForceCollect::Yes)?.unwrap()))
153 }
154 Annotatable::TraitItem(_) => |parser| {
155 Ok(Annotatable::TraitItem(
156 parser.parse_trait_item(ForceCollect::Yes)?.unwrap().unwrap(),
157 ))
158 },
159 Annotatable::ImplItem(_) => |parser| {
160 Ok(Annotatable::ImplItem(
161 parser.parse_impl_item(ForceCollect::Yes)?.unwrap().unwrap(),
162 ))
163 },
164 Annotatable::ForeignItem(_) => |parser| {
165 Ok(Annotatable::ForeignItem(
166 parser.parse_foreign_item(ForceCollect::Yes)?.unwrap().unwrap(),
167 ))
168 },
169 Annotatable::Stmt(_) => |parser| {
170 Ok(Annotatable::Stmt(P(parser
171 .parse_stmt_without_recovery(false, ForceCollect::Yes)?
172 .unwrap())))
173 },
174 Annotatable::Expr(_) => {
175 |parser| Ok(Annotatable::Expr(parser.parse_expr_force_collect()?))
176 }
177 _ => unreachable!(),
178 };
179
180 // 'Flatten' all nonterminals (i.e. `TokenKind::Interpolated`)
181 // to `None`-delimited groups containing the corresponding tokens. This
182 // is normally delayed until the proc-macro server actually needs to
183 // provide a `TokenKind::Interpolated` to a proc-macro. We do this earlier,
184 // so that we can handle cases like:
185 //
186 // ```rust
187 // #[cfg_eval] #[cfg] $item
188 //```
189 //
190 // where `$item` is `#[cfg_attr] struct Foo {}`. We want to make
191 // sure to evaluate *all* `#[cfg]` and `#[cfg_attr]` attributes - the simplest
192 // way to do this is to do a single parse of a stream without any nonterminals.
193 let orig_tokens = annotatable.to_tokens().flattened();
194
195 // Re-parse the tokens, setting the `capture_cfg` flag to save extra information
196 // to the captured `AttrTokenStream` (specifically, we capture
197 // `AttrTokenTree::AttributesData` for all occurrences of `#[cfg]` and `#[cfg_attr]`)
198 let mut parser =
199 rustc_parse::stream_to_parser(&self.cfg.sess.parse_sess, orig_tokens, None);
200 parser.capture_cfg = true;
201 match parse_annotatable_with(&mut parser) {
202 Ok(a) => annotatable = a,
203 Err(mut err) => {
204 err.emit();
205 return Some(annotatable);
206 }
207 }
208
209 // Now that we have our re-parsed `AttrTokenStream`, recursively configuring
210 // our attribute target will correctly the tokens as well.
211 flat_map_annotatable(self, annotatable)
212 }
213 }
214
215 impl MutVisitor for CfgEval<'_, '_> {
216 #[instrument(level = "trace", skip(self))]
217 fn visit_expr(&mut self, expr: &mut P<ast::Expr>) {
218 self.cfg.configure_expr(expr, false);
219 mut_visit::noop_visit_expr(expr, self);
220 }
221
222 #[instrument(level = "trace", skip(self))]
223 fn visit_method_receiver_expr(&mut self, expr: &mut P<ast::Expr>) {
224 self.cfg.configure_expr(expr, true);
225 mut_visit::noop_visit_expr(expr, self);
226 }
227
228 fn filter_map_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
229 let mut expr = configure!(self, expr);
230 mut_visit::noop_visit_expr(&mut expr, self);
231 Some(expr)
232 }
233
234 fn flat_map_generic_param(
235 &mut self,
236 param: ast::GenericParam,
237 ) -> SmallVec<[ast::GenericParam; 1]> {
238 mut_visit::noop_flat_map_generic_param(configure!(self, param), self)
239 }
240
241 fn flat_map_stmt(&mut self, stmt: ast::Stmt) -> SmallVec<[ast::Stmt; 1]> {
242 mut_visit::noop_flat_map_stmt(configure!(self, stmt), self)
243 }
244
245 fn flat_map_item(&mut self, item: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
246 mut_visit::noop_flat_map_item(configure!(self, item), self)
247 }
248
249 fn flat_map_impl_item(&mut self, item: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
250 mut_visit::noop_flat_map_assoc_item(configure!(self, item), self)
251 }
252
253 fn flat_map_trait_item(&mut self, item: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
254 mut_visit::noop_flat_map_assoc_item(configure!(self, item), self)
255 }
256
257 fn flat_map_foreign_item(
258 &mut self,
259 foreign_item: P<ast::ForeignItem>,
260 ) -> SmallVec<[P<ast::ForeignItem>; 1]> {
261 mut_visit::noop_flat_map_foreign_item(configure!(self, foreign_item), self)
262 }
263
264 fn flat_map_arm(&mut self, arm: ast::Arm) -> SmallVec<[ast::Arm; 1]> {
265 mut_visit::noop_flat_map_arm(configure!(self, arm), self)
266 }
267
268 fn flat_map_expr_field(&mut self, field: ast::ExprField) -> SmallVec<[ast::ExprField; 1]> {
269 mut_visit::noop_flat_map_expr_field(configure!(self, field), self)
270 }
271
272 fn flat_map_pat_field(&mut self, fp: ast::PatField) -> SmallVec<[ast::PatField; 1]> {
273 mut_visit::noop_flat_map_pat_field(configure!(self, fp), self)
274 }
275
276 fn flat_map_param(&mut self, p: ast::Param) -> SmallVec<[ast::Param; 1]> {
277 mut_visit::noop_flat_map_param(configure!(self, p), self)
278 }
279
280 fn flat_map_field_def(&mut self, sf: ast::FieldDef) -> SmallVec<[ast::FieldDef; 1]> {
281 mut_visit::noop_flat_map_field_def(configure!(self, sf), self)
282 }
283
284 fn flat_map_variant(&mut self, variant: ast::Variant) -> SmallVec<[ast::Variant; 1]> {
285 mut_visit::noop_flat_map_variant(configure!(self, variant), self)
286 }
287 }