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