12
12
//! - [`CombineAttributeParser`](crate::attributes::CombineAttributeParser): makes it easy to implement an attribute which should combine the
13
13
//! contents of attributes, if an attribute appear multiple times in a list
14
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
+ //!
15
19
//! Attributes should be added to `crate::context::ATTRIBUTE_PARSERS` to be parsed.
16
20
17
21
use std:: marker:: PhantomData ;
18
22
19
- use rustc_feature:: { AttributeTemplate , template} ;
23
+ use rustc_feature:: { AttributeTemplate , AttributeType , template} ;
20
24
use rustc_hir:: attrs:: AttributeKind ;
21
25
use rustc_span:: { Span , Symbol } ;
22
26
use thin_vec:: ThinVec ;
@@ -88,6 +92,8 @@ pub(crate) trait AttributeParser<S: Stage>: Default + 'static {
88
92
89
93
const ALLOWED_TARGETS : AllowedTargets ;
90
94
95
+ const TYPE : AttributeType = AttributeType :: Normal ;
96
+
91
97
/// The parser has gotten a chance to accept the attributes on an item,
92
98
/// here it can produce an attribute.
93
99
///
@@ -129,6 +135,8 @@ pub(crate) trait SingleAttributeParser<S: Stage>: 'static {
129
135
/// The template this attribute parser should implement. Used for diagnostics.
130
136
const TEMPLATE : AttributeTemplate ;
131
137
138
+ const TYPE : AttributeType = AttributeType :: Normal ;
139
+
132
140
/// Converts a single syntactical attribute to a single semantic attribute, or [`AttributeKind`]
133
141
fn convert ( cx : & mut AcceptContext < ' _ , ' _ , S > , args : & ArgParser < ' _ > ) -> Option < AttributeKind > ;
134
142
}
@@ -175,6 +183,8 @@ impl<T: SingleAttributeParser<S>, S: Stage> AttributeParser<S> for Single<T, S>
175
183
) ] ;
176
184
const ALLOWED_TARGETS : AllowedTargets = T :: ALLOWED_TARGETS ;
177
185
186
+ const TYPE : AttributeType = T :: TYPE ;
187
+
178
188
fn finalize ( self , _cx : & FinalizeContext < ' _ , ' _ , S > ) -> Option < AttributeKind > {
179
189
Some ( self . 1 ?. 0 )
180
190
}
@@ -259,6 +269,7 @@ pub(crate) trait NoArgsAttributeParser<S: Stage>: 'static {
259
269
const PATH : & [ Symbol ] ;
260
270
const ON_DUPLICATE : OnDuplicate < S > ;
261
271
const ALLOWED_TARGETS : AllowedTargets ;
272
+ const TYPE : AttributeType = AttributeType :: Normal ;
262
273
263
274
/// Create the [`AttributeKind`] given attribute's [`Span`].
264
275
const CREATE : fn ( Span ) -> AttributeKind ;
@@ -278,6 +289,7 @@ impl<T: NoArgsAttributeParser<S>, S: Stage> SingleAttributeParser<S> for Without
278
289
const ON_DUPLICATE : OnDuplicate < S > = T :: ON_DUPLICATE ;
279
290
const ALLOWED_TARGETS : AllowedTargets = T :: ALLOWED_TARGETS ;
280
291
const TEMPLATE : AttributeTemplate = template ! ( Word ) ;
292
+ const TYPE : AttributeType = T :: TYPE ;
281
293
282
294
fn convert ( cx : & mut AcceptContext < ' _ , ' _ , S > , args : & ArgParser < ' _ > ) -> Option < AttributeKind > {
283
295
if let Err ( span) = args. no_args ( ) {
@@ -311,6 +323,8 @@ pub(crate) trait CombineAttributeParser<S: Stage>: 'static {
311
323
/// The template this attribute parser should implement. Used for diagnostics.
312
324
const TEMPLATE : AttributeTemplate ;
313
325
326
+ const TYPE : AttributeType = AttributeType :: Normal ;
327
+
314
328
/// Converts a single syntactical attribute to a number of elements of the semantic attribute, or [`AttributeKind`]
315
329
fn extend < ' c > (
316
330
cx : & ' c mut AcceptContext < ' _ , ' _ , S > ,
@@ -346,6 +360,7 @@ impl<T: CombineAttributeParser<S>, S: Stage> AttributeParser<S> for Combine<T, S
346
360
group. items . extend ( T :: extend ( cx, args) )
347
361
} ) ] ;
348
362
const ALLOWED_TARGETS : AllowedTargets = T :: ALLOWED_TARGETS ;
363
+ const TYPE : AttributeType = T :: TYPE ;
349
364
350
365
fn finalize ( self , _cx : & FinalizeContext < ' _ , ' _ , S > ) -> Option < AttributeKind > {
351
366
if let Some ( first_span) = self . first_span {
0 commit comments