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;
24mod visitable;
25
26// Reads the rust version (e.g. "1.75.0") from the CFG_RELEASE env var and
27// produces a `RustcVersion` literal containing that version (e.g.
28// `RustcVersion { major: 1, minor: 75, patch: 0 }`).
29#[proc_macro]
30pub fn current_rustc_version(input: TokenStream) -> TokenStream {
31 current_version::current_version(input)
32}
33
34#[proc_macro]
35pub fn rustc_queries(input: TokenStream) -> TokenStream {
36 query::rustc_queries(input)
37}
38
39#[proc_macro]
40pub fn symbols(input: TokenStream) -> TokenStream {
41 symbols::symbols(input.into()).into()
42}
43
44/// Derive an extension trait for a given impl block. The trait name
45/// goes into the parenthesized args of the macro, for greppability.
46/// For example:
47/// ```
48/// use rustc_macros::extension;
49/// #[extension(pub trait Foo)]
50/// impl i32 { fn hello() {} }
51/// ```
52///
53/// expands to:
54/// ```
55/// pub trait Foo { fn hello(); }
56/// impl Foo for i32 { fn hello() {} }
57/// ```
58#[proc_macro_attribute]
59pub fn extension(attr: TokenStream, input: TokenStream) -> TokenStream {
60 extension::extension(attr, input)
61}
62
63decl_derive!([HashStable, attributes(stable_hasher)] => hash_stable::hash_stable_derive);
64decl_derive!(
65 [HashStable_Generic, attributes(stable_hasher)] =>
66 hash_stable::hash_stable_generic_derive
67);
68decl_derive!(
69 [HashStable_NoContext] =>
70 /// `HashStable` implementation that has no `HashStableContext` bound and
71 /// which adds `where` bounds for `HashStable` based off of fields and not
72 /// generics. This is suitable for use in crates like `rustc_type_ir`.
73 hash_stable::hash_stable_no_context_derive
74);
75
76decl_derive!([Decodable_NoContext] => serialize::decodable_nocontext_derive);
77decl_derive!([Encodable_NoContext] => serialize::encodable_nocontext_derive);
78decl_derive!([Decodable] => serialize::decodable_derive);
79decl_derive!([Encodable] => serialize::encodable_derive);
80decl_derive!([TyDecodable] => serialize::type_decodable_derive);
81decl_derive!([TyEncodable] => serialize::type_encodable_derive);
82decl_derive!([MetadataDecodable] => serialize::meta_decodable_derive);
83decl_derive!([MetadataEncodable] => serialize::meta_encodable_derive);
84decl_derive!(
85 [TypeFoldable, attributes(type_foldable)] =>
86 /// Derives `TypeFoldable` for the annotated `struct` or `enum` (`union` is not supported).
87 ///
88 /// The fold will produce a value of the same struct or enum variant as the input, with
89 /// each field respectively folded using the `TypeFoldable` implementation for its type.
90 /// However, if a field of a struct or an enum variant is annotated with
91 /// `#[type_foldable(identity)]` then that field will retain its incumbent value (and its
92 /// type is not required to implement `TypeFoldable`).
93 type_foldable::type_foldable_derive
94);
95decl_derive!(
96 [TypeVisitable, attributes(type_visitable)] =>
97 /// Derives `TypeVisitable` for the annotated `struct` or `enum` (`union` is not supported).
98 ///
99 /// Each field of the struct or enum variant will be visited in definition order, using the
100 /// `TypeVisitable` implementation for its type. However, if a field of a struct or an enum
101 /// variant is annotated with `#[type_visitable(ignore)]` then that field will not be
102 /// visited (and its type is not required to implement `TypeVisitable`).
103 type_visitable::type_visitable_derive
104);
105decl_derive!(
106 [Walkable, attributes(visitable)] =>
107 /// Derives `Walkable` for the annotated `struct` or `enum` (`union` is not supported).
108 ///
109 /// Each field of the struct or enum variant will be visited in definition order, using the
110 /// `Walkable` implementation for its type. However, if a field of a struct or an enum
111 /// variant is annotated with `#[visitable(ignore)]` then that field will not be
112 /// visited (and its type is not required to implement `Walkable`).
113 visitable::visitable_derive
114);
115decl_derive!([Lift, attributes(lift)] => lift::lift_derive);
116decl_derive!(
117 [Diagnostic, attributes(
118 // struct attributes
119 diag,
120 help,
121 help_once,
122 note,
123 note_once,
124 warning,
125 // field attributes
126 skip_arg,
127 primary_span,
128 label,
129 subdiagnostic,
130 suggestion,
131 suggestion_short,
132 suggestion_hidden,
133 suggestion_verbose)] => diagnostics::diagnostic_derive
134);
135decl_derive!(
136 [LintDiagnostic, attributes(
137 // struct attributes
138 diag,
139 help,
140 help_once,
141 note,
142 note_once,
143 warning,
144 // field attributes
145 skip_arg,
146 primary_span,
147 label,
148 subdiagnostic,
149 suggestion,
150 suggestion_short,
151 suggestion_hidden,
152 suggestion_verbose)] => diagnostics::lint_diagnostic_derive
153);
154decl_derive!(
155 [Subdiagnostic, attributes(
156 // struct/variant attributes
157 label,
158 help,
159 help_once,
160 note,
161 note_once,
162 warning,
163 subdiagnostic,
164 suggestion,
165 suggestion_short,
166 suggestion_hidden,
167 suggestion_verbose,
168 multipart_suggestion,
169 multipart_suggestion_short,
170 multipart_suggestion_hidden,
171 multipart_suggestion_verbose,
172 // field attributes
173 skip_arg,
174 primary_span,
175 suggestion_part,
176 applicability)] => diagnostics::subdiagnostic_derive
177);
178
179decl_derive! {
180 [TryFromU32] =>
181 /// Derives `TryFrom<u32>` for the annotated `enum`, which must have no fields.
182 /// Each variant maps to the value it would produce under an `as u32` cast.
183 ///
184 /// The error type is `u32`.
185 try_from::try_from_u32
186}
187decl_derive! {
188 [PrintAttribute] =>
189 /// Derives `PrintAttribute` for `AttributeKind`.
190 /// This macro is pretty specific to `rustc_hir::attrs` and likely not that useful in
191 /// other places. It's deriving something close to `Debug` without printing some extraneous
192 /// things like spans.
193 print_attribute::print_attribute
194}