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
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!(
105    [Walkable, attributes(visitable)] =>
106    /// Derives `Walkable` for the annotated `struct` or `enum` (`union` is not supported).
107    ///
108    /// Each field of the struct or enum variant will be visited in definition order, using the
109    /// `Walkable` implementation for its type. However, if a field of a struct or an enum
110    /// variant is annotated with `#[visitable(ignore)]` then that field will not be
111    /// visited (and its type is not required to implement `Walkable`).
112    visitable::visitable_derive
113);
114decl_derive!([Lift, attributes(lift)] => lift::lift_derive);
115decl_derive!(
116    [Diagnostic, attributes(
117        // struct attributes
118        diag,
119        help,
120        help_once,
121        note,
122        note_once,
123        warning,
124        // field attributes
125        skip_arg,
126        primary_span,
127        label,
128        subdiagnostic,
129        suggestion,
130        suggestion_short,
131        suggestion_hidden,
132        suggestion_verbose)] => diagnostics::diagnostic_derive
133);
134decl_derive!(
135    [LintDiagnostic, attributes(
136        // struct attributes
137        diag,
138        help,
139        help_once,
140        note,
141        note_once,
142        warning,
143        // field attributes
144        skip_arg,
145        primary_span,
146        label,
147        subdiagnostic,
148        suggestion,
149        suggestion_short,
150        suggestion_hidden,
151        suggestion_verbose)] => diagnostics::lint_diagnostic_derive
152);
153decl_derive!(
154    [Subdiagnostic, attributes(
155        // struct/variant attributes
156        label,
157        help,
158        help_once,
159        note,
160        note_once,
161        warning,
162        subdiagnostic,
163        suggestion,
164        suggestion_short,
165        suggestion_hidden,
166        suggestion_verbose,
167        multipart_suggestion,
168        multipart_suggestion_short,
169        multipart_suggestion_hidden,
170        multipart_suggestion_verbose,
171        // field attributes
172        skip_arg,
173        primary_span,
174        suggestion_part,
175        applicability)] => diagnostics::subdiagnostic_derive
176);
177
178decl_derive! {
179    [PrintAttribute] =>
180    /// Derives `PrintAttribute` for `AttributeKind`.
181    /// This macro is pretty specific to `rustc_hir::attrs` and likely not that useful in
182    /// other places. It's deriving something close to `Debug` without printing some extraneous
183    /// things like spans.
184    print_attribute::print_attribute
185}