1 use crate::util
::{check_builtin_macro_attribute, warn_on_duplicate_attribute}
;
4 use rustc_ast
::mut_visit
::MutVisitor
;
6 use rustc_ast
::visit
::Visitor
;
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
;
19 use smallvec
::SmallVec
;
22 ecx
: &mut ExtCtxt
<'_
>,
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
)]
32 pub(crate) fn cfg_eval(
35 annotatable
: 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.
46 struct CfgEval
<'a
, 'b
> {
47 cfg
: &'a
mut StripUnconfigured
<'b
>,
50 fn flat_map_annotatable(
51 vis
: &mut impl MutVisitor
,
52 annotatable
: Annotatable
,
53 ) -> Option
<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
)
59 Annotatable
::ImplItem(item
) => {
60 vis
.flat_map_impl_item(item
).pop().map(Annotatable
::ImplItem
)
62 Annotatable
::ForeignItem(item
) => {
63 vis
.flat_map_foreign_item(item
).pop().map(Annotatable
::ForeignItem
)
65 Annotatable
::Stmt(stmt
) => {
66 vis
.flat_map_stmt(stmt
.into_inner()).pop().map(P
).map(Annotatable
::Stmt
)
68 Annotatable
::Expr(mut expr
) => {
69 vis
.visit_expr(&mut expr
);
70 Some(Annotatable
::Expr(expr
))
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
)
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
)
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
))
91 has_cfg_or_cfg_attr
: bool
,
95 fn has_cfg_or_cfg_attr(annotatable
: &Annotatable
) -> bool
{
96 let mut finder
= CfgFinder { has_cfg_or_cfg_attr: false }
;
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(¶m
),
108 Annotatable
::Param(param
) => finder
.visit_param(¶m
),
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
),
113 finder
.has_cfg_or_cfg_attr
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
123 .is_some_and(|ident
| ident
.name
== sym
::cfg
|| ident
.name
== sym
::cfg_attr
);
127 impl CfgEval
<'_
, '_
> {
128 fn configure
<T
: HasAttrs
+ HasTokens
>(&mut self, node
: T
) -> Option
<T
> {
129 self.cfg
.configure(node
)
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
);
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.
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.
149 let parse_annotatable_with
: for<'a
> fn(&mut Parser
<'a
>) -> PResult
<'a
, _
> =
151 Annotatable
::Item(_
) => {
152 |parser
| Ok(Annotatable
::Item(parser
.parse_item(ForceCollect
::Yes
)?
.unwrap()))
154 Annotatable
::TraitItem(_
) => |parser
| {
155 Ok(Annotatable
::TraitItem(
156 parser
.parse_trait_item(ForceCollect
::Yes
)?
.unwrap().unwrap(),
159 Annotatable
::ImplItem(_
) => |parser
| {
160 Ok(Annotatable
::ImplItem(
161 parser
.parse_impl_item(ForceCollect
::Yes
)?
.unwrap().unwrap(),
164 Annotatable
::ForeignItem(_
) => |parser
| {
165 Ok(Annotatable
::ForeignItem(
166 parser
.parse_foreign_item(ForceCollect
::Yes
)?
.unwrap().unwrap(),
169 Annotatable
::Stmt(_
) => |parser
| {
170 Ok(Annotatable
::Stmt(P(parser
171 .parse_stmt_without_recovery(false, ForceCollect
::Yes
)?
174 Annotatable
::Expr(_
) => {
175 |parser
| Ok(Annotatable
::Expr(parser
.parse_expr_force_collect()?
))
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:
187 // #[cfg_eval] #[cfg] $item
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();
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]`)
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
,
205 return Some(annotatable
);
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
)
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);
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);
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);
234 fn flat_map_generic_param(
236 param
: ast
::GenericParam
,
237 ) -> SmallVec
<[ast
::GenericParam
; 1]> {
238 mut_visit
::noop_flat_map_generic_param(configure
!(self, param
), self)
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)
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)
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)
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)
257 fn flat_map_foreign_item(
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)
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)
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)
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)
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)
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)
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)