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