Skip to main content

rustc_attr_parsing/attributes/
mod.rs

1//! Traits for parsing attributes.
2//!
3//! This module defines traits for attribute parsers, little state machines that recognize and parse
4//! attributes out of a longer list of attributes. The main trait is called [`AttributeParser`].
5//! You can find more docs about [`AttributeParser`]s on the trait itself.
6//! However, for many types of attributes, implementing [`AttributeParser`] is not necessary.
7//! It allows for a lot of flexibility you might not want.
8//!
9//! Specifically, you might not care about managing the state of your [`AttributeParser`]
10//! state machine yourself. In this case you can choose to implement:
11//!
12//! - [`NoArgsAttributeParser`]: used for implementing an attribute that appears only once and
13//! accepts no arguments
14//! - [`SingleAttributeParser`]: makes it easy to implement an attribute which should error if it
15//! appears more than once in a list of attributes
16//! - [`CombineAttributeParser`]: makes it easy to implement an attribute which should combine the
17//! contents of attributes, if an attribute appear multiple times in a list
18//!
19//! Attributes should be added to `crate::context::ATTRIBUTE_PARSERS` to be parsed.
20
21use std::marker::PhantomData;
22
23use rustc_feature::AttributeStability;
24use rustc_hir::attrs::AttributeKind;
25use rustc_span::edition::Edition;
26use rustc_span::{Span, Symbol};
27use thin_vec::ThinVec;
28
29use crate::context::{AcceptContext, FinalizeContext};
30use crate::parser::ArgParser;
31use crate::session_diagnostics::UnusedMultiple;
32use crate::target_checking::AllowedTargets;
33use crate::{AttributeTemplate, template};
34
35/// All the parsers require roughly the same imports, so this prelude has most of the often-needed ones.
36mod prelude;
37
38pub(crate) mod allow_unstable;
39pub(crate) mod autodiff;
40pub(crate) mod body;
41pub(crate) mod cfg;
42pub(crate) mod cfg_select;
43pub(crate) mod cfi_encoding;
44pub(crate) mod codegen_attrs;
45pub(crate) mod confusables;
46pub(crate) mod crate_level;
47pub(crate) mod debugger;
48pub(crate) mod deprecation;
49pub(crate) mod diagnostic;
50pub(crate) mod doc;
51pub(crate) mod dummy;
52pub(crate) mod inline;
53pub(crate) mod instruction_set;
54pub(crate) mod link_attrs;
55pub(crate) mod lint_helpers;
56pub(crate) mod loop_match;
57pub(crate) mod macro_attrs;
58pub(crate) mod must_not_suspend;
59pub(crate) mod must_use;
60pub(crate) mod no_implicit_prelude;
61pub(crate) mod no_link;
62pub(crate) mod non_exhaustive;
63pub(crate) mod path;
64pub(crate) mod pin_v2;
65pub(crate) mod proc_macro_attrs;
66pub(crate) mod prototype;
67pub(crate) mod repr;
68pub(crate) mod rustc_allocator;
69pub(crate) mod rustc_dump;
70pub(crate) mod rustc_internal;
71pub(crate) mod semantics;
72pub(crate) mod splat;
73pub(crate) mod stability;
74pub(crate) mod test_attrs;
75pub(crate) mod traits;
76pub(crate) mod transparency;
77pub(crate) mod unroll;
78pub(crate) mod util;
79
80type AcceptFn<T> = for<'sess> fn(&mut T, &mut AcceptContext<'_, 'sess>, &ArgParser);
81type AcceptMapping<T> =
82    &'static [(&'static [Symbol], AttributeTemplate, AttributeStability, AcceptFn<T>)];
83
84/// An [`AttributeParser`] is a type which searches for syntactic attributes.
85///
86/// Parsers are often tiny state machines that gets to see all syntactical attributes on an item.
87/// [`Default::default`] creates a fresh instance that sits in some kind of initial state, usually that the
88/// attribute it is looking for was not yet seen.
89///
90/// Then, it defines what paths this group will accept in [`AttributeParser::ATTRIBUTES`].
91/// These are listed as pairs, of symbols and function pointers. The function pointer will
92/// be called when that attribute is found on an item, which can influence the state of the little
93/// state machine.
94///
95/// Finally, after all attributes on an item have been seen, and possibly been accepted,
96/// the [`finalize`](AttributeParser::finalize) functions for all attribute parsers are called. Each can then report
97/// whether it has seen the attribute it has been looking for.
98///
99/// The state machine is automatically reset to parse attributes on the next item.
100///
101/// For a simpler attribute parsing interface, consider using [`SingleAttributeParser`]
102/// or [`CombineAttributeParser`] instead.
103pub(crate) trait AttributeParser: Default + 'static {
104    /// The symbols for the attributes that this parser is interested in.
105    ///
106    /// If an attribute has this symbol, the `accept` function will be called on it.
107    const ATTRIBUTES: AcceptMapping<Self>;
108    const ALLOWED_TARGETS: AllowedTargets<'_>;
109    const SAFETY: AttributeSafety = AttributeSafety::Normal;
110
111    /// The parser has gotten a chance to accept the attributes on an item,
112    /// here it can produce an attribute.
113    ///
114    /// All finalize methods of all parsers are unconditionally called.
115    /// This means you can't unconditionally return `Some` here,
116    /// that'd be equivalent to unconditionally applying an attribute to
117    /// every single syntax item that could have attributes applied to it.
118    /// Your accept mappings should determine whether this returns something.
119    fn finalize(self, cx: &FinalizeContext<'_, '_>) -> Option<AttributeKind>;
120}
121
122/// Alternative to [`AttributeParser`] that automatically handles state management.
123/// A slightly simpler and more restricted way to convert attributes.
124/// Assumes that an attribute can only appear a single time on an item,
125/// and errors when it sees more.
126///
127/// [`Single<T> where T: SingleAttributeParser`](Single) implements [`AttributeParser`].
128///
129/// [`SingleAttributeParser`] can only convert attributes one-to-one, and cannot combine multiple
130/// attributes together like is necessary for `#[stable()]` and `#[unstable()]` for example.
131pub(crate) trait SingleAttributeParser: 'static {
132    /// The single path of the attribute this parser accepts.
133    ///
134    /// If you need the parser to accept more than one path, use [`AttributeParser`] instead
135    const PATH: &[Symbol];
136
137    /// Configures what to do when when the same attribute is
138    /// applied more than once on the same syntax node.
139    const ON_DUPLICATE: OnDuplicate = OnDuplicate::Error;
140    const SAFETY: AttributeSafety = AttributeSafety::Normal;
141    const STABILITY: AttributeStability;
142
143    const ALLOWED_TARGETS: AllowedTargets<'_>;
144
145    /// The template this attribute parser should implement. Used for diagnostics.
146    const TEMPLATE: AttributeTemplate;
147
148    /// Converts a single syntactical attribute to a single semantic attribute, or [`AttributeKind`]
149    fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind>;
150
151    /// Optional cross-attribute validation, run once during finalization after all
152    /// attributes on the item have been parsed. Unlike [`convert`](Self::convert), this
153    /// has access to the sibling attributes via [`FinalizeContext::all_attrs`], so it can
154    /// reject incompatible combinations. `attr_span` is the span of this attribute.
155    ///
156    /// Defaults to a no-op.
157    fn finalize_check(_cx: &FinalizeContext<'_, '_>, _attr_span: Span) {}
158}
159
160/// Use in combination with [`SingleAttributeParser`].
161/// `Single<T: SingleAttributeParser>` implements [`AttributeParser`].
162pub(crate) struct Single<T: SingleAttributeParser>(PhantomData<T>, Option<(AttributeKind, Span)>);
163
164impl<T: SingleAttributeParser> Default for Single<T> {
165    fn default() -> Self {
166        Self(Default::default(), Default::default())
167    }
168}
169
170impl<T: SingleAttributeParser> AttributeParser for Single<T> {
171    const ATTRIBUTES: AcceptMapping<Self> = &[(
172        T::PATH,
173        <T as SingleAttributeParser>::TEMPLATE,
174        T::STABILITY,
175        |group: &mut Single<T>, cx, args| {
176            if let Some(pa) = T::convert(cx, args) {
177                if let Some((_, used)) = group.1 {
178                    T::ON_DUPLICATE.exec::<T>(cx, used, cx.attr_span);
179                } else {
180                    group.1 = Some((pa, cx.attr_span));
181                }
182            }
183        },
184    )];
185    const ALLOWED_TARGETS: AllowedTargets<'_> = T::ALLOWED_TARGETS;
186    const SAFETY: AttributeSafety = T::SAFETY;
187
188    fn finalize(self, cx: &FinalizeContext<'_, '_>) -> Option<AttributeKind> {
189        let (kind, span) = self.1?;
190        T::finalize_check(cx, span);
191        Some(kind)
192    }
193}
194
195pub(crate) enum OnDuplicate {
196    /// Give a default warning
197    Warn,
198
199    /// Duplicates will be a warning, with a note that this will be an error in the future.
200    WarnButFutureError,
201
202    /// Give a default error
203    Error,
204
205    /// Ignore duplicates
206    Ignore,
207
208    /// Custom function called when a duplicate attribute is found.
209    ///
210    /// - `unused` is the span of the attribute that was unused or bad because of some
211    ///   duplicate reason
212    /// - `used` is the span of the attribute that was used in favor of the unused attribute
213    Custom(fn(cx: &AcceptContext<'_, '_>, used: Span, unused: Span)),
214}
215
216impl OnDuplicate {
217    fn exec<P: SingleAttributeParser>(
218        &self,
219        cx: &mut AcceptContext<'_, '_>,
220        used: Span,
221        unused: Span,
222    ) {
223        match self {
224            OnDuplicate::Warn => cx.warn_unused_duplicate(used, unused),
225            OnDuplicate::WarnButFutureError => cx.warn_unused_duplicate_future_error(used, unused),
226            OnDuplicate::Error => {
227                cx.emit_err(UnusedMultiple {
228                    this: unused,
229                    other: used,
230                    name: Symbol::intern(
231                        &P::PATH.into_iter().map(|i| i.to_string()).collect::<Vec<_>>().join(".."),
232                    ),
233                });
234            }
235            OnDuplicate::Ignore => {}
236            OnDuplicate::Custom(f) => f(cx, used, unused),
237        }
238    }
239}
240
241#[derive(#[automatically_derived]
impl ::core::marker::Copy for AttributeSafety { }Copy, #[automatically_derived]
impl ::core::clone::Clone for AttributeSafety {
    #[inline]
    fn clone(&self) -> AttributeSafety {
        let _: ::core::clone::AssertParamIsClone<&'static str>;
        let _: ::core::clone::AssertParamIsClone<Option<Edition>>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for AttributeSafety {
    #[inline]
    fn eq(&self, other: &AttributeSafety) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr &&
            match (self, other) {
                (AttributeSafety::Unsafe {
                    note: __self_0, unsafe_since: __self_1 },
                    AttributeSafety::Unsafe {
                    note: __arg1_0, unsafe_since: __arg1_1 }) =>
                    __self_0 == __arg1_0 && __self_1 == __arg1_1,
                _ => true,
            }
    }
}PartialEq, #[automatically_derived]
impl ::core::fmt::Debug for AttributeSafety {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            AttributeSafety::Normal =>
                ::core::fmt::Formatter::write_str(f, "Normal"),
            AttributeSafety::Unsafe { note: __self_0, unsafe_since: __self_1 }
                =>
                ::core::fmt::Formatter::debug_struct_field2_finish(f,
                    "Unsafe", "note", __self_0, "unsafe_since", &__self_1),
        }
    }
}Debug)]
242pub enum AttributeSafety {
243    /// Normal attribute that does not need `#[unsafe(...)]`
244    Normal,
245    /// Unsafe attribute that requires safety obligations to be discharged.
246    ///
247    /// An error is emitted when `#[unsafe(...)]` is omitted, except when the attribute's edition
248    /// is less than the one stored in `unsafe_since`. This handles attributes that were safe in
249    /// earlier editions, but become unsafe in later ones.
250    Unsafe {
251        /// The `note` is emitted during the `unsafe_code`, and explains to the user why this attribute is unsafe.
252        note: &'static str,
253        unsafe_since: Option<Edition>,
254    },
255}
256
257/// An even simpler version of [`SingleAttributeParser`]:
258/// now automatically check that there are no arguments provided to the attribute.
259///
260/// [`WithoutArgs<T> where T: NoArgsAttributeParser`](WithoutArgs) implements [`SingleAttributeParser`].
261//
262pub(crate) trait NoArgsAttributeParser: 'static {
263    const PATH: &[Symbol];
264    const ON_DUPLICATE: OnDuplicate = OnDuplicate::Error;
265    const ALLOWED_TARGETS: AllowedTargets<'_>;
266    const SAFETY: AttributeSafety = AttributeSafety::Normal;
267    const STABILITY: AttributeStability;
268
269    /// Create the [`AttributeKind`] given attribute's [`Span`].
270    const CREATE: fn(Span) -> AttributeKind;
271
272    /// Optional cross-attribute validation, run once during finalization after all
273    /// attributes on the item have been parsed. Has access to the sibling attributes via
274    /// [`FinalizeContext::all_attrs`], so it can reject incompatible combinations.
275    /// `attr_span` is the span of this attribute.
276    ///
277    /// Defaults to a no-op.
278    fn finalize_check(_cx: &FinalizeContext<'_, '_>, _attr_span: Span) {}
279}
280
281pub(crate) struct WithoutArgs<T: NoArgsAttributeParser>(PhantomData<T>);
282
283impl<T: NoArgsAttributeParser> Default for WithoutArgs<T> {
284    fn default() -> Self {
285        Self(Default::default())
286    }
287}
288
289impl<T: NoArgsAttributeParser> SingleAttributeParser for WithoutArgs<T> {
290    const PATH: &[Symbol] = T::PATH;
291    const ON_DUPLICATE: OnDuplicate = T::ON_DUPLICATE;
292    const SAFETY: AttributeSafety = T::SAFETY;
293    const STABILITY: AttributeStability = T::STABILITY;
294    const ALLOWED_TARGETS: AllowedTargets<'_> = T::ALLOWED_TARGETS;
295    const TEMPLATE: AttributeTemplate = crate::AttributeTemplate {
    word: true,
    list: None,
    one_of: &[],
    name_value_str: None,
    docs: None,
}template!(Word);
296
297    fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
298        let _ = cx.expect_no_args(args);
299        Some(T::CREATE(cx.attr_span))
300    }
301
302    fn finalize_check(cx: &FinalizeContext<'_, '_>, attr_span: Span) {
303        T::finalize_check(cx, attr_span)
304    }
305}
306
307type ConvertFn<E> = fn(ThinVec<E>, Span) -> AttributeKind;
308
309/// Alternative to [`AttributeParser`] that automatically handles state management.
310/// If multiple attributes appear on an element, combines the values of each into a
311/// [`ThinVec`].
312/// [`Combine<T> where T: CombineAttributeParser`](Combine) implements [`AttributeParser`].
313///
314/// [`CombineAttributeParser`] can only convert a single kind of attribute, and cannot combine multiple
315/// attributes together like is necessary for `#[stable()]` and `#[unstable()]` for example.
316pub(crate) trait CombineAttributeParser: 'static {
317    const PATH: &[rustc_span::Symbol];
318
319    type Item;
320    /// A function that converts individual items (of type [`Item`](Self::Item)) into the final attribute.
321    ///
322    /// For example, individual representations from `#[repr(...)]` attributes into an `AttributeKind::Repr(x)`,
323    ///  where `x` is a vec of these individual reprs.
324    const CONVERT: ConvertFn<Self::Item>;
325    const SAFETY: AttributeSafety = AttributeSafety::Normal;
326    const STABILITY: AttributeStability;
327
328    const ALLOWED_TARGETS: AllowedTargets<'_>;
329
330    /// The template this attribute parser should implement. Used for diagnostics.
331    const TEMPLATE: AttributeTemplate;
332
333    /// Converts a single syntactical attribute to a number of elements of the semantic attribute, or [`AttributeKind`]
334    fn extend(
335        cx: &mut AcceptContext<'_, '_>,
336        args: &ArgParser,
337    ) -> impl IntoIterator<Item = Self::Item>;
338
339    /// Optional cross-attribute validation, run once during finalization after all
340    /// attributes on the item have been parsed. Has access to the sibling attributes via
341    /// [`FinalizeContext::all_attrs`], so it can reject incompatible combinations.
342    /// `attr_span` is the span of the first attribute that was encountered.
343    ///
344    /// Defaults to a no-op.
345    fn finalize_check(_cx: &FinalizeContext<'_, '_>, _attr_span: Span) {}
346}
347
348/// Use in combination with [`CombineAttributeParser`].
349/// `Combine<T: CombineAttributeParser>` implements [`AttributeParser`].
350pub(crate) struct Combine<T: CombineAttributeParser> {
351    phantom: PhantomData<T>,
352    /// A list of all items produced by parsing attributes so far. One attribute can produce any amount of items.
353    items: ThinVec<<T as CombineAttributeParser>::Item>,
354    /// The full span of the first attribute that was encountered.
355    first_span: Option<Span>,
356}
357
358impl<T: CombineAttributeParser> Default for Combine<T> {
359    fn default() -> Self {
360        Self {
361            phantom: Default::default(),
362            items: Default::default(),
363            first_span: Default::default(),
364        }
365    }
366}
367
368impl<T: CombineAttributeParser> AttributeParser for Combine<T> {
369    const ATTRIBUTES: AcceptMapping<Self> =
370        &[(T::PATH, T::TEMPLATE, T::STABILITY, |group: &mut Combine<T>, cx, args| {
371            // Keep track of the span of the first attribute, for diagnostics
372            group.first_span.get_or_insert(cx.attr_span);
373            group.items.extend(T::extend(cx, args))
374        })];
375    const ALLOWED_TARGETS: AllowedTargets<'_> = T::ALLOWED_TARGETS;
376    const SAFETY: AttributeSafety = T::SAFETY;
377
378    fn finalize(self, cx: &FinalizeContext<'_, '_>) -> Option<AttributeKind> {
379        if let Some(first_span) = self.first_span {
380            T::finalize_check(cx, first_span);
381            Some(T::CONVERT(self.items, first_span))
382        } else {
383            None
384        }
385    }
386}