Skip to main content

rustc_attr_parsing/attributes/
mod.rs

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