提交 97431a40 编写于 作者: J John Kåre Alsaker

Create a derive macro for HashStable

上级 a9da8fc9
......@@ -24,6 +24,7 @@ rustc-rayon = "0.1.2"
rustc-rayon-core = "0.1.2"
rustc_apfloat = { path = "../librustc_apfloat" }
rustc_target = { path = "../librustc_target" }
rustc_macros = { path = "../librustc_macros" }
rustc_data_structures = { path = "../librustc_data_structures" }
errors = { path = "../librustc_errors", package = "rustc_errors" }
serialize = { path = "../libserialize" }
......
......@@ -32,6 +32,7 @@
use rustc_data_structures::sync::{ParallelIterator, par_iter, Send, Sync};
use rustc_data_structures::thin_vec::ThinVec;
use rustc_macros::HashStable;
use serialize::{self, Encoder, Encodable, Decoder, Decodable};
use std::collections::{BTreeSet, BTreeMap};
......@@ -149,7 +150,7 @@ pub struct ItemLocalId { .. }
pub const DUMMY_ITEM_LOCAL_ID: ItemLocalId = ItemLocalId::MAX;
#[derive(Clone, RustcEncodable, RustcDecodable, Copy)]
#[derive(Clone, RustcEncodable, RustcDecodable, Copy, HashStable)]
pub struct Lifetime {
pub hir_id: HirId,
pub span: Span,
......
......@@ -157,12 +157,6 @@ fn hash_stable<W: StableHasherResult>(&self,
ident
});
impl_stable_hash_for!(struct hir::Lifetime {
hir_id,
span,
name
});
impl_stable_hash_for!(struct hir::Path {
span,
def,
......
......@@ -148,6 +148,7 @@ pub mod util {
#[doc(hidden)]
mod rustc {
pub use crate::lint;
pub use crate::ich;
}
// FIXME(#27438): right now the unit tests of librustc don't refer to any actual
......
[package]
name = "rustc_macros"
version = "0.1.0"
authors = ["The Rust Project Developers"]
[lib]
proc-macro = true
[dependencies]
synstructure = "0.10.1"
syn = { version = "0.15.22", features = ["full"] }
proc-macro2 = "0.4.24"
quote = "0.6.10"
\ No newline at end of file
use synstructure;
use syn;
use proc_macro2;
pub fn hash_stable_derive(mut s: synstructure::Structure) -> proc_macro2::TokenStream {
let generic: syn::GenericParam = parse_quote!('__ctx);
s.add_bounds(synstructure::AddBounds::Generics);
s.add_impl_generic(generic);
let body = s.each(|bi| quote!{
::rustc_data_structures::stable_hasher::HashStable::hash_stable(#bi, __hcx, __hasher);
});
let discriminant = match s.ast().data {
syn::Data::Enum(_) => quote! {
::std::mem::discriminant(self).hash_stable(__hcx, __hasher);
},
syn::Data::Struct(_) => quote! {},
syn::Data::Union(_) => panic!("cannot derive on union"),
};
s.bound_impl(quote!(::rustc_data_structures::stable_hasher::HashStable
<::rustc::ich::StableHashingContext<'__ctx>>), quote!{
fn hash_stable<__W: ::rustc_data_structures::stable_hasher::StableHasherResult>(
&self,
__hcx: &mut ::rustc::ich::StableHashingContext<'__ctx>,
__hasher: &mut ::rustc_data_structures::stable_hasher::StableHasher<__W>) {
#discriminant
match *self { #body }
}
})
}
#![feature(proc_macro_hygiene)]
#[macro_use]
extern crate syn;
#[macro_use]
extern crate synstructure;
#[macro_use]
extern crate quote;
extern crate proc_macro;
extern crate proc_macro2;
mod hash_stable;
decl_derive!([HashStable] => hash_stable::hash_stable_derive);
......@@ -137,6 +137,7 @@
Crate("smallvec"),
Crate("stable_deref_trait"),
Crate("syn"),
Crate("synstructure"),
Crate("tempfile"),
Crate("termcolor"),
Crate("terminon"),
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册