Skip to main content

rustc_attr_parsing/
interface.rs

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