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_span)]
7#![feature(proc_macro_tracked_env)]
8// tidy-alphabetical-end
9
10use proc_macro::TokenStream;
11use synstructure::decl_derive;
12
13mod current_version;
14mod diagnostics;
15mod extension;
16mod hash_stable;
17mod lift;
18mod print_attribute;
19mod query;
20mod serialize;
21mod symbols;
22mod try_from;
23mod type_foldable;
24mod type_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!([Lift, attributes(lift)] => lift::lift_derive);
106decl_derive!(
107    [Diagnostic, attributes(
108        // struct attributes
109        diag,
110        help,
111        help_once,
112        note,
113        note_once,
114        warning,
115        // field attributes
116        skip_arg,
117        primary_span,
118        label,
119        subdiagnostic,
120        suggestion,
121        suggestion_short,
122        suggestion_hidden,
123        suggestion_verbose)] => diagnostics::diagnostic_derive
124);
125decl_derive!(
126    [LintDiagnostic, attributes(
127        // struct attributes
128        diag,
129        help,
130        help_once,
131        note,
132        note_once,
133        warning,
134        // field attributes
135        skip_arg,
136        primary_span,
137        label,
138        subdiagnostic,
139        suggestion,
140        suggestion_short,
141        suggestion_hidden,
142        suggestion_verbose)] => diagnostics::lint_diagnostic_derive
143);
144decl_derive!(
145    [Subdiagnostic, attributes(
146        // struct/variant attributes
147        label,
148        help,
149        help_once,
150        note,
151        note_once,
152        warning,
153        subdiagnostic,
154        suggestion,
155        suggestion_short,
156        suggestion_hidden,
157        suggestion_verbose,
158        multipart_suggestion,
159        multipart_suggestion_short,
160        multipart_suggestion_hidden,
161        multipart_suggestion_verbose,
162        // field attributes
163        skip_arg,
164        primary_span,
165        suggestion_part,
166        applicability)] => diagnostics::subdiagnostic_derive
167);
168
169decl_derive! {
170    [TryFromU32] =>
171    /// Derives `TryFrom<u32>` for the annotated `enum`, which must have no fields.
172    /// Each variant maps to the value it would produce under an `as u32` cast.
173    ///
174    /// The error type is `u32`.
175    try_from::try_from_u32
176}
177decl_derive! {
178    [PrintAttribute] =>
179    /// Derives `PrintAttribute` for `AttributeKind`.
180    /// This macro is pretty specific to `rustc_attr_data_structures` and likely not that useful in
181    /// other places. It's deriving something close to `Debug` without printing some extraenous
182    /// things like spans.
183    print_attribute::print_attribute
184}