]> git.lizzy.rs Git - rust.git/blob - crates/hir_def/src/builtin_attr.rs
parameters.split_last()
[rust.git] / crates / hir_def / src / builtin_attr.rs
1 //! Builtin attributes resolved by nameres.
2 //!
3 //! The actual definitions were copied from rustc's `compiler/rustc_feature/src/builtin_attrs.rs`.
4 //!
5 //! It was last synchronized with upstream commit ae90dcf0207c57c3034f00b07048d63f8b2363c8.
6 //!
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
9 //! ease updating.
10
11 use once_cell::sync::OnceCell;
12 use rustc_hash::FxHashMap;
13
14 /// Ignored attribute namespaces used by tools.
15 pub const TOOL_MODULES: &[&str] = &["rustfmt", "clippy"];
16
17 pub struct BuiltinAttribute {
18     pub name: &'static str,
19     pub template: AttributeTemplate,
20 }
21
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 {
26     pub word: bool,
27     pub list: Option<&'static str>,
28     pub name_value_str: Option<&'static str>,
29 }
30
31 pub fn find_builtin_attr_idx(name: &str) -> Option<usize> {
32     static BUILTIN_LOOKUP_TABLE: OnceCell<FxHashMap<&'static str, usize>> = OnceCell::new();
33     BUILTIN_LOOKUP_TABLE
34         .get_or_init(|| {
35             INERT_ATTRIBUTES.iter().map(|attr| attr.name).enumerate().map(|(a, b)| (b, a)).collect()
36         })
37         .get(name)
38         .copied()
39 }
40
41 // impl AttributeTemplate {
42 //     const DEFAULT: AttributeTemplate =
43 //         AttributeTemplate { word: false, list: None, name_value_str: None };
44 // }
45
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))
57     };
58     (Word, List: $descr1: expr, NameValueStr: $descr2: expr) => {
59         template!(@ true, Some($descr1), Some($descr2))
60     };
61     (@ $word: expr, $list: expr, $name_value_str: expr) => {
62         AttributeTemplate {
63             word: $word, list: $list, name_value_str: $name_value_str
64         }
65     };
66 }
67
68 macro_rules! ungated {
69     ($attr:ident, $typ:expr, $tpl:expr $(,)?) => {
70         BuiltinAttribute { name: stringify!($attr), template: $tpl }
71     };
72 }
73
74 macro_rules! gated {
75     ($attr:ident, $typ:expr, $tpl:expr, $gate:ident, $msg:expr $(,)?) => {
76         BuiltinAttribute { name: stringify!($attr), template: $tpl }
77     };
78     ($attr:ident, $typ:expr, $tpl:expr, $msg:expr $(,)?) => {
79         BuiltinAttribute { name: stringify!($attr), template: $tpl }
80     };
81 }
82
83 macro_rules! rustc_attr {
84     (TEST, $attr:ident, $typ:expr, $tpl:expr $(,)?) => {
85         rustc_attr!(
86             $attr,
87             $typ,
88             $tpl,
89             concat!(
90                 "the `#[",
91                 stringify!($attr),
92                 "]` attribute is just used for rustc unit tests \
93                 and will never be stable",
94             ),
95         )
96     };
97     ($attr:ident, $typ:expr, $tpl:expr, $msg:expr $(,)?) => {
98         BuiltinAttribute { name: stringify!($attr), template: $tpl }
99     };
100 }
101
102 /// "Inert" built-in attributes that have a special meaning to rustc or rustdoc.
103 #[rustfmt::skip]
104 pub const INERT_ATTRIBUTES: &[BuiltinAttribute] = &[
105     // ==========================================================================
106     // Stable attributes:
107     // ==========================================================================
108
109     // Conditional compilation:
110     ungated!(cfg, Normal, template!(List: "predicate")),
111     ungated!(cfg_attr, Normal, template!(List: "predicate, attr1, attr2, ...")),
112
113     // Testing:
114     ungated!(ignore, Normal, template!(Word, NameValueStr: "reason")),
115     ungated!(
116         should_panic, Normal,
117         template!(Word, List: r#"expected = "reason"#, NameValueStr: "reason"),
118     ),
119     // FIXME(Centril): This can be used on stable but shouldn't.
120     ungated!(reexport_test_harness_main, Normal, template!(NameValueStr: "name")),
121
122     // Macros:
123     ungated!(automatically_derived, Normal, template!(Word)),
124     // FIXME(#14407)
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)),
129     ungated!(
130         proc_macro_derive, Normal,
131         template!(List: "TraitName, /*opt*/ attributes(name1, name2, ...)"),
132     ),
133     ungated!(proc_macro_attribute, Normal, template!(Word)),
134
135     // Lints:
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")),
141     // FIXME(#14407)
142     ungated!(
143         deprecated, Normal,
144         template!(
145             Word,
146             List: r#"/*opt*/ since = "version", /*opt*/ note = "reason""#,
147             NameValueStr: "reason"
148         ),
149     ),
150
151     // Crate properties:
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")),
155
156     // ABI, linking, symbols, and FFI
157     ungated!(
158         link, AssumedUsed,
159         template!(List: r#"name = "...", /*opt*/ kind = "dylib|static|...", /*opt*/ wasm_import_module = "...""#),
160     ),
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)),
168
169     // Limits:
170     ungated!(recursion_limit, CrateLevel, template!(NameValueStr: "N")),
171     ungated!(type_length_limit, CrateLevel, template!(NameValueStr: "N")),
172     gated!(
173         const_eval_limit, CrateLevel, template!(NameValueStr: "N"), const_eval_limit,
174         experimental!(const_eval_limit)
175     ),
176     gated!(
177         move_size_limit, CrateLevel, template!(NameValueStr: "N"), large_assignments,
178         experimental!(move_size_limit)
179     ),
180
181     // Entry point:
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)),
186
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)),
192
193     // Runtime
194     ungated!(windows_subsystem, AssumedUsed, template!(NameValueStr: "windows|console")),
195     ungated!(panic_handler, Normal, template!(Word)), // RFC 2070
196
197     // Code generation:
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)),
203     gated!(
204         no_sanitize, AssumedUsed,
205         template!(List: "address, memory, thread"),
206         experimental!(no_sanitize)
207     ),
208     gated!(no_coverage, AssumedUsed, template!(Word), experimental!(no_coverage)),
209
210     // FIXME: #14408 assume docs are used since rustdoc looks at them.
211     ungated!(doc, AssumedUsed, template!(List: "hidden|inline|...", NameValueStr: "string")),
212
213     // ==========================================================================
214     // Unstable attributes:
215     // ==========================================================================
216
217     // Linking:
218     gated!(naked, AssumedUsed, template!(Word), naked_functions, experimental!(naked)),
219     gated!(
220         link_ordinal, AssumedUsed, template!(List: "ordinal"), raw_dylib,
221         experimental!(link_ordinal)
222     ),
223
224     // Plugins:
225     // XXX Modified for use in rust-analyzer
226     gated!(plugin_registrar, Normal, template!(Word), experimental!()),
227     gated!(plugin, CrateLevel, template!(Word), experimental!()),
228
229     // Testing:
230     gated!(allow_fail, Normal, template!(Word), experimental!(allow_fail)),
231     gated!(
232         test_runner, CrateLevel, template!(List: "path"), custom_test_frameworks,
233         "custom test frameworks are an unstable feature",
234     ),
235     // RFC #1268
236     gated!(marker, AssumedUsed, template!(Word), marker_trait_attr, experimental!(marker)),
237     gated!(
238         thread_local, AssumedUsed, template!(Word),
239         "`#[thread_local]` is an experimental feature, and does not currently handle destructors",
240     ),
241     gated!(no_core, CrateLevel, template!(Word), experimental!(no_core)),
242     // RFC 2412
243     gated!(
244         optimize, AssumedUsed, template!(List: "size|speed"), optimize_attribute,
245         experimental!(optimize),
246     ),
247     // RFC 2867
248     gated!(instruction_set, AssumedUsed, template!(List: "set"), isa_attribute, experimental!(instruction_set)),
249
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)),
253     gated!(
254         register_attr, CrateLevel, template!(List: "attr1, attr2, ..."),
255         experimental!(register_attr),
256     ),
257     gated!(
258         register_tool, CrateLevel, template!(List: "tool1, tool2, ..."),
259         experimental!(register_tool),
260     ),
261
262     gated!(cmse_nonsecure_entry, AssumedUsed, template!(Word), experimental!(cmse_nonsecure_entry)),
263     // RFC 2632
264     gated!(
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."
268     ),
269
270     // ==========================================================================
271     // Internal attributes: Stability, deprecation, and unsafe:
272     // ==========================================================================
273
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.
277     ungated!(
278         rustc_deprecated, AssumedUsed,
279         template!(List: r#"since = "version", reason = "...""#)
280     ),
281     // FIXME(#14407)
282     ungated!(stable, AssumedUsed, template!(List: r#"feature = "name", since = "version""#)),
283     // FIXME(#14407)
284     ungated!(
285         unstable, AssumedUsed,
286         template!(List: r#"feature = "name", reason = "...", issue = "N""#),
287     ),
288     // FIXME(#14407)
289     ungated!(rustc_const_unstable, AssumedUsed, template!(List: r#"feature = "name""#)),
290     // FIXME(#14407)
291     ungated!(rustc_const_stable, AssumedUsed, template!(List: r#"feature = "name""#)),
292     gated!(
293         allow_internal_unstable, AssumedUsed, template!(Word, List: "feat1, feat2, ..."),
294         "allow_internal_unstable side-steps feature gating and stability checks",
295     ),
296     gated!(
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"
299     ),
300     gated!(
301         allow_internal_unsafe, Normal, template!(Word),
302         "allow_internal_unsafe side-steps the unsafe_code lint",
303     ),
304
305     // ==========================================================================
306     // Internal attributes: Type system related:
307     // ==========================================================================
308
309     gated!(fundamental, AssumedUsed, template!(Word), experimental!(fundamental)),
310     gated!(
311         may_dangle, Normal, template!(Word), dropck_eyepatch,
312         "`may_dangle` has unstable semantics and may be removed in the future",
313     ),
314
315     // ==========================================================================
316     // Internal attributes: Runtime related:
317     // ==========================================================================
318
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)),
322     gated!(
323         default_lib_allocator, AssumedUsed, template!(Word), allocator_internals,
324         experimental!(default_lib_allocator),
325     ),
326     gated!(
327         needs_allocator, Normal, template!(Word), allocator_internals,
328         experimental!(needs_allocator),
329     ),
330     gated!(panic_runtime, AssumedUsed, template!(Word), experimental!(panic_runtime)),
331     gated!(needs_panic_runtime, AssumedUsed, template!(Word), experimental!(needs_panic_runtime)),
332     gated!(
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",
336     ),
337     gated!(
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",
341     ),
342
343     // ==========================================================================
344     // Internal attributes, Linkage:
345     // ==========================================================================
346
347     gated!(
348         linkage, AssumedUsed, template!(NameValueStr: "external|internal|..."),
349         "the `linkage` attribute is experimental and not portable across platforms",
350     ),
351     rustc_attr!(rustc_std_internal_symbol, AssumedUsed, template!(Word), INTERNAL_UNSTABLE),
352
353     // ==========================================================================
354     // Internal attributes, Macro related:
355     // ==========================================================================
356
357     rustc_attr!(
358         rustc_builtin_macro, AssumedUsed,
359         template!(Word, List: "name, /*opt*/ attributes(name1, name2, ...)"),
360         IMPL_DETAIL,
361     ),
362     rustc_attr!(rustc_proc_macro_decls, Normal, template!(Word), INTERNAL_UNSTABLE),
363     rustc_attr!(
364         rustc_macro_transparency, AssumedUsed,
365         template!(NameValueStr: "transparent|semitransparent|opaque"),
366         "used internally for testing macro hygiene",
367     ),
368
369     // ==========================================================================
370     // Internal attributes, Diagnostics related:
371     // ==========================================================================
372
373     rustc_attr!(
374         rustc_on_unimplemented, AssumedUsed,
375         template!(
376             List: r#"/*opt*/ message = "...", /*opt*/ label = "...", /*opt*/ note = "...""#,
377             NameValueStr: "message"
378         ),
379         INTERNAL_UNSTABLE
380     ),
381     // Enumerates "identity-like" conversion methods to suggest on type mismatch.
382     rustc_attr!(rustc_conversion_suggestion, AssumedUsed, template!(Word), INTERNAL_UNSTABLE),
383
384     // ==========================================================================
385     // Internal attributes, Const related:
386     // ==========================================================================
387
388     rustc_attr!(rustc_promotable, AssumedUsed, template!(Word), IMPL_DETAIL),
389     rustc_attr!(rustc_legacy_const_generics, AssumedUsed, template!(List: "N"), INTERNAL_UNSTABLE),
390
391     // ==========================================================================
392     // Internal attributes, Layout related:
393     // ==========================================================================
394
395     rustc_attr!(
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",
399     ),
400     rustc_attr!(
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",
404     ),
405     rustc_attr!(
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",
409     ),
410
411     // ==========================================================================
412     // Internal attributes, Misc:
413     // ==========================================================================
414     gated!(
415         lang, Normal, template!(NameValueStr: "name"), lang_items,
416         "language items are subject to change",
417     ),
418     gated!(rustc_diagnostic_item, Normal, template!(NameValueStr: "name"), experimental!()), // XXX Modified for use in rust-analyzer
419     gated!(
420         // Used in resolve:
421         prelude_import, AssumedUsed, template!(Word),
422         "`#[prelude_import]` is for use by rustc only",
423     ),
424     gated!(
425         rustc_paren_sugar, Normal, template!(Word), unboxed_closures,
426         "unboxed_closures are still evolving",
427     ),
428     rustc_attr!(
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",
433     ),
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"
437     ),
438     rustc_attr!(
439         rustc_test_marker, Normal, template!(Word),
440         "the `#[rustc_test_marker]` attribute is used internally to track tests",
441     ),
442     rustc_attr!(
443         rustc_unsafe_specialization_marker, Normal, template!(Word),
444         "the `#[rustc_unsafe_specialization_marker]` attribute is used to check specializations"
445     ),
446     rustc_attr!(
447         rustc_specialization_trait, Normal, template!(Word),
448         "the `#[rustc_specialization_trait]` attribute is used to check specializations"
449     ),
450     rustc_attr!(
451         rustc_main, Normal, template!(Word),
452         "the `#[rustc_main]` attribute is used internally to specify test entry point function",
453     ),
454     rustc_attr!(
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."
458     ),
459
460     // ==========================================================================
461     // Internal attributes, Testing:
462     // ==========================================================================
463
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)),
470     rustc_attr!(
471         TEST, rustc_error, AssumedUsed,
472         template!(Word, List: "delay_span_bug_from_inside_query")
473     ),
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")),
478     rustc_attr!(
479         TEST, rustc_clean, AssumedUsed,
480         template!(List: r#"cfg = "...", /*opt*/ label = "...", /*opt*/ except = "...""#),
481     ),
482     rustc_attr!(
483         TEST, rustc_partition_reused, AssumedUsed,
484         template!(List: r#"cfg = "...", module = "...""#),
485     ),
486     rustc_attr!(
487         TEST, rustc_partition_codegened, AssumedUsed,
488         template!(List: r#"cfg = "...", module = "...""#),
489     ),
490     rustc_attr!(
491         TEST, rustc_expected_cgu_reuse, AssumedUsed,
492         template!(List: r#"cfg = "...", module = "...", kind = "...""#),
493     ),
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*/)),
504     gated!(
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",
507     ),
508 ];