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 try_from;
22mod type_foldable;
23mod type_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
75decl_derive!([Decodable_NoContext] => serialize::decodable_nocontext_derive);
76decl_derive!([Encodable_NoContext] => serialize::encodable_nocontext_derive);
77decl_derive!([Decodable] => serialize::decodable_derive);
78decl_derive!([Encodable] => serialize::encodable_derive);
79decl_derive!([TyDecodable] => serialize::type_decodable_derive);
80decl_derive!([TyEncodable] => serialize::type_encodable_derive);
81decl_derive!([MetadataDecodable] => serialize::meta_decodable_derive);
82decl_derive!([MetadataEncodable] => serialize::meta_encodable_derive);
83decl_derive!(
84    [TypeFoldable, attributes(type_foldable)] =>
85    /// Derives `TypeFoldable` for the annotated `struct` or `enum` (`union` is not supported).
86    ///
87    /// The fold will produce a value of the same struct or enum variant as the input, with
88    /// each field respectively folded using the `TypeFoldable` implementation for its type.
89    /// However, if a field of a struct or an enum variant is annotated with
90    /// `#[type_foldable(identity)]` then that field will retain its incumbent value (and its
91    /// type is not required to implement `TypeFoldable`).
92    type_foldable::type_foldable_derive
93);
94decl_derive!(
95    [TypeVisitable, attributes(type_visitable)] =>
96    /// Derives `TypeVisitable` for the annotated `struct` or `enum` (`union` is not supported).
97    ///
98    /// Each field of the struct or enum variant will be visited in definition order, using the
99    /// `TypeVisitable` implementation for its type. However, if a field of a struct or an enum
100    /// variant is annotated with `#[type_visitable(ignore)]` then that field will not be
101    /// visited (and its type is not required to implement `TypeVisitable`).
102    type_visitable::type_visitable_derive
103);
104decl_derive!([Lift, attributes(lift)] => lift::lift_derive);
105decl_derive!(
106    [Diagnostic, attributes(
107        // struct attributes
108        diag,
109        help,
110        help_once,
111        note,
112        note_once,
113        warning,
114        // field attributes
115        skip_arg,
116        primary_span,
117        label,
118        subdiagnostic,
119        suggestion,
120        suggestion_short,
121        suggestion_hidden,
122        suggestion_verbose)] => diagnostics::diagnostic_derive
123);
124decl_derive!(
125    [LintDiagnostic, attributes(
126        // struct attributes
127        diag,
128        help,
129        help_once,
130        note,
131        note_once,
132        warning,
133        // field attributes
134        skip_arg,
135        primary_span,
136        label,
137        subdiagnostic,
138        suggestion,
139        suggestion_short,
140        suggestion_hidden,
141        suggestion_verbose)] => diagnostics::lint_diagnostic_derive
142);
143decl_derive!(
144    [Subdiagnostic, attributes(
145        // struct/variant attributes
146        label,
147        help,
148        help_once,
149        note,
150        note_once,
151        warning,
152        subdiagnostic,
153        suggestion,
154        suggestion_short,
155        suggestion_hidden,
156        suggestion_verbose,
157        multipart_suggestion,
158        multipart_suggestion_short,
159        multipart_suggestion_hidden,
160        multipart_suggestion_verbose,
161        // field attributes
162        skip_arg,
163        primary_span,
164        suggestion_part,
165        applicability)] => diagnostics::subdiagnostic_derive
166);
167
168decl_derive! {
169    [TryFromU32] =>
170    /// Derives `TryFrom<u32>` for the annotated `enum`, which must have no fields.
171    /// Each variant maps to the value it would produce under an `as u32` cast.
172    ///
173    /// The error type is `u32`.
174    try_from::try_from_u32
175}
176decl_derive! {
177    [PrintAttribute] =>
178    /// Derives `PrintAttribute` for `AttributeKind`.
179    /// This macro is pretty specific to `rustc_attr_data_structures` and likely not that useful in
180    /// other places. It's deriving something close to `Debug` without printing some extraneous
181    /// things like spans.
182    print_attribute::print_attribute
183}