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