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    // Probably temporary component of min_generic_const_args.
580    // `#[type_const] const ASSOC: usize;`
581    gated!(
582        type_const, Normal, template!(Word), ErrorFollowing,
583        EncodeCrossCrate::Yes, min_generic_const_args, experimental!(type_const),
584    ),
585
586    // ==========================================================================
587    // Internal attributes: Stability, deprecation, and unsafe:
588    // ==========================================================================
589
590    ungated!(
591        feature, CrateLevel,
592        template!(List: "name1, name2, ..."), DuplicatesOk, EncodeCrossCrate::No,
593    ),
594    // DuplicatesOk since it has its own validation
595    ungated!(
596        stable, Normal,
597        template!(List: r#"feature = "name", since = "version""#), DuplicatesOk, EncodeCrossCrate::No,
598    ),
599    ungated!(
600        unstable, Normal,
601        template!(List: r#"feature = "name", reason = "...", issue = "N""#), DuplicatesOk,
602        EncodeCrossCrate::Yes
603    ),
604    ungated!(
605        rustc_const_unstable, Normal, template!(List: r#"feature = "name""#),
606        DuplicatesOk, EncodeCrossCrate::Yes
607    ),
608    ungated!(
609        rustc_const_stable, Normal,
610        template!(List: r#"feature = "name""#), DuplicatesOk, EncodeCrossCrate::No,
611    ),
612    ungated!(
613        rustc_default_body_unstable, Normal,
614        template!(List: r#"feature = "name", reason = "...", issue = "N""#),
615        DuplicatesOk, EncodeCrossCrate::No
616    ),
617    gated!(
618        allow_internal_unstable, Normal, template!(Word, List: "feat1, feat2, ..."),
619        DuplicatesOk, EncodeCrossCrate::Yes,
620        "allow_internal_unstable side-steps feature gating and stability checks",
621    ),
622    gated!(
623        allow_internal_unsafe, Normal, template!(Word), WarnFollowing,
624        EncodeCrossCrate::No, "allow_internal_unsafe side-steps the unsafe_code lint",
625    ),
626    rustc_attr!(
627        rustc_allowed_through_unstable_modules, Normal, template!(NameValueStr: "deprecation message"),
628        WarnFollowing, EncodeCrossCrate::No,
629        "rustc_allowed_through_unstable_modules special cases accidental stabilizations of stable items \
630        through unstable paths"
631    ),
632    rustc_attr!(
633        rustc_deprecated_safe_2024, Normal, template!(List: r#"audit_that = "...""#),
634        ErrorFollowing, EncodeCrossCrate::Yes,
635        "rustc_deprecated_safe_2024 is supposed to be used in libstd only",
636    ),
637    rustc_attr!(
638        rustc_pub_transparent, Normal, template!(Word),
639        WarnFollowing, EncodeCrossCrate::Yes,
640        "used internally to mark types with a `transparent` representation when it is guaranteed by the documentation",
641    ),
642
643
644    // ==========================================================================
645    // Internal attributes: Type system related:
646    // ==========================================================================
647
648    gated!(fundamental, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::Yes, experimental!(fundamental)),
649    gated!(
650        may_dangle, Normal, template!(Word), WarnFollowing,
651        EncodeCrossCrate::No, dropck_eyepatch,
652        "`may_dangle` has unstable semantics and may be removed in the future",
653    ),
654
655    rustc_attr!(
656        rustc_never_type_options,
657        Normal,
658        template!(List: r#"/*opt*/ fallback = "unit|niko|never|no""#),
659        ErrorFollowing,
660        EncodeCrossCrate::No,
661        "`rustc_never_type_options` is used to experiment with never type fallback and work on \
662         never type stabilization, and will never be stable"
663    ),
664    rustc_attr!(
665        rustc_macro_edition_2021,
666        Normal,
667        template!(Word),
668        ErrorFollowing,
669        EncodeCrossCrate::No,
670        "makes spans in this macro edition 2021"
671    ),
672
673    // ==========================================================================
674    // Internal attributes: Runtime related:
675    // ==========================================================================
676
677    rustc_attr!(
678        rustc_allocator, Normal, template!(Word), WarnFollowing,
679        EncodeCrossCrate::No, IMPL_DETAIL
680    ),
681    rustc_attr!(
682        rustc_nounwind, Normal, template!(Word), WarnFollowing,
683        EncodeCrossCrate::No, IMPL_DETAIL
684    ),
685    rustc_attr!(
686        rustc_reallocator, Normal, template!(Word), WarnFollowing,
687        EncodeCrossCrate::No, IMPL_DETAIL
688    ),
689    rustc_attr!(
690        rustc_deallocator, Normal, template!(Word), WarnFollowing,
691        EncodeCrossCrate::No, IMPL_DETAIL
692    ),
693    rustc_attr!(
694        rustc_allocator_zeroed, Normal, template!(Word), WarnFollowing,
695        EncodeCrossCrate::No, IMPL_DETAIL
696    ),
697    gated!(
698        default_lib_allocator, Normal, template!(Word), WarnFollowing,
699        EncodeCrossCrate::No, allocator_internals, experimental!(default_lib_allocator),
700    ),
701    gated!(
702        needs_allocator, Normal, template!(Word), WarnFollowing,
703        EncodeCrossCrate::No, allocator_internals, experimental!(needs_allocator),
704    ),
705    gated!(
706        panic_runtime, CrateLevel, template!(Word), WarnFollowing,
707        EncodeCrossCrate::No, experimental!(panic_runtime)
708    ),
709    gated!(
710        needs_panic_runtime, CrateLevel, template!(Word), WarnFollowing,
711        EncodeCrossCrate::No, experimental!(needs_panic_runtime)
712    ),
713    gated!(
714        compiler_builtins, CrateLevel, template!(Word), WarnFollowing,
715        EncodeCrossCrate::No,
716        "the `#[compiler_builtins]` attribute is used to identify the `compiler_builtins` crate \
717        which contains compiler-rt intrinsics and will never be stable",
718    ),
719    gated!(
720        profiler_runtime, CrateLevel, template!(Word), WarnFollowing,
721        EncodeCrossCrate::No,
722        "the `#[profiler_runtime]` attribute is used to identify the `profiler_builtins` crate \
723        which contains the profiler runtime and will never be stable",
724    ),
725
726    // ==========================================================================
727    // Internal attributes, Linkage:
728    // ==========================================================================
729
730    gated!(
731        linkage, Normal, template!(NameValueStr: "external|internal|..."),
732        ErrorPreceding, EncodeCrossCrate::No,
733        "the `linkage` attribute is experimental and not portable across platforms",
734    ),
735    rustc_attr!(
736        rustc_std_internal_symbol, Normal, template!(Word), WarnFollowing,
737        EncodeCrossCrate::No, INTERNAL_UNSTABLE
738    ),
739
740    // ==========================================================================
741    // Internal attributes, Macro related:
742    // ==========================================================================
743
744    rustc_attr!(
745        rustc_builtin_macro, Normal,
746        template!(Word, List: "name, /*opt*/ attributes(name1, name2, ...)"), ErrorFollowing,
747        EncodeCrossCrate::Yes, IMPL_DETAIL
748    ),
749    rustc_attr!(
750        rustc_proc_macro_decls, Normal, template!(Word), WarnFollowing,
751        EncodeCrossCrate::No, INTERNAL_UNSTABLE
752    ),
753    rustc_attr!(
754        rustc_macro_transparency, Normal,
755        template!(NameValueStr: "transparent|semitransparent|opaque"), ErrorFollowing,
756        EncodeCrossCrate::Yes, "used internally for testing macro hygiene",
757    ),
758    rustc_attr!(
759        rustc_autodiff, Normal,
760        template!(Word, List: r#""...""#), DuplicatesOk,
761        EncodeCrossCrate::Yes, INTERNAL_UNSTABLE
762    ),
763    // Traces that are left when `cfg` and `cfg_attr` attributes are expanded.
764    // The attributes are not gated, to avoid stability errors, but they cannot be used in stable
765    // or unstable code directly because `sym::cfg_(attr_)trace` are not valid identifiers, they
766    // can only be generated by the compiler.
767    ungated!(
768        cfg_trace, Normal, template!(Word /* irrelevant */), DuplicatesOk,
769        EncodeCrossCrate::No
770    ),
771    ungated!(
772        cfg_attr_trace, Normal, template!(Word /* irrelevant */), DuplicatesOk,
773        EncodeCrossCrate::No
774    ),
775
776    // ==========================================================================
777    // Internal attributes, Diagnostics related:
778    // ==========================================================================
779
780    rustc_attr!(
781        rustc_on_unimplemented, Normal,
782        template!(
783            List: r#"/*opt*/ message = "...", /*opt*/ label = "...", /*opt*/ note = "...""#,
784            NameValueStr: "message"
785        ),
786        ErrorFollowing, EncodeCrossCrate::Yes,
787        INTERNAL_UNSTABLE
788    ),
789    rustc_attr!(
790        rustc_confusables, Normal,
791        template!(List: r#""name1", "name2", ..."#),
792        ErrorFollowing, EncodeCrossCrate::Yes,
793        INTERNAL_UNSTABLE,
794    ),
795    // Enumerates "identity-like" conversion methods to suggest on type mismatch.
796    rustc_attr!(
797        rustc_conversion_suggestion, Normal, template!(Word),
798        WarnFollowing, EncodeCrossCrate::Yes, INTERNAL_UNSTABLE
799    ),
800    // Prevents field reads in the marked trait or method to be considered
801    // during dead code analysis.
802    rustc_attr!(
803        rustc_trivial_field_reads, Normal, template!(Word),
804        WarnFollowing, EncodeCrossCrate::Yes, INTERNAL_UNSTABLE
805    ),
806    // Used by the `rustc::potential_query_instability` lint to warn methods which
807    // might not be stable during incremental compilation.
808    rustc_attr!(
809        rustc_lint_query_instability, Normal, template!(Word),
810        WarnFollowing, EncodeCrossCrate::Yes, INTERNAL_UNSTABLE
811    ),
812    // Used by the `rustc::untracked_query_information` lint to warn methods which
813    // might not be stable during incremental compilation.
814    rustc_attr!(
815        rustc_lint_untracked_query_information, Normal, template!(Word),
816        WarnFollowing, EncodeCrossCrate::Yes, INTERNAL_UNSTABLE
817    ),
818    // Used by the `rustc::diagnostic_outside_of_impl` lints to assist in changes to diagnostic
819    // APIs. Any function with this attribute will be checked by that lint.
820    rustc_attr!(
821        rustc_lint_diagnostics, Normal, template!(Word),
822        WarnFollowing, EncodeCrossCrate::Yes, INTERNAL_UNSTABLE
823    ),
824    // Used by the `rustc::bad_opt_access` lint to identify `DebuggingOptions` and `CodegenOptions`
825    // types (as well as any others in future).
826    rustc_attr!(
827        rustc_lint_opt_ty, Normal, template!(Word),
828        WarnFollowing, EncodeCrossCrate::Yes, INTERNAL_UNSTABLE
829    ),
830    // Used by the `rustc::bad_opt_access` lint on fields
831    // types (as well as any others in future).
832    rustc_attr!(
833        rustc_lint_opt_deny_field_access, Normal, template!(List: "message"),
834        WarnFollowing, EncodeCrossCrate::Yes, INTERNAL_UNSTABLE
835    ),
836
837    // ==========================================================================
838    // Internal attributes, Const related:
839    // ==========================================================================
840
841    rustc_attr!(
842        rustc_promotable, Normal, template!(Word), WarnFollowing,
843        EncodeCrossCrate::No, IMPL_DETAIL),
844    rustc_attr!(
845        rustc_legacy_const_generics, Normal, template!(List: "N"), ErrorFollowing,
846        EncodeCrossCrate::Yes, INTERNAL_UNSTABLE
847    ),
848    // Do not const-check this function's body. It will always get replaced during CTFE.
849    rustc_attr!(
850        rustc_do_not_const_check, Normal, template!(Word), WarnFollowing,
851        EncodeCrossCrate::Yes, INTERNAL_UNSTABLE
852    ),
853    // Ensure the argument to this function is &&str during const-check.
854    rustc_attr!(
855        rustc_const_panic_str, Normal, template!(Word), WarnFollowing,
856        EncodeCrossCrate::Yes, INTERNAL_UNSTABLE
857    ),
858    rustc_attr!(
859        rustc_const_stable_indirect, Normal,
860        template!(Word), WarnFollowing, EncodeCrossCrate::No, IMPL_DETAIL,
861    ),
862    rustc_attr!(
863        rustc_intrinsic_const_stable_indirect, Normal,
864        template!(Word), WarnFollowing, EncodeCrossCrate::No, IMPL_DETAIL,
865    ),
866    gated!(
867        rustc_allow_const_fn_unstable, Normal,
868        template!(Word, List: "feat1, feat2, ..."), DuplicatesOk, EncodeCrossCrate::No,
869        "rustc_allow_const_fn_unstable side-steps feature gating and stability checks"
870    ),
871
872    // ==========================================================================
873    // Internal attributes, Layout related:
874    // ==========================================================================
875
876    rustc_attr!(
877        rustc_layout_scalar_valid_range_start, Normal, template!(List: "value"), ErrorFollowing,
878        EncodeCrossCrate::Yes,
879        "the `#[rustc_layout_scalar_valid_range_start]` attribute is just used to enable \
880        niche optimizations in libcore and libstd and will never be stable",
881    ),
882    rustc_attr!(
883        rustc_layout_scalar_valid_range_end, Normal, template!(List: "value"), ErrorFollowing,
884        EncodeCrossCrate::Yes,
885        "the `#[rustc_layout_scalar_valid_range_end]` attribute is just used to enable \
886        niche optimizations in libcore and libstd and will never be stable",
887    ),
888    rustc_attr!(
889        rustc_nonnull_optimization_guaranteed, Normal, template!(Word), WarnFollowing,
890        EncodeCrossCrate::Yes,
891        "the `#[rustc_nonnull_optimization_guaranteed]` attribute is just used to document \
892        guaranteed niche optimizations in libcore and libstd and will never be stable\n\
893        (note that the compiler does not even check whether the type indeed is being non-null-optimized; \
894        it is your responsibility to ensure that the attribute is only used on types that are optimized)",
895    ),
896
897    // ==========================================================================
898    // Internal attributes, Misc:
899    // ==========================================================================
900    gated!(
901        lang, Normal, template!(NameValueStr: "name"), DuplicatesOk, EncodeCrossCrate::No, lang_items,
902        "lang items are subject to change",
903    ),
904    rustc_attr!(
905        rustc_as_ptr, Normal, template!(Word), ErrorFollowing,
906        EncodeCrossCrate::Yes,
907        "#[rustc_as_ptr] is used to mark functions returning pointers to their inner allocations."
908    ),
909    rustc_attr!(
910        rustc_pass_by_value, Normal, template!(Word), ErrorFollowing,
911        EncodeCrossCrate::Yes,
912        "#[rustc_pass_by_value] is used to mark types that must be passed by value instead of reference."
913    ),
914    rustc_attr!(
915        rustc_never_returns_null_ptr, Normal, template!(Word), ErrorFollowing,
916        EncodeCrossCrate::Yes,
917        "#[rustc_never_returns_null_ptr] is used to mark functions returning non-null pointers."
918    ),
919    rustc_attr!(
920        rustc_coherence_is_core, AttributeType::CrateLevel, template!(Word), ErrorFollowing, EncodeCrossCrate::No,
921        "#![rustc_coherence_is_core] allows inherent methods on builtin types, only intended to be used in `core`."
922    ),
923    rustc_attr!(
924        rustc_coinductive, AttributeType::Normal, template!(Word), WarnFollowing, EncodeCrossCrate::No,
925        "#![rustc_coinductive] changes a trait to be coinductive, allowing cycles in the trait solver."
926    ),
927    rustc_attr!(
928        rustc_allow_incoherent_impl, AttributeType::Normal, template!(Word), ErrorFollowing, EncodeCrossCrate::No,
929        "#[rustc_allow_incoherent_impl] has to be added to all impl items of an incoherent inherent impl."
930    ),
931    rustc_attr!(
932        rustc_preserve_ub_checks, AttributeType::CrateLevel, template!(Word), ErrorFollowing, EncodeCrossCrate::No,
933        "`#![rustc_preserve_ub_checks]` prevents the designated crate from evaluating whether UB checks are enabled when optimizing MIR",
934    ),
935    rustc_attr!(
936        rustc_deny_explicit_impl,
937        AttributeType::Normal,
938        template!(Word),
939        ErrorFollowing,
940        EncodeCrossCrate::No,
941        "#[rustc_deny_explicit_impl] enforces that a trait can have no user-provided impls"
942    ),
943    rustc_attr!(
944        rustc_do_not_implement_via_object,
945        AttributeType::Normal,
946        template!(Word),
947        ErrorFollowing,
948        EncodeCrossCrate::No,
949        "#[rustc_do_not_implement_via_object] opts out of the automatic trait impl for trait objects \
950        (`impl Trait for dyn Trait`)"
951    ),
952    rustc_attr!(
953        rustc_has_incoherent_inherent_impls, AttributeType::Normal, template!(Word),
954        ErrorFollowing, EncodeCrossCrate::Yes,
955        "#[rustc_has_incoherent_inherent_impls] allows the addition of incoherent inherent impls for \
956         the given type by annotating all impl items with #[rustc_allow_incoherent_impl]."
957    ),
958
959    BuiltinAttribute {
960        name: sym::rustc_diagnostic_item,
961        // FIXME: This can be `true` once we always use `tcx.is_diagnostic_item`.
962        encode_cross_crate: EncodeCrossCrate::Yes,
963        type_: Normal,
964        safety: AttributeSafety::Normal,
965        template: template!(NameValueStr: "name"),
966        duplicates: ErrorFollowing,
967        gate: Gated(
968            Stability::Unstable,
969            sym::rustc_attrs,
970            "diagnostic items compiler internal support for linting",
971            Features::rustc_attrs,
972        ),
973    },
974    gated!(
975        // Used in resolve:
976        prelude_import, Normal, template!(Word), WarnFollowing,
977        EncodeCrossCrate::No, "`#[prelude_import]` is for use by rustc only",
978    ),
979    gated!(
980        rustc_paren_sugar, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::No,
981        unboxed_closures, "unboxed_closures are still evolving",
982    ),
983    rustc_attr!(
984        rustc_inherit_overflow_checks, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::No,
985        "the `#[rustc_inherit_overflow_checks]` attribute is just used to control \
986        overflow checking behavior of several libcore functions that are inlined \
987        across crates and will never be stable",
988    ),
989    rustc_attr!(
990        rustc_reservation_impl, Normal,
991        template!(NameValueStr: "reservation message"), ErrorFollowing, EncodeCrossCrate::Yes,
992        "the `#[rustc_reservation_impl]` attribute is internally used \
993         for reserving for `for<T> From<!> for T` impl"
994    ),
995    rustc_attr!(
996        rustc_test_marker, Normal, template!(NameValueStr: "name"), WarnFollowing,
997        EncodeCrossCrate::No, "the `#[rustc_test_marker]` attribute is used internally to track tests",
998    ),
999    rustc_attr!(
1000        rustc_unsafe_specialization_marker, Normal, template!(Word),
1001        WarnFollowing, EncodeCrossCrate::No,
1002        "the `#[rustc_unsafe_specialization_marker]` attribute is used to check specializations"
1003    ),
1004    rustc_attr!(
1005        rustc_specialization_trait, Normal, template!(Word),
1006        WarnFollowing, EncodeCrossCrate::No,
1007        "the `#[rustc_specialization_trait]` attribute is used to check specializations"
1008    ),
1009    rustc_attr!(
1010        rustc_main, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::No,
1011        "the `#[rustc_main]` attribute is used internally to specify test entry point function",
1012    ),
1013    rustc_attr!(
1014        rustc_skip_during_method_dispatch, Normal, template!(List: "array, boxed_slice"), WarnFollowing,
1015        EncodeCrossCrate::No,
1016        "the `#[rustc_skip_during_method_dispatch]` attribute is used to exclude a trait \
1017        from method dispatch when the receiver is of the following type, for compatibility in \
1018        editions < 2021 (array) or editions < 2024 (boxed_slice)."
1019    ),
1020    rustc_attr!(
1021        rustc_must_implement_one_of, Normal, template!(List: "function1, function2, ..."),
1022        ErrorFollowing, EncodeCrossCrate::No,
1023        "the `#[rustc_must_implement_one_of]` attribute is used to change minimal complete \
1024        definition of a trait, it's currently in experimental form and should be changed before \
1025        being exposed outside of the std"
1026    ),
1027    rustc_attr!(
1028        rustc_doc_primitive, Normal, template!(NameValueStr: "primitive name"), ErrorFollowing,
1029        EncodeCrossCrate::Yes, r#"`rustc_doc_primitive` is a rustc internal attribute"#,
1030    ),
1031    gated!(
1032        rustc_intrinsic, Normal, template!(Word), ErrorFollowing, EncodeCrossCrate::Yes, intrinsics,
1033        "the `#[rustc_intrinsic]` attribute is used to declare intrinsics as function items",
1034    ),
1035    rustc_attr!(
1036        rustc_no_mir_inline, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::Yes,
1037        "#[rustc_no_mir_inline] prevents the MIR inliner from inlining a function while not affecting codegen"
1038    ),
1039    rustc_attr!(
1040        rustc_force_inline, Normal, template!(Word, NameValueStr: "reason"), WarnFollowing, EncodeCrossCrate::Yes,
1041        "#[rustc_force_inline] forces a free function to be inlined"
1042    ),
1043
1044    // ==========================================================================
1045    // Internal attributes, Testing:
1046    // ==========================================================================
1047
1048    rustc_attr!(TEST, rustc_effective_visibility, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::Yes),
1049    rustc_attr!(
1050        TEST, rustc_outlives, Normal, template!(Word),
1051        WarnFollowing, EncodeCrossCrate::No
1052    ),
1053    rustc_attr!(
1054        TEST, rustc_capture_analysis, Normal, template!(Word),
1055        WarnFollowing, EncodeCrossCrate::No
1056    ),
1057    rustc_attr!(
1058        TEST, rustc_insignificant_dtor, Normal, template!(Word),
1059        WarnFollowing, EncodeCrossCrate::Yes
1060    ),
1061    rustc_attr!(
1062        TEST, rustc_strict_coherence, Normal, template!(Word),
1063        WarnFollowing, EncodeCrossCrate::Yes
1064    ),
1065    rustc_attr!(
1066        TEST, rustc_variance, Normal, template!(Word),
1067        WarnFollowing, EncodeCrossCrate::No
1068    ),
1069    rustc_attr!(
1070        TEST, rustc_variance_of_opaques, Normal, template!(Word),
1071        WarnFollowing, EncodeCrossCrate::No
1072    ),
1073    rustc_attr!(
1074        TEST, rustc_hidden_type_of_opaques, Normal, template!(Word),
1075        WarnFollowing, EncodeCrossCrate::No
1076    ),
1077    rustc_attr!(
1078        TEST, rustc_layout, Normal, template!(List: "field1, field2, ..."),
1079        WarnFollowing, EncodeCrossCrate::Yes
1080    ),
1081    rustc_attr!(
1082        TEST, rustc_abi, Normal, template!(List: "field1, field2, ..."),
1083        WarnFollowing, EncodeCrossCrate::No
1084    ),
1085    rustc_attr!(
1086        TEST, rustc_regions, Normal, template!(Word),
1087        WarnFollowing, EncodeCrossCrate::No
1088    ),
1089    rustc_attr!(
1090        TEST, rustc_error, Normal,
1091        template!(Word, List: "delayed_bug_from_inside_query"),
1092        WarnFollowingWordOnly, EncodeCrossCrate::Yes
1093    ),
1094    rustc_attr!(
1095        TEST, rustc_dump_user_args, Normal, template!(Word),
1096        WarnFollowing, EncodeCrossCrate::No
1097    ),
1098    rustc_attr!(
1099        TEST, rustc_evaluate_where_clauses, Normal, template!(Word), WarnFollowing,
1100        EncodeCrossCrate::Yes
1101    ),
1102    rustc_attr!(
1103        TEST, rustc_if_this_changed, Normal, template!(Word, List: "DepNode"), DuplicatesOk,
1104        EncodeCrossCrate::No
1105    ),
1106    rustc_attr!(
1107        TEST, rustc_then_this_would_need, Normal, template!(List: "DepNode"), DuplicatesOk,
1108        EncodeCrossCrate::No
1109    ),
1110    rustc_attr!(
1111        TEST, rustc_clean, Normal,
1112        template!(List: r#"cfg = "...", /*opt*/ label = "...", /*opt*/ except = "...""#),
1113        DuplicatesOk, EncodeCrossCrate::No
1114    ),
1115    rustc_attr!(
1116        TEST, rustc_partition_reused, Normal,
1117        template!(List: r#"cfg = "...", module = "...""#), DuplicatesOk, EncodeCrossCrate::No
1118    ),
1119    rustc_attr!(
1120        TEST, rustc_partition_codegened, Normal,
1121        template!(List: r#"cfg = "...", module = "...""#), DuplicatesOk, EncodeCrossCrate::No
1122    ),
1123    rustc_attr!(
1124        TEST, rustc_expected_cgu_reuse, Normal,
1125        template!(List: r#"cfg = "...", module = "...", kind = "...""#), DuplicatesOk,
1126        EncodeCrossCrate::No
1127    ),
1128    rustc_attr!(
1129        TEST, rustc_symbol_name, Normal, template!(Word),
1130        WarnFollowing, EncodeCrossCrate::No
1131    ),
1132    rustc_attr!(
1133        TEST, rustc_def_path, Normal, template!(Word),
1134        WarnFollowing, EncodeCrossCrate::No
1135    ),
1136    rustc_attr!(
1137        TEST, rustc_mir, Normal, template!(List: "arg1, arg2, ..."),
1138        DuplicatesOk, EncodeCrossCrate::Yes
1139    ),
1140    gated!(
1141        custom_mir, Normal, template!(List: r#"dialect = "...", phase = "...""#),
1142        ErrorFollowing, EncodeCrossCrate::No,
1143        "the `#[custom_mir]` attribute is just used for the Rust test suite",
1144    ),
1145    rustc_attr!(
1146        TEST, rustc_dump_item_bounds, Normal, template!(Word),
1147        WarnFollowing, EncodeCrossCrate::No
1148    ),
1149    rustc_attr!(
1150        TEST, rustc_dump_predicates, Normal, template!(Word),
1151        WarnFollowing, EncodeCrossCrate::No
1152    ),
1153    rustc_attr!(
1154        TEST, rustc_dump_def_parents, Normal, template!(Word),
1155        WarnFollowing, EncodeCrossCrate::No
1156    ),
1157    rustc_attr!(
1158        TEST, rustc_object_lifetime_default, Normal, template!(Word),
1159        WarnFollowing, EncodeCrossCrate::No
1160    ),
1161    rustc_attr!(
1162        TEST, rustc_dump_vtable, Normal, template!(Word),
1163        WarnFollowing, EncodeCrossCrate::No
1164    ),
1165    rustc_attr!(
1166        TEST, rustc_dummy, Normal, template!(Word /* doesn't matter*/),
1167        DuplicatesOk, EncodeCrossCrate::No
1168    ),
1169    gated!(
1170        omit_gdb_pretty_printer_section, Normal, template!(Word),
1171        WarnFollowing, EncodeCrossCrate::No,
1172        "the `#[omit_gdb_pretty_printer_section]` attribute is just used for the Rust test suite",
1173    ),
1174    rustc_attr!(
1175        TEST, pattern_complexity_limit, CrateLevel, template!(NameValueStr: "N"),
1176        ErrorFollowing, EncodeCrossCrate::No,
1177    ),
1178];
1179
1180pub fn deprecated_attributes() -> Vec<&'static BuiltinAttribute> {
1181    BUILTIN_ATTRIBUTES.iter().filter(|attr| attr.gate.is_deprecated()).collect()
1182}
1183
1184pub fn is_builtin_attr_name(name: Symbol) -> bool {
1185    BUILTIN_ATTRIBUTE_MAP.get(&name).is_some()
1186}
1187
1188/// Whether this builtin attribute is encoded cross crate.
1189/// This means it can be used cross crate.
1190pub fn encode_cross_crate(name: Symbol) -> bool {
1191    if let Some(attr) = BUILTIN_ATTRIBUTE_MAP.get(&name) {
1192        attr.encode_cross_crate == EncodeCrossCrate::Yes
1193    } else {
1194        true
1195    }
1196}
1197
1198pub fn is_valid_for_get_attr(name: Symbol) -> bool {
1199    BUILTIN_ATTRIBUTE_MAP.get(&name).is_some_and(|attr| match attr.duplicates {
1200        WarnFollowing | ErrorFollowing | ErrorPreceding | FutureWarnFollowing
1201        | FutureWarnPreceding => true,
1202        DuplicatesOk | WarnFollowingWordOnly => false,
1203    })
1204}
1205
1206pub static BUILTIN_ATTRIBUTE_MAP: LazyLock<FxHashMap<Symbol, &BuiltinAttribute>> =
1207    LazyLock::new(|| {
1208        let mut map = FxHashMap::default();
1209        for attr in BUILTIN_ATTRIBUTES.iter() {
1210            if map.insert(attr.name, attr).is_some() {
1211                panic!("duplicate builtin attribute `{}`", attr.name);
1212            }
1213        }
1214        map
1215    });
1216
1217pub fn is_stable_diagnostic_attribute(sym: Symbol, _features: &Features) -> bool {
1218    match sym {
1219        sym::on_unimplemented | sym::do_not_recommend => true,
1220        _ => false,
1221    }
1222}