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