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_f128,
45        sym::cfg_target_has_reliable_f16_f128,
46        Features::cfg_target_has_reliable_f16_f128,
47    ),
48    (
49        sym::target_has_reliable_f128_math,
50        sym::cfg_target_has_reliable_f16_f128,
51        Features::cfg_target_has_reliable_f16_f128,
52    ),
53    (sym::target_has_threads, sym::cfg_target_has_threads, Features::cfg_target_has_threads),
54    (sym::target_object_format, sym::cfg_target_object_format, Features::cfg_target_object_format),
55];
56
57/// Find a gated cfg matching `name`.
58pub fn find_gated_cfg(name: Symbol) -> Option<&'static GatedCfg> {
59    GATED_CFGS.iter().find(|(cfg_sym, ..)| name == *cfg_sym)
60}
61
62#[derive(#[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for AttributeStability { }
#[automatically_derived]
impl ::core::clone::Clone for AttributeStability {
    #[inline]
    fn clone(&self) -> AttributeStability {
        let _: ::core::clone::AssertParamIsClone<Symbol>;
        let _: ::core::clone::AssertParamIsClone<&'static [&'static str]>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for AttributeStability {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            AttributeStability::Unstable {
                gate_name: __self_0, notes: __self_1 } =>
                ::core::fmt::Formatter::debug_struct_field2_finish(f,
                    "Unstable", "gate_name", __self_0, "notes", &__self_1),
            AttributeStability::Stable =>
                ::core::fmt::Formatter::write_str(f, "Stable"),
        }
    }
}Debug, #[automatically_derived]
impl ::core::marker::Copy for AttributeStability { }Copy)]
63pub enum AttributeStability {
64    /// An attribute that is unstable behind a specified feature gate.
65    Unstable {
66        /// The feature gate, for example `rustc_attrs` for rustc_* attributes.
67        gate_name: Symbol,
68        /// Notes to be displayed when an attempt is made to use the attribute without its feature
69        /// gate.
70        notes: &'static [&'static str],
71    },
72    /// A stable attribute, can be used on all release channels
73    Stable,
74}
75
76/// Attributes that have a special meaning to rustc or rustdoc.
77#[rustfmt::skip]
78pub static BUILTIN_ATTRIBUTES: &[Symbol] = &[
79    // ==========================================================================
80    // Stable attributes:
81    // ==========================================================================
82
83    // Conditional compilation:
84    sym::cfg,
85    sym::cfg_attr,
86
87    // Testing:
88    sym::ignore,
89    sym::should_panic,
90
91    // Macros:
92    sym::automatically_derived,
93    sym::macro_use,
94    sym::macro_escape, // Deprecated synonym for `macro_use`.
95    sym::macro_export,
96    sym::proc_macro,
97    sym::proc_macro_derive,
98    sym::proc_macro_attribute,
99
100    // Lints:
101    sym::warn,
102    sym::allow,
103    sym::expect,
104    sym::forbid,
105    sym::deny,
106    sym::must_use,
107    sym::must_not_suspend,
108    sym::deprecated,
109
110    // Crate properties:
111    sym::crate_name,
112    sym::crate_type,
113
114    // ABI, linking, symbols, and FFI
115    sym::link,
116    sym::link_name,
117    sym::no_link,
118    sym::repr,
119    // FIXME(#82232, #143834): temporarily renamed to mitigate `#[align]` nameres ambiguity
120    sym::rustc_align,
121    sym::rustc_align_static,
122    sym::export_name,
123    sym::link_section,
124    sym::no_mangle,
125    sym::used,
126    sym::link_ordinal,
127    sym::naked,
128    // See `TyAndLayout::pass_indirectly_in_non_rustic_abis` for details.
129    sym::rustc_pass_indirectly_in_non_rustic_abis,
130
131    // Limits:
132    sym::recursion_limit,
133    sym::type_length_limit,
134    sym::move_size_limit,
135
136    // Entry point:
137    sym::no_main,
138
139    // Modules, prelude, and resolution:
140    sym::path,
141    sym::no_std,
142    sym::no_implicit_prelude,
143    sym::non_exhaustive,
144
145    // Runtime
146    sym::windows_subsystem,
147    sym::panic_handler, // RFC 2070
148
149    // Code generation:
150    sym::inline,
151    sym::cold,
152    sym::no_builtins,
153    sym::target_feature,
154    sym::track_caller,
155    sym::instruction_set,
156    sym::force_target_feature,
157    sym::sanitize,
158    sym::coverage,
159
160    sym::doc,
161
162    // Debugging
163    sym::debugger_visualizer,
164    sym::collapse_debuginfo,
165
166    // ==========================================================================
167    // Unstable attributes:
168    // ==========================================================================
169
170    // Linking:
171    sym::export_stable,
172
173    // Testing:
174    sym::test_runner,
175
176    sym::reexport_test_harness_main,
177
178    // RFC #1268
179    sym::marker,
180    sym::thread_local,
181    sym::no_core,
182    // RFC 2412
183    sym::optimize,
184
185    sym::ffi_pure,
186    sym::ffi_const,
187    sym::register_attribute_tool,
188    sym::register_lint_tool,
189    sym::register_tool,
190    // `#[cfi_encoding = ""]`
191    sym::cfi_encoding,
192
193    // `#[coroutine]` attribute to be applied to closures to make them coroutines instead
194    sym::coroutine,
195
196    // RFC 3543
197    // `#[patchable_function_entry(prefix_nops = m, entry_nops = n)]`
198    sym::patchable_function_entry,
199
200    // The `#[loop_match]` and `#[const_continue]` attributes are part of the
201    // lang experiment for RFC 3720 tracked in:
202    //
203    // - https://github.com/rust-lang/rust/issues/132306
204    sym::const_continue,
205    sym::loop_match,
206
207    // The `#[pin_v2]` attribute is part of the `pin_ergonomics` experiment
208    // that allows structurally pinning, tracked in:
209    //
210    // - https://github.com/rust-lang/rust/issues/130494
211    sym::pin_v2,
212
213    // The `#[rustc_splat]` attribute is part of the `splat` experiment
214    // that improves the ergonomics of function overloading, tracked in:
215    //
216    // - https://github.com/rust-lang/rust/issues/153629
217    sym::rustc_splat,
218
219    // The `#[rustc_unroll]` attribute.
220    //
221    // - https://github.com/rust-lang/rust/pull/156816
222    //
223    // FIXME(#159429): temporarily renamed to mitigate `#[unroll]` nameres ambiguity
224    sym::rustc_unroll,
225
226    // `#[instrument_fn = "on|off"]` to insert or inhibit instrumentation function
227    // calls inside a function, usually around the prologue.
228    //
229    // - https://github.com/rust-lang/rust/issues/157081
230    sym::instrument_fn,
231
232    // ==========================================================================
233    // Internal attributes: Stability, deprecation, and unsafe:
234    // ==========================================================================
235
236    sym::feature,
237    // DuplicatesOk since it has its own validation
238    sym::stable,
239    sym::unstable,
240    sym::unstable_feature_bound,
241    sym::unstable_removed,
242    sym::rustc_const_unstable,
243    sym::rustc_const_stable,
244    sym::rustc_default_body_unstable,
245    sym::allow_internal_unstable,
246    sym::allow_internal_unsafe,
247    sym::rustc_eii_foreign_item,
248    sym::rustc_allowed_through_unstable_modules,
249    sym::rustc_deprecated_safe_2024,
250    sym::rustc_pub_transparent,
251
252    // ==========================================================================
253    // Internal attributes: Type system related:
254    // ==========================================================================
255
256    sym::fundamental,
257    sym::may_dangle,
258
259    // ==========================================================================
260    // Internal attributes: Runtime related:
261    // ==========================================================================
262
263    sym::rustc_allocator,
264    sym::rustc_nounwind,
265    sym::rustc_reallocator,
266    sym::rustc_deallocator,
267    sym::rustc_allocator_zeroed,
268    sym::rustc_allocator_zeroed_variant,
269    sym::default_lib_allocator,
270    sym::needs_allocator,
271    sym::panic_runtime,
272    sym::needs_panic_runtime,
273    sym::compiler_builtins,
274    sym::profiler_runtime,
275
276    // ==========================================================================
277    // Internal attributes, Linkage:
278    // ==========================================================================
279
280    sym::linkage,
281    sym::rustc_std_internal_symbol,
282    sym::rustc_objc_class,
283    sym::rustc_objc_selector,
284
285    // ==========================================================================
286    // Internal attributes, Macro related:
287    // ==========================================================================
288
289    sym::rustc_builtin_macro,
290    sym::rustc_proc_macro_decls,
291    sym::rustc_macro_transparency,
292    sym::rustc_autodiff,
293    sym::rustc_offload_kernel,
294
295    // ==========================================================================
296    // Internal attributes, Diagnostics related:
297    // ==========================================================================
298
299    sym::rustc_on_unimplemented,
300    sym::rustc_confusables,
301    // Enumerates "identity-like" conversion methods to suggest on type mismatch.
302    sym::rustc_conversion_suggestion,
303    // Prevents field reads in the marked trait or method to be considered
304    // during dead code analysis.
305    sym::rustc_trivial_field_reads,
306    // Used by the `rustc::potential_query_instability` lint to warn methods which
307    // might not be stable during incremental compilation.
308    sym::rustc_lint_query_instability,
309    // Used by the `rustc::untracked_query_information` lint to warn methods which
310    // might not be stable during incremental compilation.
311    sym::rustc_lint_untracked_query_information,
312    // Used by the `rustc::bad_opt_access` lint to identify `DebuggingOptions` and `CodegenOptions`
313    // types (as well as any others in future).
314    sym::rustc_lint_opt_ty,
315    // Used by the `rustc::bad_opt_access` lint on fields
316    // types (as well as any others in future).
317    sym::rustc_lint_opt_deny_field_access,
318    sym::rustc_diagnostic_opaque,
319
320    // ==========================================================================
321    // Internal attributes, Const related:
322    // ==========================================================================
323
324    sym::rustc_promotable,
325    sym::rustc_legacy_const_generics,
326    // Do not const-check this function's body. It will always get replaced during CTFE via `hook_special_const_fn`.
327    sym::rustc_do_not_const_check,
328    sym::rustc_const_stable_indirect,
329    sym::rustc_intrinsic_const_stable_indirect,
330    sym::rustc_allow_const_fn_unstable,
331
332    // ==========================================================================
333    // Internal attributes, Layout related:
334    // ==========================================================================
335
336    sym::rustc_simd_monomorphize_lane_limit,
337    sym::rustc_nonnull_optimization_guaranteed,
338
339    // ==========================================================================
340    // Internal attributes, Misc:
341    // ==========================================================================
342    sym::lang,
343    sym::rustc_as_ptr,
344    sym::rustc_should_not_be_called_on_const_items,
345    sym::rustc_pass_by_value,
346    sym::rustc_never_returns_null_ptr,
347    sym::rustc_no_implicit_autorefs,
348    sym::rustc_coherence_is_core,
349    sym::rustc_coinductive,
350    sym::rustc_comptime,
351    sym::rustc_allow_incoherent_impl,
352    sym::rustc_preserve_ub_checks,
353    sym::rustc_deny_explicit_impl,
354    sym::rustc_dyn_incompatible_trait,
355    sym::rustc_has_incoherent_inherent_impls,
356    sym::rustc_non_const_trait_method,
357    sym::rustc_panics_when_zero,
358
359    sym::rustc_canonical_symbol,
360    sym::rustc_diagnostic_item,
361    sym::prelude_import,
362    sym::rustc_paren_sugar,
363    sym::rustc_inherit_overflow_checks,
364    sym::rustc_test_marker,
365    sym::rustc_allow_lifetime_dependent_specialization,
366    sym::rustc_specialization_trait,
367    sym::rustc_main,
368    sym::rustc_skip_during_method_dispatch,
369    sym::rustc_must_implement_one_of,
370    sym::rustc_doc_primitive,
371    sym::rustc_intrinsic,
372    sym::rustc_no_mir_inline,
373    sym::rustc_force_inline,
374    sym::rustc_scalable_vector,
375    sym::rustc_must_match_exhaustively,
376    sym::rustc_no_writable,
377
378    // ==========================================================================
379    // Internal attributes, Testing:
380    // ==========================================================================
381
382    sym::rustc_effective_visibility,
383    sym::rustc_dump_inferred_outlives,
384    sym::rustc_capture_analysis,
385    sym::rustc_insignificant_dtor,
386    sym::rustc_no_implicit_bounds,
387    sym::rustc_strict_coherence,
388    sym::rustc_dump_variances,
389    sym::rustc_dump_variances_of_opaques,
390    sym::rustc_dump_generics,
391    sym::rustc_dump_hidden_type_of_opaques,
392    sym::rustc_dump_layout,
393    sym::rustc_abi,
394    sym::rustc_regions,
395    sym::rustc_delayed_bug_from_inside_query,
396    sym::rustc_dump_user_args,
397    sym::rustc_evaluate_where_clauses,
398    sym::rustc_if_this_changed,
399    sym::rustc_then_this_would_need,
400    sym::rustc_clean,
401    sym::rustc_partition_reused,
402    sym::rustc_partition_codegened,
403    sym::rustc_expected_cgu_reuse,
404    sym::rustc_dump_symbol_name,
405    sym::rustc_dump_def_path,
406    sym::rustc_mir,
407    sym::custom_mir,
408    sym::rustc_dump_item_bounds,
409    sym::rustc_dump_clauses,
410    sym::rustc_dump_def_parents,
411    sym::rustc_dump_object_lifetime_defaults,
412    sym::rustc_dump_vtable,
413    sym::rustc_dummy,
414    sym::pattern_complexity_limit,
415];
416
417pub fn is_builtin_attr_name(name: Symbol) -> bool {
418    BUILTIN_ATTRIBUTE_SET.contains(&name)
419}
420
421pub static BUILTIN_ATTRIBUTE_SET: LazyLock<FxHashSet<Symbol>> = LazyLock::new(|| {
422    let mut set = FxHashSet::default();
423    for attr in BUILTIN_ATTRIBUTES.iter() {
424        if !set.insert(*attr) {
425            {
    ::core::panicking::panic_fmt(format_args!("duplicate builtin attribute `{0}`",
            attr));
};panic!("duplicate builtin attribute `{}`", attr);
426        }
427    }
428    set
429});