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