rustc_feature/
builtin_attrs.rs

1//! Built-in attributes and `cfg` flag gating.
2
3use std::sync::LazyLock;
4
5use AttributeDuplicates::*;
6use AttributeGate::*;
7use AttributeType::*;
8use rustc_data_structures::fx::FxHashMap;
9use rustc_span::{Symbol, sym};
10
11use crate::{Features, Stability};
12
13type GateFn = fn(&Features) -> bool;
14
15pub type GatedCfg = (Symbol, Symbol, GateFn);
16
17/// `cfg(...)`'s that are feature gated.
18const GATED_CFGS: &[GatedCfg] = &[
19    // (name in cfg, feature, function to check if the feature is enabled)
20    (sym::overflow_checks, sym::cfg_overflow_checks, Features::cfg_overflow_checks),
21    (sym::ub_checks, sym::cfg_ub_checks, Features::cfg_ub_checks),
22    (sym::contract_checks, sym::cfg_contract_checks, Features::cfg_contract_checks),
23    (sym::target_thread_local, sym::cfg_target_thread_local, Features::cfg_target_thread_local),
24    (
25        sym::target_has_atomic_equal_alignment,
26        sym::cfg_target_has_atomic_equal_alignment,
27        Features::cfg_target_has_atomic_equal_alignment,
28    ),
29    (
30        sym::target_has_atomic_load_store,
31        sym::cfg_target_has_atomic,
32        Features::cfg_target_has_atomic,
33    ),
34    (sym::sanitize, sym::cfg_sanitize, Features::cfg_sanitize),
35    (sym::version, sym::cfg_version, Features::cfg_version),
36    (sym::relocation_model, sym::cfg_relocation_model, Features::cfg_relocation_model),
37    (sym::sanitizer_cfi_generalize_pointers, sym::cfg_sanitizer_cfi, Features::cfg_sanitizer_cfi),
38    (sym::sanitizer_cfi_normalize_integers, sym::cfg_sanitizer_cfi, Features::cfg_sanitizer_cfi),
39    // this is consistent with naming of the compiler flag it's for
40    (sym::fmt_debug, sym::fmt_debug, Features::fmt_debug),
41    (sym::emscripten_wasm_eh, sym::cfg_emscripten_wasm_eh, Features::cfg_emscripten_wasm_eh),
42];
43
44/// Find a gated cfg determined by the `pred`icate which is given the cfg's name.
45pub fn find_gated_cfg(pred: impl Fn(Symbol) -> bool) -> Option<&'static GatedCfg> {
46    GATED_CFGS.iter().find(|(cfg_sym, ..)| pred(*cfg_sym))
47}
48
49// If you change this, please modify `src/doc/unstable-book` as well. You must
50// move that documentation into the relevant place in the other docs, and
51// remove the chapter on the flag.
52
53#[derive(Copy, Clone, PartialEq, Debug)]
54pub enum AttributeType {
55    /// Normal, builtin attribute that is consumed
56    /// by the compiler before the unused_attribute check
57    Normal,
58
59    /// Builtin attribute that is only allowed at the crate level
60    CrateLevel,
61}
62
63#[derive(Copy, Clone, PartialEq, Debug)]
64pub enum AttributeSafety {
65    /// Normal attribute that does not need `#[unsafe(...)]`
66    Normal,
67
68    /// Unsafe attribute that requires safety obligations
69    /// to be discharged
70    Unsafe,
71}
72
73#[derive(Clone, Copy)]
74pub enum AttributeGate {
75    /// Is gated by a given feature gate, reason
76    /// and function to check if enabled
77    Gated(Stability, Symbol, &'static str, fn(&Features) -> bool),
78
79    /// Ungated attribute, can be used on all release channels
80    Ungated,
81}
82
83// fn() is not Debug
84impl std::fmt::Debug for AttributeGate {
85    fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
86        match *self {
87            Self::Gated(ref stab, name, expl, _) => {
88                write!(fmt, "Gated({stab:?}, {name}, {expl})")
89            }
90            Self::Ungated => write!(fmt, "Ungated"),
91        }
92    }
93}
94
95impl AttributeGate {
96    fn is_deprecated(&self) -> bool {
97        matches!(*self, Self::Gated(Stability::Deprecated(_, _), ..))
98    }
99}
100
101/// A template that the attribute input must match.
102/// Only top-level shape (`#[attr]` vs `#[attr(...)]` vs `#[attr = ...]`) is considered now.
103#[derive(Clone, Copy, Default)]
104pub struct AttributeTemplate {
105    /// If `true`, the attribute is allowed to be a bare word like `#[test]`.
106    pub word: bool,
107    /// If `Some`, the attribute is allowed to take a list of items like `#[allow(..)]`.
108    pub list: Option<&'static str>,
109    /// If non-empty, the attribute is allowed to take a list containing exactly
110    /// one of the listed words, like `#[coverage(off)]`.
111    pub one_of: &'static [Symbol],
112    /// If `Some`, the attribute is allowed to be a name/value pair where the
113    /// value is a string, like `#[must_use = "reason"]`.
114    pub name_value_str: Option<&'static str>,
115}
116
117/// How to handle multiple duplicate attributes on the same item.
118#[derive(Clone, Copy, Default)]
119pub enum AttributeDuplicates {
120    /// Duplicates of this attribute are allowed.
121    ///
122    /// This should only be used with attributes where duplicates have semantic
123    /// meaning, or some kind of "additive" behavior. For example, `#[warn(..)]`
124    /// can be specified multiple times, and it combines all the entries. Or use
125    /// this if there is validation done elsewhere.
126    #[default]
127    DuplicatesOk,
128    /// Duplicates after the first attribute will be an unused_attribute warning.
129    ///
130    /// This is usually used for "word" attributes, where they are used as a
131    /// boolean marker, like `#[used]`. It is not necessarily wrong that there
132    /// are duplicates, but the others should probably be removed.
133    WarnFollowing,
134    /// Same as `WarnFollowing`, but only issues warnings for word-style attributes.
135    ///
136    /// This is only for special cases, for example multiple `#[macro_use]` can
137    /// be warned, but multiple `#[macro_use(...)]` should not because the list
138    /// form has different meaning from the word form.
139    WarnFollowingWordOnly,
140    /// Duplicates after the first attribute will be an error.
141    ///
142    /// This should be used where duplicates would be ignored, but carry extra
143    /// meaning that could cause confusion. For example, `#[stable(since="1.0")]
144    /// #[stable(since="2.0")]`, which version should be used for `stable`?
145    ErrorFollowing,
146    /// Duplicates preceding the last instance of the attribute will be an error.
147    ///
148    /// This is the same as `ErrorFollowing`, except the last attribute is the
149    /// one that is "used". This is typically used in cases like codegen
150    /// attributes which usually only honor the last attribute.
151    ErrorPreceding,
152    /// Duplicates after the first attribute will be an unused_attribute warning
153    /// with a note that this will be an error in the future.
154    ///
155    /// This should be used for attributes that should be `ErrorFollowing`, but
156    /// because older versions of rustc silently accepted (and ignored) the
157    /// attributes, this is used to transition.
158    FutureWarnFollowing,
159    /// Duplicates preceding the last instance of the attribute will be a
160    /// warning, with a note that this will be an error in the future.
161    ///
162    /// This is the same as `FutureWarnFollowing`, except the last attribute is
163    /// the one that is "used". Ideally these can eventually migrate to
164    /// `ErrorPreceding`.
165    FutureWarnPreceding,
166}
167
168/// A convenience macro for constructing attribute templates.
169/// E.g., `template!(Word, List: "description")` means that the attribute
170/// supports forms `#[attr]` and `#[attr(description)]`.
171macro_rules! template {
172    (Word) => { template!(@ true, None, &[], None) };
173    (List: $descr: expr) => { template!(@ false, Some($descr), &[], None) };
174    (OneOf: $one_of: expr) => { template!(@ false, None, $one_of, None) };
175    (NameValueStr: $descr: expr) => { template!(@ false, None, &[], Some($descr)) };
176    (Word, List: $descr: expr) => { template!(@ true, Some($descr), &[], None) };
177    (Word, NameValueStr: $descr: expr) => { template!(@ true, None, &[], Some($descr)) };
178    (List: $descr1: expr, NameValueStr: $descr2: expr) => {
179        template!(@ false, Some($descr1), &[], Some($descr2))
180    };
181    (Word, List: $descr1: expr, NameValueStr: $descr2: expr) => {
182        template!(@ true, Some($descr1), &[], Some($descr2))
183    };
184    (@ $word: expr, $list: expr, $one_of: expr, $name_value_str: expr) => { AttributeTemplate {
185        word: $word, list: $list, one_of: $one_of, name_value_str: $name_value_str
186    } };
187}
188
189macro_rules! ungated {
190    (unsafe $attr:ident, $typ:expr, $tpl:expr, $duplicates:expr, $encode_cross_crate:expr $(,)?) => {
191        BuiltinAttribute {
192            name: sym::$attr,
193            encode_cross_crate: $encode_cross_crate,
194            type_: $typ,
195            safety: AttributeSafety::Unsafe,
196            template: $tpl,
197            gate: Ungated,
198            duplicates: $duplicates,
199        }
200    };
201    ($attr:ident, $typ:expr, $tpl:expr, $duplicates:expr, $encode_cross_crate:expr $(,)?) => {
202        BuiltinAttribute {
203            name: sym::$attr,
204            encode_cross_crate: $encode_cross_crate,
205            type_: $typ,
206            safety: AttributeSafety::Normal,
207            template: $tpl,
208            gate: Ungated,
209            duplicates: $duplicates,
210        }
211    };
212}
213
214macro_rules! gated {
215    (unsafe $attr:ident, $typ:expr, $tpl:expr, $duplicates:expr, $encode_cross_crate:expr, $gate:ident, $msg:expr $(,)?) => {
216        BuiltinAttribute {
217            name: sym::$attr,
218            encode_cross_crate: $encode_cross_crate,
219            type_: $typ,
220            safety: AttributeSafety::Unsafe,
221            template: $tpl,
222            duplicates: $duplicates,
223            gate: Gated(Stability::Unstable, sym::$gate, $msg, Features::$gate),
224        }
225    };
226    (unsafe $attr:ident, $typ:expr, $tpl:expr, $duplicates:expr, $encode_cross_crate:expr, $msg:expr $(,)?) => {
227        BuiltinAttribute {
228            name: sym::$attr,
229            encode_cross_crate: $encode_cross_crate,
230            type_: $typ,
231            safety: AttributeSafety::Unsafe,
232            template: $tpl,
233            duplicates: $duplicates,
234            gate: Gated(Stability::Unstable, sym::$attr, $msg, Features::$attr),
235        }
236    };
237    ($attr:ident, $typ:expr, $tpl:expr, $duplicates:expr, $encode_cross_crate:expr, $gate:ident, $msg:expr $(,)?) => {
238        BuiltinAttribute {
239            name: sym::$attr,
240            encode_cross_crate: $encode_cross_crate,
241            type_: $typ,
242            safety: AttributeSafety::Normal,
243            template: $tpl,
244            duplicates: $duplicates,
245            gate: Gated(Stability::Unstable, sym::$gate, $msg, Features::$gate),
246        }
247    };
248    ($attr:ident, $typ:expr, $tpl:expr, $duplicates:expr, $encode_cross_crate:expr, $msg:expr $(,)?) => {
249        BuiltinAttribute {
250            name: sym::$attr,
251            encode_cross_crate: $encode_cross_crate,
252            type_: $typ,
253            safety: AttributeSafety::Normal,
254            template: $tpl,
255            duplicates: $duplicates,
256            gate: Gated(Stability::Unstable, sym::$attr, $msg, Features::$attr),
257        }
258    };
259}
260
261macro_rules! rustc_attr {
262    (TEST, $attr:ident, $typ:expr, $tpl:expr, $duplicate:expr, $encode_cross_crate:expr $(,)?) => {
263        rustc_attr!(
264            $attr,
265            $typ,
266            $tpl,
267            $duplicate,
268            $encode_cross_crate,
269            concat!(
270                "the `#[",
271                stringify!($attr),
272                "]` attribute is just used for rustc unit tests \
273                and will never be stable",
274            ),
275        )
276    };
277    ($attr:ident, $typ:expr, $tpl:expr, $duplicates:expr, $encode_cross_crate:expr, $msg:expr $(,)?) => {
278        BuiltinAttribute {
279            name: sym::$attr,
280            encode_cross_crate: $encode_cross_crate,
281            type_: $typ,
282            safety: AttributeSafety::Normal,
283            template: $tpl,
284            duplicates: $duplicates,
285            gate: Gated(Stability::Unstable, sym::rustc_attrs, $msg, Features::rustc_attrs),
286        }
287    };
288}
289
290macro_rules! experimental {
291    ($attr:ident) => {
292        concat!("the `#[", stringify!($attr), "]` attribute is an experimental feature")
293    };
294}
295
296const IMPL_DETAIL: &str = "internal implementation detail";
297const INTERNAL_UNSTABLE: &str = "this is an internal attribute that will never be stable";
298
299#[derive(PartialEq)]
300pub enum EncodeCrossCrate {
301    Yes,
302    No,
303}
304
305pub struct BuiltinAttribute {
306    pub name: Symbol,
307    /// Whether this attribute is encode cross crate.
308    ///
309    /// If so, it is encoded in the crate metadata.
310    /// Otherwise, it can only be used in the local crate.
311    pub encode_cross_crate: EncodeCrossCrate,
312    pub type_: AttributeType,
313    pub safety: AttributeSafety,
314    pub template: AttributeTemplate,
315    pub duplicates: AttributeDuplicates,
316    pub gate: AttributeGate,
317}
318
319/// Attributes that have a special meaning to rustc or rustdoc.
320#[rustfmt::skip]
321pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
322    // ==========================================================================
323    // Stable attributes:
324    // ==========================================================================
325
326    // Conditional compilation:
327    ungated!(cfg, Normal, template!(List: "predicate"), DuplicatesOk, EncodeCrossCrate::Yes),
328    ungated!(cfg_attr, Normal, template!(List: "predicate, attr1, attr2, ..."), DuplicatesOk, EncodeCrossCrate::Yes),
329
330    // Testing:
331    ungated!(
332        ignore, Normal, template!(Word, NameValueStr: "reason"), WarnFollowing,
333        EncodeCrossCrate::No,
334    ),
335    ungated!(
336        should_panic, Normal,
337        template!(Word, List: r#"expected = "reason""#, NameValueStr: "reason"), FutureWarnFollowing,
338        EncodeCrossCrate::No,
339    ),
340    // FIXME(Centril): This can be used on stable but shouldn't.
341    ungated!(
342        reexport_test_harness_main, CrateLevel, template!(NameValueStr: "name"), ErrorFollowing,
343        EncodeCrossCrate::No,
344    ),
345
346    // Macros:
347    ungated!(automatically_derived, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::Yes),
348    ungated!(
349        macro_use, Normal, template!(Word, List: "name1, name2, ..."), WarnFollowingWordOnly,
350        EncodeCrossCrate::No,
351    ),
352    ungated!(macro_escape, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::No), // Deprecated synonym for `macro_use`.
353    ungated!(
354        macro_export, Normal, template!(Word, List: "local_inner_macros"),
355        WarnFollowing, EncodeCrossCrate::Yes
356    ),
357    ungated!(proc_macro, Normal, template!(Word), ErrorFollowing, EncodeCrossCrate::No),
358    ungated!(
359        proc_macro_derive, Normal, template!(List: "TraitName, /*opt*/ attributes(name1, name2, ...)"),
360        ErrorFollowing, EncodeCrossCrate::No,
361    ),
362    ungated!(proc_macro_attribute, Normal, template!(Word), ErrorFollowing, EncodeCrossCrate::No),
363
364    // Lints:
365    ungated!(
366        warn, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#),
367        DuplicatesOk, EncodeCrossCrate::No,
368    ),
369    ungated!(
370        allow, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#),
371        DuplicatesOk, EncodeCrossCrate::No,
372    ),
373    ungated!(
374        expect, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#),
375        DuplicatesOk, EncodeCrossCrate::No,
376    ),
377    ungated!(
378        forbid, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#),
379        DuplicatesOk, EncodeCrossCrate::No
380    ),
381    ungated!(
382        deny, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#),
383        DuplicatesOk, EncodeCrossCrate::No
384    ),
385    ungated!(
386        must_use, Normal, template!(Word, NameValueStr: "reason"),
387        FutureWarnFollowing, EncodeCrossCrate::Yes
388    ),
389    gated!(
390        must_not_suspend, Normal, template!(Word, NameValueStr: "reason"), WarnFollowing,
391        EncodeCrossCrate::Yes, experimental!(must_not_suspend)
392    ),
393    ungated!(
394        deprecated, Normal,
395        template!(
396            Word,
397            List: r#"/*opt*/ since = "version", /*opt*/ note = "reason""#,
398            NameValueStr: "reason"
399        ),
400        ErrorFollowing, EncodeCrossCrate::Yes
401    ),
402
403    // Crate properties:
404    ungated!(
405        crate_name, CrateLevel, template!(NameValueStr: "name"), FutureWarnFollowing,
406        EncodeCrossCrate::No,
407    ),
408    ungated!(
409        crate_type, CrateLevel, template!(NameValueStr: "bin|lib|..."), DuplicatesOk,
410        EncodeCrossCrate::No,
411    ),
412
413    // ABI, linking, symbols, and FFI
414    ungated!(
415        link, Normal,
416        template!(List: r#"name = "...", /*opt*/ kind = "dylib|static|...", /*opt*/ wasm_import_module = "...", /*opt*/ import_name_type = "decorated|noprefix|undecorated""#),
417        DuplicatesOk,
418        EncodeCrossCrate::No,
419    ),
420    ungated!(
421        link_name, Normal, template!(NameValueStr: "name"),
422        FutureWarnPreceding, EncodeCrossCrate::Yes
423    ),
424    ungated!(no_link, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::No),
425    ungated!(repr, Normal, template!(List: "C"), DuplicatesOk, EncodeCrossCrate::No),
426    ungated!(unsafe export_name, Normal, template!(NameValueStr: "name"), FutureWarnPreceding, EncodeCrossCrate::No),
427    ungated!(unsafe link_section, Normal, template!(NameValueStr: "name"), FutureWarnPreceding, EncodeCrossCrate::No),
428    ungated!(unsafe no_mangle, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::No),
429    ungated!(used, Normal, template!(Word, List: "compiler|linker"), WarnFollowing, EncodeCrossCrate::No),
430    ungated!(link_ordinal, Normal, template!(List: "ordinal"), ErrorPreceding, EncodeCrossCrate::Yes),
431
432    // Limits:
433    ungated!(
434        recursion_limit, CrateLevel, template!(NameValueStr: "N"), FutureWarnFollowing,
435        EncodeCrossCrate::No
436    ),
437    ungated!(
438        type_length_limit, CrateLevel, template!(NameValueStr: "N"), FutureWarnFollowing,
439        EncodeCrossCrate::No
440    ),
441    gated!(
442        move_size_limit, CrateLevel, template!(NameValueStr: "N"), ErrorFollowing,
443        EncodeCrossCrate::No, large_assignments, experimental!(move_size_limit)
444    ),
445
446    // Entry point:
447    ungated!(no_main, CrateLevel, template!(Word), WarnFollowing, EncodeCrossCrate::No),
448
449    // Modules, prelude, and resolution:
450    ungated!(path, Normal, template!(NameValueStr: "file"), FutureWarnFollowing, EncodeCrossCrate::No),
451    ungated!(no_std, CrateLevel, template!(Word), WarnFollowing, EncodeCrossCrate::No),
452    ungated!(no_implicit_prelude, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::No),
453    ungated!(non_exhaustive, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::Yes),
454
455    // Runtime
456    ungated!(
457        windows_subsystem, CrateLevel,
458        template!(NameValueStr: "windows|console"), FutureWarnFollowing,
459        EncodeCrossCrate::No
460    ),
461    ungated!(panic_handler, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::Yes), // RFC 2070
462
463    // Code generation:
464    ungated!(inline, Normal, template!(Word, List: "always|never"), FutureWarnFollowing, EncodeCrossCrate::No),
465    ungated!(cold, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::No),
466    ungated!(no_builtins, CrateLevel, template!(Word), WarnFollowing, EncodeCrossCrate::Yes),
467    ungated!(
468        target_feature, Normal, template!(List: r#"enable = "name""#),
469        DuplicatesOk, EncodeCrossCrate::No,
470    ),
471    ungated!(track_caller, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::Yes),
472    ungated!(instruction_set, Normal, template!(List: "set"), ErrorPreceding, EncodeCrossCrate::No),
473    gated!(
474        no_sanitize, Normal,
475        template!(List: "address, kcfi, memory, thread"), DuplicatesOk,
476        EncodeCrossCrate::No, experimental!(no_sanitize)
477    ),
478    gated!(
479        coverage, Normal, template!(OneOf: &[sym::off, sym::on]),
480        ErrorPreceding, EncodeCrossCrate::No,
481        coverage_attribute, experimental!(coverage)
482    ),
483
484    ungated!(
485        doc, Normal, template!(List: "hidden|inline|...", NameValueStr: "string"), DuplicatesOk,
486        EncodeCrossCrate::Yes
487    ),
488
489    // Debugging
490    ungated!(
491        debugger_visualizer, Normal,
492        template!(List: r#"natvis_file = "...", gdb_script_file = "...""#),
493        DuplicatesOk, EncodeCrossCrate::No
494    ),
495    ungated!(collapse_debuginfo, Normal, template!(List: "no|external|yes"), ErrorFollowing,
496        EncodeCrossCrate::Yes
497    ),
498
499    // ==========================================================================
500    // Unstable attributes:
501    // ==========================================================================
502
503    // Linking:
504    gated!(
505        naked, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::No,
506        naked_functions, experimental!(naked)
507    ),
508
509    // Testing:
510    gated!(
511        test_runner, CrateLevel, template!(List: "path"), ErrorFollowing,
512        EncodeCrossCrate::Yes, custom_test_frameworks,
513        "custom test frameworks are an unstable feature",
514    ),
515    // RFC #1268
516    gated!(
517        marker, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::No,
518        marker_trait_attr, experimental!(marker)
519    ),
520    gated!(
521        thread_local, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::No,
522        "`#[thread_local]` is an experimental feature, and does not currently handle destructors",
523    ),
524    gated!(
525        no_core, CrateLevel, template!(Word), WarnFollowing,
526        EncodeCrossCrate::No, experimental!(no_core)
527    ),
528    // RFC 2412
529    gated!(
530        optimize, Normal, template!(List: "none|size|speed"), ErrorPreceding,
531        EncodeCrossCrate::No, optimize_attribute, experimental!(optimize)
532    ),
533
534    gated!(
535        unsafe ffi_pure, Normal, template!(Word), WarnFollowing,
536        EncodeCrossCrate::No, experimental!(ffi_pure)
537    ),
538    gated!(
539        unsafe ffi_const, Normal, template!(Word), WarnFollowing,
540        EncodeCrossCrate::No, experimental!(ffi_const)
541    ),
542    gated!(
543        register_tool, CrateLevel, template!(List: "tool1, tool2, ..."), DuplicatesOk,
544        EncodeCrossCrate::No, experimental!(register_tool),
545    ),
546
547    // RFC 2632
548    gated!(
549        const_trait, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::No, const_trait_impl,
550        "`const_trait` is a temporary placeholder for marking a trait that is suitable for `const` \
551        `impls` and all default bodies as `const`, which may be removed or renamed in the \
552        future."
553    ),
554    // lang-team MCP 147
555    gated!(
556        deprecated_safe, Normal, template!(List: r#"since = "version", note = "...""#), ErrorFollowing,
557        EncodeCrossCrate::Yes, experimental!(deprecated_safe),
558    ),
559
560    // `#[cfi_encoding = ""]`
561    gated!(
562        cfi_encoding, Normal, template!(NameValueStr: "encoding"), ErrorPreceding,
563        EncodeCrossCrate::Yes, experimental!(cfi_encoding)
564    ),
565
566    // `#[coroutine]` attribute to be applied to closures to make them coroutines instead
567    gated!(
568        coroutine, Normal, template!(Word), ErrorFollowing,
569        EncodeCrossCrate::No, coroutines, experimental!(coroutine)
570    ),
571
572    // RFC 3543
573    // `#[patchable_function_entry(prefix_nops = m, entry_nops = n)]`
574    gated!(
575        patchable_function_entry, Normal, template!(List: "prefix_nops = m, entry_nops = n"), ErrorPreceding,
576        EncodeCrossCrate::Yes, experimental!(patchable_function_entry)
577    ),
578
579    // ==========================================================================
580    // Internal attributes: Stability, deprecation, and unsafe:
581    // ==========================================================================
582
583    ungated!(
584        feature, CrateLevel,
585        template!(List: "name1, name2, ..."), DuplicatesOk, EncodeCrossCrate::No,
586    ),
587    // DuplicatesOk since it has its own validation
588    ungated!(
589        stable, Normal,
590        template!(List: r#"feature = "name", since = "version""#), DuplicatesOk, EncodeCrossCrate::No,
591    ),
592    ungated!(
593        unstable, Normal,
594        template!(List: r#"feature = "name", reason = "...", issue = "N""#), DuplicatesOk,
595        EncodeCrossCrate::Yes
596    ),
597    ungated!(
598        rustc_const_unstable, Normal, template!(List: r#"feature = "name""#),
599        DuplicatesOk, EncodeCrossCrate::Yes
600    ),
601    ungated!(
602        rustc_const_stable, Normal,
603        template!(List: r#"feature = "name""#), DuplicatesOk, EncodeCrossCrate::No,
604    ),
605    ungated!(
606        rustc_default_body_unstable, Normal,
607        template!(List: r#"feature = "name", reason = "...", issue = "N""#),
608        DuplicatesOk, EncodeCrossCrate::No
609    ),
610    gated!(
611        allow_internal_unstable, Normal, template!(Word, List: "feat1, feat2, ..."),
612        DuplicatesOk, EncodeCrossCrate::Yes,
613        "allow_internal_unstable side-steps feature gating and stability checks",
614    ),
615    gated!(
616        allow_internal_unsafe, Normal, template!(Word), WarnFollowing,
617        EncodeCrossCrate::No, "allow_internal_unsafe side-steps the unsafe_code lint",
618    ),
619    rustc_attr!(
620        rustc_allowed_through_unstable_modules, Normal, template!(NameValueStr: "deprecation message"),
621        WarnFollowing, EncodeCrossCrate::No,
622        "rustc_allowed_through_unstable_modules special cases accidental stabilizations of stable items \
623        through unstable paths"
624    ),
625    rustc_attr!(
626        rustc_deprecated_safe_2024, Normal, template!(List: r#"audit_that = "...""#),
627        ErrorFollowing, EncodeCrossCrate::Yes,
628        "rustc_deprecated_safe_2024 is supposed to be used in libstd only",
629    ),
630    rustc_attr!(
631        rustc_pub_transparent, Normal, template!(Word),
632        WarnFollowing, EncodeCrossCrate::Yes,
633        "used internally to mark types with a `transparent` representation when it is guaranteed by the documentation",
634    ),
635
636
637    // ==========================================================================
638    // Internal attributes: Type system related:
639    // ==========================================================================
640
641    gated!(fundamental, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::Yes, experimental!(fundamental)),
642    gated!(
643        may_dangle, Normal, template!(Word), WarnFollowing,
644        EncodeCrossCrate::No, dropck_eyepatch,
645        "`may_dangle` has unstable semantics and may be removed in the future",
646    ),
647
648    rustc_attr!(
649        rustc_never_type_options,
650        Normal,
651        template!(List: r#"/*opt*/ fallback = "unit|niko|never|no""#),
652        ErrorFollowing,
653        EncodeCrossCrate::No,
654        "`rustc_never_type_options` is used to experiment with never type fallback and work on \
655         never type stabilization, and will never be stable"
656    ),
657
658    // ==========================================================================
659    // Internal attributes: Runtime related:
660    // ==========================================================================
661
662    rustc_attr!(
663        rustc_allocator, Normal, template!(Word), WarnFollowing,
664        EncodeCrossCrate::No, IMPL_DETAIL
665    ),
666    rustc_attr!(
667        rustc_nounwind, Normal, template!(Word), WarnFollowing,
668        EncodeCrossCrate::No, IMPL_DETAIL
669    ),
670    rustc_attr!(
671        rustc_reallocator, Normal, template!(Word), WarnFollowing,
672        EncodeCrossCrate::No, IMPL_DETAIL
673    ),
674    rustc_attr!(
675        rustc_deallocator, Normal, template!(Word), WarnFollowing,
676        EncodeCrossCrate::No, IMPL_DETAIL
677    ),
678    rustc_attr!(
679        rustc_allocator_zeroed, Normal, template!(Word), WarnFollowing,
680        EncodeCrossCrate::No, IMPL_DETAIL
681    ),
682    gated!(
683        default_lib_allocator, Normal, template!(Word), WarnFollowing,
684        EncodeCrossCrate::No, allocator_internals, experimental!(default_lib_allocator),
685    ),
686    gated!(
687        needs_allocator, Normal, template!(Word), WarnFollowing,
688        EncodeCrossCrate::No, allocator_internals, experimental!(needs_allocator),
689    ),
690    gated!(
691        panic_runtime, CrateLevel, template!(Word), WarnFollowing,
692        EncodeCrossCrate::No, experimental!(panic_runtime)
693    ),
694    gated!(
695        needs_panic_runtime, CrateLevel, template!(Word), WarnFollowing,
696        EncodeCrossCrate::No, experimental!(needs_panic_runtime)
697    ),
698    gated!(
699        compiler_builtins, CrateLevel, template!(Word), WarnFollowing,
700        EncodeCrossCrate::No,
701        "the `#[compiler_builtins]` attribute is used to identify the `compiler_builtins` crate \
702        which contains compiler-rt intrinsics and will never be stable",
703    ),
704    gated!(
705        profiler_runtime, CrateLevel, template!(Word), WarnFollowing,
706        EncodeCrossCrate::No,
707        "the `#[profiler_runtime]` attribute is used to identify the `profiler_builtins` crate \
708        which contains the profiler runtime and will never be stable",
709    ),
710
711    // ==========================================================================
712    // Internal attributes, Linkage:
713    // ==========================================================================
714
715    gated!(
716        linkage, Normal, template!(NameValueStr: "external|internal|..."),
717        ErrorPreceding, EncodeCrossCrate::No,
718        "the `linkage` attribute is experimental and not portable across platforms",
719    ),
720    rustc_attr!(
721        rustc_std_internal_symbol, Normal, template!(Word), WarnFollowing,
722        EncodeCrossCrate::No, INTERNAL_UNSTABLE
723    ),
724
725    // ==========================================================================
726    // Internal attributes, Macro related:
727    // ==========================================================================
728
729    rustc_attr!(
730        rustc_builtin_macro, Normal,
731        template!(Word, List: "name, /*opt*/ attributes(name1, name2, ...)"), ErrorFollowing,
732        EncodeCrossCrate::Yes, IMPL_DETAIL
733    ),
734    rustc_attr!(
735        rustc_proc_macro_decls, Normal, template!(Word), WarnFollowing,
736        EncodeCrossCrate::No, INTERNAL_UNSTABLE
737    ),
738    rustc_attr!(
739        rustc_macro_transparency, Normal,
740        template!(NameValueStr: "transparent|semitransparent|opaque"), ErrorFollowing,
741        EncodeCrossCrate::Yes, "used internally for testing macro hygiene",
742    ),
743    rustc_attr!(
744        rustc_autodiff, Normal,
745        template!(Word, List: r#""...""#), DuplicatesOk,
746        EncodeCrossCrate::No, INTERNAL_UNSTABLE
747    ),
748
749    // ==========================================================================
750    // Internal attributes, Diagnostics related:
751    // ==========================================================================
752
753    rustc_attr!(
754        rustc_on_unimplemented, Normal,
755        template!(
756            List: r#"/*opt*/ message = "...", /*opt*/ label = "...", /*opt*/ note = "...""#,
757            NameValueStr: "message"
758        ),
759        ErrorFollowing, EncodeCrossCrate::Yes,
760        INTERNAL_UNSTABLE
761    ),
762    rustc_attr!(
763        rustc_confusables, Normal,
764        template!(List: r#""name1", "name2", ..."#),
765        ErrorFollowing, EncodeCrossCrate::Yes,
766        INTERNAL_UNSTABLE,
767    ),
768    // Enumerates "identity-like" conversion methods to suggest on type mismatch.
769    rustc_attr!(
770        rustc_conversion_suggestion, Normal, template!(Word),
771        WarnFollowing, EncodeCrossCrate::Yes, INTERNAL_UNSTABLE
772    ),
773    // Prevents field reads in the marked trait or method to be considered
774    // during dead code analysis.
775    rustc_attr!(
776        rustc_trivial_field_reads, Normal, template!(Word),
777        WarnFollowing, EncodeCrossCrate::Yes, INTERNAL_UNSTABLE
778    ),
779    // Used by the `rustc::potential_query_instability` lint to warn methods which
780    // might not be stable during incremental compilation.
781    rustc_attr!(
782        rustc_lint_query_instability, Normal, template!(Word),
783        WarnFollowing, EncodeCrossCrate::Yes, INTERNAL_UNSTABLE
784    ),
785    // Used by the `rustc::untracked_query_information` lint to warn methods which
786    // might not be stable during incremental compilation.
787    rustc_attr!(
788        rustc_lint_untracked_query_information, Normal, template!(Word),
789        WarnFollowing, EncodeCrossCrate::Yes, INTERNAL_UNSTABLE
790    ),
791    // Used by the `rustc::diagnostic_outside_of_impl` lints to assist in changes to diagnostic
792    // APIs. Any function with this attribute will be checked by that lint.
793    rustc_attr!(
794        rustc_lint_diagnostics, Normal, template!(Word),
795        WarnFollowing, EncodeCrossCrate::Yes, INTERNAL_UNSTABLE
796    ),
797    // Used by the `rustc::bad_opt_access` lint to identify `DebuggingOptions` and `CodegenOptions`
798    // types (as well as any others in future).
799    rustc_attr!(
800        rustc_lint_opt_ty, Normal, template!(Word),
801        WarnFollowing, EncodeCrossCrate::Yes, INTERNAL_UNSTABLE
802    ),
803    // Used by the `rustc::bad_opt_access` lint on fields
804    // types (as well as any others in future).
805    rustc_attr!(
806        rustc_lint_opt_deny_field_access, Normal, template!(List: "message"),
807        WarnFollowing, EncodeCrossCrate::Yes, INTERNAL_UNSTABLE
808    ),
809
810    // ==========================================================================
811    // Internal attributes, Const related:
812    // ==========================================================================
813
814    rustc_attr!(
815        rustc_promotable, Normal, template!(Word), WarnFollowing,
816        EncodeCrossCrate::No, IMPL_DETAIL),
817    rustc_attr!(
818        rustc_legacy_const_generics, Normal, template!(List: "N"), ErrorFollowing,
819        EncodeCrossCrate::Yes, INTERNAL_UNSTABLE
820    ),
821    // Do not const-check this function's body. It will always get replaced during CTFE.
822    rustc_attr!(
823        rustc_do_not_const_check, Normal, template!(Word), WarnFollowing,
824        EncodeCrossCrate::Yes, INTERNAL_UNSTABLE
825    ),
826    // Ensure the argument to this function is &&str during const-check.
827    rustc_attr!(
828        rustc_const_panic_str, Normal, template!(Word), WarnFollowing,
829        EncodeCrossCrate::Yes, INTERNAL_UNSTABLE
830    ),
831    rustc_attr!(
832        rustc_const_stable_indirect, Normal,
833        template!(Word), WarnFollowing, EncodeCrossCrate::No, IMPL_DETAIL,
834    ),
835    rustc_attr!(
836        rustc_intrinsic_const_stable_indirect, Normal,
837        template!(Word), WarnFollowing, EncodeCrossCrate::No, IMPL_DETAIL,
838    ),
839    gated!(
840        rustc_allow_const_fn_unstable, Normal,
841        template!(Word, List: "feat1, feat2, ..."), DuplicatesOk, EncodeCrossCrate::No,
842        "rustc_allow_const_fn_unstable side-steps feature gating and stability checks"
843    ),
844
845    // ==========================================================================
846    // Internal attributes, Layout related:
847    // ==========================================================================
848
849    rustc_attr!(
850        rustc_layout_scalar_valid_range_start, Normal, template!(List: "value"), ErrorFollowing,
851        EncodeCrossCrate::Yes,
852        "the `#[rustc_layout_scalar_valid_range_start]` attribute is just used to enable \
853        niche optimizations in libcore and libstd and will never be stable",
854    ),
855    rustc_attr!(
856        rustc_layout_scalar_valid_range_end, Normal, template!(List: "value"), ErrorFollowing,
857        EncodeCrossCrate::Yes,
858        "the `#[rustc_layout_scalar_valid_range_end]` attribute is just used to enable \
859        niche optimizations in libcore and libstd and will never be stable",
860    ),
861    rustc_attr!(
862        rustc_nonnull_optimization_guaranteed, Normal, template!(Word), WarnFollowing,
863        EncodeCrossCrate::Yes,
864        "the `#[rustc_nonnull_optimization_guaranteed]` attribute is just used to document \
865        guaranteed niche optimizations in libcore and libstd and will never be stable\n\
866        (note that the compiler does not even check whether the type indeed is being non-null-optimized; \
867        it is your responsibility to ensure that the attribute is only used on types that are optimized)",
868    ),
869
870    // ==========================================================================
871    // Internal attributes, Misc:
872    // ==========================================================================
873    gated!(
874        lang, Normal, template!(NameValueStr: "name"), DuplicatesOk, EncodeCrossCrate::No, lang_items,
875        "lang items are subject to change",
876    ),
877    rustc_attr!(
878        rustc_as_ptr, Normal, template!(Word), ErrorFollowing,
879        EncodeCrossCrate::Yes,
880        "#[rustc_as_ptr] is used to mark functions returning pointers to their inner allocations."
881    ),
882    rustc_attr!(
883        rustc_pass_by_value, Normal, template!(Word), ErrorFollowing,
884        EncodeCrossCrate::Yes,
885        "#[rustc_pass_by_value] is used to mark types that must be passed by value instead of reference."
886    ),
887    rustc_attr!(
888        rustc_never_returns_null_ptr, Normal, template!(Word), ErrorFollowing,
889        EncodeCrossCrate::Yes,
890        "#[rustc_never_returns_null_ptr] is used to mark functions returning non-null pointers."
891    ),
892    rustc_attr!(
893        rustc_coherence_is_core, AttributeType::CrateLevel, template!(Word), ErrorFollowing, EncodeCrossCrate::No,
894        "#![rustc_coherence_is_core] allows inherent methods on builtin types, only intended to be used in `core`."
895    ),
896    rustc_attr!(
897        rustc_coinductive, AttributeType::Normal, template!(Word), WarnFollowing, EncodeCrossCrate::No,
898        "#![rustc_coinductive] changes a trait to be coinductive, allowing cycles in the trait solver."
899    ),
900    rustc_attr!(
901        rustc_allow_incoherent_impl, AttributeType::Normal, template!(Word), ErrorFollowing, EncodeCrossCrate::No,
902        "#[rustc_allow_incoherent_impl] has to be added to all impl items of an incoherent inherent impl."
903    ),
904    rustc_attr!(
905        rustc_preserve_ub_checks, AttributeType::CrateLevel, template!(Word), ErrorFollowing, EncodeCrossCrate::No,
906        "`#![rustc_preserve_ub_checks]` prevents the designated crate from evaluating whether UB checks are enabled when optimizing MIR",
907    ),
908    rustc_attr!(
909        rustc_deny_explicit_impl,
910        AttributeType::Normal,
911        template!(Word),
912        ErrorFollowing,
913        EncodeCrossCrate::No,
914        "#[rustc_deny_explicit_impl] enforces that a trait can have no user-provided impls"
915    ),
916    rustc_attr!(
917        rustc_do_not_implement_via_object,
918        AttributeType::Normal,
919        template!(Word),
920        ErrorFollowing,
921        EncodeCrossCrate::No,
922        "#[rustc_do_not_implement_via_object] opts out of the automatic trait impl for trait objects \
923        (`impl Trait for dyn Trait`)"
924    ),
925    rustc_attr!(
926        rustc_has_incoherent_inherent_impls, AttributeType::Normal, template!(Word),
927        ErrorFollowing, EncodeCrossCrate::Yes,
928        "#[rustc_has_incoherent_inherent_impls] allows the addition of incoherent inherent impls for \
929         the given type by annotating all impl items with #[rustc_allow_incoherent_impl]."
930    ),
931
932    BuiltinAttribute {
933        name: sym::rustc_diagnostic_item,
934        // FIXME: This can be `true` once we always use `tcx.is_diagnostic_item`.
935        encode_cross_crate: EncodeCrossCrate::Yes,
936        type_: Normal,
937        safety: AttributeSafety::Normal,
938        template: template!(NameValueStr: "name"),
939        duplicates: ErrorFollowing,
940        gate: Gated(
941            Stability::Unstable,
942            sym::rustc_attrs,
943            "diagnostic items compiler internal support for linting",
944            Features::rustc_attrs,
945        ),
946    },
947    gated!(
948        // Used in resolve:
949        prelude_import, Normal, template!(Word), WarnFollowing,
950        EncodeCrossCrate::No, "`#[prelude_import]` is for use by rustc only",
951    ),
952    gated!(
953        rustc_paren_sugar, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::No,
954        unboxed_closures, "unboxed_closures are still evolving",
955    ),
956    rustc_attr!(
957        rustc_inherit_overflow_checks, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::No,
958        "the `#[rustc_inherit_overflow_checks]` attribute is just used to control \
959        overflow checking behavior of several libcore functions that are inlined \
960        across crates and will never be stable",
961    ),
962    rustc_attr!(
963        rustc_reservation_impl, Normal,
964        template!(NameValueStr: "reservation message"), ErrorFollowing, EncodeCrossCrate::Yes,
965        "the `#[rustc_reservation_impl]` attribute is internally used \
966         for reserving for `for<T> From<!> for T` impl"
967    ),
968    rustc_attr!(
969        rustc_test_marker, Normal, template!(NameValueStr: "name"), WarnFollowing,
970        EncodeCrossCrate::No, "the `#[rustc_test_marker]` attribute is used internally to track tests",
971    ),
972    rustc_attr!(
973        rustc_unsafe_specialization_marker, Normal, template!(Word),
974        WarnFollowing, EncodeCrossCrate::No,
975        "the `#[rustc_unsafe_specialization_marker]` attribute is used to check specializations"
976    ),
977    rustc_attr!(
978        rustc_specialization_trait, Normal, template!(Word),
979        WarnFollowing, EncodeCrossCrate::No,
980        "the `#[rustc_specialization_trait]` attribute is used to check specializations"
981    ),
982    rustc_attr!(
983        rustc_main, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::No,
984        "the `#[rustc_main]` attribute is used internally to specify test entry point function",
985    ),
986    rustc_attr!(
987        rustc_skip_during_method_dispatch, Normal, template!(List: "array, boxed_slice"), WarnFollowing,
988        EncodeCrossCrate::No,
989        "the `#[rustc_skip_during_method_dispatch]` attribute is used to exclude a trait \
990        from method dispatch when the receiver is of the following type, for compatibility in \
991        editions < 2021 (array) or editions < 2024 (boxed_slice)."
992    ),
993    rustc_attr!(
994        rustc_must_implement_one_of, Normal, template!(List: "function1, function2, ..."),
995        ErrorFollowing, EncodeCrossCrate::No,
996        "the `#[rustc_must_implement_one_of]` attribute is used to change minimal complete \
997        definition of a trait, it's currently in experimental form and should be changed before \
998        being exposed outside of the std"
999    ),
1000    rustc_attr!(
1001        rustc_doc_primitive, Normal, template!(NameValueStr: "primitive name"), ErrorFollowing,
1002        EncodeCrossCrate::Yes, r#"`rustc_doc_primitive` is a rustc internal attribute"#,
1003    ),
1004    gated!(
1005        rustc_intrinsic, Normal, template!(Word), ErrorFollowing, EncodeCrossCrate::Yes, intrinsics,
1006        "the `#[rustc_intrinsic]` attribute is used to declare intrinsics as function items",
1007    ),
1008    gated!(
1009        rustc_intrinsic_must_be_overridden, Normal, template!(Word), ErrorFollowing, EncodeCrossCrate::Yes, intrinsics,
1010        "the `#[rustc_intrinsic_must_be_overridden]` attribute is used to declare intrinsics without real bodies",
1011    ),
1012    rustc_attr!(
1013        rustc_no_mir_inline, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::Yes,
1014        "#[rustc_no_mir_inline] prevents the MIR inliner from inlining a function while not affecting codegen"
1015    ),
1016    rustc_attr!(
1017        rustc_force_inline, Normal, template!(Word, NameValueStr: "reason"), WarnFollowing, EncodeCrossCrate::Yes,
1018        "#![rustc_force_inline] forces a free function to be inlined"
1019    ),
1020
1021    // ==========================================================================
1022    // Internal attributes, Testing:
1023    // ==========================================================================
1024
1025    rustc_attr!(TEST, rustc_effective_visibility, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::Yes),
1026    rustc_attr!(
1027        TEST, rustc_outlives, Normal, template!(Word),
1028        WarnFollowing, EncodeCrossCrate::No
1029    ),
1030    rustc_attr!(
1031        TEST, rustc_capture_analysis, Normal, template!(Word),
1032        WarnFollowing, EncodeCrossCrate::No
1033    ),
1034    rustc_attr!(
1035        TEST, rustc_insignificant_dtor, Normal, template!(Word),
1036        WarnFollowing, EncodeCrossCrate::Yes
1037    ),
1038    rustc_attr!(
1039        TEST, rustc_strict_coherence, Normal, template!(Word),
1040        WarnFollowing, EncodeCrossCrate::Yes
1041    ),
1042    rustc_attr!(
1043        TEST, rustc_variance, Normal, template!(Word),
1044        WarnFollowing, EncodeCrossCrate::No
1045    ),
1046    rustc_attr!(
1047        TEST, rustc_variance_of_opaques, Normal, template!(Word),
1048        WarnFollowing, EncodeCrossCrate::No
1049    ),
1050    rustc_attr!(
1051        TEST, rustc_hidden_type_of_opaques, Normal, template!(Word),
1052        WarnFollowing, EncodeCrossCrate::No
1053    ),
1054    rustc_attr!(
1055        TEST, rustc_layout, Normal, template!(List: "field1, field2, ..."),
1056        WarnFollowing, EncodeCrossCrate::Yes
1057    ),
1058    rustc_attr!(
1059        TEST, rustc_abi, Normal, template!(List: "field1, field2, ..."),
1060        WarnFollowing, EncodeCrossCrate::No
1061    ),
1062    rustc_attr!(
1063        TEST, rustc_regions, Normal, template!(Word),
1064        WarnFollowing, EncodeCrossCrate::No
1065    ),
1066    rustc_attr!(
1067        TEST, rustc_error, Normal,
1068        template!(Word, List: "delayed_bug_from_inside_query"),
1069        WarnFollowingWordOnly, EncodeCrossCrate::Yes
1070    ),
1071    rustc_attr!(
1072        TEST, rustc_dump_user_args, Normal, template!(Word),
1073        WarnFollowing, EncodeCrossCrate::No
1074    ),
1075    rustc_attr!(
1076        TEST, rustc_evaluate_where_clauses, Normal, template!(Word), WarnFollowing,
1077        EncodeCrossCrate::Yes
1078    ),
1079    rustc_attr!(
1080        TEST, rustc_if_this_changed, Normal, template!(Word, List: "DepNode"), DuplicatesOk,
1081        EncodeCrossCrate::No
1082    ),
1083    rustc_attr!(
1084        TEST, rustc_then_this_would_need, Normal, template!(List: "DepNode"), DuplicatesOk,
1085        EncodeCrossCrate::No
1086    ),
1087    rustc_attr!(
1088        TEST, rustc_clean, Normal,
1089        template!(List: r#"cfg = "...", /*opt*/ label = "...", /*opt*/ except = "...""#),
1090        DuplicatesOk, EncodeCrossCrate::No
1091    ),
1092    rustc_attr!(
1093        TEST, rustc_partition_reused, Normal,
1094        template!(List: r#"cfg = "...", module = "...""#), DuplicatesOk, EncodeCrossCrate::No
1095    ),
1096    rustc_attr!(
1097        TEST, rustc_partition_codegened, Normal,
1098        template!(List: r#"cfg = "...", module = "...""#), DuplicatesOk, EncodeCrossCrate::No
1099    ),
1100    rustc_attr!(
1101        TEST, rustc_expected_cgu_reuse, Normal,
1102        template!(List: r#"cfg = "...", module = "...", kind = "...""#), DuplicatesOk,
1103        EncodeCrossCrate::No
1104    ),
1105    rustc_attr!(
1106        TEST, rustc_symbol_name, Normal, template!(Word),
1107        WarnFollowing, EncodeCrossCrate::No
1108    ),
1109    rustc_attr!(
1110        TEST, rustc_def_path, Normal, template!(Word),
1111        WarnFollowing, EncodeCrossCrate::No
1112    ),
1113    rustc_attr!(
1114        TEST, rustc_mir, Normal, template!(List: "arg1, arg2, ..."),
1115        DuplicatesOk, EncodeCrossCrate::Yes
1116    ),
1117    gated!(
1118        custom_mir, Normal, template!(List: r#"dialect = "...", phase = "...""#),
1119        ErrorFollowing, EncodeCrossCrate::No,
1120        "the `#[custom_mir]` attribute is just used for the Rust test suite",
1121    ),
1122    rustc_attr!(
1123        TEST, rustc_dump_item_bounds, Normal, template!(Word),
1124        WarnFollowing, EncodeCrossCrate::No
1125    ),
1126    rustc_attr!(
1127        TEST, rustc_dump_predicates, Normal, template!(Word),
1128        WarnFollowing, EncodeCrossCrate::No
1129    ),
1130    rustc_attr!(
1131        TEST, rustc_dump_def_parents, Normal, template!(Word),
1132        WarnFollowing, EncodeCrossCrate::No
1133    ),
1134    rustc_attr!(
1135        TEST, rustc_object_lifetime_default, Normal, template!(Word),
1136        WarnFollowing, EncodeCrossCrate::No
1137    ),
1138    rustc_attr!(
1139        TEST, rustc_dump_vtable, Normal, template!(Word),
1140        WarnFollowing, EncodeCrossCrate::No
1141    ),
1142    rustc_attr!(
1143        TEST, rustc_dummy, Normal, template!(Word /* doesn't matter*/),
1144        DuplicatesOk, EncodeCrossCrate::No
1145    ),
1146    gated!(
1147        omit_gdb_pretty_printer_section, Normal, template!(Word),
1148        WarnFollowing, EncodeCrossCrate::No,
1149        "the `#[omit_gdb_pretty_printer_section]` attribute is just used for the Rust test suite",
1150    ),
1151    rustc_attr!(
1152        TEST, pattern_complexity, CrateLevel, template!(NameValueStr: "N"),
1153        ErrorFollowing, EncodeCrossCrate::No,
1154    ),
1155];
1156
1157pub fn deprecated_attributes() -> Vec<&'static BuiltinAttribute> {
1158    BUILTIN_ATTRIBUTES.iter().filter(|attr| attr.gate.is_deprecated()).collect()
1159}
1160
1161pub fn is_builtin_attr_name(name: Symbol) -> bool {
1162    BUILTIN_ATTRIBUTE_MAP.get(&name).is_some()
1163}
1164
1165/// Whether this builtin attribute is encoded cross crate.
1166/// This means it can be used cross crate.
1167pub fn encode_cross_crate(name: Symbol) -> bool {
1168    if let Some(attr) = BUILTIN_ATTRIBUTE_MAP.get(&name) {
1169        attr.encode_cross_crate == EncodeCrossCrate::Yes
1170    } else {
1171        true
1172    }
1173}
1174
1175pub fn is_valid_for_get_attr(name: Symbol) -> bool {
1176    BUILTIN_ATTRIBUTE_MAP.get(&name).is_some_and(|attr| match attr.duplicates {
1177        WarnFollowing | ErrorFollowing | ErrorPreceding | FutureWarnFollowing
1178        | FutureWarnPreceding => true,
1179        DuplicatesOk | WarnFollowingWordOnly => false,
1180    })
1181}
1182
1183pub static BUILTIN_ATTRIBUTE_MAP: LazyLock<FxHashMap<Symbol, &BuiltinAttribute>> =
1184    LazyLock::new(|| {
1185        let mut map = FxHashMap::default();
1186        for attr in BUILTIN_ATTRIBUTES.iter() {
1187            if map.insert(attr.name, attr).is_some() {
1188                panic!("duplicate builtin attribute `{}`", attr.name);
1189            }
1190        }
1191        map
1192    });
1193
1194pub fn is_stable_diagnostic_attribute(sym: Symbol, _features: &Features) -> bool {
1195    match sym {
1196        sym::on_unimplemented | sym::do_not_recommend => true,
1197        _ => false,
1198    }
1199}