Skip to main content

rustc_feature/
builtin_attrs.rs

1//! Built-in attributes and `cfg` flag gating.
2
3use std::sync::LazyLock;
4
5use rustc_data_structures::fx::FxHashSet;
6use rustc_span::{Symbol, sym};
7
8use crate::Features;
9
10type GateFn = fn(&Features) -> bool;
11
12pub type GatedCfg = (Symbol, Symbol, GateFn);
13
14/// `cfg(...)`'s that are feature gated.
15const GATED_CFGS: &[GatedCfg] = &[
16    // (name in cfg, feature, function to check if the feature is enabled)
17    (sym::overflow_checks, sym::cfg_overflow_checks, Features::cfg_overflow_checks),
18    (sym::ub_checks, sym::cfg_ub_checks, Features::cfg_ub_checks),
19    (sym::contract_checks, sym::cfg_contract_checks, Features::cfg_contract_checks),
20    (sym::target_thread_local, sym::cfg_target_thread_local, Features::cfg_target_thread_local),
21    (
22        sym::target_has_atomic_load_store,
23        sym::cfg_target_has_atomic,
24        Features::cfg_target_has_atomic,
25    ),
26    (sym::sanitize, sym::cfg_sanitize, Features::cfg_sanitize),
27    (sym::version, sym::cfg_version, Features::cfg_version),
28    (sym::relocation_model, sym::cfg_relocation_model, Features::cfg_relocation_model),
29    (sym::sanitizer_cfi_generalize_pointers, sym::cfg_sanitizer_cfi, Features::cfg_sanitizer_cfi),
30    (sym::sanitizer_cfi_normalize_integers, sym::cfg_sanitizer_cfi, Features::cfg_sanitizer_cfi),
31    // this is consistent with naming of the compiler flag it's for
32    (sym::fmt_debug, sym::fmt_debug, Features::fmt_debug),
33    (
34        sym::target_has_reliable_f16,
35        sym::cfg_target_has_reliable_f16_f128,
36        Features::cfg_target_has_reliable_f16_f128,
37    ),
38    (
39        sym::target_has_reliable_f16_math,
40        sym::cfg_target_has_reliable_f16_f128,
41        Features::cfg_target_has_reliable_f16_f128,
42    ),
43    (
44        sym::target_has_reliable_f16b,
45        sym::cfg_target_has_reliable_f16b,
46        Features::cfg_target_has_reliable_f16b,
47    ),
48    (
49        sym::target_has_reliable_f128,
50        sym::cfg_target_has_reliable_f16_f128,
51        Features::cfg_target_has_reliable_f16_f128,
52    ),
53    (
54        sym::target_has_reliable_f128_math,
55        sym::cfg_target_has_reliable_f16_f128,
56        Features::cfg_target_has_reliable_f16_f128,
57    ),
58    (sym::target_has_threads, sym::cfg_target_has_threads, Features::cfg_target_has_threads),
59    (sym::target_object_format, sym::cfg_target_object_format, Features::cfg_target_object_format),
60];
61
62/// Find a gated cfg matching `name`.
63pub fn find_gated_cfg(name: Symbol) -> Option<&'static GatedCfg> {
64    GATED_CFGS.iter().find(|(cfg_sym, ..)| name == *cfg_sym)
65}
66
67#[derive(#[automatically_derived]#[doc(hidden)]unsafe impl ::core::clone::TrivialClone
    for AttributeStability {
}
#[automatically_derived]impl ::core::clone::Clone for AttributeStability {
    #[inline]
    #[rustc_trivial_field_reads]
    fn clone(&self) -> Self {
        let _: ::core::clone::AssertParamIsClone<Symbol>;
        let _: ::core::clone::AssertParamIsClone<&'static [&'static str]>;
        *self
    }
}Clone, #[automatically_derived]impl ::core::fmt::Debug for AttributeStability {
    #[inline]
    #[rustc_trivial_field_reads]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            Self::Unstable { gate_name: __self_0, notes: __self_1 } =>
                ::core::fmt::Formatter::debug_struct_field2_finish(f,
                    "Unstable gate_name notes", __self_0, &__self_1),
            Self::Stable => ::core::fmt::Formatter::write_str(f, "Stable"),
        }
    }
}Debug, #[automatically_derived]impl ::core::marker::Copy for AttributeStability { }Copy)]
68pub enum AttributeStability {
69    /// An attribute that is unstable behind a specified feature gate.
70    Unstable {
71        /// The feature gate, for example `rustc_attrs` for rustc_* attributes.
72        gate_name: Symbol,
73        /// Notes to be displayed when an attempt is made to use the attribute without its feature
74        /// gate.
75        notes: &'static [&'static str],
76    },
77    /// A stable attribute, can be used on all release channels
78    Stable,
79}
80
81/// Enumerates all built-in attributes.
82pub static BUILTIN_ATTRIBUTES: &[Symbol] = &[
83    // tidy-alphabetical-start
84    sym::allow,
85    sym::allow_internal_unsafe,
86    sym::allow_internal_unstable,
87    sym::automatically_derived,
88    sym::cfg,
89    sym::cfg_attr,
90    sym::cfi_encoding,
91    sym::cold,
92    sym::collapse_debuginfo,
93    sym::compiler_builtins,
94    sym::const_continue,
95    sym::coroutine,
96    sym::coverage,
97    sym::crate_name,
98    sym::crate_type,
99    sym::custom_mir,
100    sym::debugger_visualizer,
101    sym::default_lib_allocator,
102    sym::deny,
103    sym::deprecated,
104    sym::doc,
105    sym::expect,
106    sym::export_name,
107    sym::export_stable,
108    sym::feature,
109    sym::ffi_const,
110    sym::ffi_pure,
111    sym::forbid,
112    sym::force_target_feature,
113    sym::fundamental,
114    sym::ignore,
115    sym::inline,
116    sym::instruction_set,
117    sym::instrument_fn,
118    sym::lang,
119    sym::link,
120    sym::link_name,
121    sym::link_ordinal,
122    sym::link_section,
123    sym::linkage,
124    sym::loop_match,
125    sym::macro_escape,
126    sym::macro_export,
127    sym::macro_use,
128    sym::marker,
129    sym::may_dangle,
130    sym::move_size_limit,
131    sym::must_not_suspend,
132    sym::must_use,
133    sym::naked,
134    sym::needs_allocator,
135    sym::needs_panic_runtime,
136    sym::no_builtins,
137    sym::no_core,
138    sym::no_implicit_prelude,
139    sym::no_link,
140    sym::no_main,
141    sym::no_mangle,
142    sym::no_std,
143    sym::non_exhaustive,
144    sym::optimize,
145    sym::panic_handler,
146    sym::panic_runtime,
147    sym::patchable_function_entry,
148    sym::path,
149    sym::pattern_complexity_limit,
150    sym::pin_v2,
151    sym::prelude_import,
152    sym::proc_macro,
153    sym::proc_macro_attribute,
154    sym::proc_macro_derive,
155    sym::profiler_runtime,
156    sym::recursion_limit,
157    sym::reexport_test_harness_main,
158    sym::register_attribute_tool,
159    sym::register_lint_tool,
160    sym::register_tool,
161    sym::repr,
162    sym::rustc_abi,
163    sym::rustc_align,
164    sym::rustc_align_static,
165    sym::rustc_allocator,
166    sym::rustc_allocator_zeroed,
167    sym::rustc_allocator_zeroed_variant,
168    sym::rustc_allow_const_fn_unstable,
169    sym::rustc_allow_incoherent_impl,
170    sym::rustc_allow_lifetime_dependent_specialization,
171    sym::rustc_allowed_through_unstable_modules,
172    sym::rustc_always_gca,
173    sym::rustc_assert_variance,
174    sym::rustc_autodiff,
175    sym::rustc_builtin_macro,
176    sym::rustc_canonical_symbol,
177    sym::rustc_capture_analysis,
178    sym::rustc_clean,
179    sym::rustc_coherence_is_core,
180    sym::rustc_coinductive,
181    sym::rustc_comptime,
182    sym::rustc_confusables,
183    sym::rustc_const_stable,
184    sym::rustc_const_stable_indirect,
185    sym::rustc_const_unstable,
186    sym::rustc_conversion_suggestion,
187    sym::rustc_deallocator,
188    sym::rustc_default_body_unstable,
189    sym::rustc_delayed_bug_from_inside_query,
190    sym::rustc_deny_explicit_impl,
191    sym::rustc_deprecated_safe_2024,
192    sym::rustc_diagnostic_item,
193    sym::rustc_diagnostic_opaque,
194    sym::rustc_do_not_const_check,
195    sym::rustc_doc_primitive,
196    sym::rustc_dummy,
197    sym::rustc_dump_clauses,
198    sym::rustc_dump_def_parents,
199    sym::rustc_dump_def_path,
200    sym::rustc_dump_generics,
201    sym::rustc_dump_hidden_type_of_opaques,
202    sym::rustc_dump_inferred_outlives,
203    sym::rustc_dump_item_bounds,
204    sym::rustc_dump_layout,
205    sym::rustc_dump_object_lifetime_defaults,
206    sym::rustc_dump_symbol_name,
207    sym::rustc_dump_user_args,
208    sym::rustc_dump_variances,
209    sym::rustc_dump_variances_of_opaques,
210    sym::rustc_dump_vtable,
211    sym::rustc_dyn_incompatible_trait,
212    sym::rustc_effective_visibility,
213    sym::rustc_eii_foreign_item,
214    sym::rustc_evaluate_where_clauses,
215    sym::rustc_expected_cgu_reuse,
216    sym::rustc_force_inline,
217    sym::rustc_has_incoherent_inherent_impls,
218    sym::rustc_if_this_changed,
219    sym::rustc_inherit_overflow_checks,
220    sym::rustc_insignificant_dtor,
221    sym::rustc_intrinsic,
222    sym::rustc_intrinsic_const_stable_indirect,
223    sym::rustc_legacy_const_generics,
224    sym::rustc_lint_opt_deny_field_access,
225    sym::rustc_lint_opt_ty,
226    sym::rustc_lint_query_instability,
227    sym::rustc_lint_untracked_query_information,
228    sym::rustc_macro_transparency,
229    sym::rustc_main,
230    sym::rustc_mir,
231    sym::rustc_must_implement_one_of,
232    sym::rustc_no_implicit_bounds,
233    sym::rustc_no_mir_inline,
234    sym::rustc_no_writable,
235    sym::rustc_non_const_trait_method,
236    sym::rustc_nonnull_optimization_guaranteed,
237    sym::rustc_nounwind,
238    sym::rustc_objc_class,
239    sym::rustc_objc_selector,
240    sym::rustc_offload_kernel,
241    sym::rustc_on_unimplemented,
242    sym::rustc_paren_sugar,
243    sym::rustc_partition_codegened,
244    sym::rustc_partition_reused,
245    sym::rustc_pass_indirectly_in_non_rustic_abis,
246    sym::rustc_preserve_ub_checks,
247    sym::rustc_proc_macro_decls,
248    sym::rustc_promotable,
249    sym::rustc_pub_transparent,
250    sym::rustc_reallocator,
251    sym::rustc_regions,
252    sym::rustc_scalable_vector,
253    sym::rustc_show_spans,
254    sym::rustc_simd_monomorphize_lane_limit,
255    sym::rustc_skip_during_method_dispatch,
256    sym::rustc_specialization_trait,
257    sym::rustc_splat,
258    sym::rustc_std_internal_symbol,
259    sym::rustc_strict_coherence,
260    sym::rustc_test_marker,
261    sym::rustc_then_this_would_need,
262    sym::rustc_trivial_field_reads,
263    sym::rustc_unchecked_type_alias,
264    sym::rustc_unroll,
265    sym::sanitize,
266    sym::should_panic,
267    sym::stable,
268    sym::target_feature,
269    sym::test_runner,
270    sym::thread_local,
271    sym::track_caller,
272    sym::type_length_limit,
273    sym::unstable,
274    sym::unstable_feature_bound,
275    sym::unstable_removed,
276    sym::used,
277    sym::warn,
278    sym::windows_subsystem,
279    // tidy-alphabetical-end
280];
281
282pub fn is_builtin_attr_name(name: Symbol) -> bool {
283    BUILTIN_ATTRIBUTE_SET.contains(&name)
284}
285
286pub static BUILTIN_ATTRIBUTE_SET: LazyLock<FxHashSet<Symbol>> = LazyLock::new(|| {
287    let mut set = FxHashSet::default();
288    for attr in BUILTIN_ATTRIBUTES.iter() {
289        if !set.insert(*attr) {
290            {
    ::core::panicking::panic_fmt(format_args!("duplicate builtin attribute `{0}`",
            attr));
};panic!("duplicate builtin attribute `{}`", attr);
291        }
292    }
293    set
294});