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