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)
167                || strip_unconfigured
168                    .cfg_true(attr, strip_unconfigured.lint_node_id, ShouldEmit::Nothing)
169                    .as_bool()
170        })
171        .collect()
172}
173
174pub(crate) fn attr_into_trace(mut attr: Attribute, trace_name: Symbol) -> Attribute {
175    match &mut attr.kind {
176        AttrKind::Normal(normal) => {
177            let NormalAttr { item, tokens } = &mut **normal;
178            item.path.segments[0].ident.name = trace_name;
179            // This makes the trace attributes unobservable to token-based proc macros.
180            *tokens = Some(LazyAttrTokenStream::new_direct(AttrTokenStream::default()));
181        }
182        AttrKind::DocComment(..) => unreachable!(),
183    }
184    attr
185}
186
187#[macro_export]
188macro_rules! configure {
189    ($this:ident, $node:ident) => {
190        match $this.configure($node) {
191            Some(node) => node,
192            None => return Default::default(),
193        }
194    };
195}
196
197impl<'a> StripUnconfigured<'a> {
198    pub fn configure<T: HasAttrs + HasTokens>(&self, mut node: T) -> Option<T> {
199        self.process_cfg_attrs(&mut node);
200        self.in_cfg(node.attrs()).then(|| {
201            self.try_configure_tokens(&mut node);
202            node
203        })
204    }
205
206    fn try_configure_tokens<T: HasTokens>(&self, node: &mut T) {
207        if self.config_tokens {
208            if let Some(Some(tokens)) = node.tokens_mut() {
209                let attr_stream = tokens.to_attr_token_stream();
210                *tokens = LazyAttrTokenStream::new_direct(self.configure_tokens(&attr_stream));
211            }
212        }
213    }
214
215    /// Performs cfg-expansion on `stream`, producing a new `AttrTokenStream`.
216    /// This is only used during the invocation of `derive` proc-macros,
217    /// which require that we cfg-expand their entire input.
218    /// Normal cfg-expansion operates on parsed AST nodes via the `configure` method
219    fn configure_tokens(&self, stream: &AttrTokenStream) -> AttrTokenStream {
220        fn can_skip(stream: &AttrTokenStream) -> bool {
221            stream.0.iter().all(|tree| match tree {
222                AttrTokenTree::AttrsTarget(_) => false,
223                AttrTokenTree::Token(..) => true,
224                AttrTokenTree::Delimited(.., inner) => can_skip(inner),
225            })
226        }
227
228        if can_skip(stream) {
229            return stream.clone();
230        }
231
232        let trees: Vec<_> = stream
233            .0
234            .iter()
235            .filter_map(|tree| match tree.clone() {
236                AttrTokenTree::AttrsTarget(mut target) => {
237                    // Expand any `cfg_attr` attributes.
238                    target.attrs.flat_map_in_place(|attr| self.process_cfg_attr(&attr));
239
240                    if self.in_cfg(&target.attrs) {
241                        target.tokens = LazyAttrTokenStream::new_direct(
242                            self.configure_tokens(&target.tokens.to_attr_token_stream()),
243                        );
244                        Some(AttrTokenTree::AttrsTarget(target))
245                    } else {
246                        // Remove the target if there's a `cfg` attribute and
247                        // the condition isn't satisfied.
248                        None
249                    }
250                }
251                AttrTokenTree::Delimited(sp, spacing, delim, mut inner) => {
252                    inner = self.configure_tokens(&inner);
253                    Some(AttrTokenTree::Delimited(sp, spacing, delim, inner))
254                }
255                AttrTokenTree::Token(Token { kind, .. }, _) if kind.is_delim() => {
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        // A trace attribute left in AST in place of the original `cfg_attr` attribute.
293        // It can later be used by lints or other diagnostics.
294        let trace_attr = attr_into_trace(cfg_attr.clone(), sym::cfg_attr_trace);
295
296        let Some((cfg_predicate, expanded_attrs)) =
297            rustc_attr_parsing::parse_cfg_attr(cfg_attr, &self.sess, self.features)
298        else {
299            return vec![trace_attr];
300        };
301
302        // Lint on zero attributes in source.
303        if expanded_attrs.is_empty() {
304            self.sess.psess.buffer_lint(
305                rustc_lint_defs::builtin::UNUSED_ATTRIBUTES,
306                cfg_attr.span,
307                ast::CRATE_NODE_ID,
308                crate::errors::CfgAttrNoAttributes,
309            );
310        }
311
312        if !attr::eval_config_entry(
313            self.sess,
314            &cfg_predicate,
315            ast::CRATE_NODE_ID,
316            ShouldEmit::ErrorsAndLints,
317        )
318        .as_bool()
319        {
320            return vec![trace_attr];
321        }
322
323        if recursive {
324            // We call `process_cfg_attr` recursively in case there's a
325            // `cfg_attr` inside of another `cfg_attr`. E.g.
326            //  `#[cfg_attr(false, cfg_attr(true, some_attr))]`.
327            let expanded_attrs = expanded_attrs
328                .into_iter()
329                .flat_map(|item| self.process_cfg_attr(&self.expand_cfg_attr_item(cfg_attr, item)));
330            iter::once(trace_attr).chain(expanded_attrs).collect()
331        } else {
332            let expanded_attrs =
333                expanded_attrs.into_iter().map(|item| self.expand_cfg_attr_item(cfg_attr, item));
334            iter::once(trace_attr).chain(expanded_attrs).collect()
335        }
336    }
337
338    fn expand_cfg_attr_item(
339        &self,
340        cfg_attr: &Attribute,
341        (item, item_span): (ast::AttrItem, Span),
342    ) -> Attribute {
343        // Convert `#[cfg_attr(pred, attr)]` to `#[attr]`.
344
345        // Use the `#` from `#[cfg_attr(pred, attr)]` in the result `#[attr]`.
346        let mut orig_trees = cfg_attr.token_trees().into_iter();
347        let Some(TokenTree::Token(pound_token @ Token { kind: TokenKind::Pound, .. }, _)) =
348            orig_trees.next()
349        else {
350            panic!("Bad tokens for attribute {cfg_attr:?}");
351        };
352
353        // For inner attributes, we do the same thing for the `!` in `#![attr]`.
354        let mut trees = if cfg_attr.style == AttrStyle::Inner {
355            let Some(TokenTree::Token(bang_token @ Token { kind: TokenKind::Bang, .. }, _)) =
356                orig_trees.next()
357            else {
358                panic!("Bad tokens for attribute {cfg_attr:?}");
359            };
360            vec![
361                AttrTokenTree::Token(pound_token, Spacing::Joint),
362                AttrTokenTree::Token(bang_token, Spacing::JointHidden),
363            ]
364        } else {
365            vec![AttrTokenTree::Token(pound_token, Spacing::JointHidden)]
366        };
367
368        // And the same thing for the `[`/`]` delimiters in `#[attr]`.
369        let Some(TokenTree::Delimited(delim_span, delim_spacing, Delimiter::Bracket, _)) =
370            orig_trees.next()
371        else {
372            panic!("Bad tokens for attribute {cfg_attr:?}");
373        };
374        trees.push(AttrTokenTree::Delimited(
375            delim_span,
376            delim_spacing,
377            Delimiter::Bracket,
378            item.tokens
379                .as_ref()
380                .unwrap_or_else(|| panic!("Missing tokens for {item:?}"))
381                .to_attr_token_stream(),
382        ));
383
384        let tokens = Some(LazyAttrTokenStream::new_direct(AttrTokenStream::new(trees)));
385        let attr = ast::attr::mk_attr_from_item(
386            &self.sess.psess.attr_id_generator,
387            item,
388            tokens,
389            cfg_attr.style,
390            item_span,
391        );
392        if attr.has_name(sym::crate_type) {
393            self.sess.dcx().emit_err(CrateTypeInCfgAttr { span: attr.span });
394        }
395        if attr.has_name(sym::crate_name) {
396            self.sess.dcx().emit_err(CrateNameInCfgAttr { span: attr.span });
397        }
398        attr
399    }
400
401    /// Determines if a node with the given attributes should be included in this configuration.
402    fn in_cfg(&self, attrs: &[Attribute]) -> bool {
403        attrs.iter().all(|attr| {
404            !is_cfg(attr)
405                || self.cfg_true(attr, self.lint_node_id, ShouldEmit::ErrorsAndLints).as_bool()
406        })
407    }
408
409    pub(crate) fn cfg_true(
410        &self,
411        attr: &Attribute,
412        node: NodeId,
413        emit_errors: ShouldEmit,
414    ) -> EvalConfigResult {
415        let Some(cfg) = AttributeParser::parse_single(
416            self.sess,
417            attr,
418            attr.span,
419            node,
420            self.features,
421            emit_errors,
422            parse_cfg,
423            &CFG_TEMPLATE,
424        ) else {
425            // Cfg attribute was not parsable, give up
426            return EvalConfigResult::True;
427        };
428
429        eval_config_entry(self.sess, &cfg, self.lint_node_id, emit_errors)
430    }
431
432    /// If attributes are not allowed on expressions, emit an error for `attr`
433    #[instrument(level = "trace", skip(self))]
434    pub(crate) fn maybe_emit_expr_attr_err(&self, attr: &Attribute) {
435        if self.features.is_some_and(|features| !features.stmt_expr_attributes())
436            && !attr.span.allows_unstable(sym::stmt_expr_attributes)
437        {
438            let mut err = feature_err(
439                &self.sess,
440                sym::stmt_expr_attributes,
441                attr.span,
442                crate::fluent_generated::expand_attributes_on_expressions_experimental,
443            );
444
445            if attr.is_doc_comment() {
446                err.help(if attr.style == AttrStyle::Outer {
447                    crate::fluent_generated::expand_help_outer_doc
448                } else {
449                    crate::fluent_generated::expand_help_inner_doc
450                });
451            }
452
453            err.emit();
454        }
455    }
456
457    #[instrument(level = "trace", skip(self))]
458    pub fn configure_expr(&self, expr: &mut ast::Expr, method_receiver: bool) {
459        if !method_receiver {
460            for attr in expr.attrs.iter() {
461                self.maybe_emit_expr_attr_err(attr);
462            }
463        }
464
465        // If an expr is valid to cfg away it will have been removed by the
466        // outer stmt or expression folder before descending in here.
467        // Anything else is always required, and thus has to error out
468        // in case of a cfg attr.
469        //
470        // N.B., this is intentionally not part of the visit_expr() function
471        //     in order for filter_map_expr() to be able to avoid this check
472        if let Some(attr) = expr.attrs().iter().find(|a| is_cfg(a)) {
473            self.sess.dcx().emit_err(RemoveExprNotSupported { span: attr.span });
474        }
475
476        self.process_cfg_attrs(expr);
477        self.try_configure_tokens(&mut *expr);
478    }
479}
480
481/// FIXME: Still used by Rustdoc, should be removed after
482pub fn parse_cfg_old<'a>(meta_item: &'a MetaItem, sess: &Session) -> Option<&'a MetaItemInner> {
483    let span = meta_item.span;
484    match meta_item.meta_item_list() {
485        None => {
486            sess.dcx().emit_err(InvalidCfg::NotFollowedByParens { span });
487            None
488        }
489        Some([]) => {
490            sess.dcx().emit_err(InvalidCfg::NoPredicate { span });
491            None
492        }
493        Some([_, .., l]) => {
494            sess.dcx().emit_err(InvalidCfg::MultiplePredicates { span: l.span() });
495            None
496        }
497        Some([single]) => match single.meta_item_or_bool() {
498            Some(meta_item) => Some(meta_item),
499            None => {
500                sess.dcx().emit_err(InvalidCfg::PredicateLiteral { span: single.span() });
501                None
502            }
503        },
504    }
505}
506
507fn is_cfg(attr: &Attribute) -> bool {
508    attr.has_name(sym::cfg)
509}