Skip to main content

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::visit::{AssocCtxt, Visitor};
6use rustc_ast::{Attribute, HasTokens, NodeId, mut_visit, visit};
7use rustc_errors::PResult;
8use rustc_expand::base::{Annotatable, ExtCtxt};
9use rustc_expand::config::StripUnconfigured;
10use rustc_expand::configure;
11use rustc_feature::Features;
12use rustc_parse::parser::{AllowConstBlockItems, ForceCollect, Parser};
13use rustc_session::Session;
14use rustc_span::{Span, sym};
15use smallvec::SmallVec;
16use tracing::instrument;
17
18use crate::util::{check_builtin_macro_attribute, warn_on_duplicate_attribute};
19
20pub(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    ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [cfg_eval(ecx.sess, ecx.ecfg.features, annotatable,
                    ecx.current_expansion.lint_node_id)]))vec![cfg_eval(ecx.sess, ecx.ecfg.features, annotatable, ecx.current_expansion.lint_node_id)]
29}
30
31pub(crate) fn cfg_eval(
32    sess: &Session,
33    features: &Features,
34    annotatable: Annotatable,
35    lint_node_id: NodeId,
36) -> Annotatable {
37    let features = Some(features);
38    CfgEval(StripUnconfigured { sess, features, config_tokens: true, lint_node_id })
39        .configure_annotatable(annotatable)
40}
41
42struct CfgEval<'a>(StripUnconfigured<'a>);
43
44fn has_cfg_or_cfg_attr(annotatable: &Annotatable) -> bool {
45    struct CfgFinder;
46
47    impl<'ast> visit::Visitor<'ast> for CfgFinder {
48        type Result = ControlFlow<()>;
49        fn visit_attribute(&mut self, attr: &'ast Attribute) -> ControlFlow<()> {
50            if attr.name().is_some_and(|name| name == sym::cfg || name == sym::cfg_attr) {
51                ControlFlow::Break(())
52            } else {
53                ControlFlow::Continue(())
54            }
55        }
56    }
57
58    let res = match annotatable {
59        Annotatable::Item(item) => CfgFinder.visit_item(item),
60        Annotatable::AssocItem(item, ctxt) => CfgFinder.visit_assoc_item(item, *ctxt),
61        Annotatable::ForeignItem(item) => CfgFinder.visit_foreign_item(item),
62        Annotatable::Stmt(stmt) => CfgFinder.visit_stmt(stmt),
63        Annotatable::Expr(expr) => CfgFinder.visit_expr(expr),
64        _ => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
65    };
66    res.is_break()
67}
68
69impl CfgEval<'_> {
70    fn configure<T: HasTokens>(&mut self, node: T) -> Option<T> {
71        self.0.configure(node)
72    }
73
74    fn configure_annotatable(mut self, annotatable: Annotatable) -> Annotatable {
75        // Tokenizing and re-parsing the `Annotatable` can have a significant
76        // performance impact, so try to avoid it if possible
77        if !has_cfg_or_cfg_attr(&annotatable) {
78            return annotatable;
79        }
80
81        // The majority of parsed attribute targets will never need to have early cfg-expansion
82        // run (e.g. they are not part of a `#[derive]` or `#[cfg_eval]` macro input).
83        // Therefore, we normally do not capture the necessary information about `#[cfg]`
84        // and `#[cfg_attr]` attributes during parsing.
85        //
86        // Therefore, when we actually *do* run early cfg-expansion, we need to tokenize
87        // and re-parse the attribute target, this time capturing information about
88        // the location of `#[cfg]` and `#[cfg_attr]` in the token stream. The tokenization
89        // process is lossless, so this process is invisible to proc-macros.
90
91        // Interesting cases:
92        //
93        // ```rust
94        // #[cfg_eval] #[cfg] $item
95        //```
96        //
97        // where `$item` is `#[cfg_attr] struct Foo {}`. We want to make
98        // sure to evaluate *all* `#[cfg]` and `#[cfg_attr]` attributes - the simplest
99        // way to do this is to do a single parse of the token stream.
100        let orig_tokens = annotatable.to_tokens();
101
102        // Re-parse the tokens, setting the `capture_cfg` flag to save extra information
103        // to the captured `AttrTokenStream` (specifically, we capture
104        // `AttrTokenTree::AttrsTarget` for all occurrences of `#[cfg]` and `#[cfg_attr]`)
105        //
106        // After that we have our re-parsed `AttrTokenStream`, recursively configuring
107        // our attribute target will correctly configure the tokens as well.
108        let mut parser = Parser::new(&self.0.sess.psess, orig_tokens, None);
109        parser.capture_cfg = true;
110        let res: PResult<'_, Option<Annotatable>> = try {
111            match &annotatable {
112                Annotatable::Item(_) => parser
113                    .parse_item(ForceCollect::Yes, AllowConstBlockItems::Yes)?
114                    .and_then(|item| self.flat_map_item(item).pop().map(Annotatable::Item)),
115                Annotatable::AssocItem(_, ctxt) => {
116                    parser.parse_trait_item(ForceCollect::Yes)?.flatten().and_then(|item| {
117                        self.flat_map_assoc_item(item, *ctxt)
118                            .pop()
119                            .map(|item| Annotatable::AssocItem(item, *ctxt))
120                    })
121                }
122                Annotatable::ForeignItem(_) => {
123                    parser.parse_foreign_item(ForceCollect::Yes)?.flatten().and_then(|item| {
124                        self.flat_map_foreign_item(item).pop().map(Annotatable::ForeignItem)
125                    })
126                }
127                Annotatable::Stmt(_) => {
128                    let stmt =
129                        parser.parse_stmt_without_recovery(false, ForceCollect::Yes, false)?;
130                    self.flat_map_stmt(stmt).pop().map(|stmt| Annotatable::Stmt(Box::new(stmt)))
131                }
132                Annotatable::Expr(_) => {
133                    let mut expr = parser.parse_expr_force_collect()?;
134                    self.visit_expr(&mut expr);
135                    Some(Annotatable::Expr(expr))
136                }
137                _ => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
138            }
139        };
140
141        match res {
142            Ok(Some(ann)) => ann,
143            // Parser recovery may emit errors without reconstructing an annotatable.
144            // Keep the original node so cfg-eval stays best-effort instead of ICEing.
145            Ok(None) => annotatable,
146            Err(err) => {
147                err.emit();
148                annotatable
149            }
150        }
151    }
152}
153
154impl MutVisitor for CfgEval<'_> {
155    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::TRACE <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("visit_expr",
                                    "rustc_builtin_macros::cfg_eval", ::tracing::Level::TRACE,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_builtin_macros/src/cfg_eval.rs"),
                                    ::tracing_core::__macro_support::Option::Some(155u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_builtin_macros::cfg_eval"),
                                    ::tracing_core::field::FieldSet::new(&[{
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("expr")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("expr");
                                                        NAME.as_str()
                                                    }], ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::TRACE <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::TRACE <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&expr)
                                                            as &dyn ::tracing::field::Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: () = loop {};
            return __tracing_attr_fake_return;
        }
        {
            self.0.configure_expr(expr, false);
            mut_visit::walk_expr(self, expr);
        }
    }
}#[instrument(level = "trace", skip(self))]
156    fn visit_expr(&mut self, expr: &mut ast::Expr) {
157        self.0.configure_expr(expr, false);
158        mut_visit::walk_expr(self, expr);
159    }
160
161    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::TRACE <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("visit_method_receiver_expr",
                                    "rustc_builtin_macros::cfg_eval", ::tracing::Level::TRACE,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_builtin_macros/src/cfg_eval.rs"),
                                    ::tracing_core::__macro_support::Option::Some(161u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_builtin_macros::cfg_eval"),
                                    ::tracing_core::field::FieldSet::new(&[{
                                                        const NAME:
                                                            ::tracing::__macro_support::FieldName<{
                                                                ::tracing::__macro_support::FieldName::len("expr")
                                                            }> =
                                                            ::tracing::__macro_support::FieldName::new("expr");
                                                        NAME.as_str()
                                                    }], ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::TRACE <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::TRACE <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&expr)
                                                            as &dyn ::tracing::field::Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: () = loop {};
            return __tracing_attr_fake_return;
        }
        {
            self.0.configure_expr(expr, true);
            mut_visit::walk_expr(self, expr);
        }
    }
}#[instrument(level = "trace", skip(self))]
162    fn visit_method_receiver_expr(&mut self, expr: &mut ast::Expr) {
163        self.0.configure_expr(expr, true);
164        mut_visit::walk_expr(self, expr);
165    }
166
167    fn filter_map_expr(&mut self, expr: Box<ast::Expr>) -> Option<Box<ast::Expr>> {
168        let mut expr = match self.configure(expr) {
    Some(node) => node,
    None => return Default::default(),
}configure!(self, expr);
169        mut_visit::walk_expr(self, &mut expr);
170        Some(expr)
171    }
172
173    fn flat_map_generic_param(
174        &mut self,
175        param: ast::GenericParam,
176    ) -> SmallVec<[ast::GenericParam; 1]> {
177        let param = match self.configure(param) {
    Some(node) => node,
    None => return Default::default(),
}configure!(self, param);
178        mut_visit::walk_flat_map_generic_param(self, param)
179    }
180
181    fn flat_map_stmt(&mut self, stmt: ast::Stmt) -> SmallVec<[ast::Stmt; 1]> {
182        let stmt = match self.configure(stmt) {
    Some(node) => node,
    None => return Default::default(),
}configure!(self, stmt);
183        mut_visit::walk_flat_map_stmt(self, stmt)
184    }
185
186    fn flat_map_item(&mut self, item: Box<ast::Item>) -> SmallVec<[Box<ast::Item>; 1]> {
187        let item = match self.configure(item) {
    Some(node) => node,
    None => return Default::default(),
}configure!(self, item);
188        mut_visit::walk_flat_map_item(self, item)
189    }
190
191    fn flat_map_assoc_item(
192        &mut self,
193        item: Box<ast::AssocItem>,
194        ctxt: AssocCtxt,
195    ) -> SmallVec<[Box<ast::AssocItem>; 1]> {
196        let item = match self.configure(item) {
    Some(node) => node,
    None => return Default::default(),
}configure!(self, item);
197        mut_visit::walk_flat_map_assoc_item(self, item, ctxt)
198    }
199
200    fn flat_map_foreign_item(
201        &mut self,
202        foreign_item: Box<ast::ForeignItem>,
203    ) -> SmallVec<[Box<ast::ForeignItem>; 1]> {
204        let foreign_item = match self.configure(foreign_item) {
    Some(node) => node,
    None => return Default::default(),
}configure!(self, foreign_item);
205        mut_visit::walk_flat_map_foreign_item(self, foreign_item)
206    }
207
208    fn flat_map_arm(&mut self, arm: ast::Arm) -> SmallVec<[ast::Arm; 1]> {
209        let arm = match self.configure(arm) {
    Some(node) => node,
    None => return Default::default(),
}configure!(self, arm);
210        mut_visit::walk_flat_map_arm(self, arm)
211    }
212
213    fn flat_map_expr_field(&mut self, field: ast::ExprField) -> SmallVec<[ast::ExprField; 1]> {
214        let field = match self.configure(field) {
    Some(node) => node,
    None => return Default::default(),
}configure!(self, field);
215        mut_visit::walk_flat_map_expr_field(self, field)
216    }
217
218    fn flat_map_pat_field(&mut self, fp: ast::PatField) -> SmallVec<[ast::PatField; 1]> {
219        let fp = match self.configure(fp) {
    Some(node) => node,
    None => return Default::default(),
}configure!(self, fp);
220        mut_visit::walk_flat_map_pat_field(self, fp)
221    }
222
223    fn flat_map_param(&mut self, p: ast::Param) -> SmallVec<[ast::Param; 1]> {
224        let p = match self.configure(p) {
    Some(node) => node,
    None => return Default::default(),
}configure!(self, p);
225        mut_visit::walk_flat_map_param(self, p)
226    }
227
228    fn flat_map_field_def(&mut self, sf: ast::FieldDef) -> SmallVec<[ast::FieldDef; 1]> {
229        let sf = match self.configure(sf) {
    Some(node) => node,
    None => return Default::default(),
}configure!(self, sf);
230        mut_visit::walk_flat_map_field_def(self, sf)
231    }
232
233    fn flat_map_variant(&mut self, variant: ast::Variant) -> SmallVec<[ast::Variant; 1]> {
234        let variant = match self.configure(variant) {
    Some(node) => node,
    None => return Default::default(),
}configure!(self, variant);
235        mut_visit::walk_flat_map_variant(self, variant)
236    }
237}