]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_feature/src/builtin_attrs.rs
Rollup merge of #93613 - crlf0710:rename_to_async_iter, r=yaahc
[rust.git] / compiler / rustc_feature / src / builtin_attrs.rs
1 //! Built-in attributes and `cfg` flag gating.
2
3 use AttributeDuplicates::*;
4 use AttributeGate::*;
5 use AttributeType::*;
6
7 use crate::{Features, Stability};
8
9 use rustc_data_structures::fx::FxHashMap;
10 use rustc_span::symbol::{sym, Symbol};
11
12 use std::lazy::SyncLazy;
13
14 type GateFn = fn(&Features) -> bool;
15
16 macro_rules! cfg_fn {
17     ($field: ident) => {
18         (|features| features.$field) as GateFn
19     };
20 }
21
22 pub type GatedCfg = (Symbol, Symbol, GateFn);
23
24 /// `cfg(...)`'s that are feature gated.
25 const GATED_CFGS: &[GatedCfg] = &[
26     // (name in cfg, feature, function to check if the feature is enabled)
27     (sym::target_abi, sym::cfg_target_abi, cfg_fn!(cfg_target_abi)),
28     (sym::target_thread_local, sym::cfg_target_thread_local, cfg_fn!(cfg_target_thread_local)),
29     (
30         sym::target_has_atomic_equal_alignment,
31         sym::cfg_target_has_atomic_equal_alignment,
32         cfg_fn!(cfg_target_has_atomic_equal_alignment),
33     ),
34     (sym::target_has_atomic_load_store, sym::cfg_target_has_atomic, cfg_fn!(cfg_target_has_atomic)),
35     (sym::sanitize, sym::cfg_sanitize, cfg_fn!(cfg_sanitize)),
36     (sym::version, sym::cfg_version, cfg_fn!(cfg_version)),
37     (sym::panic, sym::cfg_panic, cfg_fn!(cfg_panic)),
38 ];
39
40 /// Find a gated cfg determined by the `pred`icate which is given the cfg's name.
41 pub fn find_gated_cfg(pred: impl Fn(Symbol) -> bool) -> Option<&'static GatedCfg> {
42     GATED_CFGS.iter().find(|(cfg_sym, ..)| pred(*cfg_sym))
43 }
44
45 // If you change this, please modify `src/doc/unstable-book` as well. You must
46 // move that documentation into the relevant place in the other docs, and
47 // remove the chapter on the flag.
48
49 #[derive(Copy, Clone, PartialEq, Debug)]
50 pub enum AttributeType {
51     /// Normal, builtin attribute that is consumed
52     /// by the compiler before the unused_attribute check
53     Normal,
54
55     /// Builtin attribute that is only allowed at the crate level
56     CrateLevel,
57 }
58
59 #[derive(Clone, Copy)]
60 pub enum AttributeGate {
61     /// Is gated by a given feature gate, reason
62     /// and function to check if enabled
63     Gated(Stability, Symbol, &'static str, fn(&Features) -> bool),
64
65     /// Ungated attribute, can be used on all release channels
66     Ungated,
67 }
68
69 // fn() is not Debug
70 impl std::fmt::Debug for AttributeGate {
71     fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
72         match *self {
73             Self::Gated(ref stab, name, expl, _) => {
74                 write!(fmt, "Gated({:?}, {}, {})", stab, name, expl)
75             }
76             Self::Ungated => write!(fmt, "Ungated"),
77         }
78     }
79 }
80
81 impl AttributeGate {
82     fn is_deprecated(&self) -> bool {
83         matches!(*self, Self::Gated(Stability::Deprecated(_, _), ..))
84     }
85 }
86
87 /// A template that the attribute input must match.
88 /// Only top-level shape (`#[attr]` vs `#[attr(...)]` vs `#[attr = ...]`) is considered now.
89 #[derive(Clone, Copy, Default)]
90 pub struct AttributeTemplate {
91     /// If `true`, the attribute is allowed to be a bare word like `#[test]`.
92     pub word: bool,
93     /// If `Some`, the attribute is allowed to take a list of items like `#[allow(..)]`.
94     pub list: Option<&'static str>,
95     /// If `Some`, the attribute is allowed to be a name/value pair where the
96     /// value is a string, like `#[must_use = "reason"]`.
97     pub name_value_str: Option<&'static str>,
98 }
99
100 /// How to handle multiple duplicate attributes on the same item.
101 #[derive(Clone, Copy, Default)]
102 pub enum AttributeDuplicates {
103     /// Duplicates of this attribute are allowed.
104     ///
105     /// This should only be used with attributes where duplicates have semantic
106     /// meaning, or some kind of "additive" behavior. For example, `#[warn(..)]`
107     /// can be specified multiple times, and it combines all the entries. Or use
108     /// this if there is validation done elsewhere.
109     #[default]
110     DuplicatesOk,
111     /// Duplicates after the first attribute will be an unused_attribute warning.
112     ///
113     /// This is usually used for "word" attributes, where they are used as a
114     /// boolean marker, like `#[used]`. It is not necessarily wrong that there
115     /// are duplicates, but the others should probably be removed.
116     WarnFollowing,
117     /// Same as `WarnFollowing`, but only issues warnings for word-style attributes.
118     ///
119     /// This is only for special cases, for example multiple `#[macro_use]` can
120     /// be warned, but multiple `#[macro_use(...)]` should not because the list
121     /// form has different meaning from the word form.
122     WarnFollowingWordOnly,
123     /// Duplicates after the first attribute will be an error.
124     ///
125     /// This should be used where duplicates would be ignored, but carry extra
126     /// meaning that could cause confusion. For example, `#[stable(since="1.0")]
127     /// #[stable(since="2.0")]`, which version should be used for `stable`?
128     ErrorFollowing,
129     /// Duplicates preceding the last instance of the attribute will be an error.
130     ///
131     /// This is the same as `ErrorFollowing`, except the last attribute is the
132     /// one that is "used". This is typically used in cases like codegen
133     /// attributes which usually only honor the last attribute.
134     ErrorPreceding,
135     /// Duplicates after the first attribute will be an unused_attribute warning
136     /// with a note that this will be an error in the future.
137     ///
138     /// This should be used for attributes that should be `ErrorFollowing`, but
139     /// because older versions of rustc silently accepted (and ignored) the
140     /// attributes, this is used to transition.
141     FutureWarnFollowing,
142     /// Duplicates preceding the last instance of the attribute will be a
143     /// warning, with a note that this will be an error in the future.
144     ///
145     /// This is the same as `FutureWarnFollowing`, except the last attribute is
146     /// the one that is "used". Ideally these can eventually migrate to
147     /// `ErrorPreceding`.
148     FutureWarnPreceding,
149 }
150
151 /// A convenience macro for constructing attribute templates.
152 /// E.g., `template!(Word, List: "description")` means that the attribute
153 /// supports forms `#[attr]` and `#[attr(description)]`.
154 macro_rules! template {
155     (Word) => { template!(@ true, None, None) };
156     (List: $descr: expr) => { template!(@ false, Some($descr), None) };
157     (NameValueStr: $descr: expr) => { template!(@ false, None, Some($descr)) };
158     (Word, List: $descr: expr) => { template!(@ true, Some($descr), None) };
159     (Word, NameValueStr: $descr: expr) => { template!(@ true, None, Some($descr)) };
160     (List: $descr1: expr, NameValueStr: $descr2: expr) => {
161         template!(@ false, Some($descr1), Some($descr2))
162     };
163     (Word, List: $descr1: expr, NameValueStr: $descr2: expr) => {
164         template!(@ true, Some($descr1), Some($descr2))
165     };
166     (@ $word: expr, $list: expr, $name_value_str: expr) => { AttributeTemplate {
167         word: $word, list: $list, name_value_str: $name_value_str
168     } };
169 }
170
171 macro_rules! ungated {
172     ($attr:ident, $typ:expr, $tpl:expr, $duplicates:expr $(,)?) => {
173         BuiltinAttribute {
174             name: sym::$attr,
175             type_: $typ,
176             template: $tpl,
177             gate: Ungated,
178             duplicates: $duplicates,
179         }
180     };
181 }
182
183 macro_rules! gated {
184     ($attr:ident, $typ:expr, $tpl:expr, $duplicates:expr, $gate:ident, $msg:expr $(,)?) => {
185         BuiltinAttribute {
186             name: sym::$attr,
187             type_: $typ,
188             template: $tpl,
189             duplicates: $duplicates,
190             gate: Gated(Stability::Unstable, sym::$gate, $msg, cfg_fn!($gate)),
191         }
192     };
193     ($attr:ident, $typ:expr, $tpl:expr, $duplicates:expr, $msg:expr $(,)?) => {
194         BuiltinAttribute {
195             name: sym::$attr,
196             type_: $typ,
197             template: $tpl,
198             duplicates: $duplicates,
199             gate: Gated(Stability::Unstable, sym::$attr, $msg, cfg_fn!($attr)),
200         }
201     };
202 }
203
204 macro_rules! rustc_attr {
205     (TEST, $attr:ident, $typ:expr, $tpl:expr, $duplicate:expr $(,)?) => {
206         rustc_attr!(
207             $attr,
208             $typ,
209             $tpl,
210             $duplicate,
211             concat!(
212                 "the `#[",
213                 stringify!($attr),
214                 "]` attribute is just used for rustc unit tests \
215                 and will never be stable",
216             ),
217         )
218     };
219     ($attr:ident, $typ:expr, $tpl:expr, $duplicates:expr, $msg:expr $(,)?) => {
220         BuiltinAttribute {
221             name: sym::$attr,
222             type_: $typ,
223             template: $tpl,
224             duplicates: $duplicates,
225             gate: Gated(Stability::Unstable, sym::rustc_attrs, $msg, cfg_fn!(rustc_attrs)),
226         }
227     };
228 }
229
230 macro_rules! experimental {
231     ($attr:ident) => {
232         concat!("the `#[", stringify!($attr), "]` attribute is an experimental feature")
233     };
234 }
235
236 const IMPL_DETAIL: &str = "internal implementation detail";
237 const INTERNAL_UNSTABLE: &str = "this is an internal attribute that will never be stable";
238
239 pub struct BuiltinAttribute {
240     pub name: Symbol,
241     pub type_: AttributeType,
242     pub template: AttributeTemplate,
243     pub duplicates: AttributeDuplicates,
244     pub gate: AttributeGate,
245 }
246
247 /// Attributes that have a special meaning to rustc or rustdoc.
248 #[rustfmt::skip]
249 pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
250     // ==========================================================================
251     // Stable attributes:
252     // ==========================================================================
253
254     // Conditional compilation:
255     ungated!(cfg, Normal, template!(List: "predicate"), DuplicatesOk),
256     ungated!(cfg_attr, Normal, template!(List: "predicate, attr1, attr2, ..."), DuplicatesOk),
257
258     // Testing:
259     ungated!(ignore, Normal, template!(Word, NameValueStr: "reason"), WarnFollowing),
260     ungated!(
261         should_panic, Normal,
262         template!(Word, List: r#"expected = "reason"#, NameValueStr: "reason"), FutureWarnFollowing,
263     ),
264     // FIXME(Centril): This can be used on stable but shouldn't.
265     ungated!(reexport_test_harness_main, CrateLevel, template!(NameValueStr: "name"), ErrorFollowing),
266
267     // Macros:
268     ungated!(automatically_derived, Normal, template!(Word), WarnFollowing),
269     ungated!(macro_use, Normal, template!(Word, List: "name1, name2, ..."), WarnFollowingWordOnly),
270     ungated!(macro_escape, Normal, template!(Word), WarnFollowing), // Deprecated synonym for `macro_use`.
271     ungated!(macro_export, Normal, template!(Word, List: "local_inner_macros"), WarnFollowing),
272     ungated!(proc_macro, Normal, template!(Word), ErrorFollowing),
273     ungated!(
274         proc_macro_derive, Normal,
275         template!(List: "TraitName, /*opt*/ attributes(name1, name2, ...)"), ErrorFollowing,
276     ),
277     ungated!(proc_macro_attribute, Normal, template!(Word), ErrorFollowing),
278
279     // Lints:
280     ungated!(
281         warn, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#), DuplicatesOk
282     ),
283     ungated!(
284         allow, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#), DuplicatesOk
285     ),
286     ungated!(
287         forbid, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#), DuplicatesOk
288     ),
289     ungated!(
290         deny, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#), DuplicatesOk
291     ),
292     ungated!(must_use, Normal, template!(Word, NameValueStr: "reason"), FutureWarnFollowing),
293     gated!(
294         must_not_suspend, Normal, template!(Word, NameValueStr: "reason"), WarnFollowing,
295         must_not_suspend, experimental!(must_not_suspend)
296     ),
297     ungated!(
298         deprecated, Normal,
299         template!(
300             Word,
301             List: r#"/*opt*/ since = "version", /*opt*/ note = "reason""#,
302             NameValueStr: "reason"
303         ),
304         // This has special duplicate handling in E0550 to handle duplicates with rustc_deprecated
305         DuplicatesOk
306     ),
307
308     // Crate properties:
309     ungated!(crate_name, CrateLevel, template!(NameValueStr: "name"), FutureWarnFollowing),
310     ungated!(crate_type, CrateLevel, template!(NameValueStr: "bin|lib|..."), FutureWarnFollowing),
311     // crate_id is deprecated
312     ungated!(crate_id, CrateLevel, template!(NameValueStr: "ignored"), FutureWarnFollowing),
313
314     // ABI, linking, symbols, and FFI
315     ungated!(
316         link, Normal,
317         template!(List: r#"name = "...", /*opt*/ kind = "dylib|static|...", /*opt*/ wasm_import_module = "...""#),
318         DuplicatesOk,
319     ),
320     ungated!(link_name, Normal, template!(NameValueStr: "name"), FutureWarnPreceding),
321     ungated!(no_link, Normal, template!(Word), WarnFollowing),
322     ungated!(repr, Normal, template!(List: "C"), DuplicatesOk),
323     ungated!(export_name, Normal, template!(NameValueStr: "name"), FutureWarnPreceding),
324     ungated!(link_section, Normal, template!(NameValueStr: "name"), FutureWarnPreceding),
325     ungated!(no_mangle, Normal, template!(Word), WarnFollowing),
326     ungated!(used, Normal, template!(Word, List: "compiler|linker"), WarnFollowing),
327
328     // Limits:
329     ungated!(recursion_limit, CrateLevel, template!(NameValueStr: "N"), FutureWarnFollowing),
330     ungated!(type_length_limit, CrateLevel, template!(NameValueStr: "N"), FutureWarnFollowing),
331     gated!(
332         const_eval_limit, CrateLevel, template!(NameValueStr: "N"), ErrorFollowing,
333         const_eval_limit, experimental!(const_eval_limit)
334     ),
335     gated!(
336         move_size_limit, CrateLevel, template!(NameValueStr: "N"), ErrorFollowing,
337         large_assignments, experimental!(move_size_limit)
338     ),
339
340     // Entry point:
341     ungated!(start, Normal, template!(Word), WarnFollowing),
342     ungated!(no_start, CrateLevel, template!(Word), WarnFollowing),
343     ungated!(no_main, CrateLevel, template!(Word), WarnFollowing),
344
345     // Modules, prelude, and resolution:
346     ungated!(path, Normal, template!(NameValueStr: "file"), FutureWarnFollowing),
347     ungated!(no_std, CrateLevel, template!(Word), WarnFollowing),
348     ungated!(no_implicit_prelude, Normal, template!(Word), WarnFollowing),
349     ungated!(non_exhaustive, Normal, template!(Word), WarnFollowing),
350
351     // Runtime
352     ungated!(
353         windows_subsystem, CrateLevel,
354         template!(NameValueStr: "windows|console"), FutureWarnFollowing
355     ),
356     ungated!(panic_handler, Normal, template!(Word), WarnFollowing), // RFC 2070
357
358     // Code generation:
359     ungated!(inline, Normal, template!(Word, List: "always|never"), FutureWarnFollowing),
360     ungated!(cold, Normal, template!(Word), WarnFollowing),
361     ungated!(no_builtins, CrateLevel, template!(Word), WarnFollowing),
362     ungated!(target_feature, Normal, template!(List: r#"enable = "name""#), DuplicatesOk),
363     ungated!(track_caller, Normal, template!(Word), WarnFollowing),
364     gated!(
365         no_sanitize, Normal,
366         template!(List: "address, memory, thread"), DuplicatesOk,
367         experimental!(no_sanitize)
368     ),
369     gated!(no_coverage, Normal, template!(Word), WarnFollowing, experimental!(no_coverage)),
370
371     ungated!(
372         doc, Normal, template!(List: "hidden|inline|...", NameValueStr: "string"), DuplicatesOk
373     ),
374
375     // ==========================================================================
376     // Unstable attributes:
377     // ==========================================================================
378
379     // Linking:
380     gated!(naked, Normal, template!(Word), WarnFollowing, naked_functions, experimental!(naked)),
381     gated!(
382         link_ordinal, Normal, template!(List: "ordinal"), ErrorPreceding, raw_dylib,
383         experimental!(link_ordinal)
384     ),
385
386     // Plugins:
387     BuiltinAttribute {
388         name: sym::plugin,
389         type_: CrateLevel,
390         template: template!(List: "name"),
391         duplicates: DuplicatesOk,
392         gate: Gated(
393             Stability::Deprecated(
394                 "https://github.com/rust-lang/rust/pull/64675",
395                 Some("may be removed in a future compiler version"),
396             ),
397             sym::plugin,
398             "compiler plugins are deprecated",
399             cfg_fn!(plugin)
400         ),
401     },
402
403     // Testing:
404     gated!(
405         test_runner, CrateLevel, template!(List: "path"), ErrorFollowing, custom_test_frameworks,
406         "custom test frameworks are an unstable feature",
407     ),
408     // RFC #1268
409     gated!(
410         marker, Normal, template!(Word), WarnFollowing, marker_trait_attr, experimental!(marker)
411     ),
412     gated!(
413         thread_local, Normal, template!(Word), WarnFollowing,
414         "`#[thread_local]` is an experimental feature, and does not currently handle destructors",
415     ),
416     gated!(no_core, CrateLevel, template!(Word), WarnFollowing, experimental!(no_core)),
417     // RFC 2412
418     gated!(
419         optimize, Normal, template!(List: "size|speed"), ErrorPreceding, optimize_attribute,
420         experimental!(optimize),
421     ),
422     // RFC 2867
423     gated!(
424         instruction_set, Normal, template!(List: "set"), ErrorPreceding,
425         isa_attribute, experimental!(instruction_set)
426     ),
427
428     gated!(
429         ffi_returns_twice, Normal, template!(Word), WarnFollowing, experimental!(ffi_returns_twice)
430     ),
431     gated!(ffi_pure, Normal, template!(Word), WarnFollowing, experimental!(ffi_pure)),
432     gated!(ffi_const, Normal, template!(Word), WarnFollowing, experimental!(ffi_const)),
433     gated!(
434         register_attr, CrateLevel, template!(List: "attr1, attr2, ..."), DuplicatesOk,
435         experimental!(register_attr),
436     ),
437     gated!(
438         register_tool, CrateLevel, template!(List: "tool1, tool2, ..."), DuplicatesOk,
439         experimental!(register_tool),
440     ),
441
442     gated!(
443         cmse_nonsecure_entry, Normal, template!(Word), WarnFollowing,
444         experimental!(cmse_nonsecure_entry)
445     ),
446     // RFC 2632
447     gated!(
448         default_method_body_is_const, Normal, template!(Word), WarnFollowing, const_trait_impl,
449         "`default_method_body_is_const` is a temporary placeholder for declaring default bodies \
450         as `const`, which may be removed or renamed in the future."
451     ),
452
453     // ==========================================================================
454     // Internal attributes: Stability, deprecation, and unsafe:
455     // ==========================================================================
456
457     ungated!(feature, CrateLevel, template!(List: "name1, name1, ..."), DuplicatesOk),
458     // DuplicatesOk since it has its own validation
459     ungated!(
460         rustc_deprecated, Normal,
461         template!(List: r#"since = "version", reason = "...""#), DuplicatesOk // See E0550
462     ),
463     // DuplicatesOk since it has its own validation
464     ungated!(
465         stable, Normal, template!(List: r#"feature = "name", since = "version""#), DuplicatesOk
466     ),
467     ungated!(
468         unstable, Normal,
469         template!(List: r#"feature = "name", reason = "...", issue = "N""#), DuplicatesOk,
470     ),
471     ungated!(rustc_const_unstable, Normal, template!(List: r#"feature = "name""#), DuplicatesOk),
472     ungated!(rustc_const_stable, Normal, template!(List: r#"feature = "name""#), DuplicatesOk),
473     gated!(
474         allow_internal_unstable, Normal, template!(Word, List: "feat1, feat2, ..."), DuplicatesOk,
475         "allow_internal_unstable side-steps feature gating and stability checks",
476     ),
477     gated!(
478         rustc_allow_const_fn_unstable, Normal,
479         template!(Word, List: "feat1, feat2, ..."), DuplicatesOk,
480         "rustc_allow_const_fn_unstable side-steps feature gating and stability checks"
481     ),
482     gated!(
483         allow_internal_unsafe, Normal, template!(Word), WarnFollowing,
484         "allow_internal_unsafe side-steps the unsafe_code lint",
485     ),
486
487     // ==========================================================================
488     // Internal attributes: Type system related:
489     // ==========================================================================
490
491     gated!(fundamental, Normal, template!(Word), WarnFollowing, experimental!(fundamental)),
492     gated!(
493         may_dangle, Normal, template!(Word), WarnFollowing, dropck_eyepatch,
494         "`may_dangle` has unstable semantics and may be removed in the future",
495     ),
496
497     // ==========================================================================
498     // Internal attributes: Runtime related:
499     // ==========================================================================
500
501     rustc_attr!(rustc_allocator, Normal, template!(Word), WarnFollowing, IMPL_DETAIL),
502     rustc_attr!(rustc_allocator_nounwind, Normal, template!(Word), WarnFollowing, IMPL_DETAIL),
503     gated!(
504         alloc_error_handler, Normal, template!(Word), WarnFollowing,
505         experimental!(alloc_error_handler)
506     ),
507     gated!(
508         default_lib_allocator, Normal, template!(Word), WarnFollowing, allocator_internals,
509         experimental!(default_lib_allocator),
510     ),
511     gated!(
512         needs_allocator, Normal, template!(Word), WarnFollowing, allocator_internals,
513         experimental!(needs_allocator),
514     ),
515     gated!(panic_runtime, Normal, template!(Word), WarnFollowing, experimental!(panic_runtime)),
516     gated!(
517         needs_panic_runtime, Normal, template!(Word), WarnFollowing,
518         experimental!(needs_panic_runtime)
519     ),
520     gated!(
521         compiler_builtins, Normal, template!(Word), WarnFollowing,
522         "the `#[compiler_builtins]` attribute is used to identify the `compiler_builtins` crate \
523         which contains compiler-rt intrinsics and will never be stable",
524     ),
525     gated!(
526         profiler_runtime, Normal, template!(Word), WarnFollowing,
527         "the `#[profiler_runtime]` attribute is used to identify the `profiler_builtins` crate \
528         which contains the profiler runtime and will never be stable",
529     ),
530
531     // ==========================================================================
532     // Internal attributes, Linkage:
533     // ==========================================================================
534
535     gated!(
536         linkage, Normal, template!(NameValueStr: "external|internal|..."), ErrorPreceding,
537         "the `linkage` attribute is experimental and not portable across platforms",
538     ),
539     rustc_attr!(
540         rustc_std_internal_symbol, Normal, template!(Word), WarnFollowing, INTERNAL_UNSTABLE
541     ),
542
543     // ==========================================================================
544     // Internal attributes, Macro related:
545     // ==========================================================================
546
547     rustc_attr!(
548         rustc_builtin_macro, Normal,
549         template!(Word, List: "name, /*opt*/ attributes(name1, name2, ...)"), ErrorFollowing,
550         IMPL_DETAIL,
551     ),
552     rustc_attr!(rustc_proc_macro_decls, Normal, template!(Word), WarnFollowing, INTERNAL_UNSTABLE),
553     rustc_attr!(
554         rustc_macro_transparency, Normal,
555         template!(NameValueStr: "transparent|semitransparent|opaque"), ErrorFollowing,
556         "used internally for testing macro hygiene",
557     ),
558
559     // ==========================================================================
560     // Internal attributes, Diagnostics related:
561     // ==========================================================================
562
563     rustc_attr!(
564         rustc_on_unimplemented, Normal,
565         template!(
566             List: r#"/*opt*/ message = "...", /*opt*/ label = "...", /*opt*/ note = "...""#,
567             NameValueStr: "message"
568         ),
569         ErrorFollowing,
570         INTERNAL_UNSTABLE
571     ),
572     // Enumerates "identity-like" conversion methods to suggest on type mismatch.
573     rustc_attr!(
574         rustc_conversion_suggestion, Normal, template!(Word), WarnFollowing, INTERNAL_UNSTABLE
575     ),
576     // Prevents field reads in the marked trait or method to be considered
577     // during dead code analysis.
578     rustc_attr!(
579         rustc_trivial_field_reads, Normal, template!(Word), WarnFollowing, INTERNAL_UNSTABLE
580     ),
581     // Used by the `rustc::potential_query_instability` lint to warn methods which
582     // might not be stable during incremental compilation.
583     rustc_attr!(rustc_lint_query_instability, Normal, template!(Word), WarnFollowing, INTERNAL_UNSTABLE),
584
585     // ==========================================================================
586     // Internal attributes, Const related:
587     // ==========================================================================
588
589     rustc_attr!(rustc_promotable, Normal, template!(Word), WarnFollowing, IMPL_DETAIL),
590     rustc_attr!(
591         rustc_legacy_const_generics, Normal, template!(List: "N"), ErrorFollowing,
592         INTERNAL_UNSTABLE
593     ),
594     // Do not const-check this function's body. It will always get replaced during CTFE.
595     rustc_attr!(
596         rustc_do_not_const_check, Normal, template!(Word), WarnFollowing, INTERNAL_UNSTABLE
597     ),
598
599     // ==========================================================================
600     // Internal attributes, Layout related:
601     // ==========================================================================
602
603     rustc_attr!(
604         rustc_layout_scalar_valid_range_start, Normal, template!(List: "value"), ErrorFollowing,
605         "the `#[rustc_layout_scalar_valid_range_start]` attribute is just used to enable \
606         niche optimizations in libcore and will never be stable",
607     ),
608     rustc_attr!(
609         rustc_layout_scalar_valid_range_end, Normal, template!(List: "value"), ErrorFollowing,
610         "the `#[rustc_layout_scalar_valid_range_end]` attribute is just used to enable \
611         niche optimizations in libcore and will never be stable",
612     ),
613     rustc_attr!(
614         rustc_nonnull_optimization_guaranteed, Normal, template!(Word), WarnFollowing,
615         "the `#[rustc_nonnull_optimization_guaranteed]` attribute is just used to enable \
616         niche optimizations in libcore and will never be stable",
617     ),
618
619     // ==========================================================================
620     // Internal attributes, Misc:
621     // ==========================================================================
622     gated!(
623         lang, Normal, template!(NameValueStr: "name"), DuplicatesOk, lang_items,
624         "language items are subject to change",
625     ),
626     rustc_attr!(
627         rustc_pass_by_value, Normal,
628         template!(Word), ErrorFollowing,
629         "#[rustc_pass_by_value] is used to mark types that must be passed by value instead of reference."
630     ),
631     BuiltinAttribute {
632         name: sym::rustc_diagnostic_item,
633         type_: Normal,
634         template: template!(NameValueStr: "name"),
635         duplicates: ErrorFollowing,
636         gate: Gated(
637             Stability::Unstable,
638             sym::rustc_attrs,
639             "diagnostic items compiler internal support for linting",
640             cfg_fn!(rustc_attrs),
641         ),
642     },
643     gated!(
644         // Used in resolve:
645         prelude_import, Normal, template!(Word), WarnFollowing,
646         "`#[prelude_import]` is for use by rustc only",
647     ),
648     gated!(
649         rustc_paren_sugar, Normal, template!(Word), WarnFollowing, unboxed_closures,
650         "unboxed_closures are still evolving",
651     ),
652     rustc_attr!(
653         rustc_inherit_overflow_checks, Normal, template!(Word), WarnFollowing,
654         "the `#[rustc_inherit_overflow_checks]` attribute is just used to control \
655         overflow checking behavior of several libcore functions that are inlined \
656         across crates and will never be stable",
657     ),
658     rustc_attr!(
659         rustc_reservation_impl, Normal,
660         template!(NameValueStr: "reservation message"), ErrorFollowing,
661         "the `#[rustc_reservation_impl]` attribute is internally used \
662          for reserving for `for<T> From<!> for T` impl"
663     ),
664     rustc_attr!(
665         rustc_test_marker, Normal, template!(Word), WarnFollowing,
666         "the `#[rustc_test_marker]` attribute is used internally to track tests",
667     ),
668     rustc_attr!(
669         rustc_unsafe_specialization_marker, Normal, template!(Word), WarnFollowing,
670         "the `#[rustc_unsafe_specialization_marker]` attribute is used to check specializations"
671     ),
672     rustc_attr!(
673         rustc_specialization_trait, Normal, template!(Word), WarnFollowing,
674         "the `#[rustc_specialization_trait]` attribute is used to check specializations"
675     ),
676     rustc_attr!(
677         rustc_main, Normal, template!(Word), WarnFollowing,
678         "the `#[rustc_main]` attribute is used internally to specify test entry point function",
679     ),
680     rustc_attr!(
681         rustc_skip_array_during_method_dispatch, Normal, template!(Word), WarnFollowing,
682         "the `#[rustc_skip_array_during_method_dispatch]` attribute is used to exclude a trait \
683         from method dispatch when the receiver is an array, for compatibility in editions < 2021."
684     ),
685     rustc_attr!(
686         rustc_must_implement_one_of, Normal, template!(List: "function1, function2, ..."), ErrorFollowing,
687         "the `#[rustc_must_implement_one_of]` attribute is used to change minimal complete \
688         definition of a trait, it's currently in experimental form and should be changed before \
689         being exposed outside of the std"
690     ),
691
692     // ==========================================================================
693     // Internal attributes, Testing:
694     // ==========================================================================
695
696     rustc_attr!(TEST, rustc_outlives, Normal, template!(Word), WarnFollowing),
697     rustc_attr!(TEST, rustc_capture_analysis, Normal, template!(Word), WarnFollowing),
698     rustc_attr!(TEST, rustc_insignificant_dtor, Normal, template!(Word), WarnFollowing),
699     rustc_attr!(TEST, rustc_strict_coherence, Normal, template!(Word), WarnFollowing),
700     rustc_attr!(TEST, rustc_variance, Normal, template!(Word), WarnFollowing),
701     rustc_attr!(TEST, rustc_layout, Normal, template!(List: "field1, field2, ..."), WarnFollowing),
702     rustc_attr!(TEST, rustc_regions, Normal, template!(Word), WarnFollowing),
703     rustc_attr!(
704         TEST, rustc_error, Normal,
705         template!(Word, List: "delay_span_bug_from_inside_query"), WarnFollowingWordOnly
706     ),
707     rustc_attr!(TEST, rustc_dump_user_substs, Normal, template!(Word), WarnFollowing),
708     rustc_attr!(TEST, rustc_evaluate_where_clauses, Normal, template!(Word), WarnFollowing),
709     rustc_attr!(
710         TEST, rustc_if_this_changed, Normal, template!(Word, List: "DepNode"), DuplicatesOk
711     ),
712     rustc_attr!(
713         TEST, rustc_then_this_would_need, Normal, template!(List: "DepNode"), DuplicatesOk
714     ),
715     rustc_attr!(
716         TEST, rustc_clean, Normal,
717         template!(List: r#"cfg = "...", /*opt*/ label = "...", /*opt*/ except = "...""#),
718         DuplicatesOk,
719     ),
720     rustc_attr!(
721         TEST, rustc_partition_reused, Normal,
722         template!(List: r#"cfg = "...", module = "...""#), DuplicatesOk,
723     ),
724     rustc_attr!(
725         TEST, rustc_partition_codegened, Normal,
726         template!(List: r#"cfg = "...", module = "...""#), DuplicatesOk,
727     ),
728     rustc_attr!(
729         TEST, rustc_expected_cgu_reuse, Normal,
730         template!(List: r#"cfg = "...", module = "...", kind = "...""#), DuplicatesOk,
731     ),
732     rustc_attr!(TEST, rustc_symbol_name, Normal, template!(Word), WarnFollowing),
733     rustc_attr!(TEST, rustc_polymorphize_error, Normal, template!(Word), WarnFollowing),
734     rustc_attr!(TEST, rustc_def_path, Normal, template!(Word), WarnFollowing),
735     rustc_attr!(TEST, rustc_mir, Normal, template!(List: "arg1, arg2, ..."), DuplicatesOk),
736     rustc_attr!(TEST, rustc_dump_program_clauses, Normal, template!(Word), WarnFollowing),
737     rustc_attr!(TEST, rustc_dump_env_program_clauses, Normal, template!(Word), WarnFollowing),
738     rustc_attr!(TEST, rustc_object_lifetime_default, Normal, template!(Word), WarnFollowing),
739     rustc_attr!(TEST, rustc_dump_vtable, Normal, template!(Word), WarnFollowing),
740     rustc_attr!(TEST, rustc_dummy, Normal, template!(Word /* doesn't matter*/), DuplicatesOk),
741     gated!(
742         omit_gdb_pretty_printer_section, Normal, template!(Word), WarnFollowing,
743         "the `#[omit_gdb_pretty_printer_section]` attribute is just used for the Rust test suite",
744     ),
745 ];
746
747 pub fn deprecated_attributes() -> Vec<&'static BuiltinAttribute> {
748     BUILTIN_ATTRIBUTES.iter().filter(|attr| attr.gate.is_deprecated()).collect()
749 }
750
751 pub fn is_builtin_attr_name(name: Symbol) -> bool {
752     BUILTIN_ATTRIBUTE_MAP.get(&name).is_some()
753 }
754
755 pub static BUILTIN_ATTRIBUTE_MAP: SyncLazy<FxHashMap<Symbol, &BuiltinAttribute>> =
756     SyncLazy::new(|| {
757         let mut map = FxHashMap::default();
758         for attr in BUILTIN_ATTRIBUTES.iter() {
759             if map.insert(attr.name, attr).is_some() {
760                 panic!("duplicate builtin attribute `{}`", attr.name);
761             }
762         }
763         map
764     });