rustc_expand/
config.rs

1//! Conditional compilation stripping.
2
3use std::iter;
4
5use rustc_ast::ptr::P;
6use rustc_ast::token::{Delimiter, Token, TokenKind};
7use rustc_ast::tokenstream::{
8    AttrTokenStream, AttrTokenTree, LazyAttrTokenStream, Spacing, TokenTree,
9};
10use rustc_ast::{
11    self as ast, AttrKind, AttrStyle, Attribute, HasAttrs, HasTokens, MetaItem, MetaItemInner,
12    NodeId, NormalAttr,
13};
14use rustc_attr_parsing as attr;
15use rustc_data_structures::flat_map_in_place::FlatMapInPlace;
16use rustc_feature::{
17    ACCEPTED_LANG_FEATURES, AttributeSafety, EnabledLangFeature, EnabledLibFeature, Features,
18    REMOVED_LANG_FEATURES, UNSTABLE_LANG_FEATURES,
19};
20use rustc_lint_defs::BuiltinLintDiag;
21use rustc_parse::validate_attr;
22use rustc_session::Session;
23use rustc_session::parse::feature_err;
24use rustc_span::{STDLIB_STABLE_CRATES, Span, Symbol, sym};
25use thin_vec::ThinVec;
26use tracing::instrument;
27
28use crate::errors::{
29    CrateNameInCfgAttr, CrateTypeInCfgAttr, FeatureNotAllowed, FeatureRemoved,
30    FeatureRemovedReason, InvalidCfg, MalformedFeatureAttribute, MalformedFeatureAttributeHelp,
31    RemoveExprNotSupported,
32};
33
34/// A folder that strips out items that do not belong in the current configuration.
35pub struct StripUnconfigured<'a> {
36    pub sess: &'a Session,
37    pub features: Option<&'a Features>,
38    /// If `true`, perform cfg-stripping on attached tokens.
39    /// This is only used for the input to derive macros,
40    /// which needs eager expansion of `cfg` and `cfg_attr`
41    pub config_tokens: bool,
42    pub lint_node_id: NodeId,
43}
44
45pub fn features(sess: &Session, krate_attrs: &[Attribute], crate_name: Symbol) -> Features {
46    fn feature_list(attr: &Attribute) -> ThinVec<ast::MetaItemInner> {
47        if attr.has_name(sym::feature)
48            && let Some(list) = attr.meta_item_list()
49        {
50            list
51        } else {
52            ThinVec::new()
53        }
54    }
55
56    let mut features = Features::default();
57
58    // Process all features enabled in the code.
59    for attr in krate_attrs {
60        for mi in feature_list(attr) {
61            let name = match mi.ident() {
62                Some(ident) if mi.is_word() => ident.name,
63                Some(ident) => {
64                    sess.dcx().emit_err(MalformedFeatureAttribute {
65                        span: mi.span(),
66                        help: MalformedFeatureAttributeHelp::Suggestion {
67                            span: mi.span(),
68                            suggestion: ident.name,
69                        },
70                    });
71                    continue;
72                }
73                None => {
74                    sess.dcx().emit_err(MalformedFeatureAttribute {
75                        span: mi.span(),
76                        help: MalformedFeatureAttributeHelp::Label { span: mi.span() },
77                    });
78                    continue;
79                }
80            };
81
82            // If the enabled feature has been removed, issue an error.
83            if let Some(f) = REMOVED_LANG_FEATURES.iter().find(|f| name == f.feature.name) {
84                sess.dcx().emit_err(FeatureRemoved {
85                    span: mi.span(),
86                    reason: f.reason.map(|reason| FeatureRemovedReason { reason }),
87                });
88                continue;
89            }
90
91            // If the enabled feature is stable, record it.
92            if let Some(f) = ACCEPTED_LANG_FEATURES.iter().find(|f| name == f.name) {
93                features.set_enabled_lang_feature(EnabledLangFeature {
94                    gate_name: name,
95                    attr_sp: mi.span(),
96                    stable_since: Some(Symbol::intern(f.since)),
97                });
98                continue;
99            }
100
101            // If `-Z allow-features` is used and the enabled feature is
102            // unstable and not also listed as one of the allowed features,
103            // issue an error.
104            if let Some(allowed) = sess.opts.unstable_opts.allow_features.as_ref() {
105                if allowed.iter().all(|f| name.as_str() != f) {
106                    sess.dcx().emit_err(FeatureNotAllowed { span: mi.span(), name });
107                    continue;
108                }
109            }
110
111            // If the enabled feature is unstable, record it.
112            if UNSTABLE_LANG_FEATURES.iter().find(|f| name == f.name).is_some() {
113                // When the ICE comes a standard library crate, there's a chance that the person
114                // hitting the ICE may be using -Zbuild-std or similar with an untested target.
115                // The bug is probably in the standard library and not the compiler in that case,
116                // but that doesn't really matter - we want a bug report.
117                if features.internal(name) && !STDLIB_STABLE_CRATES.contains(&crate_name) {
118                    sess.using_internal_features.store(true, std::sync::atomic::Ordering::Relaxed);
119                }
120
121                features.set_enabled_lang_feature(EnabledLangFeature {
122                    gate_name: name,
123                    attr_sp: mi.span(),
124                    stable_since: None,
125                });
126                continue;
127            }
128
129            // Otherwise, the feature is unknown. Enable it as a lib feature.
130            // It will be checked later whether the feature really exists.
131            features
132                .set_enabled_lib_feature(EnabledLibFeature { gate_name: name, attr_sp: mi.span() });
133
134            // Similar to above, detect internal lib features to suppress
135            // the ICE message that asks for a report.
136            if features.internal(name) && !STDLIB_STABLE_CRATES.contains(&crate_name) {
137                sess.using_internal_features.store(true, std::sync::atomic::Ordering::Relaxed);
138            }
139        }
140    }
141
142    features
143}
144
145pub fn pre_configure_attrs(sess: &Session, attrs: &[Attribute]) -> ast::AttrVec {
146    let strip_unconfigured = StripUnconfigured {
147        sess,
148        features: None,
149        config_tokens: false,
150        lint_node_id: ast::CRATE_NODE_ID,
151    };
152    attrs
153        .iter()
154        .flat_map(|attr| strip_unconfigured.process_cfg_attr(attr))
155        .take_while(|attr| !is_cfg(attr) || strip_unconfigured.cfg_true(attr).0)
156        .collect()
157}
158
159pub(crate) fn attr_into_trace(mut attr: Attribute, trace_name: Symbol) -> Attribute {
160    match &mut attr.kind {
161        AttrKind::Normal(normal) => {
162            let NormalAttr { item, tokens } = &mut **normal;
163            item.path.segments[0].ident.name = trace_name;
164            // This makes the trace attributes unobservable to token-based proc macros.
165            *tokens = Some(LazyAttrTokenStream::new(AttrTokenStream::default()));
166        }
167        AttrKind::DocComment(..) => unreachable!(),
168    }
169    attr
170}
171
172#[macro_export]
173macro_rules! configure {
174    ($this:ident, $node:ident) => {
175        match $this.configure($node) {
176            Some(node) => node,
177            None => return Default::default(),
178        }
179    };
180}
181
182impl<'a> StripUnconfigured<'a> {
183    pub fn configure<T: HasAttrs + HasTokens>(&self, mut node: T) -> Option<T> {
184        self.process_cfg_attrs(&mut node);
185        self.in_cfg(node.attrs()).then(|| {
186            self.try_configure_tokens(&mut node);
187            node
188        })
189    }
190
191    fn try_configure_tokens<T: HasTokens>(&self, node: &mut T) {
192        if self.config_tokens {
193            if let Some(Some(tokens)) = node.tokens_mut() {
194                let attr_stream = tokens.to_attr_token_stream();
195                *tokens = LazyAttrTokenStream::new(self.configure_tokens(&attr_stream));
196            }
197        }
198    }
199
200    /// Performs cfg-expansion on `stream`, producing a new `AttrTokenStream`.
201    /// This is only used during the invocation of `derive` proc-macros,
202    /// which require that we cfg-expand their entire input.
203    /// Normal cfg-expansion operates on parsed AST nodes via the `configure` method
204    fn configure_tokens(&self, stream: &AttrTokenStream) -> AttrTokenStream {
205        fn can_skip(stream: &AttrTokenStream) -> bool {
206            stream.0.iter().all(|tree| match tree {
207                AttrTokenTree::AttrsTarget(_) => false,
208                AttrTokenTree::Token(..) => true,
209                AttrTokenTree::Delimited(.., inner) => can_skip(inner),
210            })
211        }
212
213        if can_skip(stream) {
214            return stream.clone();
215        }
216
217        let trees: Vec<_> = stream
218            .0
219            .iter()
220            .filter_map(|tree| match tree.clone() {
221                AttrTokenTree::AttrsTarget(mut target) => {
222                    // Expand any `cfg_attr` attributes.
223                    target.attrs.flat_map_in_place(|attr| self.process_cfg_attr(&attr));
224
225                    if self.in_cfg(&target.attrs) {
226                        target.tokens = LazyAttrTokenStream::new(
227                            self.configure_tokens(&target.tokens.to_attr_token_stream()),
228                        );
229                        Some(AttrTokenTree::AttrsTarget(target))
230                    } else {
231                        // Remove the target if there's a `cfg` attribute and
232                        // the condition isn't satisfied.
233                        None
234                    }
235                }
236                AttrTokenTree::Delimited(sp, spacing, delim, mut inner) => {
237                    inner = self.configure_tokens(&inner);
238                    Some(AttrTokenTree::Delimited(sp, spacing, delim, inner))
239                }
240                AttrTokenTree::Token(
241                    Token {
242                        kind:
243                            TokenKind::NtIdent(..)
244                            | TokenKind::NtLifetime(..)
245                            | TokenKind::Interpolated(..),
246                        ..
247                    },
248                    _,
249                ) => {
250                    panic!("Nonterminal should have been flattened: {:?}", tree);
251                }
252                AttrTokenTree::Token(
253                    Token { kind: TokenKind::OpenDelim(_) | TokenKind::CloseDelim(_), .. },
254                    _,
255                ) => {
256                    panic!("Should be `AttrTokenTree::Delimited`, not delim tokens: {:?}", tree);
257                }
258                AttrTokenTree::Token(token, spacing) => Some(AttrTokenTree::Token(token, spacing)),
259            })
260            .collect();
261        AttrTokenStream::new(trees)
262    }
263
264    /// Parse and expand all `cfg_attr` attributes into a list of attributes
265    /// that are within each `cfg_attr` that has a true configuration predicate.
266    ///
267    /// Gives compiler warnings if any `cfg_attr` does not contain any
268    /// attributes and is in the original source code. Gives compiler errors if
269    /// the syntax of any `cfg_attr` is incorrect.
270    fn process_cfg_attrs<T: HasAttrs>(&self, node: &mut T) {
271        node.visit_attrs(|attrs| {
272            attrs.flat_map_in_place(|attr| self.process_cfg_attr(&attr));
273        });
274    }
275
276    fn process_cfg_attr(&self, attr: &Attribute) -> Vec<Attribute> {
277        if attr.has_name(sym::cfg_attr) {
278            self.expand_cfg_attr(attr, true)
279        } else {
280            vec![attr.clone()]
281        }
282    }
283
284    /// Parse and expand a single `cfg_attr` attribute into a list of attributes
285    /// when the configuration predicate is true, or otherwise expand into an
286    /// empty list of attributes.
287    ///
288    /// Gives a compiler warning when the `cfg_attr` contains no attributes and
289    /// is in the original source file. Gives a compiler error if the syntax of
290    /// the attribute is incorrect.
291    pub(crate) fn expand_cfg_attr(&self, cfg_attr: &Attribute, recursive: bool) -> Vec<Attribute> {
292        validate_attr::check_attribute_safety(&self.sess.psess, AttributeSafety::Normal, &cfg_attr);
293
294        // A trace attribute left in AST in place of the original `cfg_attr` attribute.
295        // It can later be used by lints or other diagnostics.
296        let trace_attr = attr_into_trace(cfg_attr.clone(), sym::cfg_attr_trace);
297
298        let Some((cfg_predicate, expanded_attrs)) =
299            rustc_parse::parse_cfg_attr(cfg_attr, &self.sess.psess)
300        else {
301            return vec![trace_attr];
302        };
303
304        // Lint on zero attributes in source.
305        if expanded_attrs.is_empty() {
306            self.sess.psess.buffer_lint(
307                rustc_lint_defs::builtin::UNUSED_ATTRIBUTES,
308                cfg_attr.span,
309                ast::CRATE_NODE_ID,
310                BuiltinLintDiag::CfgAttrNoAttributes,
311            );
312        }
313
314        if !attr::cfg_matches(&cfg_predicate, &self.sess, self.lint_node_id, self.features) {
315            return vec![trace_attr];
316        }
317
318        if recursive {
319            // We call `process_cfg_attr` recursively in case there's a
320            // `cfg_attr` inside of another `cfg_attr`. E.g.
321            //  `#[cfg_attr(false, cfg_attr(true, some_attr))]`.
322            let expanded_attrs = expanded_attrs
323                .into_iter()
324                .flat_map(|item| self.process_cfg_attr(&self.expand_cfg_attr_item(cfg_attr, item)));
325            iter::once(trace_attr).chain(expanded_attrs).collect()
326        } else {
327            let expanded_attrs =
328                expanded_attrs.into_iter().map(|item| self.expand_cfg_attr_item(cfg_attr, item));
329            iter::once(trace_attr).chain(expanded_attrs).collect()
330        }
331    }
332
333    fn expand_cfg_attr_item(
334        &self,
335        cfg_attr: &Attribute,
336        (item, item_span): (ast::AttrItem, Span),
337    ) -> Attribute {
338        // Convert `#[cfg_attr(pred, attr)]` to `#[attr]`.
339
340        // Use the `#` from `#[cfg_attr(pred, attr)]` in the result `#[attr]`.
341        let mut orig_trees = cfg_attr.token_trees().into_iter();
342        let Some(TokenTree::Token(pound_token @ Token { kind: TokenKind::Pound, .. }, _)) =
343            orig_trees.next()
344        else {
345            panic!("Bad tokens for attribute {cfg_attr:?}");
346        };
347
348        // For inner attributes, we do the same thing for the `!` in `#![attr]`.
349        let mut trees = if cfg_attr.style == AttrStyle::Inner {
350            let Some(TokenTree::Token(bang_token @ Token { kind: TokenKind::Bang, .. }, _)) =
351                orig_trees.next()
352            else {
353                panic!("Bad tokens for attribute {cfg_attr:?}");
354            };
355            vec![
356                AttrTokenTree::Token(pound_token, Spacing::Joint),
357                AttrTokenTree::Token(bang_token, Spacing::JointHidden),
358            ]
359        } else {
360            vec![AttrTokenTree::Token(pound_token, Spacing::JointHidden)]
361        };
362
363        // And the same thing for the `[`/`]` delimiters in `#[attr]`.
364        let Some(TokenTree::Delimited(delim_span, delim_spacing, Delimiter::Bracket, _)) =
365            orig_trees.next()
366        else {
367            panic!("Bad tokens for attribute {cfg_attr:?}");
368        };
369        trees.push(AttrTokenTree::Delimited(
370            delim_span,
371            delim_spacing,
372            Delimiter::Bracket,
373            item.tokens
374                .as_ref()
375                .unwrap_or_else(|| panic!("Missing tokens for {item:?}"))
376                .to_attr_token_stream(),
377        ));
378
379        let tokens = Some(LazyAttrTokenStream::new(AttrTokenStream::new(trees)));
380        let attr = ast::attr::mk_attr_from_item(
381            &self.sess.psess.attr_id_generator,
382            item,
383            tokens,
384            cfg_attr.style,
385            item_span,
386        );
387        if attr.has_name(sym::crate_type) {
388            self.sess.dcx().emit_err(CrateTypeInCfgAttr { span: attr.span });
389        }
390        if attr.has_name(sym::crate_name) {
391            self.sess.dcx().emit_err(CrateNameInCfgAttr { span: attr.span });
392        }
393        attr
394    }
395
396    /// Determines if a node with the given attributes should be included in this configuration.
397    fn in_cfg(&self, attrs: &[Attribute]) -> bool {
398        attrs.iter().all(|attr| !is_cfg(attr) || self.cfg_true(attr).0)
399    }
400
401    pub(crate) fn cfg_true(&self, attr: &Attribute) -> (bool, Option<MetaItem>) {
402        let meta_item = match validate_attr::parse_meta(&self.sess.psess, attr) {
403            Ok(meta_item) => meta_item,
404            Err(err) => {
405                err.emit();
406                return (true, None);
407            }
408        };
409
410        validate_attr::deny_builtin_meta_unsafety(&self.sess.psess, &meta_item);
411
412        (
413            parse_cfg(&meta_item, self.sess).is_none_or(|meta_item| {
414                attr::cfg_matches(meta_item, &self.sess, self.lint_node_id, self.features)
415            }),
416            Some(meta_item),
417        )
418    }
419
420    /// If attributes are not allowed on expressions, emit an error for `attr`
421    #[instrument(level = "trace", skip(self))]
422    pub(crate) fn maybe_emit_expr_attr_err(&self, attr: &Attribute) {
423        if self.features.is_some_and(|features| !features.stmt_expr_attributes())
424            && !attr.span.allows_unstable(sym::stmt_expr_attributes)
425        {
426            let mut err = feature_err(
427                &self.sess,
428                sym::stmt_expr_attributes,
429                attr.span,
430                crate::fluent_generated::expand_attributes_on_expressions_experimental,
431            );
432
433            if attr.is_doc_comment() {
434                err.help(if attr.style == AttrStyle::Outer {
435                    crate::fluent_generated::expand_help_outer_doc
436                } else {
437                    crate::fluent_generated::expand_help_inner_doc
438                });
439            }
440
441            err.emit();
442        }
443    }
444
445    #[instrument(level = "trace", skip(self))]
446    pub fn configure_expr(&self, expr: &mut P<ast::Expr>, method_receiver: bool) {
447        if !method_receiver {
448            for attr in expr.attrs.iter() {
449                self.maybe_emit_expr_attr_err(attr);
450            }
451        }
452
453        // If an expr is valid to cfg away it will have been removed by the
454        // outer stmt or expression folder before descending in here.
455        // Anything else is always required, and thus has to error out
456        // in case of a cfg attr.
457        //
458        // N.B., this is intentionally not part of the visit_expr() function
459        //     in order for filter_map_expr() to be able to avoid this check
460        if let Some(attr) = expr.attrs().iter().find(|a| is_cfg(a)) {
461            self.sess.dcx().emit_err(RemoveExprNotSupported { span: attr.span });
462        }
463
464        self.process_cfg_attrs(expr);
465        self.try_configure_tokens(&mut *expr);
466    }
467}
468
469pub fn parse_cfg<'a>(meta_item: &'a MetaItem, sess: &Session) -> Option<&'a MetaItemInner> {
470    let span = meta_item.span;
471    match meta_item.meta_item_list() {
472        None => {
473            sess.dcx().emit_err(InvalidCfg::NotFollowedByParens { span });
474            None
475        }
476        Some([]) => {
477            sess.dcx().emit_err(InvalidCfg::NoPredicate { span });
478            None
479        }
480        Some([_, .., l]) => {
481            sess.dcx().emit_err(InvalidCfg::MultiplePredicates { span: l.span() });
482            None
483        }
484        Some([single]) => match single.meta_item_or_bool() {
485            Some(meta_item) => Some(meta_item),
486            None => {
487                sess.dcx().emit_err(InvalidCfg::PredicateLiteral { span: single.span() });
488                None
489            }
490        },
491    }
492}
493
494fn is_cfg(attr: &Attribute) -> bool {
495    attr.has_name(sym::cfg)
496}