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::{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    ) -> Option<T> {
143        let ast::AttrKind::Normal(normal_attr) = &attr.kind else {
144            {
    ::core::panicking::panic_fmt(format_args!("parse_single called on a doc attr"));
}panic!("parse_single called on a doc attr")
145        };
146        let parts =
147            normal_attr.item.path.segments.iter().map(|seg| seg.ident.name).collect::<Vec<_>>();
148
149        let path = AttrPath::from_ast(&normal_attr.item.path, identity);
150        let args = ArgParser::from_attr_args(
151            &normal_attr.item.args.unparsed_ref().unwrap(),
152            &parts,
153            &sess.psess,
154            emit_errors,
155        )?;
156        Self::parse_single_args(
157            sess,
158            attr.span,
159            normal_attr.item.span(),
160            attr.style,
161            path,
162            Some(normal_attr.item.unsafety),
163            ParsedDescription::Attribute,
164            target_span,
165            target_node_id,
166            target,
167            features,
168            emit_errors,
169            &args,
170            parse_fn,
171            template,
172        )
173    }
174
175    /// This method is equivalent to `parse_single`, but parses arguments using `parse_fn` using manually created `args`.
176    /// This is useful when you want to parse other things than attributes using attribute parsers.
177    pub fn parse_single_args<T, I>(
178        sess: &'sess Session,
179        attr_span: Span,
180        inner_span: Span,
181        attr_style: AttrStyle,
182        attr_path: AttrPath,
183        attr_safety: Option<Safety>,
184        parsed_description: ParsedDescription,
185        target_span: Span,
186        target_node_id: NodeId,
187        target: Target,
188        features: Option<&'sess Features>,
189        emit_errors: ShouldEmit,
190        args: &I,
191        parse_fn: fn(cx: &mut AcceptContext<'_, '_, Early>, item: &I) -> T,
192        template: &AttributeTemplate,
193    ) -> T {
194        let mut parser = Self {
195            features,
196            tools: Vec::new(),
197            parse_only: None,
198            sess,
199            stage: Early { emit_errors },
200        };
201        let mut emit_lint = |lint_id: LintId, span: Span, kind: AttributeLintKind| {
202            sess.psess.buffer_lint(
203                lint_id.lint,
204                span,
205                target_node_id,
206                BuiltinLintDiag::AttributeLint(kind),
207            )
208        };
209        if let Some(safety) = attr_safety {
210            parser.check_attribute_safety(&attr_path, inner_span, safety, &mut emit_lint)
211        }
212        let mut cx: AcceptContext<'_, 'sess, Early> = AcceptContext {
213            shared: SharedContext {
214                cx: &mut parser,
215                target_span,
216                target,
217                emit_lint: &mut emit_lint,
218            },
219            attr_span,
220            inner_span,
221            attr_style,
222            parsed_description,
223            template,
224            attr_path,
225        };
226        parse_fn(&mut cx, args)
227    }
228}
229
230impl<'sess, S: Stage> AttributeParser<'sess, S> {
231    pub fn new(
232        sess: &'sess Session,
233        features: &'sess Features,
234        tools: Vec<Symbol>,
235        stage: S,
236    ) -> Self {
237        Self { features: Some(features), tools, parse_only: None, sess, stage }
238    }
239
240    pub(crate) fn sess(&self) -> &'sess Session {
241        &self.sess
242    }
243
244    pub(crate) fn features(&self) -> &'sess Features {
245        self.features.expect("features not available at this point in the compiler")
246    }
247
248    pub(crate) fn features_option(&self) -> Option<&'sess Features> {
249        self.features
250    }
251
252    pub(crate) fn dcx(&self) -> DiagCtxtHandle<'sess> {
253        self.sess().dcx()
254    }
255
256    /// Parse a list of attributes.
257    ///
258    /// `target_span` is the span of the thing this list of attributes is applied to,
259    /// and when `omit_doc` is set, doc attributes are filtered out.
260    pub fn parse_attribute_list(
261        &mut self,
262        attrs: &[ast::Attribute],
263        target_span: Span,
264        target: Target,
265        omit_doc: OmitDoc,
266        lower_span: impl Copy + Fn(Span) -> Span,
267        mut emit_lint: impl FnMut(LintId, Span, AttributeLintKind),
268    ) -> Vec<Attribute> {
269        let mut attributes = Vec::new();
270        let mut attr_paths: Vec<RefPathParser<'_>> = Vec::new();
271        let mut early_parsed_state = EarlyParsedState::default();
272
273        let mut finalizers: Vec<&FinalizeFn<S>> = Vec::with_capacity(attrs.len());
274
275        for attr in attrs {
276            // If we're only looking for a single attribute, skip all the ones we don't care about.
277            if let Some(expected) = self.parse_only {
278                if !attr.has_name(expected) {
279                    continue;
280                }
281            }
282
283            // Sometimes, for example for `#![doc = include_str!("readme.md")]`,
284            // doc still contains a non-literal. You might say, when we're lowering attributes
285            // that's expanded right? But no, sometimes, when parsing attributes on macros,
286            // we already use the lowering logic and these are still there. So, when `omit_doc`
287            // is set we *also* want to ignore these.
288            let is_doc_attribute = attr.has_name(sym::doc);
289            if omit_doc == OmitDoc::Skip && is_doc_attribute {
290                continue;
291            }
292
293            let attr_span = lower_span(attr.span);
294            match &attr.kind {
295                ast::AttrKind::DocComment(comment_kind, symbol) => {
296                    if omit_doc == OmitDoc::Skip {
297                        continue;
298                    }
299
300                    attributes.push(Attribute::Parsed(AttributeKind::DocComment {
301                        style: attr.style,
302                        kind: DocFragmentKind::Sugared(*comment_kind),
303                        span: attr_span,
304                        comment: *symbol,
305                    }))
306                }
307                ast::AttrKind::Normal(n) => {
308                    attr_paths.push(PathParser(&n.item.path));
309                    let attr_path = AttrPath::from_ast(&n.item.path, lower_span);
310
311                    let args = match &n.item.args {
312                        AttrItemKind::Unparsed(args) => args,
313                        AttrItemKind::Parsed(parsed) => {
314                            early_parsed_state
315                                .accept_early_parsed_attribute(attr_span, lower_span, parsed);
316                            continue;
317                        }
318                    };
319
320                    self.check_attribute_safety(
321                        &attr_path,
322                        lower_span(n.item.span()),
323                        n.item.unsafety,
324                        &mut emit_lint,
325                    );
326
327                    let parts =
328                        n.item.path.segments.iter().map(|seg| seg.ident.name).collect::<Vec<_>>();
329
330                    if let Some(accepts) = S::parsers().accepters.get(parts.as_slice()) {
331                        let Some(args) = ArgParser::from_attr_args(
332                            args,
333                            &parts,
334                            &self.sess.psess,
335                            self.stage.should_emit(),
336                        ) else {
337                            continue;
338                        };
339
340                        // Special-case handling for `#[doc = "..."]`: if we go through with
341                        // `DocParser`, the order of doc comments will be messed up because `///`
342                        // doc comments are added into `attributes` whereas attributes parsed with
343                        // `DocParser` are added into `parsed_attributes` which are then appended
344                        // to `attributes`. So if you have:
345                        //
346                        // /// bla
347                        // #[doc = "a"]
348                        // /// blob
349                        //
350                        // You would get:
351                        //
352                        // bla
353                        // blob
354                        // a
355                        if is_doc_attribute
356                            && let ArgParser::NameValue(nv) = &args
357                            // If not a string key/value, it should emit an error, but to make
358                            // things simpler, it's handled in `DocParser` because it's simpler to
359                            // emit an error with `AcceptContext`.
360                            && let Some(comment) = nv.value_as_str()
361                        {
362                            attributes.push(Attribute::Parsed(AttributeKind::DocComment {
363                                style: attr.style,
364                                kind: DocFragmentKind::Raw(nv.value_span),
365                                span: attr_span,
366                                comment,
367                            }));
368                            continue;
369                        }
370
371                        for accept in accepts {
372                            let mut cx: AcceptContext<'_, 'sess, S> = AcceptContext {
373                                shared: SharedContext {
374                                    cx: self,
375                                    target_span,
376                                    target,
377                                    emit_lint: &mut emit_lint,
378                                },
379                                attr_span,
380                                inner_span: lower_span(n.item.span()),
381                                attr_style: attr.style,
382                                parsed_description: ParsedDescription::Attribute,
383                                template: &accept.template,
384                                attr_path: attr_path.clone(),
385                            };
386
387                            (accept.accept_fn)(&mut cx, &args);
388                            finalizers.push(&accept.finalizer);
389
390                            if !#[allow(non_exhaustive_omitted_patterns)] match cx.stage.should_emit() {
    ShouldEmit::Nothing => true,
    _ => false,
}matches!(cx.stage.should_emit(), ShouldEmit::Nothing) {
391                                Self::check_target(&accept.allowed_targets, target, &mut cx);
392                            }
393                        }
394                    } else {
395                        // If we're here, we must be compiling a tool attribute... Or someone
396                        // forgot to parse their fancy new attribute. Let's warn them in any case.
397                        // If you are that person, and you really think your attribute should
398                        // remain unparsed, carefully read the documentation in this module and if
399                        // you still think so you can add an exception to this assertion.
400
401                        // FIXME(jdonszelmann): convert other attributes, and check with this that
402                        // we caught em all
403                        // const FIXME_TEMPORARY_ATTR_ALLOWLIST: &[Symbol] = &[sym::cfg];
404                        // assert!(
405                        //     self.tools.contains(&parts[0]) || true,
406                        //     // || FIXME_TEMPORARY_ATTR_ALLOWLIST.contains(&parts[0]),
407                        //     "attribute {path} wasn't parsed and isn't a know tool attribute",
408                        // );
409
410                        attributes.push(Attribute::Unparsed(Box::new(AttrItem {
411                            path: attr_path.clone(),
412                            args: self
413                                .lower_attr_args(n.item.args.unparsed_ref().unwrap(), lower_span),
414                            id: HashIgnoredAttrId { attr_id: attr.id },
415                            style: attr.style,
416                            span: attr_span,
417                        })));
418                    }
419                }
420            }
421        }
422
423        early_parsed_state.finalize_early_parsed_attributes(&mut attributes);
424        for f in &finalizers {
425            if let Some(attr) = f(&mut FinalizeContext {
426                shared: SharedContext { cx: self, target_span, target, emit_lint: &mut emit_lint },
427                all_attrs: &attr_paths,
428            }) {
429                attributes.push(Attribute::Parsed(attr));
430            }
431        }
432
433        attributes
434    }
435
436    /// Returns whether there is a parser for an attribute with this name
437    pub fn is_parsed_attribute(path: &[Symbol]) -> bool {
438        /// The list of attributes that are parsed attributes,
439        /// even though they don't have a parser in `Late::parsers()`
440        const SPECIAL_ATTRIBUTES: &[&[Symbol]] = &[
441            // Cfg attrs are removed after being early-parsed, so don't need to be in the parser list
442            &[sym::cfg],
443            &[sym::cfg_attr],
444        ];
445
446        Late::parsers().accepters.contains_key(path)
447            || EARLY_PARSED_ATTRIBUTES.contains(&path)
448            || SPECIAL_ATTRIBUTES.contains(&path)
449    }
450
451    fn lower_attr_args(&self, args: &ast::AttrArgs, lower_span: impl Fn(Span) -> Span) -> AttrArgs {
452        match args {
453            ast::AttrArgs::Empty => AttrArgs::Empty,
454            ast::AttrArgs::Delimited(args) => AttrArgs::Delimited(args.clone()),
455            // This is an inert key-value attribute - it will never be visible to macros
456            // after it gets lowered to HIR. Therefore, we can extract literals to handle
457            // nonterminals in `#[doc]` (e.g. `#[doc = $e]`).
458            ast::AttrArgs::Eq { eq_span, expr } => {
459                // In valid code the value always ends up as a single literal. Otherwise, a dummy
460                // literal suffices because the error is handled elsewhere.
461                let lit = if let ast::ExprKind::Lit(token_lit) = expr.kind
462                    && let Ok(lit) =
463                        ast::MetaItemLit::from_token_lit(token_lit, lower_span(expr.span))
464                {
465                    lit
466                } else {
467                    let guar = self.dcx().span_delayed_bug(
468                        args.span().unwrap_or(DUMMY_SP),
469                        "expr in place where literal is expected (builtin attr parsing)",
470                    );
471                    ast::MetaItemLit {
472                        symbol: sym::dummy,
473                        suffix: None,
474                        kind: ast::LitKind::Err(guar),
475                        span: DUMMY_SP,
476                    }
477                };
478                AttrArgs::Eq { eq_span: lower_span(*eq_span), expr: lit }
479            }
480        }
481    }
482}