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