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