]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_macros/src/lib.rs
Rollup merge of #102589 - RalfJung:scoped-threads-dangling, r=m-ou-se
[rust.git] / compiler / rustc_macros / src / lib.rs
1 #![feature(allow_internal_unstable)]
2 #![feature(never_type)]
3 #![feature(proc_macro_diagnostic)]
4 #![feature(proc_macro_span)]
5 #![allow(rustc::default_hash_types)]
6 #![deny(rustc::untranslatable_diagnostic)]
7 #![deny(rustc::diagnostic_outside_of_impl)]
8 #![recursion_limit = "128"]
9
10 use synstructure::decl_derive;
11
12 use proc_macro::TokenStream;
13
14 mod diagnostics;
15 mod hash_stable;
16 mod lift;
17 mod newtype;
18 mod query;
19 mod serialize;
20 mod symbols;
21 mod type_foldable;
22 mod type_visitable;
23
24 #[proc_macro]
25 pub fn rustc_queries(input: TokenStream) -> TokenStream {
26     query::rustc_queries(input)
27 }
28
29 #[proc_macro]
30 pub fn symbols(input: TokenStream) -> TokenStream {
31     symbols::symbols(input.into()).into()
32 }
33
34 /// Creates a struct type `S` that can be used as an index with
35 /// `IndexVec` and so on.
36 ///
37 /// There are two ways of interacting with these indices:
38 ///
39 /// - The `From` impls are the preferred way. So you can do
40 ///   `S::from(v)` with a `usize` or `u32`. And you can convert back
41 ///   to an integer with `u32::from(s)`.
42 ///
43 /// - Alternatively, you can use the methods `S::new(v)` and `s.index()`
44 ///   to create/return a value.
45 ///
46 /// Internally, the index uses a u32, so the index must not exceed
47 /// `u32::MAX`. You can also customize things like the `Debug` impl,
48 /// what traits are derived, and so forth via the macro.
49 #[proc_macro]
50 #[allow_internal_unstable(step_trait, rustc_attrs, trusted_step)]
51 pub fn newtype_index(input: TokenStream) -> TokenStream {
52     newtype::newtype(input)
53 }
54
55 /// Implements the `fluent_messages` macro, which performs compile-time validation of the
56 /// compiler's Fluent resources (i.e. that the resources parse and don't multiply define the same
57 /// messages) and generates constants that make using those messages in diagnostics more ergonomic.
58 ///
59 /// For example, given the following invocation of the macro..
60 ///
61 /// ```ignore (rust)
62 /// fluent_messages! {
63 ///     typeck => "./typeck.ftl",
64 /// }
65 /// ```
66 /// ..where `typeck.ftl` has the following contents..
67 ///
68 /// ```fluent
69 /// typeck_field_multiply_specified_in_initializer =
70 ///     field `{$ident}` specified more than once
71 ///     .label = used more than once
72 ///     .label_previous_use = first use of `{$ident}`
73 /// ```
74 /// ...then the macro parse the Fluent resource, emitting a diagnostic if it fails to do so, and
75 /// will generate the following code:
76 ///
77 /// ```ignore (rust)
78 /// pub static DEFAULT_LOCALE_RESOURCES: &'static [&'static str] = &[
79 ///     include_str!("./typeck.ftl"),
80 /// ];
81 ///
82 /// mod fluent_generated {
83 ///     mod typeck {
84 ///         pub const field_multiply_specified_in_initializer: DiagnosticMessage =
85 ///             DiagnosticMessage::fluent("typeck_field_multiply_specified_in_initializer");
86 ///         pub const field_multiply_specified_in_initializer_label_previous_use: DiagnosticMessage =
87 ///             DiagnosticMessage::fluent_attr(
88 ///                 "typeck_field_multiply_specified_in_initializer",
89 ///                 "previous_use_label"
90 ///             );
91 ///     }
92 /// }
93 /// ```
94 /// When emitting a diagnostic, the generated constants can be used as follows:
95 ///
96 /// ```ignore (rust)
97 /// let mut err = sess.struct_span_err(
98 ///     span,
99 ///     fluent::typeck::field_multiply_specified_in_initializer
100 /// );
101 /// err.span_default_label(span);
102 /// err.span_label(
103 ///     previous_use_span,
104 ///     fluent::typeck::field_multiply_specified_in_initializer_label_previous_use
105 /// );
106 /// err.emit();
107 /// ```
108 #[proc_macro]
109 pub fn fluent_messages(input: TokenStream) -> TokenStream {
110     diagnostics::fluent_messages(input)
111 }
112
113 decl_derive!([HashStable, attributes(stable_hasher)] => hash_stable::hash_stable_derive);
114 decl_derive!(
115     [HashStable_Generic, attributes(stable_hasher)] =>
116     hash_stable::hash_stable_generic_derive
117 );
118
119 decl_derive!([Decodable] => serialize::decodable_derive);
120 decl_derive!([Encodable] => serialize::encodable_derive);
121 decl_derive!([TyDecodable] => serialize::type_decodable_derive);
122 decl_derive!([TyEncodable] => serialize::type_encodable_derive);
123 decl_derive!([MetadataDecodable] => serialize::meta_decodable_derive);
124 decl_derive!([MetadataEncodable] => serialize::meta_encodable_derive);
125 decl_derive!([TypeFoldable, attributes(type_foldable)] => type_foldable::type_foldable_derive);
126 decl_derive!([TypeVisitable, attributes(type_visitable)] => type_visitable::type_visitable_derive);
127 decl_derive!([Lift, attributes(lift)] => lift::lift_derive);
128 decl_derive!(
129     [Diagnostic, attributes(
130         // struct attributes
131         diag,
132         help,
133         note,
134         warning,
135         // field attributes
136         skip_arg,
137         primary_span,
138         label,
139         subdiagnostic,
140         suggestion,
141         suggestion_short,
142         suggestion_hidden,
143         suggestion_verbose)] => diagnostics::session_diagnostic_derive
144 );
145 decl_derive!(
146     [LintDiagnostic, attributes(
147         // struct attributes
148         diag,
149         help,
150         note,
151         warning,
152         // field attributes
153         skip_arg,
154         primary_span,
155         label,
156         subdiagnostic,
157         suggestion,
158         suggestion_short,
159         suggestion_hidden,
160         suggestion_verbose)] => diagnostics::lint_diagnostic_derive
161 );
162 decl_derive!(
163     [Subdiagnostic, attributes(
164         // struct/variant attributes
165         label,
166         help,
167         note,
168         warning,
169         suggestion,
170         suggestion_short,
171         suggestion_hidden,
172         suggestion_verbose,
173         multipart_suggestion,
174         multipart_suggestion_short,
175         multipart_suggestion_hidden,
176         multipart_suggestion_verbose,
177         // field attributes
178         skip_arg,
179         primary_span,
180         suggestion_part,
181         applicability)] => diagnostics::session_subdiagnostic_derive
182 );