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