2
2
//!
3
3
//! The actual definitions were copied from rustc's `compiler/rustc_feature/src/builtin_attrs.rs`.
4
4
//!
5
- //! It was last synchronized with upstream commit 835150e70288535bc57bb624792229b9dc94991d .
5
+ //! It was last synchronized with upstream commit ae90dcf0207c57c3034f00b07048d63f8b2363c8 .
6
6
//!
7
7
//! The macros were adjusted to only expand to the attribute name, since that is all we need to do
8
8
//! name resolution, and `BUILTIN_ATTRIBUTES` is almost entirely unchanged from the original, to
9
9
//! ease updating.
10
10
11
+ use once_cell:: sync:: OnceCell ;
12
+ use rustc_hash:: FxHashMap ;
13
+
11
14
/// Ignored attribute namespaces used by tools.
12
15
pub const TOOL_MODULES : & [ & str ] = & [ "rustfmt" , "clippy" ] ;
13
16
14
- type BuiltinAttribute = & ' static str ;
17
+ pub struct BuiltinAttribute {
18
+ pub name : & ' static str ,
19
+ pub template : AttributeTemplate ,
20
+ }
21
+
22
+ /// A template that the attribute input must match.
23
+ /// Only top-level shape (`#[attr]` vs `#[attr(...)]` vs `#[attr = ...]`) is considered now.
24
+ #[ derive( Clone , Copy ) ]
25
+ pub struct AttributeTemplate {
26
+ pub word : bool ,
27
+ pub list : Option < & ' static str > ,
28
+ pub name_value_str : Option < & ' static str > ,
29
+ }
30
+
31
+ pub fn find_builtin_attr_idx ( name : & str ) -> Option < usize > {
32
+ static BUILTIN_LOOKUP_TABLE : OnceCell < FxHashMap < & ' static str , usize > > = OnceCell :: new ( ) ;
33
+ BUILTIN_LOOKUP_TABLE
34
+ . get_or_init ( || {
35
+ INERT_ATTRIBUTES . iter ( ) . map ( |attr| attr. name ) . enumerate ( ) . map ( |( a, b) | ( b, a) ) . collect ( )
36
+ } )
37
+ . get ( name)
38
+ . copied ( )
39
+ }
40
+
41
+ // impl AttributeTemplate {
42
+ // const DEFAULT: AttributeTemplate =
43
+ // AttributeTemplate { word: false, list: None, name_value_str: None };
44
+ // }
45
+
46
+ /// A convenience macro for constructing attribute templates.
47
+ /// E.g., `template!(Word, List: "description")` means that the attribute
48
+ /// supports forms `#[attr]` and `#[attr(description)]`.
49
+ macro_rules! template {
50
+ ( Word ) => { template!( @ true , None , None ) } ;
51
+ ( List : $descr: expr) => { template!( @ false , Some ( $descr) , None ) } ;
52
+ ( NameValueStr : $descr: expr) => { template!( @ false , None , Some ( $descr) ) } ;
53
+ ( Word , List : $descr: expr) => { template!( @ true , Some ( $descr) , None ) } ;
54
+ ( Word , NameValueStr : $descr: expr) => { template!( @ true , None , Some ( $descr) ) } ;
55
+ ( List : $descr1: expr, NameValueStr : $descr2: expr) => {
56
+ template!( @ false , Some ( $descr1) , Some ( $descr2) )
57
+ } ;
58
+ ( Word , List : $descr1: expr, NameValueStr : $descr2: expr) => {
59
+ template!( @ true , Some ( $descr1) , Some ( $descr2) )
60
+ } ;
61
+ ( @ $word: expr, $list: expr, $name_value_str: expr) => {
62
+ AttributeTemplate {
63
+ word: $word, list: $list, name_value_str: $name_value_str
64
+ }
65
+ } ;
66
+ }
15
67
16
68
macro_rules! ungated {
17
69
( $attr: ident, $typ: expr, $tpl: expr $( , ) ?) => {
18
- stringify!( $attr)
70
+ BuiltinAttribute { name : stringify!( $attr) , template : $tpl }
19
71
} ;
20
72
}
21
73
22
74
macro_rules! gated {
23
- ( $attr: ident $( $rest: tt) * ) => {
24
- stringify!( $attr)
75
+ ( $attr: ident, $typ: expr, $tpl: expr, $gate: ident, $msg: expr $( , ) ?) => {
76
+ BuiltinAttribute { name: stringify!( $attr) , template: $tpl }
77
+ } ;
78
+ ( $attr: ident, $typ: expr, $tpl: expr, $msg: expr $( , ) ?) => {
79
+ BuiltinAttribute { name: stringify!( $attr) , template: $tpl }
25
80
} ;
26
81
}
27
82
28
83
macro_rules! rustc_attr {
29
- ( TEST , $attr: ident $( $rest: tt) * ) => {
30
- stringify!( $attr)
84
+ ( TEST , $attr: ident, $typ: expr, $tpl: expr $( , ) ?) => {
85
+ rustc_attr!(
86
+ $attr,
87
+ $typ,
88
+ $tpl,
89
+ concat!(
90
+ "the `#[" ,
91
+ stringify!( $attr) ,
92
+ "]` attribute is just used for rustc unit tests \
93
+ and will never be stable",
94
+ ) ,
95
+ )
31
96
} ;
32
- ( $attr: ident $ ( $rest : tt ) * ) => {
33
- stringify!( $attr)
97
+ ( $attr: ident, $typ : expr , $tpl : expr , $msg : expr $ ( , ) ? ) => {
98
+ BuiltinAttribute { name : stringify!( $attr) , template : $tpl }
34
99
} ;
35
100
}
36
101
@@ -158,8 +223,8 @@ pub const INERT_ATTRIBUTES: &[BuiltinAttribute] = &[
158
223
159
224
// Plugins:
160
225
// XXX Modified for use in rust-analyzer
161
- gated ! ( plugin_registrar) ,
162
- gated ! ( plugin) ,
226
+ gated ! ( plugin_registrar, Normal , template! ( Word ) , experimental! ( ) ) ,
227
+ gated ! ( plugin, CrateLevel , template! ( Word ) , experimental! ( ) ) ,
163
228
164
229
// Testing:
165
230
gated ! ( allow_fail, Normal , template!( Word ) , experimental!( allow_fail) ) ,
@@ -195,6 +260,12 @@ pub const INERT_ATTRIBUTES: &[BuiltinAttribute] = &[
195
260
) ,
196
261
197
262
gated ! ( cmse_nonsecure_entry, AssumedUsed , template!( Word ) , experimental!( cmse_nonsecure_entry) ) ,
263
+ // RFC 2632
264
+ gated ! (
265
+ default_method_body_is_const, AssumedUsed , template!( Word ) , const_trait_impl,
266
+ "`default_method_body_is_const` is a temporary placeholder for declaring default bodies \
267
+ as `const`, which may be removed or renamed in the future."
268
+ ) ,
198
269
199
270
// ==========================================================================
200
271
// Internal attributes: Stability, deprecation, and unsafe:
@@ -258,10 +329,6 @@ pub const INERT_ATTRIBUTES: &[BuiltinAttribute] = &[
258
329
) ,
259
330
gated ! ( panic_runtime, AssumedUsed , template!( Word ) , experimental!( panic_runtime) ) ,
260
331
gated ! ( needs_panic_runtime, AssumedUsed , template!( Word ) , experimental!( needs_panic_runtime) ) ,
261
- gated ! (
262
- unwind, AssumedUsed , template!( List : "allowed|aborts" ) , unwind_attributes,
263
- experimental!( unwind) ,
264
- ) ,
265
332
gated ! (
266
333
compiler_builtins, AssumedUsed , template!( Word ) ,
267
334
"the `#[compiler_builtins]` attribute is used to identify the `compiler_builtins` crate \
@@ -287,7 +354,11 @@ pub const INERT_ATTRIBUTES: &[BuiltinAttribute] = &[
287
354
// Internal attributes, Macro related:
288
355
// ==========================================================================
289
356
290
- rustc_attr ! ( rustc_builtin_macro, AssumedUsed , template!( Word , NameValueStr : "name" ) , IMPL_DETAIL ) ,
357
+ rustc_attr ! (
358
+ rustc_builtin_macro, AssumedUsed ,
359
+ template!( Word , List : "name, /*opt*/ attributes(name1, name2, ...)" ) ,
360
+ IMPL_DETAIL ,
361
+ ) ,
291
362
rustc_attr ! ( rustc_proc_macro_decls, Normal , template!( Word ) , INTERNAL_UNSTABLE ) ,
292
363
rustc_attr ! (
293
364
rustc_macro_transparency, AssumedUsed ,
@@ -344,7 +415,7 @@ pub const INERT_ATTRIBUTES: &[BuiltinAttribute] = &[
344
415
lang, Normal , template!( NameValueStr : "name" ) , lang_items,
345
416
"language items are subject to change" ,
346
417
) ,
347
- gated ! ( rustc_diagnostic_item) , // XXX modified in rust-analyzer
418
+ gated ! ( rustc_diagnostic_item, Normal , template! ( NameValueStr : "name" ) , experimental! ( ) ) , // XXX Modified for use in rust-analyzer
348
419
gated ! (
349
420
// Used in resolve:
350
421
prelude_import, AssumedUsed , template!( Word ) ,
@@ -428,6 +499,7 @@ pub const INERT_ATTRIBUTES: &[BuiltinAttribute] = &[
428
499
rustc_attr ! ( TEST , rustc_dump_program_clauses, AssumedUsed , template!( Word ) ) ,
429
500
rustc_attr ! ( TEST , rustc_dump_env_program_clauses, AssumedUsed , template!( Word ) ) ,
430
501
rustc_attr ! ( TEST , rustc_object_lifetime_default, AssumedUsed , template!( Word ) ) ,
502
+ rustc_attr ! ( TEST , rustc_dump_vtable, AssumedUsed , template!( Word ) ) ,
431
503
rustc_attr ! ( TEST , rustc_dummy, Normal , template!( Word /* doesn't matter*/ ) ) ,
432
504
gated ! (
433
505
omit_gdb_pretty_printer_section, AssumedUsed , template!( Word ) ,
0 commit comments