Skip to content

Commit 97431a4

Browse files
committed
Create a derive macro for HashStable
1 parent a9da8fc commit 97431a4

File tree

8 files changed

+63
-7
lines changed

8 files changed

+63
-7
lines changed

src/librustc/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ rustc-rayon = "0.1.2"
2424
rustc-rayon-core = "0.1.2"
2525
rustc_apfloat = { path = "../librustc_apfloat" }
2626
rustc_target = { path = "../librustc_target" }
27+
rustc_macros = { path = "../librustc_macros" }
2728
rustc_data_structures = { path = "../librustc_data_structures" }
2829
errors = { path = "../librustc_errors", package = "rustc_errors" }
2930
serialize = { path = "../libserialize" }

src/librustc/hir/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use crate::ty::query::Providers;
3232

3333
use rustc_data_structures::sync::{ParallelIterator, par_iter, Send, Sync};
3434
use rustc_data_structures::thin_vec::ThinVec;
35+
use rustc_macros::HashStable;
3536

3637
use serialize::{self, Encoder, Encodable, Decoder, Decodable};
3738
use std::collections::{BTreeSet, BTreeMap};
@@ -149,7 +150,7 @@ pub const DUMMY_HIR_ID: HirId = HirId {
149150

150151
pub const DUMMY_ITEM_LOCAL_ID: ItemLocalId = ItemLocalId::MAX;
151152

152-
#[derive(Clone, RustcEncodable, RustcDecodable, Copy)]
153+
#[derive(Clone, RustcEncodable, RustcDecodable, Copy, HashStable)]
153154
pub struct Lifetime {
154155
pub hir_id: HirId,
155156
pub span: Span,

src/librustc/ich/impls_hir.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,6 @@ impl_stable_hash_for!(struct ast::Label {
157157
ident
158158
});
159159

160-
impl_stable_hash_for!(struct hir::Lifetime {
161-
hir_id,
162-
span,
163-
name
164-
});
165-
166160
impl_stable_hash_for!(struct hir::Path {
167161
span,
168162
def,

src/librustc/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ pub mod util {
148148
#[doc(hidden)]
149149
mod rustc {
150150
pub use crate::lint;
151+
pub use crate::ich;
151152
}
152153

153154
// FIXME(#27438): right now the unit tests of librustc don't refer to any actual

src/librustc_macros/Cargo.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "rustc_macros"
3+
version = "0.1.0"
4+
authors = ["The Rust Project Developers"]
5+
6+
[lib]
7+
proc-macro = true
8+
9+
[dependencies]
10+
synstructure = "0.10.1"
11+
syn = { version = "0.15.22", features = ["full"] }
12+
proc-macro2 = "0.4.24"
13+
quote = "0.6.10"
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use synstructure;
2+
use syn;
3+
use proc_macro2;
4+
5+
pub fn hash_stable_derive(mut s: synstructure::Structure) -> proc_macro2::TokenStream {
6+
let generic: syn::GenericParam = parse_quote!('__ctx);
7+
s.add_bounds(synstructure::AddBounds::Generics);
8+
s.add_impl_generic(generic);
9+
let body = s.each(|bi| quote!{
10+
::rustc_data_structures::stable_hasher::HashStable::hash_stable(#bi, __hcx, __hasher);
11+
});
12+
13+
let discriminant = match s.ast().data {
14+
syn::Data::Enum(_) => quote! {
15+
::std::mem::discriminant(self).hash_stable(__hcx, __hasher);
16+
},
17+
syn::Data::Struct(_) => quote! {},
18+
syn::Data::Union(_) => panic!("cannot derive on union"),
19+
};
20+
21+
s.bound_impl(quote!(::rustc_data_structures::stable_hasher::HashStable
22+
<::rustc::ich::StableHashingContext<'__ctx>>), quote!{
23+
fn hash_stable<__W: ::rustc_data_structures::stable_hasher::StableHasherResult>(
24+
&self,
25+
__hcx: &mut ::rustc::ich::StableHashingContext<'__ctx>,
26+
__hasher: &mut ::rustc_data_structures::stable_hasher::StableHasher<__W>) {
27+
#discriminant
28+
match *self { #body }
29+
}
30+
})
31+
}

src/librustc_macros/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![feature(proc_macro_hygiene)]
2+
3+
#[macro_use]
4+
extern crate syn;
5+
#[macro_use]
6+
extern crate synstructure;
7+
#[macro_use]
8+
extern crate quote;
9+
extern crate proc_macro;
10+
extern crate proc_macro2;
11+
12+
mod hash_stable;
13+
14+
decl_derive!([HashStable] => hash_stable::hash_stable_derive);

src/tools/tidy/src/deps.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ const WHITELIST: &[Crate<'_>] = &[
137137
Crate("smallvec"),
138138
Crate("stable_deref_trait"),
139139
Crate("syn"),
140+
Crate("synstructure"),
140141
Crate("tempfile"),
141142
Crate("termcolor"),
142143
Crate("terminon"),

0 commit comments

Comments
 (0)