Skip to main content

rustc_attr_parsing/
interface.rs

1use std::convert::identity;
2
3use rustc_ast as ast;
4use rustc_ast::token::DocFragmentKind;
5use rustc_ast::{AttrItemKind, AttrStyle, NodeId, Safety};
6use rustc_data_structures::fx::FxIndexSet;
7use rustc_errors::DiagCtxtHandle;
8use rustc_feature::{AttributeTemplate, Features};
9use rustc_hir::attrs::AttributeKind;
10use rustc_hir::lints::AttributeLintKind;
11use rustc_hir::{AttrArgs, AttrItem, AttrPath, Attribute, HashIgnoredAttrId, Target};
12use rustc_session::Session;
13use rustc_session::lint::LintId;
14use rustc_span::{DUMMY_SP, Ident, Span, Symbol, sym};
15
16use crate::context::{AcceptContext, FinalizeContext, FinalizeFn, SharedContext, Stage};
17use crate::early_parsed::{EARLY_PARSED_ATTRIBUTES, EarlyParsedState};
18use crate::parser::{AllowExprMetavar, ArgParser, PathParser, RefPathParser};
19use crate::session_diagnostics::ParsedDescription;
20use crate::{Early, Late, OmitDoc, ShouldEmit};
21
22/// Context created once, for example as part of the ast lowering
23/// context, through which all attributes can be lowered.
24pub struct AttributeParser<'sess, S: Stage = Late> {
25    pub(crate) tools: Option<&'sess FxIndexSet<Ident>>,
26    pub(crate) features: Option<&'sess Features>,
27    pub(crate) sess: &'sess Session,
28    pub(crate) stage: S,
29
30    /// *Only* parse attributes with this symbol.
31    ///
32    /// Used in cases where we want the lowering infrastructure for parse just a single attribute.
33    parse_only: Option<Symbol>,
34}
35
36impl<'sess> AttributeParser<'sess, Early> {
37    /// This method allows you to parse attributes *before* you have access to features or tools.
38    /// One example where this is necessary, is to parse `feature` attributes themselves for
39    /// example.
40    ///
41    /// Try to use this as little as possible. Attributes *should* be lowered during
42    /// `rustc_ast_lowering`. Some attributes require access to features to parse, which would
43    /// crash if you tried to do so through [`parse_limited`](Self::parse_limited).
44    ///
45    /// To make sure use is limited, supply a `Symbol` you'd like to parse. Only attributes with
46    /// that symbol are picked out of the list of instructions and parsed. Those are returned.
47    ///
48    /// No diagnostics will be emitted when parsing limited. Lints are not emitted at all, while
49    /// errors will be emitted as a delayed bugs. in other words, we *expect* attributes parsed
50    /// with `parse_limited` to be reparsed later during ast lowering where we *do* emit the errors
51    ///
52    /// Due to this function not taking in RegisteredTools (`FxIndexSet<Ident>`), *do not* use this for parsing any lint attributes
53    pub fn parse_limited(
54        sess: &'sess Session,
55        attrs: &[ast::Attribute],
56        sym: Symbol,
57        target_span: Span,
58        target_node_id: NodeId,
59        features: Option<&'sess Features>,
60    ) -> Option<Attribute> {
61        Self::parse_limited_should_emit(
62            sess,
63            attrs,
64            sym,
65            target_span,
66            target_node_id,
67            Target::Crate, // Does not matter, we're not going to emit errors anyways
68            features,
69            ShouldEmit::Nothing,
70        )
71    }
72
73    /// This does the same as `parse_limited`, except it has a `should_emit` parameter which allows it to emit errors.
74    /// Usually you want `parse_limited`, which emits no errors.
75    ///
76    /// Due to this function not taking in RegisteredTools (`FxIndexSet<Ident>`), *do not* use this for parsing any lint attributes
77    pub fn parse_limited_should_emit(
78        sess: &'sess Session,
79        attrs: &[ast::Attribute],
80        sym: Symbol,
81        target_span: Span,
82        target_node_id: NodeId,
83        target: Target,
84        features: Option<&'sess Features>,
85        should_emit: ShouldEmit,
86    ) -> Option<Attribute> {
87        let mut parsed = Self::parse_limited_all(
88            sess,
89            attrs,
90            Some(sym),
91            target,
92            target_span,
93            target_node_id,
94            features,
95            should_emit,
96            None,
97        );
98        if !(parsed.len() <= 1) {
    ::core::panicking::panic("assertion failed: parsed.len() <= 1")
};assert!(parsed.len() <= 1);
99        parsed.pop()
100    }
101
102    /// This method allows you to parse a list of attributes *before* `rustc_ast_lowering`.
103    /// This can be used for attributes that would be removed before `rustc_ast_lowering`, such as attributes on macro calls.
104    ///
105    /// Try to use this as little as possible. Attributes *should* be lowered during
106    /// `rustc_ast_lowering`. Some attributes require access to features to parse, which would
107    /// crash if you tried to do so through [`parse_limited_all`](Self::parse_limited_all).
108    /// Therefore, if `parse_only` is None, then features *must* be provided.
109    pub fn parse_limited_all<'a>(
110        sess: &'sess Session,
111        attrs: impl IntoIterator<Item = &'a ast::Attribute>,
112        parse_only: Option<Symbol>,
113        target: Target,
114        target_span: Span,
115        target_node_id: NodeId,
116        features: Option<&'sess Features>,
117        emit_errors: ShouldEmit,
118        tools: Option<&'sess FxIndexSet<Ident>>,
119    ) -> Vec<Attribute> {
120        let mut p = Self { features, tools, parse_only, sess, stage: Early { emit_errors } };
121        p.parse_attribute_list(
122            attrs,
123            target_span,
124            target,
125            OmitDoc::Skip,
126            std::convert::identity,
127            |lint_id, span, kind| sess.psess.buffer_lint(lint_id.lint, span, target_node_id, kind),
128        )
129    }
130
131    /// This method provides the same functionality as [`parse_limited_all`](Self::parse_limited_all) except filtered,
132    /// making sure that only allow-listed symbols are parsed
133    pub fn parse_limited_all_filtered<'a>(
134        sess: &'sess Session,
135        attrs: impl IntoIterator<Item = &'a ast::Attribute>,
136        filter: &[Symbol],
137        target: Target,
138        target_span: Span,
139        target_node_id: NodeId,
140        features: Option<&'sess Features>,
141        emit_errors: ShouldEmit,
142        tools: &'sess FxIndexSet<Ident>,
143    ) -> Vec<Attribute> {
144        Self::parse_limited_all(
145            sess,
146            attrs.into_iter().filter(|attr| attr.has_any_name(filter)),
147            None,
148            target,
149            target_span,
150            target_node_id,
151            features,
152            emit_errors,
153            Some(tools),
154        )
155    }
156
157    /// This method parses a single attribute, using `parse_fn`.
158    /// This is useful if you already know what exact attribute this is, and want to parse it.
159    pub fn parse_single<T>(
160        sess: &'sess Session,
161        attr: &ast::Attribute,
162        target_span: Span,
163        target_node_id: NodeId,
164        target: Target,
165        features: Option<&'sess Features>,
166        emit_errors: ShouldEmit,
167        parse_fn: fn(cx: &mut AcceptContext<'_, '_, Early>, item: &ArgParser) -> Option<T>,
168        template: &AttributeTemplate,
169        allow_expr_metavar: AllowExprMetavar,
170    ) -> Option<T> {
171        let ast::AttrKind::Normal(normal_attr) = &attr.kind else {
172            {
    ::core::panicking::panic_fmt(format_args!("parse_single called on a doc attr"));
}panic!("parse_single called on a doc attr")
173        };
174        let parts =
175            normal_attr.item.path.segments.iter().map(|seg| seg.ident.name).collect::<Vec<_>>();
176
177        let path = AttrPath::from_ast(&normal_attr.item.path, identity);
178        let args = ArgParser::from_attr_args(
179            &normal_attr.item.args.unparsed_ref().unwrap(),
180            &parts,
181            &sess.psess,
182            emit_errors,
183            allow_expr_metavar,
184        )?;
185        Self::parse_single_args(
186            sess,
187            attr.span,
188            normal_attr.item.span(),
189            attr.style,
190            path,
191            Some(normal_attr.item.unsafety),
192            ParsedDescription::Attribute,
193            target_span,
194            target_node_id,
195            target,
196            features,
197            emit_errors,
198            &args,
199            parse_fn,
200            template,
201        )
202    }
203
204    /// This method is equivalent to `parse_single`, but parses arguments using `parse_fn` using manually created `args`.
205    /// This is useful when you want to parse other things than attributes using attribute parsers.
206    pub fn parse_single_args<T, I>(
207        sess: &'sess Session,
208        attr_span: Span,
209        inner_span: Span,
210        attr_style: AttrStyle,
211        attr_path: AttrPath,
212        attr_safety: Option<Safety>,
213        parsed_description: ParsedDescription,
214        target_span: Span,
215        target_node_id: NodeId,
216        target: Target,
217        features: Option<&'sess Features>,
218        emit_errors: ShouldEmit,
219        args: &I,
220        parse_fn: fn(cx: &mut AcceptContext<'_, '_, Early>, item: &I) -> T,
221        template: &AttributeTemplate,
222    ) -> T {
223        let mut parser =
224            Self { features, tools: None, parse_only: None, sess, stage: Early { emit_errors } };
225        let mut emit_lint = |lint_id: LintId, span: Span, kind: AttributeLintKind| {
226            sess.psess.buffer_lint(lint_id.lint, span, target_node_id, kind)
227        };
228        if let Some(safety) = attr_safety {
229            parser.check_attribute_safety(&attr_path, inner_span, safety, &mut emit_lint)
230        }
231        let attr_id = sess.psess.attr_id_generator.mk_attr_id();
232        let mut cx: AcceptContext<'_, 'sess, Early> = AcceptContext {
233            shared: SharedContext {
234                cx: &mut parser,
235                target_span,
236                target,
237                emit_lint: &mut emit_lint,
238            },
239            attr_span,
240            inner_span,
241            attr_style,
242            parsed_description,
243            template,
244            attr_path,
245            attr_id,
246        };
247        parse_fn(&mut cx, args)
248    }
249}
250
251impl<'sess, S: Stage> AttributeParser<'sess, S> {
252    pub fn new(
253        sess: &'sess Session,
254        features: &'sess Features,
255        tools: &'sess FxIndexSet<Ident>,
256        stage: S,
257    ) -> Self {
258        Self { features: Some(features), tools: Some(tools), parse_only: None, sess, stage }
259    }
260
261    pub(crate) fn sess(&self) -> &'sess Session {
262        &self.sess
263    }
264
265    pub(crate) fn features(&self) -> &'sess Features {
266        self.features.expect("features not available at this point in the compiler")
267    }
268
269    pub(crate) fn features_option(&self) -> Option<&'sess Features> {
270        self.features
271    }
272
273    pub(crate) fn dcx(&self) -> DiagCtxtHandle<'sess> {
274        self.sess().dcx()
275    }
276
277    /// Parse a list of attributes.
278    ///
279    /// `target_span` is the span of the thing this list of attributes is applied to,
280    /// and when `omit_doc` is set, doc attributes are filtered out.
281    pub fn parse_attribute_list<'a>(
282        &mut self,
283        attrs: impl IntoIterator<Item = &'a ast::Attribute>,
284        target_span: Span,
285        target: Target,
286        omit_doc: OmitDoc,
287        lower_span: impl Copy + Fn(Span) -> Span,
288        mut emit_lint: impl FnMut(LintId, Span, AttributeLintKind),
289    ) -> Vec<Attribute> {
290        let mut attributes = Vec::new();
291        // We store the attributes we intend to discard at the end of this function in order to
292        // check they are applied to the right target and error out if necessary. In practice, we
293        // end up dropping only derive attributes and derive helpers, both being fully processed
294        // at macro expansion.
295        let mut dropped_attributes = Vec::new();
296        let mut attr_paths: Vec<RefPathParser<'_>> = Vec::new();
297        let mut early_parsed_state = EarlyParsedState::default();
298
299        let mut finalizers: Vec<&FinalizeFn<S>> = Vec::new();
300
301        for attr in attrs.into_iter() {
302            // If we're only looking for a single attribute, skip all the ones we don't care about.
303            if let Some(expected) = self.parse_only {
304                if !attr.has_name(expected) {
305                    continue;
306                }
307            }
308
309            // Sometimes, for example for `#![doc = include_str!("readme.md")]`,
310            // doc still contains a non-literal. You might say, when we're lowering attributes
311            // that's expanded right? But no, sometimes, when parsing attributes on macros,
312            // we already use the lowering logic and these are still there. So, when `omit_doc`
313            // is set we *also* want to ignore these.
314            let is_doc_attribute = attr.has_name(sym::doc);
315            if omit_doc == OmitDoc::Skip && is_doc_attribute {
316                continue;
317            }
318
319            let attr_span = lower_span(attr.span);
320            match &attr.kind {
321                ast::AttrKind::DocComment(comment_kind, symbol) => {
322                    if omit_doc == OmitDoc::Skip {
323                        continue;
324                    }
325
326                    attributes.push(Attribute::Parsed(AttributeKind::DocComment {
327                        style: attr.style,
328                        kind: DocFragmentKind::Sugared(*comment_kind),
329                        span: attr_span,
330                        comment: *symbol,
331                    }));
332                }
333                ast::AttrKind::Normal(n) => {
334                    attr_paths.push(PathParser(&n.item.path));
335                    let attr_path = AttrPath::from_ast(&n.item.path, lower_span);
336
337                    let args = match &n.item.args {
338                        AttrItemKind::Unparsed(args) => args,
339                        AttrItemKind::Parsed(parsed) => {
340                            early_parsed_state
341                                .accept_early_parsed_attribute(attr_span, lower_span, parsed);
342                            continue;
343                        }
344                    };
345
346                    self.check_attribute_safety(
347                        &attr_path,
348                        lower_span(n.item.span()),
349                        n.item.unsafety,
350                        &mut emit_lint,
351                    );
352
353                    let parts =
354                        n.item.path.segments.iter().map(|seg| seg.ident.name).collect::<Vec<_>>();
355
356                    if let Some(accept) = S::parsers().accepters.get(parts.as_slice()) {
357                        let Some(args) = ArgParser::from_attr_args(
358                            args,
359                            &parts,
360                            &self.sess.psess,
361                            self.stage.should_emit(),
362                            AllowExprMetavar::No,
363                        ) else {
364                            continue;
365                        };
366
367                        // Special-case handling for `#[doc = "..."]`: if we go through with
368                        // `DocParser`, the order of doc comments will be messed up because `///`
369                        // doc comments are added into `attributes` whereas attributes parsed with
370                        // `DocParser` are added into `parsed_attributes` which are then appended
371                        // to `attributes`. So if you have:
372                        //
373                        // /// bla
374                        // #[doc = "a"]
375                        // /// blob
376                        //
377                        // You would get:
378                        //
379                        // bla
380                        // blob
381                        // a
382                        if is_doc_attribute
383                            && let ArgParser::NameValue(nv) = &args
384                            // If not a string key/value, it should emit an error, but to make
385                            // things simpler, it's handled in `DocParser` because it's simpler to
386                            // emit an error with `AcceptContext`.
387                            && let Some(comment) = nv.value_as_str()
388                        {
389                            attributes.push(Attribute::Parsed(AttributeKind::DocComment {
390                                style: attr.style,
391                                kind: DocFragmentKind::Raw(nv.value_span),
392                                span: attr_span,
393                                comment,
394                            }));
395                            continue;
396                        }
397
398                        let mut cx: AcceptContext<'_, 'sess, S> = AcceptContext {
399                            shared: SharedContext {
400                                cx: self,
401                                target_span,
402                                target,
403                                emit_lint: &mut emit_lint,
404                            },
405                            attr_span,
406                            inner_span: lower_span(n.item.span()),
407                            attr_style: attr.style,
408                            parsed_description: ParsedDescription::Attribute,
409                            template: &accept.template,
410                            attr_path: attr_path.clone(),
411                            attr_id: attr.id,
412                        };
413
414                        (accept.accept_fn)(&mut cx, &args);
415                        finalizers.push(&accept.finalizer);
416
417                        if !#[allow(non_exhaustive_omitted_patterns)] match cx.stage.should_emit() {
    ShouldEmit::Nothing => true,
    _ => false,
}matches!(cx.stage.should_emit(), ShouldEmit::Nothing) {
418                            Self::check_target(&accept.allowed_targets, target, &mut cx);
419                        }
420                    } else {
421                        let attr = AttrItem {
422                            path: attr_path.clone(),
423                            args: self
424                                .lower_attr_args(n.item.args.unparsed_ref().unwrap(), lower_span),
425                            id: HashIgnoredAttrId { attr_id: attr.id },
426                            style: attr.style,
427                            span: attr_span,
428                        };
429
430                        if !#[allow(non_exhaustive_omitted_patterns)] match self.stage.should_emit() {
    ShouldEmit::Nothing => true,
    _ => false,
}matches!(self.stage.should_emit(), ShouldEmit::Nothing)
431                            && target == Target::Crate
432                        {
433                            self.check_invalid_crate_level_attr_item(&attr, n.item.span());
434                        }
435
436                        let attr = Attribute::Unparsed(Box::new(attr));
437
438                        if self
439                            .tools
440                            .is_some_and(|tools| tools.iter().any(|tool| tool.name == parts[0]))
441                        {
442                            attributes.push(attr);
443                        } else {
444                            dropped_attributes.push(attr);
445                        }
446                    }
447                }
448            }
449        }
450
451        early_parsed_state.finalize_early_parsed_attributes(&mut attributes);
452        for f in &finalizers {
453            if let Some(attr) = f(&mut FinalizeContext {
454                shared: SharedContext { cx: self, target_span, target, emit_lint: &mut emit_lint },
455                all_attrs: &attr_paths,
456            }) {
457                attributes.push(Attribute::Parsed(attr));
458            }
459        }
460
461        if !#[allow(non_exhaustive_omitted_patterns)] match self.stage.should_emit() {
    ShouldEmit::Nothing => true,
    _ => false,
}matches!(self.stage.should_emit(), ShouldEmit::Nothing)
462            && target == Target::WherePredicate
463        {
464            self.check_invalid_where_predicate_attrs(attributes.iter().chain(&dropped_attributes));
465        }
466
467        attributes
468    }
469
470    /// Returns whether there is a parser for an attribute with this name
471    pub fn is_parsed_attribute(path: &[Symbol]) -> bool {
472        /// The list of attributes that are parsed attributes,
473        /// even though they don't have a parser in `Late::parsers()`
474        const SPECIAL_ATTRIBUTES: &[&[Symbol]] = &[
475            // Cfg attrs are removed after being early-parsed, so don't need to be in the parser list
476            &[sym::cfg],
477            &[sym::cfg_attr],
478        ];
479
480        Late::parsers().accepters.contains_key(path)
481            || EARLY_PARSED_ATTRIBUTES.contains(&path)
482            || SPECIAL_ATTRIBUTES.contains(&path)
483    }
484
485    fn lower_attr_args(&self, args: &ast::AttrArgs, lower_span: impl Fn(Span) -> Span) -> AttrArgs {
486        match args {
487            ast::AttrArgs::Empty => AttrArgs::Empty,
488            ast::AttrArgs::Delimited(args) => AttrArgs::Delimited(args.clone()),
489            // This is an inert key-value attribute - it will never be visible to macros
490            // after it gets lowered to HIR. Therefore, we can extract literals to handle
491            // nonterminals in `#[doc]` (e.g. `#[doc = $e]`).
492            ast::AttrArgs::Eq { eq_span, expr } => {
493                // In valid code the value always ends up as a single literal. Otherwise, a dummy
494                // literal suffices because the error is handled elsewhere.
495                let lit = if let ast::ExprKind::Lit(token_lit) = expr.kind
496                    && let Ok(lit) =
497                        ast::MetaItemLit::from_token_lit(token_lit, lower_span(expr.span))
498                {
499                    lit
500                } else {
501                    let guar = self.dcx().span_delayed_bug(
502                        args.span().unwrap_or(DUMMY_SP),
503                        "expr in place where literal is expected (builtin attr parsing)",
504                    );
505                    ast::MetaItemLit {
506                        symbol: sym::dummy,
507                        suffix: None,
508                        kind: ast::LitKind::Err(guar),
509                        span: DUMMY_SP,
510                    }
511                };
512                AttrArgs::Eq { eq_span: lower_span(*eq_span), expr: lit }
513            }
514        }
515    }
516}