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