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, HasAttrs, 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    <[_]>::into_vec(::alloc::boxed::box_new([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: HasAttrs + 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<'_, Annotatable> = try {
111            match annotatable {
112                Annotatable::Item(_) => {
113                    let item =
114                        parser.parse_item(ForceCollect::Yes, AllowConstBlockItems::Yes)?.unwrap();
115                    Annotatable::Item(self.flat_map_item(item).pop().unwrap())
116                }
117                Annotatable::AssocItem(_, ctxt) => {
118                    let item = parser.parse_trait_item(ForceCollect::Yes)?.unwrap().unwrap();
119                    Annotatable::AssocItem(
120                        self.flat_map_assoc_item(item, ctxt).pop().unwrap(),
121                        ctxt,
122                    )
123                }
124                Annotatable::ForeignItem(_) => {
125                    let item = parser.parse_foreign_item(ForceCollect::Yes)?.unwrap().unwrap();
126                    Annotatable::ForeignItem(self.flat_map_foreign_item(item).pop().unwrap())
127                }
128                Annotatable::Stmt(_) => {
129                    let stmt = parser
130                        .parse_stmt_without_recovery(false, ForceCollect::Yes, false)?
131                        .unwrap();
132                    Annotatable::Stmt(Box::new(self.flat_map_stmt(stmt).pop().unwrap()))
133                }
134                Annotatable::Expr(_) => {
135                    let mut expr = parser.parse_expr_force_collect()?;
136                    self.visit_expr(&mut expr);
137                    Annotatable::Expr(expr)
138                }
139                _ => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
140            }
141        };
142
143        match res {
144            Ok(ann) => ann,
145            Err(err) => {
146                err.emit();
147                annotatable
148            }
149        }
150    }
151}
152
153impl MutVisitor for CfgEval<'_> {
154    #[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(154u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_builtin_macros::cfg_eval"),
                                    ::tracing_core::field::FieldSet::new(&["expr"],
                                        ::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};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&expr)
                                                            as &dyn 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))]
155    fn visit_expr(&mut self, expr: &mut ast::Expr) {
156        self.0.configure_expr(expr, false);
157        mut_visit::walk_expr(self, expr);
158    }
159
160    #[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(160u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_builtin_macros::cfg_eval"),
                                    ::tracing_core::field::FieldSet::new(&["expr"],
                                        ::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};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&expr)
                                                            as &dyn 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))]
161    fn visit_method_receiver_expr(&mut self, expr: &mut ast::Expr) {
162        self.0.configure_expr(expr, true);
163        mut_visit::walk_expr(self, expr);
164    }
165
166    fn filter_map_expr(&mut self, expr: Box<ast::Expr>) -> Option<Box<ast::Expr>> {
167        let mut expr = match self.configure(expr) {
    Some(node) => node,
    None => return Default::default(),
}configure!(self, expr);
168        mut_visit::walk_expr(self, &mut expr);
169        Some(expr)
170    }
171
172    fn flat_map_generic_param(
173        &mut self,
174        param: ast::GenericParam,
175    ) -> SmallVec<[ast::GenericParam; 1]> {
176        let param = match self.configure(param) {
    Some(node) => node,
    None => return Default::default(),
}configure!(self, param);
177        mut_visit::walk_flat_map_generic_param(self, param)
178    }
179
180    fn flat_map_stmt(&mut self, stmt: ast::Stmt) -> SmallVec<[ast::Stmt; 1]> {
181        let stmt = match self.configure(stmt) {
    Some(node) => node,
    None => return Default::default(),
}configure!(self, stmt);
182        mut_visit::walk_flat_map_stmt(self, stmt)
183    }
184
185    fn flat_map_item(&mut self, item: Box<ast::Item>) -> SmallVec<[Box<ast::Item>; 1]> {
186        let item = match self.configure(item) {
    Some(node) => node,
    None => return Default::default(),
}configure!(self, item);
187        mut_visit::walk_flat_map_item(self, item)
188    }
189
190    fn flat_map_assoc_item(
191        &mut self,
192        item: Box<ast::AssocItem>,
193        ctxt: AssocCtxt,
194    ) -> SmallVec<[Box<ast::AssocItem>; 1]> {
195        let item = match self.configure(item) {
    Some(node) => node,
    None => return Default::default(),
}configure!(self, item);
196        mut_visit::walk_flat_map_assoc_item(self, item, ctxt)
197    }
198
199    fn flat_map_foreign_item(
200        &mut self,
201        foreign_item: Box<ast::ForeignItem>,
202    ) -> SmallVec<[Box<ast::ForeignItem>; 1]> {
203        let foreign_item = match self.configure(foreign_item) {
    Some(node) => node,
    None => return Default::default(),
}configure!(self, foreign_item);
204        mut_visit::walk_flat_map_foreign_item(self, foreign_item)
205    }
206
207    fn flat_map_arm(&mut self, arm: ast::Arm) -> SmallVec<[ast::Arm; 1]> {
208        let arm = match self.configure(arm) {
    Some(node) => node,
    None => return Default::default(),
}configure!(self, arm);
209        mut_visit::walk_flat_map_arm(self, arm)
210    }
211
212    fn flat_map_expr_field(&mut self, field: ast::ExprField) -> SmallVec<[ast::ExprField; 1]> {
213        let field = match self.configure(field) {
    Some(node) => node,
    None => return Default::default(),
}configure!(self, field);
214        mut_visit::walk_flat_map_expr_field(self, field)
215    }
216
217    fn flat_map_pat_field(&mut self, fp: ast::PatField) -> SmallVec<[ast::PatField; 1]> {
218        let fp = match self.configure(fp) {
    Some(node) => node,
    None => return Default::default(),
}configure!(self, fp);
219        mut_visit::walk_flat_map_pat_field(self, fp)
220    }
221
222    fn flat_map_param(&mut self, p: ast::Param) -> SmallVec<[ast::Param; 1]> {
223        let p = match self.configure(p) {
    Some(node) => node,
    None => return Default::default(),
}configure!(self, p);
224        mut_visit::walk_flat_map_param(self, p)
225    }
226
227    fn flat_map_field_def(&mut self, sf: ast::FieldDef) -> SmallVec<[ast::FieldDef; 1]> {
228        let sf = match self.configure(sf) {
    Some(node) => node,
    None => return Default::default(),
}configure!(self, sf);
229        mut_visit::walk_flat_map_field_def(self, sf)
230    }
231
232    fn flat_map_variant(&mut self, variant: ast::Variant) -> SmallVec<[ast::Variant; 1]> {
233        let variant = match self.configure(variant) {
    Some(node) => node,
    None => return Default::default(),
}configure!(self, variant);
234        mut_visit::walk_flat_map_variant(self, variant)
235    }
236}