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