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