1 //! Built-in attributes and `cfg` flag gating.
2 
3 use AttributeGate::*;
4 use AttributeType::*;
5 
6 use crate::{Features, Stability};
7 
8 use rustc_data_structures::fx::FxHashMap;
9 use rustc_span::symbol::{sym, Symbol};
10 
11 use std::lazy::SyncLazy;
12 
13 type GateFn = fn(&Features) -> bool;
14 
15 macro_rules! cfg_fn {
16     ($field: ident) => {
17         (|features| features.$field) as GateFn
18     };
19 }
20 
21 pub type GatedCfg = (Symbol, Symbol, GateFn);
22 
23 /// `cfg(...)`'s that are feature gated.
24 const GATED_CFGS: &[GatedCfg] = &[
25     // (name in cfg, feature, function to check if the feature is enabled)
26     (sym::target_thread_local, sym::cfg_target_thread_local, cfg_fn!(cfg_target_thread_local)),
27     (sym::target_has_atomic, sym::cfg_target_has_atomic, cfg_fn!(cfg_target_has_atomic)),
28     (sym::target_has_atomic_load_store, sym::cfg_target_has_atomic, cfg_fn!(cfg_target_has_atomic)),
29     (
30         sym::target_has_atomic_equal_alignment,
31         sym::cfg_target_has_atomic,
32         cfg_fn!(cfg_target_has_atomic),
33     ),
34     (sym::sanitize, sym::cfg_sanitize, cfg_fn!(cfg_sanitize)),
35     (sym::version, sym::cfg_version, cfg_fn!(cfg_version)),
36     (sym::panic, sym::cfg_panic, cfg_fn!(cfg_panic)),
37 ];
38 
39 /// Find a gated cfg determined by the `pred`icate which is given the cfg's name.
find_gated_cfg(pred: impl Fn(Symbol) -> bool) -> Option<&'static GatedCfg>40 pub fn find_gated_cfg(pred: impl Fn(Symbol) -> bool) -> Option<&'static GatedCfg> {
41     GATED_CFGS.iter().find(|(cfg_sym, ..)| pred(*cfg_sym))
42 }
43 
44 // If you change this, please modify `src/doc/unstable-book` as well. You must
45 // move that documentation into the relevant place in the other docs, and
46 // remove the chapter on the flag.
47 
48 #[derive(Copy, Clone, PartialEq, Debug)]
49 pub enum AttributeType {
50     /// Normal, builtin attribute that is consumed
51     /// by the compiler before the unused_attribute check
52     Normal,
53 
54     /// Builtin attribute that may not be consumed by the compiler
55     /// before the unused_attribute check. These attributes
56     /// will be ignored by the unused_attribute lint
57     AssumedUsed,
58 
59     /// Builtin attribute that is only allowed at the crate level
60     CrateLevel,
61 }
62 
63 #[derive(Clone, Copy)]
64 pub enum AttributeGate {
65     /// Is gated by a given feature gate, reason
66     /// and function to check if enabled
67     Gated(Stability, Symbol, &'static str, fn(&Features) -> bool),
68 
69     /// Ungated attribute, can be used on all release channels
70     Ungated,
71 }
72 
73 // fn() is not Debug
74 impl std::fmt::Debug for AttributeGate {
fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result75     fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
76         match *self {
77             Self::Gated(ref stab, name, expl, _) => {
78                 write!(fmt, "Gated({:?}, {}, {})", stab, name, expl)
79             }
80             Self::Ungated => write!(fmt, "Ungated"),
81         }
82     }
83 }
84 
85 impl AttributeGate {
is_deprecated(&self) -> bool86     fn is_deprecated(&self) -> bool {
87         matches!(*self, Self::Gated(Stability::Deprecated(_, _), ..))
88     }
89 }
90 
91 /// A template that the attribute input must match.
92 /// Only top-level shape (`#[attr]` vs `#[attr(...)]` vs `#[attr = ...]`) is considered now.
93 #[derive(Clone, Copy, Default)]
94 pub struct AttributeTemplate {
95     pub word: bool,
96     pub list: Option<&'static str>,
97     pub name_value_str: Option<&'static str>,
98 }
99 
100 /// A convenience macro for constructing attribute templates.
101 /// E.g., `template!(Word, List: "description")` means that the attribute
102 /// supports forms `#[attr]` and `#[attr(description)]`.
103 macro_rules! template {
104     (Word) => { template!(@ true, None, None) };
105     (List: $descr: expr) => { template!(@ false, Some($descr), None) };
106     (NameValueStr: $descr: expr) => { template!(@ false, None, Some($descr)) };
107     (Word, List: $descr: expr) => { template!(@ true, Some($descr), None) };
108     (Word, NameValueStr: $descr: expr) => { template!(@ true, None, Some($descr)) };
109     (List: $descr1: expr, NameValueStr: $descr2: expr) => {
110         template!(@ false, Some($descr1), Some($descr2))
111     };
112     (Word, List: $descr1: expr, NameValueStr: $descr2: expr) => {
113         template!(@ true, Some($descr1), Some($descr2))
114     };
115     (@ $word: expr, $list: expr, $name_value_str: expr) => { AttributeTemplate {
116         word: $word, list: $list, name_value_str: $name_value_str
117     } };
118 }
119 
120 macro_rules! ungated {
121     ($attr:ident, $typ:expr, $tpl:expr $(,)?) => {
122         (sym::$attr, $typ, $tpl, Ungated)
123     };
124 }
125 
126 macro_rules! gated {
127     ($attr:ident, $typ:expr, $tpl:expr, $gate:ident, $msg:expr $(,)?) => {
128         (sym::$attr, $typ, $tpl, Gated(Stability::Unstable, sym::$gate, $msg, cfg_fn!($gate)))
129     };
130     ($attr:ident, $typ:expr, $tpl:expr, $msg:expr $(,)?) => {
131         (sym::$attr, $typ, $tpl, Gated(Stability::Unstable, sym::$attr, $msg, cfg_fn!($attr)))
132     };
133 }
134 
135 macro_rules! rustc_attr {
136     (TEST, $attr:ident, $typ:expr, $tpl:expr $(,)?) => {
137         rustc_attr!(
138             $attr,
139             $typ,
140             $tpl,
141             concat!(
142                 "the `#[",
143                 stringify!($attr),
144                 "]` attribute is just used for rustc unit tests \
145                 and will never be stable",
146             ),
147         )
148     };
149     ($attr:ident, $typ:expr, $tpl:expr, $msg:expr $(,)?) => {
150         (
151             sym::$attr,
152             $typ,
153             $tpl,
154             Gated(Stability::Unstable, sym::rustc_attrs, $msg, cfg_fn!(rustc_attrs)),
155         )
156     };
157 }
158 
159 macro_rules! experimental {
160     ($attr:ident) => {
161         concat!("the `#[", stringify!($attr), "]` attribute is an experimental feature")
162     };
163 }
164 
165 const IMPL_DETAIL: &str = "internal implementation detail";
166 const INTERNAL_UNSTABLE: &str = "this is an internal attribute that will never be stable";
167 
168 pub type BuiltinAttribute = (Symbol, AttributeType, AttributeTemplate, AttributeGate);
169 
170 /// Attributes that have a special meaning to rustc or rustdoc.
171 #[rustfmt::skip]
172 pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
173     // ==========================================================================
174     // Stable attributes:
175     // ==========================================================================
176 
177     // Conditional compilation:
178     ungated!(cfg, Normal, template!(List: "predicate")),
179     ungated!(cfg_attr, Normal, template!(List: "predicate, attr1, attr2, ...")),
180 
181     // Testing:
182     ungated!(ignore, Normal, template!(Word, NameValueStr: "reason")),
183     ungated!(
184         should_panic, Normal,
185         template!(Word, List: r#"expected = "reason"#, NameValueStr: "reason"),
186     ),
187     // FIXME(Centril): This can be used on stable but shouldn't.
188     ungated!(reexport_test_harness_main, Normal, template!(NameValueStr: "name")),
189 
190     // Macros:
191     ungated!(automatically_derived, Normal, template!(Word)),
192     // FIXME(#14407)
193     ungated!(macro_use, Normal, template!(Word, List: "name1, name2, ...")),
194     ungated!(macro_escape, Normal, template!(Word)), // Deprecated synonym for `macro_use`.
195     ungated!(macro_export, Normal, template!(Word, List: "local_inner_macros")),
196     ungated!(proc_macro, Normal, template!(Word)),
197     ungated!(
198         proc_macro_derive, Normal,
199         template!(List: "TraitName, /*opt*/ attributes(name1, name2, ...)"),
200     ),
201     ungated!(proc_macro_attribute, Normal, template!(Word)),
202 
203     // Lints:
204     ungated!(warn, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#)),
205     ungated!(allow, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#)),
206     ungated!(forbid, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#)),
207     ungated!(deny, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#)),
208     ungated!(must_use, AssumedUsed, template!(Word, NameValueStr: "reason")),
209     // FIXME(#14407)
210     ungated!(
211         deprecated, Normal,
212         template!(
213             Word,
214             List: r#"/*opt*/ since = "version", /*opt*/ note = "reason""#,
215             NameValueStr: "reason"
216         ),
217     ),
218 
219     // Crate properties:
220     ungated!(crate_name, CrateLevel, template!(NameValueStr: "name")),
221     ungated!(crate_type, CrateLevel, template!(NameValueStr: "bin|lib|...")),
222     ungated!(crate_id, CrateLevel, template!(NameValueStr: "ignored")),
223 
224     // ABI, linking, symbols, and FFI
225     ungated!(
226         link, AssumedUsed,
227         template!(List: r#"name = "...", /*opt*/ kind = "dylib|static|...", /*opt*/ wasm_import_module = "...""#),
228     ),
229     ungated!(link_name, AssumedUsed, template!(NameValueStr: "name")),
230     ungated!(no_link, AssumedUsed, template!(Word)),
231     ungated!(repr, AssumedUsed, template!(List: "C")),
232     ungated!(export_name, AssumedUsed, template!(NameValueStr: "name")),
233     ungated!(link_section, AssumedUsed, template!(NameValueStr: "name")),
234     ungated!(no_mangle, AssumedUsed, template!(Word)),
235     ungated!(used, AssumedUsed, template!(Word)),
236 
237     // Limits:
238     ungated!(recursion_limit, CrateLevel, template!(NameValueStr: "N")),
239     ungated!(type_length_limit, CrateLevel, template!(NameValueStr: "N")),
240     gated!(
241         const_eval_limit, CrateLevel, template!(NameValueStr: "N"), const_eval_limit,
242         experimental!(const_eval_limit)
243     ),
244     gated!(
245         move_size_limit, CrateLevel, template!(NameValueStr: "N"), large_assignments,
246         experimental!(move_size_limit)
247     ),
248 
249     // Entry point:
250     ungated!(main, Normal, template!(Word)),
251     ungated!(start, Normal, template!(Word)),
252     ungated!(no_start, CrateLevel, template!(Word)),
253     ungated!(no_main, CrateLevel, template!(Word)),
254 
255     // Modules, prelude, and resolution:
256     ungated!(path, Normal, template!(NameValueStr: "file")),
257     ungated!(no_std, CrateLevel, template!(Word)),
258     ungated!(no_implicit_prelude, Normal, template!(Word)),
259     ungated!(non_exhaustive, AssumedUsed, template!(Word)),
260 
261     // Runtime
262     ungated!(windows_subsystem, AssumedUsed, template!(NameValueStr: "windows|console")),
263     ungated!(panic_handler, Normal, template!(Word)), // RFC 2070
264 
265     // Code generation:
266     ungated!(inline, AssumedUsed, template!(Word, List: "always|never")),
267     ungated!(cold, AssumedUsed, template!(Word)),
268     ungated!(no_builtins, AssumedUsed, template!(Word)),
269     ungated!(target_feature, AssumedUsed, template!(List: r#"enable = "name""#)),
270     ungated!(track_caller, AssumedUsed, template!(Word)),
271     gated!(
272         no_sanitize, AssumedUsed,
273         template!(List: "address, memory, thread"),
274         experimental!(no_sanitize)
275     ),
276     ungated!(
277         // Not exclusively gated at the crate level (though crate-level is
278         // supported). The feature can alternatively be enabled on individual
279         // functions.
280         no_coverage, AssumedUsed,
281         template!(Word),
282     ),
283 
284     // FIXME: #14408 assume docs are used since rustdoc looks at them.
285     ungated!(doc, AssumedUsed, template!(List: "hidden|inline|...", NameValueStr: "string")),
286 
287     // ==========================================================================
288     // Unstable attributes:
289     // ==========================================================================
290 
291     // Linking:
292     gated!(naked, AssumedUsed, template!(Word), naked_functions, experimental!(naked)),
293     gated!(
294         link_ordinal, AssumedUsed, template!(List: "ordinal"), raw_dylib,
295         experimental!(link_ordinal)
296     ),
297 
298     // Plugins:
299     (
300         sym::plugin_registrar, Normal, template!(Word),
301         Gated(
302             Stability::Deprecated(
303                 "https://github.com/rust-lang/rust/pull/64675",
304                 Some("may be removed in a future compiler version"),
305             ),
306             sym::plugin_registrar,
307             "compiler plugins are deprecated",
308             cfg_fn!(plugin_registrar)
309         )
310     ),
311     (
312         sym::plugin, CrateLevel, template!(List: "name"),
313         Gated(
314             Stability::Deprecated(
315                 "https://github.com/rust-lang/rust/pull/64675",
316                 Some("may be removed in a future compiler version"),
317             ),
318             sym::plugin,
319             "compiler plugins are deprecated",
320             cfg_fn!(plugin)
321         )
322     ),
323 
324     // Testing:
325     gated!(allow_fail, Normal, template!(Word), experimental!(allow_fail)),
326     gated!(
327         test_runner, CrateLevel, template!(List: "path"), custom_test_frameworks,
328         "custom test frameworks are an unstable feature",
329     ),
330     // RFC #1268
331     gated!(marker, AssumedUsed, template!(Word), marker_trait_attr, experimental!(marker)),
332     gated!(
333         thread_local, AssumedUsed, template!(Word),
334         "`#[thread_local]` is an experimental feature, and does not currently handle destructors",
335     ),
336     gated!(no_core, CrateLevel, template!(Word), experimental!(no_core)),
337     // RFC 2412
338     gated!(
339         optimize, AssumedUsed, template!(List: "size|speed"), optimize_attribute,
340         experimental!(optimize),
341     ),
342     // RFC 2867
343     gated!(instruction_set, AssumedUsed, template!(List: "set"), isa_attribute, experimental!(instruction_set)),
344 
345     gated!(ffi_returns_twice, AssumedUsed, template!(Word), experimental!(ffi_returns_twice)),
346     gated!(ffi_pure, AssumedUsed, template!(Word), experimental!(ffi_pure)),
347     gated!(ffi_const, AssumedUsed, template!(Word), experimental!(ffi_const)),
348     gated!(
349         register_attr, CrateLevel, template!(List: "attr1, attr2, ..."),
350         experimental!(register_attr),
351     ),
352     gated!(
353         register_tool, CrateLevel, template!(List: "tool1, tool2, ..."),
354         experimental!(register_tool),
355     ),
356 
357     gated!(cmse_nonsecure_entry, AssumedUsed, template!(Word), experimental!(cmse_nonsecure_entry)),
358 
359     // ==========================================================================
360     // Internal attributes: Stability, deprecation, and unsafe:
361     // ==========================================================================
362 
363     ungated!(feature, CrateLevel, template!(List: "name1, name1, ...")),
364     // FIXME(#14407) -- only looked at on-demand so we can't
365     // guarantee they'll have already been checked.
366     ungated!(
367         rustc_deprecated, AssumedUsed,
368         template!(List: r#"since = "version", reason = "...""#)
369     ),
370     // FIXME(#14407)
371     ungated!(stable, AssumedUsed, template!(List: r#"feature = "name", since = "version""#)),
372     // FIXME(#14407)
373     ungated!(
374         unstable, AssumedUsed,
375         template!(List: r#"feature = "name", reason = "...", issue = "N""#),
376     ),
377     // FIXME(#14407)
378     ungated!(rustc_const_unstable, AssumedUsed, template!(List: r#"feature = "name""#)),
379     // FIXME(#14407)
380     ungated!(rustc_const_stable, AssumedUsed, template!(List: r#"feature = "name""#)),
381     gated!(
382         allow_internal_unstable, AssumedUsed, template!(Word, List: "feat1, feat2, ..."),
383         "allow_internal_unstable side-steps feature gating and stability checks",
384     ),
385     gated!(
386         rustc_allow_const_fn_unstable, AssumedUsed, template!(Word, List: "feat1, feat2, ..."),
387         "rustc_allow_const_fn_unstable side-steps feature gating and stability checks"
388     ),
389     gated!(
390         allow_internal_unsafe, Normal, template!(Word),
391         "allow_internal_unsafe side-steps the unsafe_code lint",
392     ),
393 
394     // ==========================================================================
395     // Internal attributes: Type system related:
396     // ==========================================================================
397 
398     gated!(fundamental, AssumedUsed, template!(Word), experimental!(fundamental)),
399     gated!(
400         may_dangle, Normal, template!(Word), dropck_eyepatch,
401         "`may_dangle` has unstable semantics and may be removed in the future",
402     ),
403 
404     // ==========================================================================
405     // Internal attributes: Runtime related:
406     // ==========================================================================
407 
408     rustc_attr!(rustc_allocator, AssumedUsed, template!(Word), IMPL_DETAIL),
409     rustc_attr!(rustc_allocator_nounwind, AssumedUsed, template!(Word), IMPL_DETAIL),
410     gated!(alloc_error_handler, Normal, template!(Word), experimental!(alloc_error_handler)),
411     gated!(
412         default_lib_allocator, AssumedUsed, template!(Word), allocator_internals,
413         experimental!(default_lib_allocator),
414     ),
415     gated!(
416         needs_allocator, Normal, template!(Word), allocator_internals,
417         experimental!(needs_allocator),
418     ),
419     gated!(panic_runtime, AssumedUsed, template!(Word), experimental!(panic_runtime)),
420     gated!(needs_panic_runtime, AssumedUsed, template!(Word), experimental!(needs_panic_runtime)),
421     gated!(
422         unwind, AssumedUsed, template!(List: "allowed|aborts"), unwind_attributes,
423         experimental!(unwind),
424     ),
425     gated!(
426         compiler_builtins, AssumedUsed, template!(Word),
427         "the `#[compiler_builtins]` attribute is used to identify the `compiler_builtins` crate \
428         which contains compiler-rt intrinsics and will never be stable",
429     ),
430     gated!(
431         profiler_runtime, AssumedUsed, template!(Word),
432         "the `#[profiler_runtime]` attribute is used to identify the `profiler_builtins` crate \
433         which contains the profiler runtime and will never be stable",
434     ),
435 
436     // ==========================================================================
437     // Internal attributes, Linkage:
438     // ==========================================================================
439 
440     gated!(
441         linkage, AssumedUsed, template!(NameValueStr: "external|internal|..."),
442         "the `linkage` attribute is experimental and not portable across platforms",
443     ),
444     rustc_attr!(rustc_std_internal_symbol, AssumedUsed, template!(Word), INTERNAL_UNSTABLE),
445 
446     // ==========================================================================
447     // Internal attributes, Macro related:
448     // ==========================================================================
449 
450     rustc_attr!(rustc_builtin_macro, AssumedUsed, template!(Word, NameValueStr: "name"), IMPL_DETAIL),
451     rustc_attr!(rustc_proc_macro_decls, Normal, template!(Word), INTERNAL_UNSTABLE),
452     rustc_attr!(
453         rustc_macro_transparency, AssumedUsed,
454         template!(NameValueStr: "transparent|semitransparent|opaque"),
455         "used internally for testing macro hygiene",
456     ),
457 
458     // ==========================================================================
459     // Internal attributes, Diagnostics related:
460     // ==========================================================================
461 
462     rustc_attr!(
463         rustc_on_unimplemented, AssumedUsed,
464         template!(
465             List: r#"/*opt*/ message = "...", /*opt*/ label = "...", /*opt*/ note = "...""#,
466             NameValueStr: "message"
467         ),
468         INTERNAL_UNSTABLE
469     ),
470     // Enumerates "identity-like" conversion methods to suggest on type mismatch.
471     rustc_attr!(rustc_conversion_suggestion, AssumedUsed, template!(Word), INTERNAL_UNSTABLE),
472 
473     // ==========================================================================
474     // Internal attributes, Const related:
475     // ==========================================================================
476 
477     rustc_attr!(rustc_promotable, AssumedUsed, template!(Word), IMPL_DETAIL),
478     rustc_attr!(rustc_args_required_const, AssumedUsed, template!(List: "N"), INTERNAL_UNSTABLE),
479     rustc_attr!(rustc_legacy_const_generics, AssumedUsed, template!(List: "N"), INTERNAL_UNSTABLE),
480 
481     // ==========================================================================
482     // Internal attributes, Layout related:
483     // ==========================================================================
484 
485     rustc_attr!(
486         rustc_layout_scalar_valid_range_start, AssumedUsed, template!(List: "value"),
487         "the `#[rustc_layout_scalar_valid_range_start]` attribute is just used to enable \
488         niche optimizations in libcore and will never be stable",
489     ),
490     rustc_attr!(
491         rustc_layout_scalar_valid_range_end, AssumedUsed, template!(List: "value"),
492         "the `#[rustc_layout_scalar_valid_range_end]` attribute is just used to enable \
493         niche optimizations in libcore and will never be stable",
494     ),
495     rustc_attr!(
496         rustc_nonnull_optimization_guaranteed, AssumedUsed, template!(Word),
497         "the `#[rustc_nonnull_optimization_guaranteed]` attribute is just used to enable \
498         niche optimizations in libcore and will never be stable",
499     ),
500 
501     // ==========================================================================
502     // Internal attributes, Misc:
503     // ==========================================================================
504     gated!(
505         lang, Normal, template!(NameValueStr: "name"), lang_items,
506         "language items are subject to change",
507     ),
508     (
509         sym::rustc_diagnostic_item,
510         Normal,
511         template!(NameValueStr: "name"),
512         Gated(
513             Stability::Unstable,
514             sym::rustc_attrs,
515             "diagnostic items compiler internal support for linting",
516             cfg_fn!(rustc_attrs),
517         ),
518     ),
519     gated!(
520         // Used in resolve:
521         prelude_import, AssumedUsed, template!(Word),
522         "`#[prelude_import]` is for use by rustc only",
523     ),
524     gated!(
525         rustc_paren_sugar, Normal, template!(Word), unboxed_closures,
526         "unboxed_closures are still evolving",
527     ),
528     rustc_attr!(
529         rustc_inherit_overflow_checks, AssumedUsed, template!(Word),
530         "the `#[rustc_inherit_overflow_checks]` attribute is just used to control \
531         overflow checking behavior of several libcore functions that are inlined \
532         across crates and will never be stable",
533     ),
534     rustc_attr!(rustc_reservation_impl, Normal, template!(NameValueStr: "reservation message"),
535                 "the `#[rustc_reservation_impl]` attribute is internally used \
536                  for reserving for `for<T> From<!> for T` impl"
537     ),
538     rustc_attr!(
539         rustc_test_marker, Normal, template!(Word),
540         "the `#[rustc_test_marker]` attribute is used internally to track tests",
541     ),
542     rustc_attr!(
543         rustc_unsafe_specialization_marker, Normal, template!(Word),
544         "the `#[rustc_unsafe_specialization_marker]` attribute is used to check specializations"
545     ),
546     rustc_attr!(
547         rustc_specialization_trait, Normal, template!(Word),
548         "the `#[rustc_specialization_trait]` attribute is used to check specializations"
549     ),
550     rustc_attr!(
551         rustc_main, Normal, template!(Word),
552         "the `#[rustc_main]` attribute is used internally to specify test entry point function",
553     ),
554     rustc_attr!(
555         rustc_skip_array_during_method_dispatch, Normal, template!(Word),
556         "the `#[rustc_skip_array_during_method_dispatch]` attribute is used to exclude a trait \
557         from method dispatch when the receiver is an array, for compatibility in editions < 2021."
558     ),
559 
560     // ==========================================================================
561     // Internal attributes, Testing:
562     // ==========================================================================
563 
564     rustc_attr!(TEST, rustc_outlives, Normal, template!(Word)),
565     rustc_attr!(TEST, rustc_capture_analysis, Normal, template!(Word)),
566     rustc_attr!(TEST, rustc_variance, Normal, template!(Word)),
567     rustc_attr!(TEST, rustc_layout, Normal, template!(List: "field1, field2, ...")),
568     rustc_attr!(TEST, rustc_regions, Normal, template!(Word)),
569     rustc_attr!(
570         TEST, rustc_error, AssumedUsed,
571         template!(Word, List: "delay_span_bug_from_inside_query")
572     ),
573     rustc_attr!(TEST, rustc_dump_user_substs, AssumedUsed, template!(Word)),
574     rustc_attr!(TEST, rustc_if_this_changed, AssumedUsed, template!(Word, List: "DepNode")),
575     rustc_attr!(TEST, rustc_then_this_would_need, AssumedUsed, template!(List: "DepNode")),
576     rustc_attr!(
577         TEST, rustc_dirty, AssumedUsed,
578         template!(List: r#"cfg = "...", /*opt*/ label = "...", /*opt*/ except = "...""#),
579     ),
580     rustc_attr!(
581         TEST, rustc_clean, AssumedUsed,
582         template!(List: r#"cfg = "...", /*opt*/ label = "...", /*opt*/ except = "...""#),
583     ),
584     rustc_attr!(
585         TEST, rustc_partition_reused, AssumedUsed,
586         template!(List: r#"cfg = "...", module = "...""#),
587     ),
588     rustc_attr!(
589         TEST, rustc_partition_codegened, AssumedUsed,
590         template!(List: r#"cfg = "...", module = "...""#),
591     ),
592     rustc_attr!(
593         TEST, rustc_expected_cgu_reuse, AssumedUsed,
594         template!(List: r#"cfg = "...", module = "...", kind = "...""#),
595     ),
596     rustc_attr!(TEST, rustc_synthetic, AssumedUsed, template!(Word)),
597     rustc_attr!(TEST, rustc_symbol_name, AssumedUsed, template!(Word)),
598     rustc_attr!(TEST, rustc_polymorphize_error, AssumedUsed, template!(Word)),
599     rustc_attr!(TEST, rustc_def_path, AssumedUsed, template!(Word)),
600     rustc_attr!(TEST, rustc_mir, AssumedUsed, template!(List: "arg1, arg2, ...")),
601     rustc_attr!(TEST, rustc_dump_program_clauses, AssumedUsed, template!(Word)),
602     rustc_attr!(TEST, rustc_dump_env_program_clauses, AssumedUsed, template!(Word)),
603     rustc_attr!(TEST, rustc_object_lifetime_default, AssumedUsed, template!(Word)),
604     rustc_attr!(TEST, rustc_dummy, Normal, template!(Word /* doesn't matter*/)),
605     gated!(
606         omit_gdb_pretty_printer_section, AssumedUsed, template!(Word),
607         "the `#[omit_gdb_pretty_printer_section]` attribute is just used for the Rust test suite",
608     ),
609 ];
610 
deprecated_attributes() -> Vec<&'static BuiltinAttribute>611 pub fn deprecated_attributes() -> Vec<&'static BuiltinAttribute> {
612     BUILTIN_ATTRIBUTES.iter().filter(|(.., gate)| gate.is_deprecated()).collect()
613 }
614 
is_builtin_attr_name(name: Symbol) -> bool615 pub fn is_builtin_attr_name(name: Symbol) -> bool {
616     BUILTIN_ATTRIBUTE_MAP.get(&name).is_some()
617 }
618 
619 pub static BUILTIN_ATTRIBUTE_MAP: SyncLazy<FxHashMap<Symbol, &BuiltinAttribute>> =
620     SyncLazy::new(|| {
621         let mut map = FxHashMap::default();
622         for attr in BUILTIN_ATTRIBUTES.iter() {
623             if map.insert(attr.0, attr).is_some() {
624                 panic!("duplicate builtin attribute `{}`", attr.0);
625             }
626         }
627         map
628     });
629