Skip to main content

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!(
62    [HashStable, attributes(stable_hasher)] => hash_stable::hash_stable_derive
63);
64decl_derive!(
65    [HashStable_NoContext, attributes(stable_hasher)] => hash_stable::hash_stable_no_context_derive
66);
67
68// Encoding and Decoding derives
69decl_derive!([Decodable_NoContext] =>
70    /// See docs on derive [`Decodable`].
71    ///
72    /// Derives `Decodable<D> for T where D: Decoder`.
73    serialize::decodable_nocontext_derive
74);
75decl_derive!([Encodable_NoContext] => serialize::encodable_nocontext_derive);
76decl_derive!([Decodable] =>
77    /// Derives `Decodable<D> for T where D: SpanDecoder`
78    ///
79    /// # Deriving decoding traits
80    ///
81    /// > Some shared docs about decoding traits, since this is likely the first trait you find
82    ///
83    /// The difference between these derives can be subtle!
84    /// At a high level, there's the `T: Decodable<D>` trait that says some type `T`
85    /// can be decoded using a decoder `D`. There are various decoders!
86    /// The different derives place different *trait* bounds on this type `D`.
87    ///
88    /// Even though this derive, based on its name, seems like the most vanilla one,
89    /// it actually places a pretty strict bound on `D`: `SpanDecoder`.
90    /// It means that types that derive this can contain spans, among other things,
91    /// and still be decoded. The reason this is hard is that at least in metadata,
92    /// spans can only be decoded later, once some information from the header
93    /// is already decoded to properly deal with spans.
94    ///
95    /// The hierarchy is roughly:
96    ///
97    /// - derive [`Decodable_NoContext`] is the most relaxed bounds that could be placed on `D`,
98    ///   and is only really suited for structs and enums containing primitive types.
99    /// - derive [`BlobDecodable`] may be a better default, than deriving `Decodable`:
100    ///   it places fewer requirements on `D`, while still allowing some complex types to be decoded.
101    /// - derive [`LazyDecodable`]: Only for types containing `Lazy{Array,Table,Value}`.
102    /// - derive [`Decodable`] for structures containing spans. Requires `D: SpanDecoder`
103    /// - derive [`TyDecodable`] for types that require access to the `TyCtxt` while decoding.
104    ///   For example: arena allocated types.
105    serialize::decodable_derive
106);
107decl_derive!([Encodable] => serialize::encodable_derive);
108decl_derive!([TyDecodable] =>
109    /// See docs on derive [`Decodable`].
110    ///
111    /// Derives `Decodable<D> for T where D: TyDecoder`.
112    serialize::type_decodable_derive
113);
114decl_derive!([TyEncodable] => serialize::type_encodable_derive);
115decl_derive!([LazyDecodable] =>
116    /// See docs on derive [`Decodable`].
117    ///
118    /// Derives `Decodable<D> for T where D: LazyDecoder`.
119    /// This constrains the decoder to be specifically the decoder that can decode
120    /// `LazyArray`s, `LazyValue`s amd `LazyTable`s in metadata.
121    /// Therefore, we only need this on things containing LazyArray really.
122    ///
123    /// Most decodable derives mirror an encodable derive.
124    /// [`LazyDecodable`] and [`BlobDecodable`] together roughly mirror [`MetadataEncodable`]
125    serialize::lazy_decodable_derive
126);
127decl_derive!([BlobDecodable] =>
128    /// See docs on derive [`Decodable`].
129    ///
130    /// Derives `Decodable<D> for T where D: BlobDecoder`.
131    ///
132    /// Most decodable derives mirror an encodable derive.
133    /// [`LazyDecodable`] and [`BlobDecodable`] together roughly mirror [`MetadataEncodable`]
134    serialize::blob_decodable_derive
135);
136decl_derive!([MetadataEncodable] =>
137    /// Most encodable derives mirror a decodable derive.
138    /// [`MetadataEncodable`] is roughly mirrored by the combination of [`LazyDecodable`] and [`BlobDecodable`]
139    serialize::meta_encodable_derive
140);
141
142decl_derive!(
143    [TypeFoldable, attributes(type_foldable)] =>
144    /// Derives `TypeFoldable` for the annotated `struct` or `enum` (`union` is not supported).
145    ///
146    /// The fold will produce a value of the same struct or enum variant as the input, with
147    /// each field respectively folded using the `TypeFoldable` implementation for its type.
148    /// However, if a field of a struct or an enum variant is annotated with
149    /// `#[type_foldable(identity)]` then that field will retain its incumbent value (and its
150    /// type is not required to implement `TypeFoldable`).
151    type_foldable::type_foldable_derive
152);
153decl_derive!(
154    [TypeVisitable, attributes(type_visitable)] =>
155    /// Derives `TypeVisitable` for the annotated `struct` or `enum` (`union` is not supported).
156    ///
157    /// Each field of the struct or enum variant will be visited in definition order, using the
158    /// `TypeVisitable` implementation for its type. However, if a field of a struct or an enum
159    /// variant is annotated with `#[type_visitable(ignore)]` then that field will not be
160    /// visited (and its type is not required to implement `TypeVisitable`).
161    type_visitable::type_visitable_derive
162);
163decl_derive!(
164    [Walkable, attributes(visitable)] =>
165    /// Derives `Walkable` for the annotated `struct` or `enum` (`union` is not supported).
166    ///
167    /// Each field of the struct or enum variant will be visited in definition order, using the
168    /// `Walkable` implementation for its type. However, if a field of a struct or an enum
169    /// variant is annotated with `#[visitable(ignore)]` then that field will not be
170    /// visited (and its type is not required to implement `Walkable`).
171    visitable::visitable_derive
172);
173decl_derive!([Lift, attributes(lift)] => lift::lift_derive);
174decl_derive!(
175    [Diagnostic, attributes(
176        // struct attributes
177        diag,
178        help,
179        help_once,
180        note,
181        note_once,
182        warning,
183        // field attributes
184        skip_arg,
185        primary_span,
186        label,
187        subdiagnostic,
188        suggestion,
189        suggestion_short,
190        suggestion_hidden,
191        suggestion_verbose)] => diagnostics::diagnostic_derive
192);
193decl_derive!(
194    [Subdiagnostic, attributes(
195        // struct/variant attributes
196        label,
197        help,
198        help_once,
199        note,
200        note_once,
201        warning,
202        subdiagnostic,
203        suggestion,
204        suggestion_short,
205        suggestion_hidden,
206        suggestion_verbose,
207        multipart_suggestion,
208        multipart_suggestion_short,
209        multipart_suggestion_hidden,
210        // field attributes
211        skip_arg,
212        primary_span,
213        suggestion_part,
214        applicability)] => diagnostics::subdiagnostic_derive
215);
216
217/// This macro creates a translatable `DiagMessage` from a fluent format string.
218/// It should be used in places where a translatable message is needed, but struct diagnostics are undesired.
219///
220/// This macro statically checks that the message is valid Fluent, but not that variables in the Fluent message actually exist.
221#[proc_macro]
222pub fn msg(input: TokenStream) -> TokenStream {
223    diagnostics::msg_macro(input)
224}
225
226decl_derive! {
227    [PrintAttribute] =>
228    /// Derives `PrintAttribute` for `AttributeKind`.
229    /// This macro is pretty specific to `rustc_hir::attrs` and likely not that useful in
230    /// other places. It's deriving something close to `Debug` without printing some extraneous
231    /// things like spans.
232    print_attribute::print_attribute
233}