]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_feature/src/builtin_attrs.rs
Rollup merge of #93416 - name1e5s:chore/remove_allow_fail, r=m-ou-se
[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!(
407         test_runner, CrateLevel, template!(List: "path"), ErrorFollowing, custom_test_frameworks,
408         "custom test frameworks are an unstable feature",
409     ),
410     // RFC #1268
411     gated!(
412         marker, Normal, template!(Word), WarnFollowing, marker_trait_attr, experimental!(marker)
413     ),
414     gated!(
415         thread_local, Normal, template!(Word), WarnFollowing,
416         "`#[thread_local]` is an experimental feature, and does not currently handle destructors",
417     ),
418     gated!(no_core, CrateLevel, template!(Word), WarnFollowing, experimental!(no_core)),
419     // RFC 2412
420     gated!(
421         optimize, Normal, template!(List: "size|speed"), ErrorPreceding, optimize_attribute,
422         experimental!(optimize),
423     ),
424     // RFC 2867
425     gated!(
426         instruction_set, Normal, template!(List: "set"), ErrorPreceding,
427         isa_attribute, experimental!(instruction_set)
428     ),
429
430     gated!(
431         ffi_returns_twice, Normal, template!(Word), WarnFollowing, experimental!(ffi_returns_twice)
432     ),
433     gated!(ffi_pure, Normal, template!(Word), WarnFollowing, experimental!(ffi_pure)),
434     gated!(ffi_const, Normal, template!(Word), WarnFollowing, experimental!(ffi_const)),
435     gated!(
436         register_attr, CrateLevel, template!(List: "attr1, attr2, ..."), DuplicatesOk,
437         experimental!(register_attr),
438     ),
439     gated!(
440         register_tool, CrateLevel, template!(List: "tool1, tool2, ..."), DuplicatesOk,
441         experimental!(register_tool),
442     ),
443
444     gated!(
445         cmse_nonsecure_entry, Normal, template!(Word), WarnFollowing,
446         experimental!(cmse_nonsecure_entry)
447     ),
448     // RFC 2632
449     gated!(
450         default_method_body_is_const, Normal, template!(Word), WarnFollowing, const_trait_impl,
451         "`default_method_body_is_const` is a temporary placeholder for declaring default bodies \
452         as `const`, which may be removed or renamed in the future."
453     ),
454
455     // ==========================================================================
456     // Internal attributes: Stability, deprecation, and unsafe:
457     // ==========================================================================
458
459     ungated!(feature, CrateLevel, template!(List: "name1, name1, ..."), DuplicatesOk),
460     // DuplicatesOk since it has its own validation
461     ungated!(
462         rustc_deprecated, Normal,
463         template!(List: r#"since = "version", reason = "...""#), DuplicatesOk // See E0550
464     ),
465     // DuplicatesOk since it has its own validation
466     ungated!(
467         stable, Normal, template!(List: r#"feature = "name", since = "version""#), DuplicatesOk
468     ),
469     ungated!(
470         unstable, Normal,
471         template!(List: r#"feature = "name", reason = "...", issue = "N""#), DuplicatesOk,
472     ),
473     ungated!(rustc_const_unstable, Normal, template!(List: r#"feature = "name""#), DuplicatesOk),
474     ungated!(rustc_const_stable, Normal, template!(List: r#"feature = "name""#), DuplicatesOk),
475     gated!(
476         allow_internal_unstable, Normal, template!(Word, List: "feat1, feat2, ..."), DuplicatesOk,
477         "allow_internal_unstable side-steps feature gating and stability checks",
478     ),
479     gated!(
480         rustc_allow_const_fn_unstable, Normal,
481         template!(Word, List: "feat1, feat2, ..."), DuplicatesOk,
482         "rustc_allow_const_fn_unstable side-steps feature gating and stability checks"
483     ),
484     gated!(
485         allow_internal_unsafe, Normal, template!(Word), WarnFollowing,
486         "allow_internal_unsafe side-steps the unsafe_code lint",
487     ),
488
489     // ==========================================================================
490     // Internal attributes: Type system related:
491     // ==========================================================================
492
493     gated!(fundamental, Normal, template!(Word), WarnFollowing, experimental!(fundamental)),
494     gated!(
495         may_dangle, Normal, template!(Word), WarnFollowing, dropck_eyepatch,
496         "`may_dangle` has unstable semantics and may be removed in the future",
497     ),
498
499     // ==========================================================================
500     // Internal attributes: Runtime related:
501     // ==========================================================================
502
503     rustc_attr!(rustc_allocator, Normal, template!(Word), WarnFollowing, IMPL_DETAIL),
504     rustc_attr!(rustc_allocator_nounwind, Normal, template!(Word), WarnFollowing, IMPL_DETAIL),
505     gated!(
506         alloc_error_handler, Normal, template!(Word), WarnFollowing,
507         experimental!(alloc_error_handler)
508     ),
509     gated!(
510         default_lib_allocator, Normal, template!(Word), WarnFollowing, allocator_internals,
511         experimental!(default_lib_allocator),
512     ),
513     gated!(
514         needs_allocator, Normal, template!(Word), WarnFollowing, allocator_internals,
515         experimental!(needs_allocator),
516     ),
517     gated!(panic_runtime, Normal, template!(Word), WarnFollowing, experimental!(panic_runtime)),
518     gated!(
519         needs_panic_runtime, Normal, template!(Word), WarnFollowing,
520         experimental!(needs_panic_runtime)
521     ),
522     gated!(
523         compiler_builtins, Normal, template!(Word), WarnFollowing,
524         "the `#[compiler_builtins]` attribute is used to identify the `compiler_builtins` crate \
525         which contains compiler-rt intrinsics and will never be stable",
526     ),
527     gated!(
528         profiler_runtime, Normal, template!(Word), WarnFollowing,
529         "the `#[profiler_runtime]` attribute is used to identify the `profiler_builtins` crate \
530         which contains the profiler runtime and will never be stable",
531     ),
532
533     // ==========================================================================
534     // Internal attributes, Linkage:
535     // ==========================================================================
536
537     gated!(
538         linkage, Normal, template!(NameValueStr: "external|internal|..."), ErrorPreceding,
539         "the `linkage` attribute is experimental and not portable across platforms",
540     ),
541     rustc_attr!(
542         rustc_std_internal_symbol, Normal, template!(Word), WarnFollowing, INTERNAL_UNSTABLE
543     ),
544
545     // ==========================================================================
546     // Internal attributes, Macro related:
547     // ==========================================================================
548
549     rustc_attr!(
550         rustc_builtin_macro, Normal,
551         template!(Word, List: "name, /*opt*/ attributes(name1, name2, ...)"), ErrorFollowing,
552         IMPL_DETAIL,
553     ),
554     rustc_attr!(rustc_proc_macro_decls, Normal, template!(Word), WarnFollowing, INTERNAL_UNSTABLE),
555     rustc_attr!(
556         rustc_macro_transparency, Normal,
557         template!(NameValueStr: "transparent|semitransparent|opaque"), ErrorFollowing,
558         "used internally for testing macro hygiene",
559     ),
560
561     // ==========================================================================
562     // Internal attributes, Diagnostics related:
563     // ==========================================================================
564
565     rustc_attr!(
566         rustc_on_unimplemented, Normal,
567         template!(
568             List: r#"/*opt*/ message = "...", /*opt*/ label = "...", /*opt*/ note = "...""#,
569             NameValueStr: "message"
570         ),
571         ErrorFollowing,
572         INTERNAL_UNSTABLE
573     ),
574     // Enumerates "identity-like" conversion methods to suggest on type mismatch.
575     rustc_attr!(
576         rustc_conversion_suggestion, Normal, template!(Word), WarnFollowing, INTERNAL_UNSTABLE
577     ),
578     // Prevents field reads in the marked trait or method to be considered
579     // during dead code analysis.
580     rustc_attr!(
581         rustc_trivial_field_reads, Normal, template!(Word), WarnFollowing, INTERNAL_UNSTABLE
582     ),
583     // Used by the `rustc::potential_query_instability` lint to warn methods which
584     // might not be stable during incremental compilation.
585     rustc_attr!(rustc_lint_query_instability, Normal, template!(Word), WarnFollowing, INTERNAL_UNSTABLE),
586
587     // ==========================================================================
588     // Internal attributes, Const related:
589     // ==========================================================================
590
591     rustc_attr!(rustc_promotable, Normal, template!(Word), WarnFollowing, IMPL_DETAIL),
592     rustc_attr!(
593         rustc_legacy_const_generics, Normal, template!(List: "N"), ErrorFollowing,
594         INTERNAL_UNSTABLE
595     ),
596     // Do not const-check this function's body. It will always get replaced during CTFE.
597     rustc_attr!(
598         rustc_do_not_const_check, Normal, template!(Word), WarnFollowing, INTERNAL_UNSTABLE
599     ),
600
601     // ==========================================================================
602     // Internal attributes, Layout related:
603     // ==========================================================================
604
605     rustc_attr!(
606         rustc_layout_scalar_valid_range_start, Normal, template!(List: "value"), ErrorFollowing,
607         "the `#[rustc_layout_scalar_valid_range_start]` attribute is just used to enable \
608         niche optimizations in libcore and will never be stable",
609     ),
610     rustc_attr!(
611         rustc_layout_scalar_valid_range_end, Normal, template!(List: "value"), ErrorFollowing,
612         "the `#[rustc_layout_scalar_valid_range_end]` attribute is just used to enable \
613         niche optimizations in libcore and will never be stable",
614     ),
615     rustc_attr!(
616         rustc_nonnull_optimization_guaranteed, Normal, template!(Word), WarnFollowing,
617         "the `#[rustc_nonnull_optimization_guaranteed]` attribute is just used to enable \
618         niche optimizations in libcore and will never be stable",
619     ),
620
621     // ==========================================================================
622     // Internal attributes, Misc:
623     // ==========================================================================
624     gated!(
625         lang, Normal, template!(NameValueStr: "name"), DuplicatesOk, lang_items,
626         "language items are subject to change",
627     ),
628     rustc_attr!(
629         rustc_pass_by_value, Normal,
630         template!(Word), ErrorFollowing,
631         "#[rustc_pass_by_value] is used to mark types that must be passed by value instead of reference."
632     ),
633     BuiltinAttribute {
634         name: sym::rustc_diagnostic_item,
635         type_: Normal,
636         template: template!(NameValueStr: "name"),
637         duplicates: ErrorFollowing,
638         gate: Gated(
639             Stability::Unstable,
640             sym::rustc_attrs,
641             "diagnostic items compiler internal support for linting",
642             cfg_fn!(rustc_attrs),
643         ),
644     },
645     gated!(
646         // Used in resolve:
647         prelude_import, Normal, template!(Word), WarnFollowing,
648         "`#[prelude_import]` is for use by rustc only",
649     ),
650     gated!(
651         rustc_paren_sugar, Normal, template!(Word), WarnFollowing, unboxed_closures,
652         "unboxed_closures are still evolving",
653     ),
654     rustc_attr!(
655         rustc_inherit_overflow_checks, Normal, template!(Word), WarnFollowing,
656         "the `#[rustc_inherit_overflow_checks]` attribute is just used to control \
657         overflow checking behavior of several libcore functions that are inlined \
658         across crates and will never be stable",
659     ),
660     rustc_attr!(
661         rustc_reservation_impl, Normal,
662         template!(NameValueStr: "reservation message"), ErrorFollowing,
663         "the `#[rustc_reservation_impl]` attribute is internally used \
664          for reserving for `for<T> From<!> for T` impl"
665     ),
666     rustc_attr!(
667         rustc_test_marker, Normal, template!(Word), WarnFollowing,
668         "the `#[rustc_test_marker]` attribute is used internally to track tests",
669     ),
670     rustc_attr!(
671         rustc_unsafe_specialization_marker, Normal, template!(Word), WarnFollowing,
672         "the `#[rustc_unsafe_specialization_marker]` attribute is used to check specializations"
673     ),
674     rustc_attr!(
675         rustc_specialization_trait, Normal, template!(Word), WarnFollowing,
676         "the `#[rustc_specialization_trait]` attribute is used to check specializations"
677     ),
678     rustc_attr!(
679         rustc_main, Normal, template!(Word), WarnFollowing,
680         "the `#[rustc_main]` attribute is used internally to specify test entry point function",
681     ),
682     rustc_attr!(
683         rustc_skip_array_during_method_dispatch, Normal, template!(Word), WarnFollowing,
684         "the `#[rustc_skip_array_during_method_dispatch]` attribute is used to exclude a trait \
685         from method dispatch when the receiver is an array, for compatibility in editions < 2021."
686     ),
687     rustc_attr!(
688         rustc_must_implement_one_of, Normal, template!(List: "function1, function2, ..."), ErrorFollowing,
689         "the `#[rustc_must_implement_one_of]` attribute is used to change minimal complete \
690         definition of a trait, it's currently in experimental form and should be changed before \
691         being exposed outside of the std"
692     ),
693
694     // ==========================================================================
695     // Internal attributes, Testing:
696     // ==========================================================================
697
698     rustc_attr!(TEST, rustc_outlives, Normal, template!(Word), WarnFollowing),
699     rustc_attr!(TEST, rustc_capture_analysis, Normal, template!(Word), WarnFollowing),
700     rustc_attr!(TEST, rustc_insignificant_dtor, Normal, template!(Word), WarnFollowing),
701     rustc_attr!(TEST, rustc_strict_coherence, Normal, template!(Word), WarnFollowing),
702     rustc_attr!(TEST, rustc_variance, Normal, template!(Word), WarnFollowing),
703     rustc_attr!(TEST, rustc_layout, Normal, template!(List: "field1, field2, ..."), WarnFollowing),
704     rustc_attr!(TEST, rustc_regions, Normal, template!(Word), WarnFollowing),
705     rustc_attr!(
706         TEST, rustc_error, Normal,
707         template!(Word, List: "delay_span_bug_from_inside_query"), WarnFollowingWordOnly
708     ),
709     rustc_attr!(TEST, rustc_dump_user_substs, Normal, template!(Word), WarnFollowing),
710     rustc_attr!(TEST, rustc_evaluate_where_clauses, Normal, template!(Word), WarnFollowing),
711     rustc_attr!(
712         TEST, rustc_if_this_changed, Normal, template!(Word, List: "DepNode"), DuplicatesOk
713     ),
714     rustc_attr!(
715         TEST, rustc_then_this_would_need, Normal, template!(List: "DepNode"), DuplicatesOk
716     ),
717     rustc_attr!(
718         TEST, rustc_clean, Normal,
719         template!(List: r#"cfg = "...", /*opt*/ label = "...", /*opt*/ except = "...""#),
720         DuplicatesOk,
721     ),
722     rustc_attr!(
723         TEST, rustc_partition_reused, Normal,
724         template!(List: r#"cfg = "...", module = "...""#), DuplicatesOk,
725     ),
726     rustc_attr!(
727         TEST, rustc_partition_codegened, Normal,
728         template!(List: r#"cfg = "...", module = "...""#), DuplicatesOk,
729     ),
730     rustc_attr!(
731         TEST, rustc_expected_cgu_reuse, Normal,
732         template!(List: r#"cfg = "...", module = "...", kind = "...""#), DuplicatesOk,
733     ),
734     rustc_attr!(TEST, rustc_symbol_name, Normal, template!(Word), WarnFollowing),
735     rustc_attr!(TEST, rustc_polymorphize_error, Normal, template!(Word), WarnFollowing),
736     rustc_attr!(TEST, rustc_def_path, Normal, template!(Word), WarnFollowing),
737     rustc_attr!(TEST, rustc_mir, Normal, template!(List: "arg1, arg2, ..."), DuplicatesOk),
738     rustc_attr!(TEST, rustc_dump_program_clauses, Normal, template!(Word), WarnFollowing),
739     rustc_attr!(TEST, rustc_dump_env_program_clauses, Normal, template!(Word), WarnFollowing),
740     rustc_attr!(TEST, rustc_object_lifetime_default, Normal, template!(Word), WarnFollowing),
741     rustc_attr!(TEST, rustc_dump_vtable, Normal, template!(Word), WarnFollowing),
742     rustc_attr!(TEST, rustc_dummy, Normal, template!(Word /* doesn't matter*/), DuplicatesOk),
743     gated!(
744         omit_gdb_pretty_printer_section, Normal, template!(Word), WarnFollowing,
745         "the `#[omit_gdb_pretty_printer_section]` attribute is just used for the Rust test suite",
746     ),
747 ];
748
749 pub fn deprecated_attributes() -> Vec<&'static BuiltinAttribute> {
750     BUILTIN_ATTRIBUTES.iter().filter(|attr| attr.gate.is_deprecated()).collect()
751 }
752
753 pub fn is_builtin_attr_name(name: Symbol) -> bool {
754     BUILTIN_ATTRIBUTE_MAP.get(&name).is_some()
755 }
756
757 pub static BUILTIN_ATTRIBUTE_MAP: SyncLazy<FxHashMap<Symbol, &BuiltinAttribute>> =
758     SyncLazy::new(|| {
759         let mut map = FxHashMap::default();
760         for attr in BUILTIN_ATTRIBUTES.iter() {
761             if map.insert(attr.name, attr).is_some() {
762                 panic!("duplicate builtin attribute `{}`", attr.name);
763             }
764         }
765         map
766     });