]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_macros/src/lib.rs
macros: reuse `SetOnce` trait in diagnostic derive
[rust.git] / compiler / rustc_macros / src / lib.rs
1 #![feature(allow_internal_unstable)]
2 #![feature(let_else)]
3 #![feature(proc_macro_diagnostic)]
4 #![allow(rustc::default_hash_types)]
5 #![recursion_limit = "128"]
6
7 use synstructure::decl_derive;
8
9 use proc_macro::TokenStream;
10
11 mod diagnostics;
12 mod hash_stable;
13 mod lift;
14 mod newtype;
15 mod query;
16 mod serialize;
17 mod symbols;
18 mod type_foldable;
19
20 #[proc_macro]
21 pub fn rustc_queries(input: TokenStream) -> TokenStream {
22     query::rustc_queries(input)
23 }
24
25 #[proc_macro]
26 pub fn symbols(input: TokenStream) -> TokenStream {
27     symbols::symbols(input.into()).into()
28 }
29
30 /// Creates a struct type `S` that can be used as an index with
31 /// `IndexVec` and so on.
32 ///
33 /// There are two ways of interacting with these indices:
34 ///
35 /// - The `From` impls are the preferred way. So you can do
36 ///   `S::from(v)` with a `usize` or `u32`. And you can convert back
37 ///   to an integer with `u32::from(s)`.
38 ///
39 /// - Alternatively, you can use the methods `S::new(v)` and `s.index()`
40 ///   to create/return a value.
41 ///
42 /// Internally, the index uses a u32, so the index must not exceed
43 /// `u32::MAX`. You can also customize things like the `Debug` impl,
44 /// what traits are derived, and so forth via the macro.
45 #[proc_macro]
46 #[allow_internal_unstable(step_trait, rustc_attrs, trusted_step)]
47 pub fn newtype_index(input: TokenStream) -> TokenStream {
48     newtype::newtype(input)
49 }
50
51 decl_derive!([HashStable, attributes(stable_hasher)] => hash_stable::hash_stable_derive);
52 decl_derive!(
53     [HashStable_Generic, attributes(stable_hasher)] =>
54     hash_stable::hash_stable_generic_derive
55 );
56
57 decl_derive!([Decodable] => serialize::decodable_derive);
58 decl_derive!([Encodable] => serialize::encodable_derive);
59 decl_derive!([TyDecodable] => serialize::type_decodable_derive);
60 decl_derive!([TyEncodable] => serialize::type_encodable_derive);
61 decl_derive!([MetadataDecodable] => serialize::meta_decodable_derive);
62 decl_derive!([MetadataEncodable] => serialize::meta_encodable_derive);
63 decl_derive!([TypeFoldable, attributes(type_foldable)] => type_foldable::type_foldable_derive);
64 decl_derive!([Lift, attributes(lift)] => lift::lift_derive);
65 decl_derive!(
66     [SessionDiagnostic, attributes(
67         // struct attributes
68         warning,
69         error,
70         note,
71         help,
72         // field attributes
73         skip_arg,
74         primary_span,
75         label,
76         suggestion,
77         suggestion_short,
78         suggestion_hidden,
79         suggestion_verbose)] => diagnostics::session_diagnostic_derive
80 );
81 decl_derive!(
82     [SessionSubdiagnostic, attributes(
83         // struct/variant attributes
84         label,
85         help,
86         note,
87         suggestion,
88         suggestion_short,
89         suggestion_hidden,
90         suggestion_verbose,
91         // field attributes
92         skip_arg,
93         primary_span,
94         applicability)] => diagnostics::session_subdiagnostic_derive
95 );