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 { kind: TokenKind::OpenDelim(_) | TokenKind::CloseDelim(_), .. },
242                    _,
243                ) => {
244                    panic!("Should be `AttrTokenTree::Delimited`, not delim tokens: {:?}", tree);
245                }
246                AttrTokenTree::Token(token, spacing) => Some(AttrTokenTree::Token(token, spacing)),
247            })
248            .collect();
249        AttrTokenStream::new(trees)
250    }
251
252    /// Parse and expand all `cfg_attr` attributes into a list of attributes
253    /// that are within each `cfg_attr` that has a true configuration predicate.
254    ///
255    /// Gives compiler warnings if any `cfg_attr` does not contain any
256    /// attributes and is in the original source code. Gives compiler errors if
257    /// the syntax of any `cfg_attr` is incorrect.
258    fn process_cfg_attrs<T: HasAttrs>(&self, node: &mut T) {
259        node.visit_attrs(|attrs| {
260            attrs.flat_map_in_place(|attr| self.process_cfg_attr(&attr));
261        });
262    }
263
264    fn process_cfg_attr(&self, attr: &Attribute) -> Vec<Attribute> {
265        if attr.has_name(sym::cfg_attr) {
266            self.expand_cfg_attr(attr, true)
267        } else {
268            vec![attr.clone()]
269        }
270    }
271
272    /// Parse and expand a single `cfg_attr` attribute into a list of attributes
273    /// when the configuration predicate is true, or otherwise expand into an
274    /// empty list of attributes.
275    ///
276    /// Gives a compiler warning when the `cfg_attr` contains no attributes and
277    /// is in the original source file. Gives a compiler error if the syntax of
278    /// the attribute is incorrect.
279    pub(crate) fn expand_cfg_attr(&self, cfg_attr: &Attribute, recursive: bool) -> Vec<Attribute> {
280        validate_attr::check_attribute_safety(&self.sess.psess, AttributeSafety::Normal, &cfg_attr);
281
282        // A trace attribute left in AST in place of the original `cfg_attr` attribute.
283        // It can later be used by lints or other diagnostics.
284        let trace_attr = attr_into_trace(cfg_attr.clone(), sym::cfg_attr_trace);
285
286        let Some((cfg_predicate, expanded_attrs)) =
287            rustc_parse::parse_cfg_attr(cfg_attr, &self.sess.psess)
288        else {
289            return vec![trace_attr];
290        };
291
292        // Lint on zero attributes in source.
293        if expanded_attrs.is_empty() {
294            self.sess.psess.buffer_lint(
295                rustc_lint_defs::builtin::UNUSED_ATTRIBUTES,
296                cfg_attr.span,
297                ast::CRATE_NODE_ID,
298                BuiltinLintDiag::CfgAttrNoAttributes,
299            );
300        }
301
302        if !attr::cfg_matches(&cfg_predicate, &self.sess, self.lint_node_id, self.features) {
303            return vec![trace_attr];
304        }
305
306        if recursive {
307            // We call `process_cfg_attr` recursively in case there's a
308            // `cfg_attr` inside of another `cfg_attr`. E.g.
309            //  `#[cfg_attr(false, cfg_attr(true, some_attr))]`.
310            let expanded_attrs = expanded_attrs
311                .into_iter()
312                .flat_map(|item| self.process_cfg_attr(&self.expand_cfg_attr_item(cfg_attr, item)));
313            iter::once(trace_attr).chain(expanded_attrs).collect()
314        } else {
315            let expanded_attrs =
316                expanded_attrs.into_iter().map(|item| self.expand_cfg_attr_item(cfg_attr, item));
317            iter::once(trace_attr).chain(expanded_attrs).collect()
318        }
319    }
320
321    fn expand_cfg_attr_item(
322        &self,
323        cfg_attr: &Attribute,
324        (item, item_span): (ast::AttrItem, Span),
325    ) -> Attribute {
326        // Convert `#[cfg_attr(pred, attr)]` to `#[attr]`.
327
328        // Use the `#` from `#[cfg_attr(pred, attr)]` in the result `#[attr]`.
329        let mut orig_trees = cfg_attr.token_trees().into_iter();
330        let Some(TokenTree::Token(pound_token @ Token { kind: TokenKind::Pound, .. }, _)) =
331            orig_trees.next()
332        else {
333            panic!("Bad tokens for attribute {cfg_attr:?}");
334        };
335
336        // For inner attributes, we do the same thing for the `!` in `#![attr]`.
337        let mut trees = if cfg_attr.style == AttrStyle::Inner {
338            let Some(TokenTree::Token(bang_token @ Token { kind: TokenKind::Bang, .. }, _)) =
339                orig_trees.next()
340            else {
341                panic!("Bad tokens for attribute {cfg_attr:?}");
342            };
343            vec![
344                AttrTokenTree::Token(pound_token, Spacing::Joint),
345                AttrTokenTree::Token(bang_token, Spacing::JointHidden),
346            ]
347        } else {
348            vec![AttrTokenTree::Token(pound_token, Spacing::JointHidden)]
349        };
350
351        // And the same thing for the `[`/`]` delimiters in `#[attr]`.
352        let Some(TokenTree::Delimited(delim_span, delim_spacing, Delimiter::Bracket, _)) =
353            orig_trees.next()
354        else {
355            panic!("Bad tokens for attribute {cfg_attr:?}");
356        };
357        trees.push(AttrTokenTree::Delimited(
358            delim_span,
359            delim_spacing,
360            Delimiter::Bracket,
361            item.tokens
362                .as_ref()
363                .unwrap_or_else(|| panic!("Missing tokens for {item:?}"))
364                .to_attr_token_stream(),
365        ));
366
367        let tokens = Some(LazyAttrTokenStream::new(AttrTokenStream::new(trees)));
368        let attr = ast::attr::mk_attr_from_item(
369            &self.sess.psess.attr_id_generator,
370            item,
371            tokens,
372            cfg_attr.style,
373            item_span,
374        );
375        if attr.has_name(sym::crate_type) {
376            self.sess.dcx().emit_err(CrateTypeInCfgAttr { span: attr.span });
377        }
378        if attr.has_name(sym::crate_name) {
379            self.sess.dcx().emit_err(CrateNameInCfgAttr { span: attr.span });
380        }
381        attr
382    }
383
384    /// Determines if a node with the given attributes should be included in this configuration.
385    fn in_cfg(&self, attrs: &[Attribute]) -> bool {
386        attrs.iter().all(|attr| !is_cfg(attr) || self.cfg_true(attr).0)
387    }
388
389    pub(crate) fn cfg_true(&self, attr: &Attribute) -> (bool, Option<MetaItem>) {
390        let meta_item = match validate_attr::parse_meta(&self.sess.psess, attr) {
391            Ok(meta_item) => meta_item,
392            Err(err) => {
393                err.emit();
394                return (true, None);
395            }
396        };
397
398        validate_attr::deny_builtin_meta_unsafety(&self.sess.psess, &meta_item);
399
400        (
401            parse_cfg(&meta_item, self.sess).is_none_or(|meta_item| {
402                attr::cfg_matches(meta_item, &self.sess, self.lint_node_id, self.features)
403            }),
404            Some(meta_item),
405        )
406    }
407
408    /// If attributes are not allowed on expressions, emit an error for `attr`
409    #[instrument(level = "trace", skip(self))]
410    pub(crate) fn maybe_emit_expr_attr_err(&self, attr: &Attribute) {
411        if self.features.is_some_and(|features| !features.stmt_expr_attributes())
412            && !attr.span.allows_unstable(sym::stmt_expr_attributes)
413        {
414            let mut err = feature_err(
415                &self.sess,
416                sym::stmt_expr_attributes,
417                attr.span,
418                crate::fluent_generated::expand_attributes_on_expressions_experimental,
419            );
420
421            if attr.is_doc_comment() {
422                err.help(if attr.style == AttrStyle::Outer {
423                    crate::fluent_generated::expand_help_outer_doc
424                } else {
425                    crate::fluent_generated::expand_help_inner_doc
426                });
427            }
428
429            err.emit();
430        }
431    }
432
433    #[instrument(level = "trace", skip(self))]
434    pub fn configure_expr(&self, expr: &mut P<ast::Expr>, method_receiver: bool) {
435        if !method_receiver {
436            for attr in expr.attrs.iter() {
437                self.maybe_emit_expr_attr_err(attr);
438            }
439        }
440
441        // If an expr is valid to cfg away it will have been removed by the
442        // outer stmt or expression folder before descending in here.
443        // Anything else is always required, and thus has to error out
444        // in case of a cfg attr.
445        //
446        // N.B., this is intentionally not part of the visit_expr() function
447        //     in order for filter_map_expr() to be able to avoid this check
448        if let Some(attr) = expr.attrs().iter().find(|a| is_cfg(a)) {
449            self.sess.dcx().emit_err(RemoveExprNotSupported { span: attr.span });
450        }
451
452        self.process_cfg_attrs(expr);
453        self.try_configure_tokens(&mut *expr);
454    }
455}
456
457pub fn parse_cfg<'a>(meta_item: &'a MetaItem, sess: &Session) -> Option<&'a MetaItemInner> {
458    let span = meta_item.span;
459    match meta_item.meta_item_list() {
460        None => {
461            sess.dcx().emit_err(InvalidCfg::NotFollowedByParens { span });
462            None
463        }
464        Some([]) => {
465            sess.dcx().emit_err(InvalidCfg::NoPredicate { span });
466            None
467        }
468        Some([_, .., l]) => {
469            sess.dcx().emit_err(InvalidCfg::MultiplePredicates { span: l.span() });
470            None
471        }
472        Some([single]) => match single.meta_item_or_bool() {
473            Some(meta_item) => Some(meta_item),
474            None => {
475                sess.dcx().emit_err(InvalidCfg::PredicateLiteral { span: single.span() });
476                None
477            }
478        },
479    }
480}
481
482fn is_cfg(attr: &Attribute) -> bool {
483    attr.has_name(sym::cfg)
484}