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