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