]> git.lizzy.rs Git - rust.git/blob - crates/hir_def/src/builtin_attr.rs
Merge #10025
[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 835150e70288535bc57bb624792229b9dc94991d.
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 /// Ignored attribute namespaces used by tools.
12 pub const TOOL_MODULES: &[&str] = &["rustfmt", "clippy"];
13
14 type BuiltinAttribute = &'static str;
15
16 macro_rules! ungated {
17     ($attr:ident, $typ:expr, $tpl:expr $(,)?) => {
18         stringify!($attr)
19     };
20 }
21
22 macro_rules! gated {
23     ($attr:ident $($rest:tt)*) => {
24         stringify!($attr)
25     };
26 }
27
28 macro_rules! rustc_attr {
29     (TEST, $attr:ident $($rest:tt)*) => {
30         stringify!($attr)
31     };
32     ($attr:ident $($rest:tt)*) => {
33         stringify!($attr)
34     };
35 }
36
37 // FIXME: We shouldn't special case these at all, but as of now expanding attributes severely degrades
38 // user experience due to lacking support.
39 /// Built-in macro-like attributes.
40 pub const EXTRA_ATTRIBUTES: &[BuiltinAttribute] = &["test", "bench"];
41
42 /// "Inert" built-in attributes that have a special meaning to rustc or rustdoc.
43 #[rustfmt::skip]
44 pub const INERT_ATTRIBUTES: &[BuiltinAttribute] = &[
45     // ==========================================================================
46     // Stable attributes:
47     // ==========================================================================
48
49     // Conditional compilation:
50     ungated!(cfg, Normal, template!(List: "predicate")),
51     ungated!(cfg_attr, Normal, template!(List: "predicate, attr1, attr2, ...")),
52
53     // Testing:
54     ungated!(ignore, Normal, template!(Word, NameValueStr: "reason")),
55     ungated!(
56         should_panic, Normal,
57         template!(Word, List: r#"expected = "reason"#, NameValueStr: "reason"),
58     ),
59     // FIXME(Centril): This can be used on stable but shouldn't.
60     ungated!(reexport_test_harness_main, Normal, template!(NameValueStr: "name")),
61
62     // Macros:
63     ungated!(automatically_derived, Normal, template!(Word)),
64     // FIXME(#14407)
65     ungated!(macro_use, Normal, template!(Word, List: "name1, name2, ...")),
66     ungated!(macro_escape, Normal, template!(Word)), // Deprecated synonym for `macro_use`.
67     ungated!(macro_export, Normal, template!(Word, List: "local_inner_macros")),
68     ungated!(proc_macro, Normal, template!(Word)),
69     ungated!(
70         proc_macro_derive, Normal,
71         template!(List: "TraitName, /*opt*/ attributes(name1, name2, ...)"),
72     ),
73     ungated!(proc_macro_attribute, Normal, template!(Word)),
74
75     // Lints:
76     ungated!(warn, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#)),
77     ungated!(allow, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#)),
78     ungated!(forbid, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#)),
79     ungated!(deny, Normal, template!(List: r#"lint1, lint2, ..., /*opt*/ reason = "...""#)),
80     ungated!(must_use, AssumedUsed, template!(Word, NameValueStr: "reason")),
81     // FIXME(#14407)
82     ungated!(
83         deprecated, Normal,
84         template!(
85             Word,
86             List: r#"/*opt*/ since = "version", /*opt*/ note = "reason""#,
87             NameValueStr: "reason"
88         ),
89     ),
90
91     // Crate properties:
92     ungated!(crate_name, CrateLevel, template!(NameValueStr: "name")),
93     ungated!(crate_type, CrateLevel, template!(NameValueStr: "bin|lib|...")),
94     ungated!(crate_id, CrateLevel, template!(NameValueStr: "ignored")),
95
96     // ABI, linking, symbols, and FFI
97     ungated!(
98         link, AssumedUsed,
99         template!(List: r#"name = "...", /*opt*/ kind = "dylib|static|...", /*opt*/ wasm_import_module = "...""#),
100     ),
101     ungated!(link_name, AssumedUsed, template!(NameValueStr: "name")),
102     ungated!(no_link, AssumedUsed, template!(Word)),
103     ungated!(repr, AssumedUsed, template!(List: "C")),
104     ungated!(export_name, AssumedUsed, template!(NameValueStr: "name")),
105     ungated!(link_section, AssumedUsed, template!(NameValueStr: "name")),
106     ungated!(no_mangle, AssumedUsed, template!(Word)),
107     ungated!(used, AssumedUsed, template!(Word)),
108
109     // Limits:
110     ungated!(recursion_limit, CrateLevel, template!(NameValueStr: "N")),
111     ungated!(type_length_limit, CrateLevel, template!(NameValueStr: "N")),
112     gated!(
113         const_eval_limit, CrateLevel, template!(NameValueStr: "N"), const_eval_limit,
114         experimental!(const_eval_limit)
115     ),
116     gated!(
117         move_size_limit, CrateLevel, template!(NameValueStr: "N"), large_assignments,
118         experimental!(move_size_limit)
119     ),
120
121     // Entry point:
122     ungated!(main, Normal, template!(Word)),
123     ungated!(start, Normal, template!(Word)),
124     ungated!(no_start, CrateLevel, template!(Word)),
125     ungated!(no_main, CrateLevel, template!(Word)),
126
127     // Modules, prelude, and resolution:
128     ungated!(path, Normal, template!(NameValueStr: "file")),
129     ungated!(no_std, CrateLevel, template!(Word)),
130     ungated!(no_implicit_prelude, Normal, template!(Word)),
131     ungated!(non_exhaustive, AssumedUsed, template!(Word)),
132
133     // Runtime
134     ungated!(windows_subsystem, AssumedUsed, template!(NameValueStr: "windows|console")),
135     ungated!(panic_handler, Normal, template!(Word)), // RFC 2070
136
137     // Code generation:
138     ungated!(inline, AssumedUsed, template!(Word, List: "always|never")),
139     ungated!(cold, AssumedUsed, template!(Word)),
140     ungated!(no_builtins, AssumedUsed, template!(Word)),
141     ungated!(target_feature, AssumedUsed, template!(List: r#"enable = "name""#)),
142     ungated!(track_caller, AssumedUsed, template!(Word)),
143     gated!(
144         no_sanitize, AssumedUsed,
145         template!(List: "address, memory, thread"),
146         experimental!(no_sanitize)
147     ),
148     gated!(no_coverage, AssumedUsed, template!(Word), experimental!(no_coverage)),
149
150     // FIXME: #14408 assume docs are used since rustdoc looks at them.
151     ungated!(doc, AssumedUsed, template!(List: "hidden|inline|...", NameValueStr: "string")),
152
153     // ==========================================================================
154     // Unstable attributes:
155     // ==========================================================================
156
157     // Linking:
158     gated!(naked, AssumedUsed, template!(Word), naked_functions, experimental!(naked)),
159     gated!(
160         link_ordinal, AssumedUsed, template!(List: "ordinal"), raw_dylib,
161         experimental!(link_ordinal)
162     ),
163
164     // Plugins:
165     // XXX Modified for use in rust-analyzer
166     gated!(plugin_registrar),
167     gated!(plugin),
168
169     // Testing:
170     gated!(allow_fail, Normal, template!(Word), experimental!(allow_fail)),
171     gated!(
172         test_runner, CrateLevel, template!(List: "path"), custom_test_frameworks,
173         "custom test frameworks are an unstable feature",
174     ),
175     // RFC #1268
176     gated!(marker, AssumedUsed, template!(Word), marker_trait_attr, experimental!(marker)),
177     gated!(
178         thread_local, AssumedUsed, template!(Word),
179         "`#[thread_local]` is an experimental feature, and does not currently handle destructors",
180     ),
181     gated!(no_core, CrateLevel, template!(Word), experimental!(no_core)),
182     // RFC 2412
183     gated!(
184         optimize, AssumedUsed, template!(List: "size|speed"), optimize_attribute,
185         experimental!(optimize),
186     ),
187     // RFC 2867
188     gated!(instruction_set, AssumedUsed, template!(List: "set"), isa_attribute, experimental!(instruction_set)),
189
190     gated!(ffi_returns_twice, AssumedUsed, template!(Word), experimental!(ffi_returns_twice)),
191     gated!(ffi_pure, AssumedUsed, template!(Word), experimental!(ffi_pure)),
192     gated!(ffi_const, AssumedUsed, template!(Word), experimental!(ffi_const)),
193     gated!(
194         register_attr, CrateLevel, template!(List: "attr1, attr2, ..."),
195         experimental!(register_attr),
196     ),
197     gated!(
198         register_tool, CrateLevel, template!(List: "tool1, tool2, ..."),
199         experimental!(register_tool),
200     ),
201
202     gated!(cmse_nonsecure_entry, AssumedUsed, template!(Word), experimental!(cmse_nonsecure_entry)),
203
204     // ==========================================================================
205     // Internal attributes: Stability, deprecation, and unsafe:
206     // ==========================================================================
207
208     ungated!(feature, CrateLevel, template!(List: "name1, name1, ...")),
209     // FIXME(#14407) -- only looked at on-demand so we can't
210     // guarantee they'll have already been checked.
211     ungated!(
212         rustc_deprecated, AssumedUsed,
213         template!(List: r#"since = "version", reason = "...""#)
214     ),
215     // FIXME(#14407)
216     ungated!(stable, AssumedUsed, template!(List: r#"feature = "name", since = "version""#)),
217     // FIXME(#14407)
218     ungated!(
219         unstable, AssumedUsed,
220         template!(List: r#"feature = "name", reason = "...", issue = "N""#),
221     ),
222     // FIXME(#14407)
223     ungated!(rustc_const_unstable, AssumedUsed, template!(List: r#"feature = "name""#)),
224     // FIXME(#14407)
225     ungated!(rustc_const_stable, AssumedUsed, template!(List: r#"feature = "name""#)),
226     gated!(
227         allow_internal_unstable, AssumedUsed, template!(Word, List: "feat1, feat2, ..."),
228         "allow_internal_unstable side-steps feature gating and stability checks",
229     ),
230     gated!(
231         rustc_allow_const_fn_unstable, AssumedUsed, template!(Word, List: "feat1, feat2, ..."),
232         "rustc_allow_const_fn_unstable side-steps feature gating and stability checks"
233     ),
234     gated!(
235         allow_internal_unsafe, Normal, template!(Word),
236         "allow_internal_unsafe side-steps the unsafe_code lint",
237     ),
238
239     // ==========================================================================
240     // Internal attributes: Type system related:
241     // ==========================================================================
242
243     gated!(fundamental, AssumedUsed, template!(Word), experimental!(fundamental)),
244     gated!(
245         may_dangle, Normal, template!(Word), dropck_eyepatch,
246         "`may_dangle` has unstable semantics and may be removed in the future",
247     ),
248
249     // ==========================================================================
250     // Internal attributes: Runtime related:
251     // ==========================================================================
252
253     rustc_attr!(rustc_allocator, AssumedUsed, template!(Word), IMPL_DETAIL),
254     rustc_attr!(rustc_allocator_nounwind, AssumedUsed, template!(Word), IMPL_DETAIL),
255     gated!(alloc_error_handler, Normal, template!(Word), experimental!(alloc_error_handler)),
256     gated!(
257         default_lib_allocator, AssumedUsed, template!(Word), allocator_internals,
258         experimental!(default_lib_allocator),
259     ),
260     gated!(
261         needs_allocator, Normal, template!(Word), allocator_internals,
262         experimental!(needs_allocator),
263     ),
264     gated!(panic_runtime, AssumedUsed, template!(Word), experimental!(panic_runtime)),
265     gated!(needs_panic_runtime, AssumedUsed, template!(Word), experimental!(needs_panic_runtime)),
266     gated!(
267         unwind, AssumedUsed, template!(List: "allowed|aborts"), unwind_attributes,
268         experimental!(unwind),
269     ),
270     gated!(
271         compiler_builtins, AssumedUsed, template!(Word),
272         "the `#[compiler_builtins]` attribute is used to identify the `compiler_builtins` crate \
273         which contains compiler-rt intrinsics and will never be stable",
274     ),
275     gated!(
276         profiler_runtime, AssumedUsed, template!(Word),
277         "the `#[profiler_runtime]` attribute is used to identify the `profiler_builtins` crate \
278         which contains the profiler runtime and will never be stable",
279     ),
280
281     // ==========================================================================
282     // Internal attributes, Linkage:
283     // ==========================================================================
284
285     gated!(
286         linkage, AssumedUsed, template!(NameValueStr: "external|internal|..."),
287         "the `linkage` attribute is experimental and not portable across platforms",
288     ),
289     rustc_attr!(rustc_std_internal_symbol, AssumedUsed, template!(Word), INTERNAL_UNSTABLE),
290
291     // ==========================================================================
292     // Internal attributes, Macro related:
293     // ==========================================================================
294
295     rustc_attr!(rustc_builtin_macro, AssumedUsed, template!(Word, NameValueStr: "name"), IMPL_DETAIL),
296     rustc_attr!(rustc_proc_macro_decls, Normal, template!(Word), INTERNAL_UNSTABLE),
297     rustc_attr!(
298         rustc_macro_transparency, AssumedUsed,
299         template!(NameValueStr: "transparent|semitransparent|opaque"),
300         "used internally for testing macro hygiene",
301     ),
302
303     // ==========================================================================
304     // Internal attributes, Diagnostics related:
305     // ==========================================================================
306
307     rustc_attr!(
308         rustc_on_unimplemented, AssumedUsed,
309         template!(
310             List: r#"/*opt*/ message = "...", /*opt*/ label = "...", /*opt*/ note = "...""#,
311             NameValueStr: "message"
312         ),
313         INTERNAL_UNSTABLE
314     ),
315     // Enumerates "identity-like" conversion methods to suggest on type mismatch.
316     rustc_attr!(rustc_conversion_suggestion, AssumedUsed, template!(Word), INTERNAL_UNSTABLE),
317
318     // ==========================================================================
319     // Internal attributes, Const related:
320     // ==========================================================================
321
322     rustc_attr!(rustc_promotable, AssumedUsed, template!(Word), IMPL_DETAIL),
323     rustc_attr!(rustc_legacy_const_generics, AssumedUsed, template!(List: "N"), INTERNAL_UNSTABLE),
324
325     // ==========================================================================
326     // Internal attributes, Layout related:
327     // ==========================================================================
328
329     rustc_attr!(
330         rustc_layout_scalar_valid_range_start, AssumedUsed, template!(List: "value"),
331         "the `#[rustc_layout_scalar_valid_range_start]` attribute is just used to enable \
332         niche optimizations in libcore and will never be stable",
333     ),
334     rustc_attr!(
335         rustc_layout_scalar_valid_range_end, AssumedUsed, template!(List: "value"),
336         "the `#[rustc_layout_scalar_valid_range_end]` attribute is just used to enable \
337         niche optimizations in libcore and will never be stable",
338     ),
339     rustc_attr!(
340         rustc_nonnull_optimization_guaranteed, AssumedUsed, template!(Word),
341         "the `#[rustc_nonnull_optimization_guaranteed]` attribute is just used to enable \
342         niche optimizations in libcore and will never be stable",
343     ),
344
345     // ==========================================================================
346     // Internal attributes, Misc:
347     // ==========================================================================
348     gated!(
349         lang, Normal, template!(NameValueStr: "name"), lang_items,
350         "language items are subject to change",
351     ),
352     gated!(rustc_diagnostic_item), // XXX modified in rust-analyzer
353     gated!(
354         // Used in resolve:
355         prelude_import, AssumedUsed, template!(Word),
356         "`#[prelude_import]` is for use by rustc only",
357     ),
358     gated!(
359         rustc_paren_sugar, Normal, template!(Word), unboxed_closures,
360         "unboxed_closures are still evolving",
361     ),
362     rustc_attr!(
363         rustc_inherit_overflow_checks, AssumedUsed, template!(Word),
364         "the `#[rustc_inherit_overflow_checks]` attribute is just used to control \
365         overflow checking behavior of several libcore functions that are inlined \
366         across crates and will never be stable",
367     ),
368     rustc_attr!(rustc_reservation_impl, Normal, template!(NameValueStr: "reservation message"),
369                 "the `#[rustc_reservation_impl]` attribute is internally used \
370                  for reserving for `for<T> From<!> for T` impl"
371     ),
372     rustc_attr!(
373         rustc_test_marker, Normal, template!(Word),
374         "the `#[rustc_test_marker]` attribute is used internally to track tests",
375     ),
376     rustc_attr!(
377         rustc_unsafe_specialization_marker, Normal, template!(Word),
378         "the `#[rustc_unsafe_specialization_marker]` attribute is used to check specializations"
379     ),
380     rustc_attr!(
381         rustc_specialization_trait, Normal, template!(Word),
382         "the `#[rustc_specialization_trait]` attribute is used to check specializations"
383     ),
384     rustc_attr!(
385         rustc_main, Normal, template!(Word),
386         "the `#[rustc_main]` attribute is used internally to specify test entry point function",
387     ),
388     rustc_attr!(
389         rustc_skip_array_during_method_dispatch, Normal, template!(Word),
390         "the `#[rustc_skip_array_during_method_dispatch]` attribute is used to exclude a trait \
391         from method dispatch when the receiver is an array, for compatibility in editions < 2021."
392     ),
393
394     // ==========================================================================
395     // Internal attributes, Testing:
396     // ==========================================================================
397
398     rustc_attr!(TEST, rustc_outlives, Normal, template!(Word)),
399     rustc_attr!(TEST, rustc_capture_analysis, Normal, template!(Word)),
400     rustc_attr!(TEST, rustc_insignificant_dtor, Normal, template!(Word)),
401     rustc_attr!(TEST, rustc_variance, Normal, template!(Word)),
402     rustc_attr!(TEST, rustc_layout, Normal, template!(List: "field1, field2, ...")),
403     rustc_attr!(TEST, rustc_regions, Normal, template!(Word)),
404     rustc_attr!(
405         TEST, rustc_error, AssumedUsed,
406         template!(Word, List: "delay_span_bug_from_inside_query")
407     ),
408     rustc_attr!(TEST, rustc_dump_user_substs, AssumedUsed, template!(Word)),
409     rustc_attr!(TEST, rustc_evaluate_where_clauses, AssumedUsed, template!(Word)),
410     rustc_attr!(TEST, rustc_if_this_changed, AssumedUsed, template!(Word, List: "DepNode")),
411     rustc_attr!(TEST, rustc_then_this_would_need, AssumedUsed, template!(List: "DepNode")),
412     rustc_attr!(
413         TEST, rustc_clean, AssumedUsed,
414         template!(List: r#"cfg = "...", /*opt*/ label = "...", /*opt*/ except = "...""#),
415     ),
416     rustc_attr!(
417         TEST, rustc_partition_reused, AssumedUsed,
418         template!(List: r#"cfg = "...", module = "...""#),
419     ),
420     rustc_attr!(
421         TEST, rustc_partition_codegened, AssumedUsed,
422         template!(List: r#"cfg = "...", module = "...""#),
423     ),
424     rustc_attr!(
425         TEST, rustc_expected_cgu_reuse, AssumedUsed,
426         template!(List: r#"cfg = "...", module = "...", kind = "...""#),
427     ),
428     rustc_attr!(TEST, rustc_synthetic, AssumedUsed, template!(Word)),
429     rustc_attr!(TEST, rustc_symbol_name, AssumedUsed, template!(Word)),
430     rustc_attr!(TEST, rustc_polymorphize_error, AssumedUsed, template!(Word)),
431     rustc_attr!(TEST, rustc_def_path, AssumedUsed, template!(Word)),
432     rustc_attr!(TEST, rustc_mir, AssumedUsed, template!(List: "arg1, arg2, ...")),
433     rustc_attr!(TEST, rustc_dump_program_clauses, AssumedUsed, template!(Word)),
434     rustc_attr!(TEST, rustc_dump_env_program_clauses, AssumedUsed, template!(Word)),
435     rustc_attr!(TEST, rustc_object_lifetime_default, AssumedUsed, template!(Word)),
436     rustc_attr!(TEST, rustc_dummy, Normal, template!(Word /* doesn't matter*/)),
437     gated!(
438         omit_gdb_pretty_printer_section, AssumedUsed, template!(Word),
439         "the `#[omit_gdb_pretty_printer_section]` attribute is just used for the Rust test suite",
440     ),
441 ];