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