rustc_expand/
config.rs

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