rustc_macros/lib.rs
1// tidy-alphabetical-start
2#![allow(rustc::default_hash_types)]
3#![feature(never_type)]
4#![feature(proc_macro_diagnostic)]
5#![feature(proc_macro_tracked_env)]
6// tidy-alphabetical-end
7
8use proc_macro::TokenStream;
9use synstructure::decl_derive;
10
11mod current_version;
12mod diagnostics;
13mod extension;
14mod hash_stable;
15mod lift;
16mod print_attribute;
17mod query;
18mod serialize;
19mod symbols;
20mod type_foldable;
21mod type_visitable;
22mod visitable;
23
24// Reads the rust version (e.g. "1.75.0") from the CFG_RELEASE env var and
25// produces a `RustcVersion` literal containing that version (e.g.
26// `RustcVersion { major: 1, minor: 75, patch: 0 }`).
27#[proc_macro]
28pub fn current_rustc_version(input: TokenStream) -> TokenStream {
29 current_version::current_version(input)
30}
31
32#[proc_macro]
33pub fn rustc_queries(input: TokenStream) -> TokenStream {
34 query::rustc_queries(input)
35}
36
37#[proc_macro]
38pub fn symbols(input: TokenStream) -> TokenStream {
39 symbols::symbols(input.into()).into()
40}
41
42/// Derive an extension trait for a given impl block. The trait name
43/// goes into the parenthesized args of the macro, for greppability.
44/// For example:
45/// ```
46/// use rustc_macros::extension;
47/// #[extension(pub trait Foo)]
48/// impl i32 { fn hello() {} }
49/// ```
50///
51/// expands to:
52/// ```
53/// pub trait Foo { fn hello(); }
54/// impl Foo for i32 { fn hello() {} }
55/// ```
56#[proc_macro_attribute]
57pub fn extension(attr: TokenStream, input: TokenStream) -> TokenStream {
58 extension::extension(attr, input)
59}
60
61decl_derive!([HashStable, attributes(stable_hasher)] => hash_stable::hash_stable_derive);
62decl_derive!(
63 [HashStable_Generic, attributes(stable_hasher)] =>
64 hash_stable::hash_stable_generic_derive
65);
66decl_derive!(
67 [HashStable_NoContext] =>
68 /// `HashStable` implementation that has no `HashStableContext` bound and
69 /// which adds `where` bounds for `HashStable` based off of fields and not
70 /// generics. This is suitable for use in crates like `rustc_type_ir`.
71 hash_stable::hash_stable_no_context_derive
72);
73
74// Encoding and Decoding derives
75decl_derive!([Decodable_NoContext] =>
76 /// See docs on derive [`Decodable`].
77 ///
78 /// Derives `Decodable<D> for T where D: Decoder`.
79 serialize::decodable_nocontext_derive
80);
81decl_derive!([Encodable_NoContext] => serialize::encodable_nocontext_derive);
82decl_derive!([Decodable] =>
83 /// Derives `Decodable<D> for T where D: SpanDecoder`
84 ///
85 /// # Deriving decoding traits
86 ///
87 /// > Some shared docs about decoding traits, since this is likely the first trait you find
88 ///
89 /// The difference between these derives can be subtle!
90 /// At a high level, there's the `T: Decodable<D>` trait that says some type `T`
91 /// can be decoded using a decoder `D`. There are various decoders!
92 /// The different derives place different *trait* bounds on this type `D`.
93 ///
94 /// Even though this derive, based on its name, seems like the most vanilla one,
95 /// it actually places a pretty strict bound on `D`: `SpanDecoder`.
96 /// It means that types that derive this can contain spans, among other things,
97 /// and still be decoded. The reason this is hard is that at least in metadata,
98 /// spans can only be decoded later, once some information from the header
99 /// is already decoded to properly deal with spans.
100 ///
101 /// The hierarchy is roughly:
102 ///
103 /// - derive [`Decodable_NoContext`] is the most relaxed bounds that could be placed on `D`,
104 /// and is only really suited for structs and enums containing primitive types.
105 /// - derive [`BlobDecodable`] may be a better default, than deriving `Decodable`:
106 /// it places fewer requirements on `D`, while still allowing some complex types to be decoded.
107 /// - derive [`LazyDecodable`]: Only for types containing `Lazy{Array,Table,Value}`.
108 /// - derive [`Decodable`] for structures containing spans. Requires `D: SpanDecoder`
109 /// - derive [`TyDecodable`] for types that require access to the `TyCtxt` while decoding.
110 /// For example: arena allocated types.
111 serialize::decodable_derive
112);
113decl_derive!([Encodable] => serialize::encodable_derive);
114decl_derive!([TyDecodable] =>
115 /// See docs on derive [`Decodable`].
116 ///
117 /// Derives `Decodable<D> for T where D: TyDecoder`.
118 serialize::type_decodable_derive
119);
120decl_derive!([TyEncodable] => serialize::type_encodable_derive);
121decl_derive!([LazyDecodable] =>
122 /// See docs on derive [`Decodable`].
123 ///
124 /// Derives `Decodable<D> for T where D: LazyDecoder`.
125 /// This constrains the decoder to be specifically the decoder that can decode
126 /// `LazyArray`s, `LazyValue`s amd `LazyTable`s in metadata.
127 /// Therefore, we only need this on things containing LazyArray really.
128 ///
129 /// Most decodable derives mirror an encodable derive.
130 /// [`LazyDecodable`] and [`BlobDecodable`] together roughly mirror [`MetadataEncodable`]
131 serialize::lazy_decodable_derive
132);
133decl_derive!([BlobDecodable] =>
134 /// See docs on derive [`Decodable`].
135 ///
136 /// Derives `Decodable<D> for T where D: BlobDecoder`.
137 ///
138 /// Most decodable derives mirror an encodable derive.
139 /// [`LazyDecodable`] and [`BlobDecodable`] together roughly mirror [`MetadataEncodable`]
140 serialize::blob_decodable_derive
141);
142decl_derive!([MetadataEncodable] =>
143 /// Most encodable derives mirror a decodable derive.
144 /// [`MetadataEncodable`] is roughly mirrored by the combination of [`LazyDecodable`] and [`BlobDecodable`]
145 serialize::meta_encodable_derive
146);
147
148decl_derive!(
149 [TypeFoldable, attributes(type_foldable)] =>
150 /// Derives `TypeFoldable` for the annotated `struct` or `enum` (`union` is not supported).
151 ///
152 /// The fold will produce a value of the same struct or enum variant as the input, with
153 /// each field respectively folded using the `TypeFoldable` implementation for its type.
154 /// However, if a field of a struct or an enum variant is annotated with
155 /// `#[type_foldable(identity)]` then that field will retain its incumbent value (and its
156 /// type is not required to implement `TypeFoldable`).
157 type_foldable::type_foldable_derive
158);
159decl_derive!(
160 [TypeVisitable, attributes(type_visitable)] =>
161 /// Derives `TypeVisitable` for the annotated `struct` or `enum` (`union` is not supported).
162 ///
163 /// Each field of the struct or enum variant will be visited in definition order, using the
164 /// `TypeVisitable` implementation for its type. However, if a field of a struct or an enum
165 /// variant is annotated with `#[type_visitable(ignore)]` then that field will not be
166 /// visited (and its type is not required to implement `TypeVisitable`).
167 type_visitable::type_visitable_derive
168);
169decl_derive!(
170 [Walkable, attributes(visitable)] =>
171 /// Derives `Walkable` for the annotated `struct` or `enum` (`union` is not supported).
172 ///
173 /// Each field of the struct or enum variant will be visited in definition order, using the
174 /// `Walkable` implementation for its type. However, if a field of a struct or an enum
175 /// variant is annotated with `#[visitable(ignore)]` then that field will not be
176 /// visited (and its type is not required to implement `Walkable`).
177 visitable::visitable_derive
178);
179decl_derive!([Lift, attributes(lift)] => lift::lift_derive);
180decl_derive!(
181 [Diagnostic, attributes(
182 // struct attributes
183 diag,
184 help,
185 help_once,
186 note,
187 note_once,
188 warning,
189 // field attributes
190 skip_arg,
191 primary_span,
192 label,
193 subdiagnostic,
194 suggestion,
195 suggestion_short,
196 suggestion_hidden,
197 suggestion_verbose)] => diagnostics::diagnostic_derive
198);
199decl_derive!(
200 [LintDiagnostic, attributes(
201 // struct attributes
202 diag,
203 help,
204 help_once,
205 note,
206 note_once,
207 warning,
208 // field attributes
209 skip_arg,
210 primary_span,
211 label,
212 subdiagnostic,
213 suggestion,
214 suggestion_short,
215 suggestion_hidden,
216 suggestion_verbose)] => diagnostics::lint_diagnostic_derive
217);
218decl_derive!(
219 [Subdiagnostic, attributes(
220 // struct/variant attributes
221 label,
222 help,
223 help_once,
224 note,
225 note_once,
226 warning,
227 subdiagnostic,
228 suggestion,
229 suggestion_short,
230 suggestion_hidden,
231 suggestion_verbose,
232 multipart_suggestion,
233 multipart_suggestion_short,
234 multipart_suggestion_hidden,
235 multipart_suggestion_verbose,
236 // field attributes
237 skip_arg,
238 primary_span,
239 suggestion_part,
240 applicability)] => diagnostics::subdiagnostic_derive
241);
242
243/// This macro creates a translatable `DiagMessage` from a fluent format string.
244/// It should be used in places where a translatable message is needed, but struct diagnostics are undesired.
245///
246/// This macro statically checks that the message is valid Fluent, but not that variables in the Fluent message actually exist.
247#[proc_macro]
248pub fn msg(input: TokenStream) -> TokenStream {
249 diagnostics::msg_macro(input)
250}
251
252decl_derive! {
253 [PrintAttribute] =>
254 /// Derives `PrintAttribute` for `AttributeKind`.
255 /// This macro is pretty specific to `rustc_hir::attrs` and likely not that useful in
256 /// other places. It's deriving something close to `Debug` without printing some extraneous
257 /// things like spans.
258 print_attribute::print_attribute
259}