1 //! Builtin attributes resolved by nameres.
3 //! The actual definitions were copied from rustc's `compiler/rustc_feature/src/builtin_attrs.rs`.
5 //! It was last synchronized with upstream commit ae90dcf0207c57c3034f00b07048d63f8b2363c8.
7 //! The macros were adjusted to only expand to the attribute name, since that is all we need to do
8 //! name resolution, and `BUILTIN_ATTRIBUTES` is almost entirely unchanged from the original, to
11 use once_cell::sync::OnceCell;
12 use rustc_hash::FxHashMap;
14 /// Ignored attribute namespaces used by tools.
15 pub const TOOL_MODULES: &[&str] = &["rustfmt", "clippy"];
17 pub struct BuiltinAttribute {
18 pub name: &'static str,
19 pub template: AttributeTemplate,
22 /// A template that the attribute input must match.
23 /// Only top-level shape (`#[attr]` vs `#[attr(...)]` vs `#[attr = ...]`) is considered now.
24 #[derive(Clone, Copy)]
25 pub struct AttributeTemplate {
27 pub list: Option<&'static str>,
28 pub name_value_str: Option<&'static str>,
31 pub fn find_builtin_attr_idx(name: &str) -> Option<usize> {
32 static BUILTIN_LOOKUP_TABLE: OnceCell<FxHashMap<&'static str, usize>> = OnceCell::new();
35 INERT_ATTRIBUTES.iter().map(|attr| attr.name).enumerate().map(|(a, b)| (b, a)).collect()
41 // impl AttributeTemplate {
42 // const DEFAULT: AttributeTemplate =
43 // AttributeTemplate { word: false, list: None, name_value_str: None };
46 /// A convenience macro for constructing attribute templates.
47 /// E.g., `template!(Word, List: "description")` means that the attribute
48 /// supports forms `#[attr]` and `#[attr(description)]`.
49 macro_rules! template {
50 (Word) => { template!(@ true, None, None) };
51 (List: $descr: expr) => { template!(@ false, Some($descr), None) };
52 (NameValueStr: $descr: expr) => { template!(@ false, None, Some($descr)) };
53 (Word, List: $descr: expr) => { template!(@ true, Some($descr), None) };
54 (Word, NameValueStr: $descr: expr) => { template!(@ true, None, Some($descr)) };
55 (List: $descr1: expr, NameValueStr: $descr2: expr) => {
56 template!(@ false, Some($descr1), Some($descr2))
58 (Word, List: $descr1: expr, NameValueStr: $descr2: expr) => {
59 template!(@ true, Some($descr1), Some($descr2))
61 (@ $word: expr, $list: expr, $name_value_str: expr) => {
63 word: $word, list: $list, name_value_str: $name_value_str
68 macro_rules! ungated {
69 ($attr:ident, $typ:expr, $tpl:expr $(,)?) => {
70 BuiltinAttribute { name: stringify!($attr), template: $tpl }
75 ($attr:ident, $typ:expr, $tpl:expr, $gate:ident, $msg:expr $(,)?) => {
76 BuiltinAttribute { name: stringify!($attr), template: $tpl }
78 ($attr:ident, $typ:expr, $tpl:expr, $msg:expr $(,)?) => {
79 BuiltinAttribute { name: stringify!($attr), template: $tpl }
83 macro_rules! rustc_attr {
84 (TEST, $attr:ident, $typ:expr, $tpl:expr $(,)?) => {
92 "]` attribute is just used for rustc unit tests \
93 and will never be stable",
97 ($attr:ident, $typ:expr, $tpl:expr, $msg:expr $(,)?) => {
98 BuiltinAttribute { name: stringify!($attr), template: $tpl }
102 /// "Inert" built-in attributes that have a special meaning to rustc or rustdoc.
104 pub const INERT_ATTRIBUTES: &[BuiltinAttribute] = &[
105 // ==========================================================================
106 // Stable attributes:
107 // ==========================================================================
109 // Conditional compilation:
110 ungated!(cfg, Normal, template!(List: "predicate")),
111 ungated!(cfg_attr, Normal, template!(List: "predicate, attr1, attr2, ...")),
114 ungated!(ignore, Normal, template!(Word, NameValueStr: "reason")),
116 should_panic, Normal,
117 template!(Word, List: r#"expected = "reason"#, NameValueStr: "reason"),
119 // FIXME(Centril): This can be used on stable but shouldn't.
120 ungated!(reexport_test_harness_main, Normal, template!(NameValueStr: "name")),
123 ungated!(automatically_derived, Normal, template!(Word)),
125 ungated!(macro_use, Normal, template!(Word, List: "name1, name2, ...")),
126 ungated!(macro_escape, Normal, template!(Word)), // Deprecated synonym for `macro_use`.
127 ungated!(macro_export, Normal, template!(Word, List: "local_inner_macros")),
128 ungated!(proc_macro, Normal, template!(Word)),
130 proc_macro_derive, Normal,
131 template!(List: "TraitName, /*opt*/ attributes(name1, name2, ...)"),
133 ungated!(proc_macro_attribute, Normal, template!(Word)),
136 ungated!(warn, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#)),
137 ungated!(allow, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#)),
138 ungated!(forbid, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#)),
139 ungated!(deny, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#)),
140 ungated!(must_use, AssumedUsed, template!(Word, NameValueStr: "reason")),
146 List: r#"/*opt*/ since = "version", /*opt*/ note = "reason""#,
147 NameValueStr: "reason"
152 ungated!(crate_name, CrateLevel, template!(NameValueStr: "name")),
153 ungated!(crate_type, CrateLevel, template!(NameValueStr: "bin|lib|...")),
154 ungated!(crate_id, CrateLevel, template!(NameValueStr: "ignored")),
156 // ABI, linking, symbols, and FFI
159 template!(List: r#"name = "...", /*opt*/ kind = "dylib|static|...", /*opt*/ wasm_import_module = "...""#),
161 ungated!(link_name, AssumedUsed, template!(NameValueStr: "name")),
162 ungated!(no_link, AssumedUsed, template!(Word)),
163 ungated!(repr, AssumedUsed, template!(List: "C")),
164 ungated!(export_name, AssumedUsed, template!(NameValueStr: "name")),
165 ungated!(link_section, AssumedUsed, template!(NameValueStr: "name")),
166 ungated!(no_mangle, AssumedUsed, template!(Word)),
167 ungated!(used, AssumedUsed, template!(Word)),
170 ungated!(recursion_limit, CrateLevel, template!(NameValueStr: "N")),
171 ungated!(type_length_limit, CrateLevel, template!(NameValueStr: "N")),
173 const_eval_limit, CrateLevel, template!(NameValueStr: "N"), const_eval_limit,
174 experimental!(const_eval_limit)
177 move_size_limit, CrateLevel, template!(NameValueStr: "N"), large_assignments,
178 experimental!(move_size_limit)
182 ungated!(main, Normal, template!(Word)),
183 ungated!(start, Normal, template!(Word)),
184 ungated!(no_start, CrateLevel, template!(Word)),
185 ungated!(no_main, CrateLevel, template!(Word)),
187 // Modules, prelude, and resolution:
188 ungated!(path, Normal, template!(NameValueStr: "file")),
189 ungated!(no_std, CrateLevel, template!(Word)),
190 ungated!(no_implicit_prelude, Normal, template!(Word)),
191 ungated!(non_exhaustive, AssumedUsed, template!(Word)),
194 ungated!(windows_subsystem, AssumedUsed, template!(NameValueStr: "windows|console")),
195 ungated!(panic_handler, Normal, template!(Word)), // RFC 2070
198 ungated!(inline, AssumedUsed, template!(Word, List: "always|never")),
199 ungated!(cold, AssumedUsed, template!(Word)),
200 ungated!(no_builtins, AssumedUsed, template!(Word)),
201 ungated!(target_feature, AssumedUsed, template!(List: r#"enable = "name""#)),
202 ungated!(track_caller, AssumedUsed, template!(Word)),
204 no_sanitize, AssumedUsed,
205 template!(List: "address, memory, thread"),
206 experimental!(no_sanitize)
208 gated!(no_coverage, AssumedUsed, template!(Word), experimental!(no_coverage)),
210 // FIXME: #14408 assume docs are used since rustdoc looks at them.
211 ungated!(doc, AssumedUsed, template!(List: "hidden|inline|...", NameValueStr: "string")),
213 // ==========================================================================
214 // Unstable attributes:
215 // ==========================================================================
218 gated!(naked, AssumedUsed, template!(Word), naked_functions, experimental!(naked)),
220 link_ordinal, AssumedUsed, template!(List: "ordinal"), raw_dylib,
221 experimental!(link_ordinal)
225 // XXX Modified for use in rust-analyzer
226 gated!(plugin_registrar, Normal, template!(Word), experimental!()),
227 gated!(plugin, CrateLevel, template!(Word), experimental!()),
230 gated!(allow_fail, Normal, template!(Word), experimental!(allow_fail)),
232 test_runner, CrateLevel, template!(List: "path"), custom_test_frameworks,
233 "custom test frameworks are an unstable feature",
236 gated!(marker, AssumedUsed, template!(Word), marker_trait_attr, experimental!(marker)),
238 thread_local, AssumedUsed, template!(Word),
239 "`#[thread_local]` is an experimental feature, and does not currently handle destructors",
241 gated!(no_core, CrateLevel, template!(Word), experimental!(no_core)),
244 optimize, AssumedUsed, template!(List: "size|speed"), optimize_attribute,
245 experimental!(optimize),
248 gated!(instruction_set, AssumedUsed, template!(List: "set"), isa_attribute, experimental!(instruction_set)),
250 gated!(ffi_returns_twice, AssumedUsed, template!(Word), experimental!(ffi_returns_twice)),
251 gated!(ffi_pure, AssumedUsed, template!(Word), experimental!(ffi_pure)),
252 gated!(ffi_const, AssumedUsed, template!(Word), experimental!(ffi_const)),
254 register_attr, CrateLevel, template!(List: "attr1, attr2, ..."),
255 experimental!(register_attr),
258 register_tool, CrateLevel, template!(List: "tool1, tool2, ..."),
259 experimental!(register_tool),
262 gated!(cmse_nonsecure_entry, AssumedUsed, template!(Word), experimental!(cmse_nonsecure_entry)),
265 default_method_body_is_const, AssumedUsed, template!(Word), const_trait_impl,
266 "`default_method_body_is_const` is a temporary placeholder for declaring default bodies \
267 as `const`, which may be removed or renamed in the future."
270 // ==========================================================================
271 // Internal attributes: Stability, deprecation, and unsafe:
272 // ==========================================================================
274 ungated!(feature, CrateLevel, template!(List: "name1, name1, ...")),
275 // FIXME(#14407) -- only looked at on-demand so we can't
276 // guarantee they'll have already been checked.
278 rustc_deprecated, AssumedUsed,
279 template!(List: r#"since = "version", reason = "...""#)
282 ungated!(stable, AssumedUsed, template!(List: r#"feature = "name", since = "version""#)),
285 unstable, AssumedUsed,
286 template!(List: r#"feature = "name", reason = "...", issue = "N""#),
289 ungated!(rustc_const_unstable, AssumedUsed, template!(List: r#"feature = "name""#)),
291 ungated!(rustc_const_stable, AssumedUsed, template!(List: r#"feature = "name""#)),
293 allow_internal_unstable, AssumedUsed, template!(Word, List: "feat1, feat2, ..."),
294 "allow_internal_unstable side-steps feature gating and stability checks",
297 rustc_allow_const_fn_unstable, AssumedUsed, template!(Word, List: "feat1, feat2, ..."),
298 "rustc_allow_const_fn_unstable side-steps feature gating and stability checks"
301 allow_internal_unsafe, Normal, template!(Word),
302 "allow_internal_unsafe side-steps the unsafe_code lint",
305 // ==========================================================================
306 // Internal attributes: Type system related:
307 // ==========================================================================
309 gated!(fundamental, AssumedUsed, template!(Word), experimental!(fundamental)),
311 may_dangle, Normal, template!(Word), dropck_eyepatch,
312 "`may_dangle` has unstable semantics and may be removed in the future",
315 // ==========================================================================
316 // Internal attributes: Runtime related:
317 // ==========================================================================
319 rustc_attr!(rustc_allocator, AssumedUsed, template!(Word), IMPL_DETAIL),
320 rustc_attr!(rustc_allocator_nounwind, AssumedUsed, template!(Word), IMPL_DETAIL),
321 gated!(alloc_error_handler, Normal, template!(Word), experimental!(alloc_error_handler)),
323 default_lib_allocator, AssumedUsed, template!(Word), allocator_internals,
324 experimental!(default_lib_allocator),
327 needs_allocator, Normal, template!(Word), allocator_internals,
328 experimental!(needs_allocator),
330 gated!(panic_runtime, AssumedUsed, template!(Word), experimental!(panic_runtime)),
331 gated!(needs_panic_runtime, AssumedUsed, template!(Word), experimental!(needs_panic_runtime)),
333 compiler_builtins, AssumedUsed, template!(Word),
334 "the `#[compiler_builtins]` attribute is used to identify the `compiler_builtins` crate \
335 which contains compiler-rt intrinsics and will never be stable",
338 profiler_runtime, AssumedUsed, template!(Word),
339 "the `#[profiler_runtime]` attribute is used to identify the `profiler_builtins` crate \
340 which contains the profiler runtime and will never be stable",
343 // ==========================================================================
344 // Internal attributes, Linkage:
345 // ==========================================================================
348 linkage, AssumedUsed, template!(NameValueStr: "external|internal|..."),
349 "the `linkage` attribute is experimental and not portable across platforms",
351 rustc_attr!(rustc_std_internal_symbol, AssumedUsed, template!(Word), INTERNAL_UNSTABLE),
353 // ==========================================================================
354 // Internal attributes, Macro related:
355 // ==========================================================================
358 rustc_builtin_macro, AssumedUsed,
359 template!(Word, List: "name, /*opt*/ attributes(name1, name2, ...)"),
362 rustc_attr!(rustc_proc_macro_decls, Normal, template!(Word), INTERNAL_UNSTABLE),
364 rustc_macro_transparency, AssumedUsed,
365 template!(NameValueStr: "transparent|semitransparent|opaque"),
366 "used internally for testing macro hygiene",
369 // ==========================================================================
370 // Internal attributes, Diagnostics related:
371 // ==========================================================================
374 rustc_on_unimplemented, AssumedUsed,
376 List: r#"/*opt*/ message = "...", /*opt*/ label = "...", /*opt*/ note = "...""#,
377 NameValueStr: "message"
381 // Enumerates "identity-like" conversion methods to suggest on type mismatch.
382 rustc_attr!(rustc_conversion_suggestion, AssumedUsed, template!(Word), INTERNAL_UNSTABLE),
384 // ==========================================================================
385 // Internal attributes, Const related:
386 // ==========================================================================
388 rustc_attr!(rustc_promotable, AssumedUsed, template!(Word), IMPL_DETAIL),
389 rustc_attr!(rustc_legacy_const_generics, AssumedUsed, template!(List: "N"), INTERNAL_UNSTABLE),
391 // ==========================================================================
392 // Internal attributes, Layout related:
393 // ==========================================================================
396 rustc_layout_scalar_valid_range_start, AssumedUsed, template!(List: "value"),
397 "the `#[rustc_layout_scalar_valid_range_start]` attribute is just used to enable \
398 niche optimizations in libcore and will never be stable",
401 rustc_layout_scalar_valid_range_end, AssumedUsed, template!(List: "value"),
402 "the `#[rustc_layout_scalar_valid_range_end]` attribute is just used to enable \
403 niche optimizations in libcore and will never be stable",
406 rustc_nonnull_optimization_guaranteed, AssumedUsed, template!(Word),
407 "the `#[rustc_nonnull_optimization_guaranteed]` attribute is just used to enable \
408 niche optimizations in libcore and will never be stable",
411 // ==========================================================================
412 // Internal attributes, Misc:
413 // ==========================================================================
415 lang, Normal, template!(NameValueStr: "name"), lang_items,
416 "language items are subject to change",
418 gated!(rustc_diagnostic_item, Normal, template!(NameValueStr: "name"), experimental!()), // XXX Modified for use in rust-analyzer
421 prelude_import, AssumedUsed, template!(Word),
422 "`#[prelude_import]` is for use by rustc only",
425 rustc_paren_sugar, Normal, template!(Word), unboxed_closures,
426 "unboxed_closures are still evolving",
429 rustc_inherit_overflow_checks, AssumedUsed, template!(Word),
430 "the `#[rustc_inherit_overflow_checks]` attribute is just used to control \
431 overflow checking behavior of several libcore functions that are inlined \
432 across crates and will never be stable",
434 rustc_attr!(rustc_reservation_impl, Normal, template!(NameValueStr: "reservation message"),
435 "the `#[rustc_reservation_impl]` attribute is internally used \
436 for reserving for `for<T> From<!> for T` impl"
439 rustc_test_marker, Normal, template!(Word),
440 "the `#[rustc_test_marker]` attribute is used internally to track tests",
443 rustc_unsafe_specialization_marker, Normal, template!(Word),
444 "the `#[rustc_unsafe_specialization_marker]` attribute is used to check specializations"
447 rustc_specialization_trait, Normal, template!(Word),
448 "the `#[rustc_specialization_trait]` attribute is used to check specializations"
451 rustc_main, Normal, template!(Word),
452 "the `#[rustc_main]` attribute is used internally to specify test entry point function",
455 rustc_skip_array_during_method_dispatch, Normal, template!(Word),
456 "the `#[rustc_skip_array_during_method_dispatch]` attribute is used to exclude a trait \
457 from method dispatch when the receiver is an array, for compatibility in editions < 2021."
460 // ==========================================================================
461 // Internal attributes, Testing:
462 // ==========================================================================
464 rustc_attr!(TEST, rustc_outlives, Normal, template!(Word)),
465 rustc_attr!(TEST, rustc_capture_analysis, Normal, template!(Word)),
466 rustc_attr!(TEST, rustc_insignificant_dtor, Normal, template!(Word)),
467 rustc_attr!(TEST, rustc_variance, Normal, template!(Word)),
468 rustc_attr!(TEST, rustc_layout, Normal, template!(List: "field1, field2, ...")),
469 rustc_attr!(TEST, rustc_regions, Normal, template!(Word)),
471 TEST, rustc_error, AssumedUsed,
472 template!(Word, List: "delay_span_bug_from_inside_query")
474 rustc_attr!(TEST, rustc_dump_user_substs, AssumedUsed, template!(Word)),
475 rustc_attr!(TEST, rustc_evaluate_where_clauses, AssumedUsed, template!(Word)),
476 rustc_attr!(TEST, rustc_if_this_changed, AssumedUsed, template!(Word, List: "DepNode")),
477 rustc_attr!(TEST, rustc_then_this_would_need, AssumedUsed, template!(List: "DepNode")),
479 TEST, rustc_clean, AssumedUsed,
480 template!(List: r#"cfg = "...", /*opt*/ label = "...", /*opt*/ except = "...""#),
483 TEST, rustc_partition_reused, AssumedUsed,
484 template!(List: r#"cfg = "...", module = "...""#),
487 TEST, rustc_partition_codegened, AssumedUsed,
488 template!(List: r#"cfg = "...", module = "...""#),
491 TEST, rustc_expected_cgu_reuse, AssumedUsed,
492 template!(List: r#"cfg = "...", module = "...", kind = "...""#),
494 rustc_attr!(TEST, rustc_synthetic, AssumedUsed, template!(Word)),
495 rustc_attr!(TEST, rustc_symbol_name, AssumedUsed, template!(Word)),
496 rustc_attr!(TEST, rustc_polymorphize_error, AssumedUsed, template!(Word)),
497 rustc_attr!(TEST, rustc_def_path, AssumedUsed, template!(Word)),
498 rustc_attr!(TEST, rustc_mir, AssumedUsed, template!(List: "arg1, arg2, ...")),
499 rustc_attr!(TEST, rustc_dump_program_clauses, AssumedUsed, template!(Word)),
500 rustc_attr!(TEST, rustc_dump_env_program_clauses, AssumedUsed, template!(Word)),
501 rustc_attr!(TEST, rustc_object_lifetime_default, AssumedUsed, template!(Word)),
502 rustc_attr!(TEST, rustc_dump_vtable, AssumedUsed, template!(Word)),
503 rustc_attr!(TEST, rustc_dummy, Normal, template!(Word /* doesn't matter*/)),
505 omit_gdb_pretty_printer_section, AssumedUsed, template!(Word),
506 "the `#[omit_gdb_pretty_printer_section]` attribute is just used for the Rust test suite",