1 //! Built-in attributes and `cfg` flag gating.
2
3 use AttributeDuplicates::*;
4 use AttributeGate::*;
5 use AttributeType::*;
6
7 use crate::{Features, Stability};
8
9 use rustc_data_structures::fx::FxHashMap;
10 use rustc_span::symbol::{sym, Symbol};
11
12 use std::sync::LazyLock;
13
14 type GateFn = fn(&Features) -> bool;
15
16 macro_rules! cfg_fn {
17 ($field: ident) => {
18 (|features| features.$field) as GateFn
19 };
20 }
21
22 pub type GatedCfg = (Symbol, Symbol, GateFn);
23
24 /// `cfg(...)`'s that are feature gated.
25 const GATED_CFGS: &[GatedCfg] = &[
26 // (name in cfg, feature, function to check if the feature is enabled)
27 (sym::overflow_checks, sym::cfg_overflow_checks, cfg_fn!(cfg_overflow_checks)),
28 (sym::target_abi, sym::cfg_target_abi, cfg_fn!(cfg_target_abi)),
29 (sym::target_thread_local, sym::cfg_target_thread_local, cfg_fn!(cfg_target_thread_local)),
30 (
31 sym::target_has_atomic_equal_alignment,
32 sym::cfg_target_has_atomic_equal_alignment,
33 cfg_fn!(cfg_target_has_atomic_equal_alignment),
34 ),
35 (sym::target_has_atomic_load_store, sym::cfg_target_has_atomic, cfg_fn!(cfg_target_has_atomic)),
36 (sym::sanitize, sym::cfg_sanitize, cfg_fn!(cfg_sanitize)),
37 (sym::version, sym::cfg_version, cfg_fn!(cfg_version)),
38 ];
39
40 /// Find a gated cfg determined by the `pred`icate which is given the cfg's name.
find_gated_cfg(pred: impl Fn(Symbol) -> bool) -> Option<&'static GatedCfg>41 pub fn find_gated_cfg(pred: impl Fn(Symbol) -> bool) -> Option<&'static GatedCfg> {
42 GATED_CFGS.iter().find(|(cfg_sym, ..)| pred(*cfg_sym))
43 }
44
45 // If you change this, please modify `src/doc/unstable-book` as well. You must
46 // move that documentation into the relevant place in the other docs, and
47 // remove the chapter on the flag.
48
49 #[derive(Copy, Clone, PartialEq, Debug)]
50 pub enum AttributeType {
51 /// Normal, builtin attribute that is consumed
52 /// by the compiler before the unused_attribute check
53 Normal,
54
55 /// Builtin attribute that is only allowed at the crate level
56 CrateLevel,
57 }
58
59 #[derive(Clone, Copy)]
60 pub enum AttributeGate {
61 /// Is gated by a given feature gate, reason
62 /// and function to check if enabled
63 Gated(Stability, Symbol, &'static str, fn(&Features) -> bool),
64
65 /// Ungated attribute, can be used on all release channels
66 Ungated,
67 }
68
69 // fn() is not Debug
70 impl std::fmt::Debug for AttributeGate {
fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result71 fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
72 match *self {
73 Self::Gated(ref stab, name, expl, _) => {
74 write!(fmt, "Gated({stab:?}, {name}, {expl})")
75 }
76 Self::Ungated => write!(fmt, "Ungated"),
77 }
78 }
79 }
80
81 impl AttributeGate {
is_deprecated(&self) -> bool82 fn is_deprecated(&self) -> bool {
83 matches!(*self, Self::Gated(Stability::Deprecated(_, _), ..))
84 }
85 }
86
87 /// A template that the attribute input must match.
88 /// Only top-level shape (`#[attr]` vs `#[attr(...)]` vs `#[attr = ...]`) is considered now.
89 #[derive(Clone, Copy, Default)]
90 pub struct AttributeTemplate {
91 /// If `true`, the attribute is allowed to be a bare word like `#[test]`.
92 pub word: bool,
93 /// If `Some`, the attribute is allowed to take a list of items like `#[allow(..)]`.
94 pub list: Option<&'static str>,
95 /// If `Some`, the attribute is allowed to be a name/value pair where the
96 /// value is a string, like `#[must_use = "reason"]`.
97 pub name_value_str: Option<&'static str>,
98 }
99
100 /// How to handle multiple duplicate attributes on the same item.
101 #[derive(Clone, Copy, Default)]
102 pub enum AttributeDuplicates {
103 /// Duplicates of this attribute are allowed.
104 ///
105 /// This should only be used with attributes where duplicates have semantic
106 /// meaning, or some kind of "additive" behavior. For example, `#[warn(..)]`
107 /// can be specified multiple times, and it combines all the entries. Or use
108 /// this if there is validation done elsewhere.
109 #[default]
110 DuplicatesOk,
111 /// Duplicates after the first attribute will be an unused_attribute warning.
112 ///
113 /// This is usually used for "word" attributes, where they are used as a
114 /// boolean marker, like `#[used]`. It is not necessarily wrong that there
115 /// are duplicates, but the others should probably be removed.
116 WarnFollowing,
117 /// Same as `WarnFollowing`, but only issues warnings for word-style attributes.
118 ///
119 /// This is only for special cases, for example multiple `#[macro_use]` can
120 /// be warned, but multiple `#[macro_use(...)]` should not because the list
121 /// form has different meaning from the word form.
122 WarnFollowingWordOnly,
123 /// Duplicates after the first attribute will be an error.
124 ///
125 /// This should be used where duplicates would be ignored, but carry extra
126 /// meaning that could cause confusion. For example, `#[stable(since="1.0")]
127 /// #[stable(since="2.0")]`, which version should be used for `stable`?
128 ErrorFollowing,
129 /// Duplicates preceding the last instance of the attribute will be an error.
130 ///
131 /// This is the same as `ErrorFollowing`, except the last attribute is the
132 /// one that is "used". This is typically used in cases like codegen
133 /// attributes which usually only honor the last attribute.
134 ErrorPreceding,
135 /// Duplicates after the first attribute will be an unused_attribute warning
136 /// with a note that this will be an error in the future.
137 ///
138 /// This should be used for attributes that should be `ErrorFollowing`, but
139 /// because older versions of rustc silently accepted (and ignored) the
140 /// attributes, this is used to transition.
141 FutureWarnFollowing,
142 /// Duplicates preceding the last instance of the attribute will be a
143 /// warning, with a note that this will be an error in the future.
144 ///
145 /// This is the same as `FutureWarnFollowing`, except the last attribute is
146 /// the one that is "used". Ideally these can eventually migrate to
147 /// `ErrorPreceding`.
148 FutureWarnPreceding,
149 }
150
151 /// A convenience macro to deal with `$($expr)?`.
152 macro_rules! or_default {
153 ($default:expr,) => {
154 $default
155 };
156 ($default:expr, $next:expr) => {
157 $next
158 };
159 }
160
161 /// A convenience macro for constructing attribute templates.
162 /// E.g., `template!(Word, List: "description")` means that the attribute
163 /// supports forms `#[attr]` and `#[attr(description)]`.
164 macro_rules! template {
165 (Word) => { template!(@ true, None, None) };
166 (List: $descr: expr) => { template!(@ false, Some($descr), None) };
167 (NameValueStr: $descr: expr) => { template!(@ false, None, Some($descr)) };
168 (Word, List: $descr: expr) => { template!(@ true, Some($descr), None) };
169 (Word, NameValueStr: $descr: expr) => { template!(@ true, None, Some($descr)) };
170 (List: $descr1: expr, NameValueStr: $descr2: expr) => {
171 template!(@ false, Some($descr1), Some($descr2))
172 };
173 (Word, List: $descr1: expr, NameValueStr: $descr2: expr) => {
174 template!(@ true, Some($descr1), Some($descr2))
175 };
176 (@ $word: expr, $list: expr, $name_value_str: expr) => { AttributeTemplate {
177 word: $word, list: $list, name_value_str: $name_value_str
178 } };
179 }
180
181 macro_rules! ungated {
182 ($attr:ident, $typ:expr, $tpl:expr, $duplicates:expr $(, @only_local: $only_local:expr)? $(,)?) => {
183 BuiltinAttribute {
184 name: sym::$attr,
185 only_local: or_default!(false, $($only_local)?),
186 type_: $typ,
187 template: $tpl,
188 gate: Ungated,
189 duplicates: $duplicates,
190 }
191 };
192 }
193
194 macro_rules! gated {
195 ($attr:ident, $typ:expr, $tpl:expr, $duplicates:expr $(, @only_local: $only_local:expr)?, $gate:ident, $msg:expr $(,)?) => {
196 BuiltinAttribute {
197 name: sym::$attr,
198 only_local: or_default!(false, $($only_local)?),
199 type_: $typ,
200 template: $tpl,
201 duplicates: $duplicates,
202 gate: Gated(Stability::Unstable, sym::$gate, $msg, cfg_fn!($gate)),
203 }
204 };
205 ($attr:ident, $typ:expr, $tpl:expr, $duplicates:expr $(, @only_local: $only_local:expr)?, $msg:expr $(,)?) => {
206 BuiltinAttribute {
207 name: sym::$attr,
208 only_local: or_default!(false, $($only_local)?),
209 type_: $typ,
210 template: $tpl,
211 duplicates: $duplicates,
212 gate: Gated(Stability::Unstable, sym::$attr, $msg, cfg_fn!($attr)),
213 }
214 };
215 }
216
217 macro_rules! rustc_attr {
218 (TEST, $attr:ident, $typ:expr, $tpl:expr, $duplicate:expr $(, @only_local: $only_local:expr)? $(,)?) => {
219 rustc_attr!(
220 $attr,
221 $typ,
222 $tpl,
223 $duplicate,
224 $(@only_local: $only_local,)?
225 concat!(
226 "the `#[",
227 stringify!($attr),
228 "]` attribute is just used for rustc unit tests \
229 and will never be stable",
230 ),
231 )
232 };
233 ($attr:ident, $typ:expr, $tpl:expr, $duplicates:expr $(, @only_local: $only_local:expr)?, $msg:expr $(,)?) => {
234 BuiltinAttribute {
235 name: sym::$attr,
236 only_local: or_default!(false, $($only_local)?),
237 type_: $typ,
238 template: $tpl,
239 duplicates: $duplicates,
240 gate: Gated(Stability::Unstable, sym::rustc_attrs, $msg, cfg_fn!(rustc_attrs)),
241 }
242 };
243 }
244
245 macro_rules! experimental {
246 ($attr:ident) => {
247 concat!("the `#[", stringify!($attr), "]` attribute is an experimental feature")
248 };
249 }
250
251 const IMPL_DETAIL: &str = "internal implementation detail";
252 const INTERNAL_UNSTABLE: &str = "this is an internal attribute that will never be stable";
253
254 pub struct BuiltinAttribute {
255 pub name: Symbol,
256 /// Whether this attribute is only used in the local crate.
257 ///
258 /// If so, it is not encoded in the crate metadata.
259 pub only_local: bool,
260 pub type_: AttributeType,
261 pub template: AttributeTemplate,
262 pub duplicates: AttributeDuplicates,
263 pub gate: AttributeGate,
264 }
265
266 /// Attributes that have a special meaning to rustc or rustdoc.
267 #[rustfmt::skip]
268 pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
269 // ==========================================================================
270 // Stable attributes:
271 // ==========================================================================
272
273 // Conditional compilation:
274 ungated!(cfg, Normal, template!(List: "predicate"), DuplicatesOk),
275 ungated!(cfg_attr, Normal, template!(List: "predicate, attr1, attr2, ..."), DuplicatesOk),
276
277 // Testing:
278 ungated!(ignore, Normal, template!(Word, NameValueStr: "reason"), WarnFollowing),
279 ungated!(
280 should_panic, Normal,
281 template!(Word, List: r#"expected = "reason""#, NameValueStr: "reason"), FutureWarnFollowing,
282 ),
283 // FIXME(Centril): This can be used on stable but shouldn't.
284 ungated!(reexport_test_harness_main, CrateLevel, template!(NameValueStr: "name"), ErrorFollowing),
285
286 // Macros:
287 ungated!(automatically_derived, Normal, template!(Word), WarnFollowing),
288 ungated!(macro_use, Normal, template!(Word, List: "name1, name2, ..."), WarnFollowingWordOnly),
289 ungated!(macro_escape, Normal, template!(Word), WarnFollowing), // Deprecated synonym for `macro_use`.
290 ungated!(macro_export, Normal, template!(Word, List: "local_inner_macros"), WarnFollowing),
291 ungated!(proc_macro, Normal, template!(Word), ErrorFollowing),
292 ungated!(
293 proc_macro_derive, Normal,
294 template!(List: "TraitName, /*opt*/ attributes(name1, name2, ...)"), ErrorFollowing,
295 ),
296 ungated!(proc_macro_attribute, Normal, template!(Word), ErrorFollowing),
297
298 // Lints:
299 ungated!(
300 warn, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#),
301 DuplicatesOk, @only_local: true,
302 ),
303 ungated!(
304 allow, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#),
305 DuplicatesOk, @only_local: true,
306 ),
307 gated!(
308 expect, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#), DuplicatesOk,
309 lint_reasons, experimental!(expect)
310 ),
311 ungated!(
312 forbid, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#),
313 DuplicatesOk, @only_local: true,
314 ),
315 ungated!(
316 deny, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#),
317 DuplicatesOk, @only_local: true,
318 ),
319 ungated!(must_use, Normal, template!(Word, NameValueStr: "reason"), FutureWarnFollowing),
320 gated!(
321 must_not_suspend, Normal, template!(Word, NameValueStr: "reason"), WarnFollowing,
322 experimental!(must_not_suspend)
323 ),
324 ungated!(
325 deprecated, Normal,
326 template!(
327 Word,
328 List: r#"/*opt*/ since = "version", /*opt*/ note = "reason""#,
329 NameValueStr: "reason"
330 ),
331 ErrorFollowing
332 ),
333
334 // Crate properties:
335 ungated!(crate_name, CrateLevel, template!(NameValueStr: "name"), FutureWarnFollowing),
336 ungated!(crate_type, CrateLevel, template!(NameValueStr: "bin|lib|..."), DuplicatesOk),
337 // crate_id is deprecated
338 ungated!(crate_id, CrateLevel, template!(NameValueStr: "ignored"), FutureWarnFollowing),
339
340 // ABI, linking, symbols, and FFI
341 ungated!(
342 link, Normal,
343 template!(List: r#"name = "...", /*opt*/ kind = "dylib|static|...", /*opt*/ wasm_import_module = "...", /*opt*/ import_name_type = "decorated|noprefix|undecorated""#),
344 DuplicatesOk,
345 ),
346 ungated!(link_name, Normal, template!(NameValueStr: "name"), FutureWarnPreceding),
347 ungated!(no_link, Normal, template!(Word), WarnFollowing),
348 ungated!(repr, Normal, template!(List: "C"), DuplicatesOk, @only_local: true),
349 ungated!(export_name, Normal, template!(NameValueStr: "name"), FutureWarnPreceding),
350 ungated!(link_section, Normal, template!(NameValueStr: "name"), FutureWarnPreceding),
351 ungated!(no_mangle, Normal, template!(Word), WarnFollowing, @only_local: true),
352 ungated!(used, Normal, template!(Word, List: "compiler|linker"), WarnFollowing, @only_local: true),
353 ungated!(link_ordinal, Normal, template!(List: "ordinal"), ErrorPreceding),
354
355 // Limits:
356 ungated!(recursion_limit, CrateLevel, template!(NameValueStr: "N"), FutureWarnFollowing),
357 ungated!(type_length_limit, CrateLevel, template!(NameValueStr: "N"), FutureWarnFollowing),
358 gated!(
359 move_size_limit, CrateLevel, template!(NameValueStr: "N"), ErrorFollowing,
360 large_assignments, experimental!(move_size_limit)
361 ),
362
363 // Entry point:
364 gated!(unix_sigpipe, Normal, template!(Word, NameValueStr: "inherit|sig_ign|sig_dfl"), ErrorFollowing, experimental!(unix_sigpipe)),
365 ungated!(start, Normal, template!(Word), WarnFollowing),
366 ungated!(no_start, CrateLevel, template!(Word), WarnFollowing),
367 ungated!(no_main, CrateLevel, template!(Word), WarnFollowing),
368
369 // Modules, prelude, and resolution:
370 ungated!(path, Normal, template!(NameValueStr: "file"), FutureWarnFollowing),
371 ungated!(no_std, CrateLevel, template!(Word), WarnFollowing),
372 ungated!(no_implicit_prelude, Normal, template!(Word), WarnFollowing),
373 ungated!(non_exhaustive, Normal, template!(Word), WarnFollowing),
374
375 // Runtime
376 ungated!(
377 windows_subsystem, CrateLevel,
378 template!(NameValueStr: "windows|console"), FutureWarnFollowing
379 ),
380 ungated!(panic_handler, Normal, template!(Word), WarnFollowing), // RFC 2070
381
382 // Code generation:
383 ungated!(inline, Normal, template!(Word, List: "always|never"), FutureWarnFollowing, @only_local: true),
384 ungated!(cold, Normal, template!(Word), WarnFollowing, @only_local: true),
385 ungated!(no_builtins, CrateLevel, template!(Word), WarnFollowing),
386 ungated!(
387 target_feature, Normal, template!(List: r#"enable = "name""#),
388 DuplicatesOk, @only_local: true,
389 ),
390 ungated!(track_caller, Normal, template!(Word), WarnFollowing),
391 ungated!(instruction_set, Normal, template!(List: "set"), ErrorPreceding),
392 gated!(
393 no_sanitize, Normal,
394 template!(List: "address, kcfi, memory, thread"), DuplicatesOk,
395 experimental!(no_sanitize)
396 ),
397 gated!(no_coverage, Normal, template!(Word), WarnFollowing, experimental!(no_coverage)),
398
399 ungated!(
400 doc, Normal, template!(List: "hidden|inline|...", NameValueStr: "string"), DuplicatesOk
401 ),
402
403 // Debugging
404 ungated!(
405 debugger_visualizer, Normal,
406 template!(List: r#"natvis_file = "...", gdb_script_file = "...""#), DuplicatesOk
407 ),
408
409 // ==========================================================================
410 // Unstable attributes:
411 // ==========================================================================
412
413 // Linking:
414 gated!(
415 naked, Normal, template!(Word), WarnFollowing, @only_local: true,
416 naked_functions, experimental!(naked)
417 ),
418
419 // Plugins:
420 BuiltinAttribute {
421 name: sym::plugin,
422 only_local: false,
423 type_: CrateLevel,
424 template: template!(List: "name"),
425 duplicates: DuplicatesOk,
426 gate: Gated(
427 Stability::Deprecated(
428 "https://github.com/rust-lang/rust/pull/64675",
429 Some("may be removed in a future compiler version"),
430 ),
431 sym::plugin,
432 "compiler plugins are deprecated",
433 cfg_fn!(plugin)
434 ),
435 },
436
437 // Testing:
438 gated!(
439 test_runner, CrateLevel, template!(List: "path"), ErrorFollowing, custom_test_frameworks,
440 "custom test frameworks are an unstable feature",
441 ),
442 // RFC #1268
443 gated!(
444 marker, Normal, template!(Word), WarnFollowing, @only_local: true,
445 marker_trait_attr, experimental!(marker)
446 ),
447 gated!(
448 thread_local, Normal, template!(Word), WarnFollowing,
449 "`#[thread_local]` is an experimental feature, and does not currently handle destructors",
450 ),
451 gated!(no_core, CrateLevel, template!(Word), WarnFollowing, experimental!(no_core)),
452 // RFC 2412
453 gated!(
454 optimize, Normal, template!(List: "size|speed"), ErrorPreceding, optimize_attribute,
455 experimental!(optimize),
456 ),
457
458 gated!(
459 ffi_returns_twice, Normal, template!(Word), WarnFollowing, experimental!(ffi_returns_twice)
460 ),
461 gated!(ffi_pure, Normal, template!(Word), WarnFollowing, experimental!(ffi_pure)),
462 gated!(ffi_const, Normal, template!(Word), WarnFollowing, experimental!(ffi_const)),
463 gated!(
464 register_tool, CrateLevel, template!(List: "tool1, tool2, ..."), DuplicatesOk,
465 experimental!(register_tool),
466 ),
467
468 gated!(
469 cmse_nonsecure_entry, Normal, template!(Word), WarnFollowing,
470 experimental!(cmse_nonsecure_entry)
471 ),
472 // RFC 2632
473 gated!(
474 const_trait, Normal, template!(Word), WarnFollowing, const_trait_impl,
475 "`const_trait` is a temporary placeholder for marking a trait that is suitable for `const` \
476 `impls` and all default bodies as `const`, which may be removed or renamed in the \
477 future."
478 ),
479 // lang-team MCP 147
480 gated!(
481 deprecated_safe, Normal, template!(List: r#"since = "version", note = "...""#), ErrorFollowing,
482 experimental!(deprecated_safe),
483 ),
484
485 // `#[collapse_debuginfo]`
486 gated!(
487 collapse_debuginfo, Normal, template!(Word), WarnFollowing,
488 experimental!(collapse_debuginfo)
489 ),
490
491 // RFC 2397
492 gated!(do_not_recommend, Normal, template!(Word), WarnFollowing, experimental!(do_not_recommend)),
493
494 // `#[cfi_encoding = ""]`
495 gated!(
496 cfi_encoding, Normal, template!(NameValueStr: "encoding"), ErrorPreceding,
497 experimental!(cfi_encoding)
498 ),
499
500 // ==========================================================================
501 // Internal attributes: Stability, deprecation, and unsafe:
502 // ==========================================================================
503
504 ungated!(
505 feature, CrateLevel,
506 template!(List: "name1, name2, ..."), DuplicatesOk, @only_local: true,
507 ),
508 // DuplicatesOk since it has its own validation
509 ungated!(
510 stable, Normal,
511 template!(List: r#"feature = "name", since = "version""#), DuplicatesOk, @only_local: true,
512 ),
513 ungated!(
514 unstable, Normal,
515 template!(List: r#"feature = "name", reason = "...", issue = "N""#), DuplicatesOk,
516 ),
517 ungated!(rustc_const_unstable, Normal, template!(List: r#"feature = "name""#), DuplicatesOk),
518 ungated!(
519 rustc_const_stable, Normal,
520 template!(List: r#"feature = "name""#), DuplicatesOk, @only_local: true,
521 ),
522 ungated!(
523 rustc_default_body_unstable, Normal,
524 template!(List: r#"feature = "name", reason = "...", issue = "N""#), DuplicatesOk
525 ),
526 gated!(
527 allow_internal_unstable, Normal, template!(Word, List: "feat1, feat2, ..."), DuplicatesOk,
528 "allow_internal_unstable side-steps feature gating and stability checks",
529 ),
530 gated!(
531 rustc_allow_const_fn_unstable, Normal,
532 template!(Word, List: "feat1, feat2, ..."), DuplicatesOk,
533 "rustc_allow_const_fn_unstable side-steps feature gating and stability checks"
534 ),
535 gated!(
536 allow_internal_unsafe, Normal, template!(Word), WarnFollowing,
537 "allow_internal_unsafe side-steps the unsafe_code lint",
538 ),
539 ungated!(rustc_safe_intrinsic, Normal, template!(Word), DuplicatesOk),
540 rustc_attr!(rustc_allowed_through_unstable_modules, Normal, template!(Word), WarnFollowing,
541 "rustc_allowed_through_unstable_modules special cases accidental stabilizations of stable items \
542 through unstable paths"),
543
544 // ==========================================================================
545 // Internal attributes: Type system related:
546 // ==========================================================================
547
548 gated!(fundamental, Normal, template!(Word), WarnFollowing, experimental!(fundamental)),
549 gated!(
550 may_dangle, Normal, template!(Word), WarnFollowing, dropck_eyepatch,
551 "`may_dangle` has unstable semantics and may be removed in the future",
552 ),
553
554 // ==========================================================================
555 // Internal attributes: Runtime related:
556 // ==========================================================================
557
558 rustc_attr!(rustc_allocator, Normal, template!(Word), WarnFollowing, IMPL_DETAIL),
559 rustc_attr!(rustc_nounwind, Normal, template!(Word), WarnFollowing, IMPL_DETAIL),
560 rustc_attr!(rustc_reallocator, Normal, template!(Word), WarnFollowing, IMPL_DETAIL),
561 rustc_attr!(rustc_deallocator, Normal, template!(Word), WarnFollowing, IMPL_DETAIL),
562 rustc_attr!(rustc_allocator_zeroed, Normal, template!(Word), WarnFollowing, IMPL_DETAIL),
563 gated!(
564 default_lib_allocator, Normal, template!(Word), WarnFollowing, allocator_internals,
565 experimental!(default_lib_allocator),
566 ),
567 gated!(
568 needs_allocator, Normal, template!(Word), WarnFollowing, allocator_internals,
569 experimental!(needs_allocator),
570 ),
571 gated!(panic_runtime, Normal, template!(Word), WarnFollowing, experimental!(panic_runtime)),
572 gated!(
573 needs_panic_runtime, Normal, template!(Word), WarnFollowing,
574 experimental!(needs_panic_runtime)
575 ),
576 gated!(
577 compiler_builtins, Normal, template!(Word), WarnFollowing,
578 "the `#[compiler_builtins]` attribute is used to identify the `compiler_builtins` crate \
579 which contains compiler-rt intrinsics and will never be stable",
580 ),
581 gated!(
582 profiler_runtime, Normal, template!(Word), WarnFollowing,
583 "the `#[profiler_runtime]` attribute is used to identify the `profiler_builtins` crate \
584 which contains the profiler runtime and will never be stable",
585 ),
586
587 // ==========================================================================
588 // Internal attributes, Linkage:
589 // ==========================================================================
590
591 gated!(
592 linkage, Normal, template!(NameValueStr: "external|internal|..."), ErrorPreceding, @only_local: true,
593 "the `linkage` attribute is experimental and not portable across platforms",
594 ),
595 rustc_attr!(
596 rustc_std_internal_symbol, Normal, template!(Word), WarnFollowing, @only_local: true, INTERNAL_UNSTABLE
597 ),
598
599 // ==========================================================================
600 // Internal attributes, Macro related:
601 // ==========================================================================
602
603 rustc_attr!(
604 rustc_builtin_macro, Normal,
605 template!(Word, List: "name, /*opt*/ attributes(name1, name2, ...)"), ErrorFollowing,
606 IMPL_DETAIL,
607 ),
608 rustc_attr!(rustc_proc_macro_decls, Normal, template!(Word), WarnFollowing, INTERNAL_UNSTABLE),
609 rustc_attr!(
610 rustc_macro_transparency, Normal,
611 template!(NameValueStr: "transparent|semitransparent|opaque"), ErrorFollowing,
612 "used internally for testing macro hygiene",
613 ),
614
615 // ==========================================================================
616 // Internal attributes, Diagnostics related:
617 // ==========================================================================
618
619 rustc_attr!(
620 rustc_on_unimplemented, Normal,
621 template!(
622 List: r#"/*opt*/ message = "...", /*opt*/ label = "...", /*opt*/ note = "...""#,
623 NameValueStr: "message"
624 ),
625 ErrorFollowing,
626 INTERNAL_UNSTABLE
627 ),
628 // Enumerates "identity-like" conversion methods to suggest on type mismatch.
629 rustc_attr!(
630 rustc_conversion_suggestion, Normal, template!(Word), WarnFollowing, INTERNAL_UNSTABLE
631 ),
632 // Prevents field reads in the marked trait or method to be considered
633 // during dead code analysis.
634 rustc_attr!(
635 rustc_trivial_field_reads, Normal, template!(Word), WarnFollowing, INTERNAL_UNSTABLE
636 ),
637 // Used by the `rustc::potential_query_instability` lint to warn methods which
638 // might not be stable during incremental compilation.
639 rustc_attr!(rustc_lint_query_instability, Normal, template!(Word), WarnFollowing, INTERNAL_UNSTABLE),
640 // Used by the `rustc::untranslatable_diagnostic` and `rustc::diagnostic_outside_of_impl` lints
641 // to assist in changes to diagnostic APIs.
642 rustc_attr!(rustc_lint_diagnostics, Normal, template!(Word), WarnFollowing, INTERNAL_UNSTABLE),
643 // Used by the `rustc::bad_opt_access` lint to identify `DebuggingOptions` and `CodegenOptions`
644 // types (as well as any others in future).
645 rustc_attr!(rustc_lint_opt_ty, Normal, template!(Word), WarnFollowing, INTERNAL_UNSTABLE),
646 // Used by the `rustc::bad_opt_access` lint on fields
647 // types (as well as any others in future).
648 rustc_attr!(rustc_lint_opt_deny_field_access, Normal, template!(List: "message"), WarnFollowing, INTERNAL_UNSTABLE),
649
650 // ==========================================================================
651 // Internal attributes, Const related:
652 // ==========================================================================
653
654 rustc_attr!(rustc_promotable, Normal, template!(Word), WarnFollowing, IMPL_DETAIL),
655 rustc_attr!(
656 rustc_legacy_const_generics, Normal, template!(List: "N"), ErrorFollowing,
657 INTERNAL_UNSTABLE
658 ),
659 // Do not const-check this function's body. It will always get replaced during CTFE.
660 rustc_attr!(
661 rustc_do_not_const_check, Normal, template!(Word), WarnFollowing, INTERNAL_UNSTABLE
662 ),
663
664 // ==========================================================================
665 // Internal attributes, Layout related:
666 // ==========================================================================
667
668 rustc_attr!(
669 rustc_layout_scalar_valid_range_start, Normal, template!(List: "value"), ErrorFollowing,
670 "the `#[rustc_layout_scalar_valid_range_start]` attribute is just used to enable \
671 niche optimizations in libcore and libstd and will never be stable",
672 ),
673 rustc_attr!(
674 rustc_layout_scalar_valid_range_end, Normal, template!(List: "value"), ErrorFollowing,
675 "the `#[rustc_layout_scalar_valid_range_end]` attribute is just used to enable \
676 niche optimizations in libcore and libstd and will never be stable",
677 ),
678 rustc_attr!(
679 rustc_nonnull_optimization_guaranteed, Normal, template!(Word), WarnFollowing,
680 "the `#[rustc_nonnull_optimization_guaranteed]` attribute is just used to enable \
681 niche optimizations in libcore and libstd and will never be stable",
682 ),
683
684 // ==========================================================================
685 // Internal attributes, Misc:
686 // ==========================================================================
687 gated!(
688 lang, Normal, template!(NameValueStr: "name"), DuplicatesOk, @only_local: true, lang_items,
689 "language items are subject to change",
690 ),
691 rustc_attr!(
692 rustc_pass_by_value, Normal, template!(Word), ErrorFollowing,
693 "#[rustc_pass_by_value] is used to mark types that must be passed by value instead of reference."
694 ),
695 rustc_attr!(
696 rustc_coherence_is_core, AttributeType::CrateLevel, template!(Word), ErrorFollowing, @only_local: true,
697 "#![rustc_coherence_is_core] allows inherent methods on builtin types, only intended to be used in `core`."
698 ),
699 rustc_attr!(
700 rustc_coinductive, AttributeType::Normal, template!(Word), WarnFollowing, @only_local: true,
701 "#![rustc_coinductive] changes a trait to be coinductive, allowing cycles in the trait solver."
702 ),
703 rustc_attr!(
704 rustc_allow_incoherent_impl, AttributeType::Normal, template!(Word), ErrorFollowing, @only_local: true,
705 "#[rustc_allow_incoherent_impl] has to be added to all impl items of an incoherent inherent impl."
706 ),
707 rustc_attr!(
708 rustc_deny_explicit_impl,
709 AttributeType::Normal,
710 template!(List: "implement_via_object = (true|false)"),
711 ErrorFollowing,
712 @only_local: true,
713 "#[rustc_deny_explicit_impl] enforces that a trait can have no user-provided impls"
714 ),
715 rustc_attr!(
716 rustc_has_incoherent_inherent_impls, AttributeType::Normal, template!(Word), ErrorFollowing,
717 "#[rustc_has_incoherent_inherent_impls] allows the addition of incoherent inherent impls for \
718 the given type by annotating all impl items with #[rustc_allow_incoherent_impl]."
719 ),
720 rustc_attr!(
721 rustc_box, AttributeType::Normal, template!(Word), ErrorFollowing,
722 "#[rustc_box] allows creating boxes \
723 and it is only intended to be used in `alloc`."
724 ),
725
726 rustc_attr!(
727 rustc_host, AttributeType::Normal, template!(Word), ErrorFollowing,
728 "#[rustc_host] annotates const generic parameters as the `host` effect param, \
729 and it is only intended for internal use and as a desugaring."
730 ),
731
732 BuiltinAttribute {
733 name: sym::rustc_diagnostic_item,
734 // FIXME: This can be `true` once we always use `tcx.is_diagnostic_item`.
735 only_local: false,
736 type_: Normal,
737 template: template!(NameValueStr: "name"),
738 duplicates: ErrorFollowing,
739 gate: Gated(
740 Stability::Unstable,
741 sym::rustc_attrs,
742 "diagnostic items compiler internal support for linting",
743 cfg_fn!(rustc_attrs),
744 ),
745 },
746 gated!(
747 // Used in resolve:
748 prelude_import, Normal, template!(Word), WarnFollowing,
749 "`#[prelude_import]` is for use by rustc only",
750 ),
751 gated!(
752 rustc_paren_sugar, Normal, template!(Word), WarnFollowing, unboxed_closures,
753 "unboxed_closures are still evolving",
754 ),
755 rustc_attr!(
756 rustc_inherit_overflow_checks, Normal, template!(Word), WarnFollowing, @only_local: true,
757 "the `#[rustc_inherit_overflow_checks]` attribute is just used to control \
758 overflow checking behavior of several libcore functions that are inlined \
759 across crates and will never be stable",
760 ),
761 rustc_attr!(
762 rustc_reservation_impl, Normal,
763 template!(NameValueStr: "reservation message"), ErrorFollowing,
764 "the `#[rustc_reservation_impl]` attribute is internally used \
765 for reserving for `for<T> From<!> for T` impl"
766 ),
767 rustc_attr!(
768 rustc_test_marker, Normal, template!(NameValueStr: "name"), WarnFollowing,
769 "the `#[rustc_test_marker]` attribute is used internally to track tests",
770 ),
771 rustc_attr!(
772 rustc_unsafe_specialization_marker, Normal, template!(Word), WarnFollowing,
773 "the `#[rustc_unsafe_specialization_marker]` attribute is used to check specializations"
774 ),
775 rustc_attr!(
776 rustc_specialization_trait, Normal, template!(Word), WarnFollowing,
777 "the `#[rustc_specialization_trait]` attribute is used to check specializations"
778 ),
779 rustc_attr!(
780 rustc_main, Normal, template!(Word), WarnFollowing,
781 "the `#[rustc_main]` attribute is used internally to specify test entry point function",
782 ),
783 rustc_attr!(
784 rustc_skip_array_during_method_dispatch, Normal, template!(Word), WarnFollowing,
785 "the `#[rustc_skip_array_during_method_dispatch]` attribute is used to exclude a trait \
786 from method dispatch when the receiver is an array, for compatibility in editions < 2021."
787 ),
788 rustc_attr!(
789 rustc_must_implement_one_of, Normal, template!(List: "function1, function2, ..."), ErrorFollowing,
790 "the `#[rustc_must_implement_one_of]` attribute is used to change minimal complete \
791 definition of a trait, it's currently in experimental form and should be changed before \
792 being exposed outside of the std"
793 ),
794 rustc_attr!(
795 rustc_doc_primitive, Normal, template!(NameValueStr: "primitive name"), ErrorFollowing,
796 r#"`rustc_doc_primitive` is a rustc internal attribute"#,
797 ),
798
799 // ==========================================================================
800 // Internal attributes, Testing:
801 // ==========================================================================
802
803 rustc_attr!(TEST, rustc_effective_visibility, Normal, template!(Word), WarnFollowing),
804 rustc_attr!(TEST, rustc_outlives, Normal, template!(Word), WarnFollowing),
805 rustc_attr!(TEST, rustc_capture_analysis, Normal, template!(Word), WarnFollowing),
806 rustc_attr!(TEST, rustc_insignificant_dtor, Normal, template!(Word), WarnFollowing),
807 rustc_attr!(TEST, rustc_strict_coherence, Normal, template!(Word), WarnFollowing),
808 rustc_attr!(TEST, rustc_variance, Normal, template!(Word), WarnFollowing),
809 rustc_attr!(TEST, rustc_layout, Normal, template!(List: "field1, field2, ..."), WarnFollowing),
810 rustc_attr!(TEST, rustc_regions, Normal, template!(Word), WarnFollowing),
811 rustc_attr!(
812 TEST, rustc_error, Normal,
813 template!(Word, List: "delay_span_bug_from_inside_query"), WarnFollowingWordOnly
814 ),
815 rustc_attr!(TEST, rustc_dump_user_substs, Normal, template!(Word), WarnFollowing),
816 rustc_attr!(TEST, rustc_evaluate_where_clauses, Normal, template!(Word), WarnFollowing),
817 rustc_attr!(
818 TEST, rustc_if_this_changed, Normal, template!(Word, List: "DepNode"), DuplicatesOk
819 ),
820 rustc_attr!(
821 TEST, rustc_then_this_would_need, Normal, template!(List: "DepNode"), DuplicatesOk
822 ),
823 rustc_attr!(
824 TEST, rustc_clean, Normal,
825 template!(List: r#"cfg = "...", /*opt*/ label = "...", /*opt*/ except = "...""#),
826 DuplicatesOk,
827 ),
828 rustc_attr!(
829 TEST, rustc_partition_reused, Normal,
830 template!(List: r#"cfg = "...", module = "...""#), DuplicatesOk,
831 ),
832 rustc_attr!(
833 TEST, rustc_partition_codegened, Normal,
834 template!(List: r#"cfg = "...", module = "...""#), DuplicatesOk,
835 ),
836 rustc_attr!(
837 TEST, rustc_expected_cgu_reuse, Normal,
838 template!(List: r#"cfg = "...", module = "...", kind = "...""#), DuplicatesOk,
839 ),
840 rustc_attr!(TEST, rustc_symbol_name, Normal, template!(Word), WarnFollowing),
841 rustc_attr!(TEST, rustc_polymorphize_error, Normal, template!(Word), WarnFollowing),
842 rustc_attr!(TEST, rustc_def_path, Normal, template!(Word), WarnFollowing),
843 rustc_attr!(TEST, rustc_mir, Normal, template!(List: "arg1, arg2, ..."), DuplicatesOk),
844 gated!(
845 custom_mir, Normal, template!(List: r#"dialect = "...", phase = "...""#),
846 ErrorFollowing, "the `#[custom_mir]` attribute is just used for the Rust test suite",
847 ),
848 rustc_attr!(TEST, rustc_dump_program_clauses, Normal, template!(Word), WarnFollowing),
849 rustc_attr!(TEST, rustc_dump_env_program_clauses, Normal, template!(Word), WarnFollowing),
850 rustc_attr!(TEST, rustc_object_lifetime_default, Normal, template!(Word), WarnFollowing),
851 rustc_attr!(TEST, rustc_dump_vtable, Normal, template!(Word), WarnFollowing),
852 rustc_attr!(TEST, rustc_dummy, Normal, template!(Word /* doesn't matter*/), DuplicatesOk),
853 gated!(
854 omit_gdb_pretty_printer_section, Normal, template!(Word), WarnFollowing,
855 "the `#[omit_gdb_pretty_printer_section]` attribute is just used for the Rust test suite",
856 ),
857 ];
858
deprecated_attributes() -> Vec<&'static BuiltinAttribute>859 pub fn deprecated_attributes() -> Vec<&'static BuiltinAttribute> {
860 BUILTIN_ATTRIBUTES.iter().filter(|attr| attr.gate.is_deprecated()).collect()
861 }
862
is_builtin_attr_name(name: Symbol) -> bool863 pub fn is_builtin_attr_name(name: Symbol) -> bool {
864 BUILTIN_ATTRIBUTE_MAP.get(&name).is_some()
865 }
866
867 /// Whether this builtin attribute is only used in the local crate.
868 /// If so, it is not encoded in the crate metadata.
is_builtin_only_local(name: Symbol) -> bool869 pub fn is_builtin_only_local(name: Symbol) -> bool {
870 BUILTIN_ATTRIBUTE_MAP.get(&name).is_some_and(|attr| attr.only_local)
871 }
872
is_valid_for_get_attr(name: Symbol) -> bool873 pub fn is_valid_for_get_attr(name: Symbol) -> bool {
874 BUILTIN_ATTRIBUTE_MAP.get(&name).is_some_and(|attr| match attr.duplicates {
875 WarnFollowing | ErrorFollowing | ErrorPreceding | FutureWarnFollowing
876 | FutureWarnPreceding => true,
877 DuplicatesOk | WarnFollowingWordOnly => false,
878 })
879 }
880
881 pub static BUILTIN_ATTRIBUTE_MAP: LazyLock<FxHashMap<Symbol, &BuiltinAttribute>> =
882 LazyLock::new(|| {
883 let mut map = FxHashMap::default();
884 for attr in BUILTIN_ATTRIBUTES.iter() {
885 if map.insert(attr.name, attr).is_some() {
886 panic!("duplicate builtin attribute `{}`", attr.name);
887 }
888 }
889 map
890 });
891