rustc_macros/
lib.rs

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