From d5e9dffddf5eb064f6b2b62bc698e35645d4d3c7 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sat, 16 Jun 2018 17:09:42 +0300 Subject: [PATCH] Support delegation in stable hashing macros --- src/librustc/ich/impls_hir.rs | 253 ++++++++++------------------------ src/librustc/ich/impls_ty.rs | 120 +++++----------- src/librustc/macros.rs | 21 ++- src/librustc_metadata/lib.rs | 1 + 4 files changed, 115 insertions(+), 280 deletions(-) diff --git a/src/librustc/ich/impls_hir.rs b/src/librustc/ich/impls_hir.rs index 9ab3acdce5e7..ecdca43ae2d4 100644 --- a/src/librustc/ich/impls_hir.rs +++ b/src/librustc/ich/impls_hir.rs @@ -357,20 +357,11 @@ impl_stable_hash_for!(enum hir::FunctionRetTy { Return(t) }); -impl<'a> HashStable> for hir::TraitRef { - fn hash_stable(&self, - hcx: &mut StableHashingContext<'a>, - hasher: &mut StableHasher) { - let hir::TraitRef { - ref path, - // Don't hash the ref_id. It is tracked via the thing it is used to access - ref_id: _, - } = *self; - - path.hash_stable(hcx, hasher); - } -} - +impl_stable_hash_for!(struct hir::TraitRef { + // Don't hash the ref_id. It is tracked via the thing it is used to access + ref_id -> _, + path, +}); impl_stable_hash_for!(struct hir::PolyTraitRef { bound_generic_params, @@ -393,66 +384,32 @@ impl_stable_hash_for!(struct hir::MacroDef { body }); +impl_stable_hash_for!(struct hir::Block { + stmts, + expr, + id -> _, + hir_id -> _, + rules, + span, + targeted_by_break, + recovered, +}); -impl<'a> HashStable> for hir::Block { - fn hash_stable(&self, - hcx: &mut StableHashingContext<'a>, - hasher: &mut StableHasher) { - let hir::Block { - ref stmts, - ref expr, - id: _, - hir_id: _, - rules, - span, - targeted_by_break, - recovered, - } = *self; - - stmts.hash_stable(hcx, hasher); - expr.hash_stable(hcx, hasher); - rules.hash_stable(hcx, hasher); - span.hash_stable(hcx, hasher); - recovered.hash_stable(hcx, hasher); - targeted_by_break.hash_stable(hcx, hasher); - } -} - -impl<'a> HashStable> for hir::Pat { - fn hash_stable(&self, - hcx: &mut StableHashingContext<'a>, - hasher: &mut StableHasher) { - let hir::Pat { - id: _, - hir_id: _, - ref node, - ref span - } = *self; - - - node.hash_stable(hcx, hasher); - span.hash_stable(hcx, hasher); - } -} +impl_stable_hash_for!(struct hir::Pat { + id -> _, + hir_id -> _, + node, + span, +}); impl_stable_hash_for_spanned!(hir::FieldPat); -impl<'a> HashStable> for hir::FieldPat { - fn hash_stable(&self, - hcx: &mut StableHashingContext<'a>, - hasher: &mut StableHasher) { - let hir::FieldPat { - id: _, - ident, - ref pat, - is_shorthand, - } = *self; - - ident.hash_stable(hcx, hasher); - pat.hash_stable(hcx, hasher); - is_shorthand.hash_stable(hcx, hasher); - } -} +impl_stable_hash_for!(struct hir::FieldPat { + id -> _, + ident, + pat, + is_shorthand, +}); impl_stable_hash_for!(enum hir::BindingAnnotation { Unannotated, @@ -535,24 +492,13 @@ impl_stable_hash_for!(struct hir::Arm { body }); -impl<'a> HashStable> for hir::Field { - fn hash_stable(&self, - hcx: &mut StableHashingContext<'a>, - hasher: &mut StableHasher) { - let hir::Field { - id: _, - ident, - ref expr, - span, - is_shorthand, - } = *self; - - ident.hash_stable(hcx, hasher); - expr.hash_stable(hcx, hasher); - span.hash_stable(hcx, hasher); - is_shorthand.hash_stable(hcx, hasher); - } -} +impl_stable_hash_for!(struct hir::Field { + id -> _, + ident, + expr, + span, + is_shorthand, +}); impl_stable_hash_for_spanned!(ast::Name); @@ -684,19 +630,10 @@ impl_stable_hash_for!(enum hir::LoopIdError { UnresolvedLabel }); -impl<'a> HashStable> for ast::Ident { - fn hash_stable(&self, - hcx: &mut StableHashingContext<'a>, - hasher: &mut StableHasher) { - let ast::Ident { - name, - span, - } = *self; - - name.hash_stable(hcx, hasher); - span.hash_stable(hcx, hasher); - } -} +impl_stable_hash_for!(struct ast::Ident { + name, + span, +}); impl<'a> HashStable> for hir::TraitItem { fn hash_stable(&self, @@ -816,21 +753,13 @@ impl_stable_hash_for!(enum hir::ImplPolarity { Negative }); -impl<'a> HashStable> for hir::Mod { - fn hash_stable(&self, - hcx: &mut StableHashingContext<'a>, - hasher: &mut StableHasher) { - let hir::Mod { - inner, - // We are not hashing the IDs of the items contained in the module. - // This is harmless and matches the current behavior but it's not - // actually correct. See issue #40876. - item_ids: _, - } = *self; - - inner.hash_stable(hcx, hasher); - } -} +impl_stable_hash_for!(struct hir::Mod { + inner, + // We are not hashing the IDs of the items contained in the module. + // This is harmless and matches the current behavior but it's not + // actually correct. See issue #40876. + item_ids -> _, +}); impl_stable_hash_for!(struct hir::ForeignMod { abi, @@ -931,8 +860,7 @@ impl_stable_hash_for!(struct hir::ImplItemRef { defaultness }); -impl<'a> HashStable> -for hir::AssociatedItemKind { +impl<'a> HashStable> for hir::AssociatedItemKind { fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { @@ -1012,45 +940,22 @@ impl_stable_hash_for!(struct hir::InlineAsmOutput { is_indirect }); -impl<'a> HashStable> for hir::GlobalAsm { - fn hash_stable(&self, - hcx: &mut StableHashingContext<'a>, - hasher: &mut StableHasher) { - let hir::GlobalAsm { - asm, - ctxt: _ - } = *self; +impl_stable_hash_for!(struct hir::GlobalAsm { + asm, + ctxt -> _, // This is used for error reporting +}); - asm.hash_stable(hcx, hasher); - } -} - -impl<'a> HashStable> for hir::InlineAsm { - fn hash_stable(&self, - hcx: &mut StableHashingContext<'a>, - hasher: &mut StableHasher) { - let hir::InlineAsm { - asm, - asm_str_style, - ref outputs, - ref inputs, - ref clobbers, - volatile, - alignstack, - dialect, - ctxt: _, // This is used for error reporting - } = *self; - - asm.hash_stable(hcx, hasher); - asm_str_style.hash_stable(hcx, hasher); - outputs.hash_stable(hcx, hasher); - inputs.hash_stable(hcx, hasher); - clobbers.hash_stable(hcx, hasher); - volatile.hash_stable(hcx, hasher); - alignstack.hash_stable(hcx, hasher); - dialect.hash_stable(hcx, hasher); - } -} +impl_stable_hash_for!(struct hir::InlineAsm { + asm, + asm_str_style, + outputs, + inputs, + clobbers, + volatile, + alignstack, + dialect, + ctxt -> _, // This is used for error reporting +}); impl_stable_hash_for!(enum hir::def::CtorKind { Fn, @@ -1113,8 +1018,7 @@ impl_stable_hash_for!(enum hir::Constness { NotConst }); -impl<'a> HashStable> -for hir::def_id::DefIndex { +impl<'a> HashStable> for hir::def_id::DefIndex { fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, @@ -1140,8 +1044,7 @@ impl_stable_hash_for!(struct hir::def::Export { span }); -impl<'a> HashStable> -for ::middle::lang_items::LangItem { +impl<'a> HashStable> for ::middle::lang_items::LangItem { fn hash_stable(&self, _: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { @@ -1154,8 +1057,7 @@ impl_stable_hash_for!(struct ::middle::lang_items::LanguageItems { missing }); -impl<'a> HashStable> -for hir::TraitCandidate { +impl<'a> HashStable> for hir::TraitCandidate { fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { @@ -1189,26 +1091,13 @@ impl<'a> ToStableHashKey> for hir::TraitCandidate { } } -impl<'hir> HashStable> for hir::CodegenFnAttrs -{ - fn hash_stable(&self, - hcx: &mut StableHashingContext<'hir>, - hasher: &mut StableHasher) { - let hir::CodegenFnAttrs { - flags, - inline, - export_name, - ref target_features, - linkage, - } = *self; - - flags.hash_stable(hcx, hasher); - inline.hash_stable(hcx, hasher); - export_name.hash_stable(hcx, hasher); - target_features.hash_stable(hcx, hasher); - linkage.hash_stable(hcx, hasher); - } -} +impl_stable_hash_for!(struct hir::CodegenFnAttrs { + flags, + inline, + export_name, + target_features, + linkage, +}); impl<'hir> HashStable> for hir::CodegenFnAttrFlags { diff --git a/src/librustc/ich/impls_ty.rs b/src/librustc/ich/impls_ty.rs index e4214926b4ca..2b3336e2467d 100644 --- a/src/librustc/ich/impls_ty.rs +++ b/src/librustc/ich/impls_ty.rs @@ -357,17 +357,11 @@ impl_stable_hash_for!(enum ty::VariantDiscr { Relative(distance) }); -impl<'a, 'gcx> HashStable> for ty::FieldDef { - fn hash_stable(&self, - hcx: &mut StableHashingContext<'a>, - hasher: &mut StableHasher) { - let ty::FieldDef { did, ident, vis } = *self; - - did.hash_stable(hcx, hasher); - ident.name.hash_stable(hcx, hasher); - vis.hash_stable(hcx, hasher); - } -} +impl_stable_hash_for!(struct ty::FieldDef { + did, + ident -> (ident.name), + vis, +}); impl<'a, 'gcx> HashStable> for ::middle::const_val::ConstVal<'gcx> { @@ -545,15 +539,7 @@ impl_stable_hash_for!(struct ty::GenericPredicates<'tcx> { predicates }); - -impl<'a, 'gcx> HashStable> -for ::mir::interpret::EvalError<'gcx> { - fn hash_stable(&self, - hcx: &mut StableHashingContext<'a>, - hasher: &mut StableHasher) { - self.kind.hash_stable(hcx, hasher) - } -} +impl_stable_hash_for!(struct ::mir::interpret::EvalError<'tcx> { kind }); impl<'a, 'gcx, O: HashStable>> HashStable> for ::mir::interpret::EvalErrorKind<'gcx, O> { @@ -726,28 +712,15 @@ impl_stable_hash_for!(enum ty::adjustment::CustomCoerceUnsized { Struct(index) }); -impl<'a> HashStable> for ty::Generics { - fn hash_stable(&self, - hcx: &mut StableHashingContext<'a>, - hasher: &mut StableHasher) { - let ty::Generics { - parent, - ref parent_count, - ref params, - - // Reverse map to each param's `index` field, from its `def_id`. - param_def_id_to_index: _, // Don't hash this - has_self, - has_late_bound_regions, - } = *self; - - parent.hash_stable(hcx, hasher); - parent_count.hash_stable(hcx, hasher); - params.hash_stable(hcx, hasher); - has_self.hash_stable(hcx, hasher); - has_late_bound_regions.hash_stable(hcx, hasher); - } -} +impl_stable_hash_for!(struct ty::Generics { + parent, + parent_count, + params, + // Reverse map to each param's `index` field, from its `def_id`. + param_def_id_to_index -> _, // Don't hash this + has_self, + has_late_bound_regions, +}); impl_stable_hash_for!(struct ty::GenericParamDef { name, @@ -1079,57 +1052,30 @@ impl<'a, 'gcx> HashStable> for ty::InstanceDef<'gcx> { } } -impl<'a> HashStable> for ty::TraitDef { - fn hash_stable(&self, - hcx: &mut StableHashingContext<'a>, - hasher: &mut StableHasher) { - let ty::TraitDef { - // We already have the def_path_hash below, no need to hash it twice - def_id: _, - unsafety, - paren_sugar, - has_auto_impl, - def_path_hash, - } = *self; - - unsafety.hash_stable(hcx, hasher); - paren_sugar.hash_stable(hcx, hasher); - has_auto_impl.hash_stable(hcx, hasher); - def_path_hash.hash_stable(hcx, hasher); - } -} +impl_stable_hash_for!(struct ty::TraitDef { + // We already have the def_path_hash below, no need to hash it twice + def_id -> _, + unsafety, + paren_sugar, + has_auto_impl, + def_path_hash, +}); impl_stable_hash_for!(struct ty::Destructor { did }); -impl<'a> HashStable> for ty::CrateVariancesMap { - fn hash_stable(&self, - hcx: &mut StableHashingContext<'a>, - hasher: &mut StableHasher) { - let ty::CrateVariancesMap { - ref variances, - // This is just an irrelevant helper value. - empty_variance: _, - } = *self; +impl_stable_hash_for!(struct ty::CrateVariancesMap { + variances, + // This is just an irrelevant helper value. + empty_variance -> _, +}); - variances.hash_stable(hcx, hasher); - } -} - -impl<'a, 'gcx> HashStable> for ty::CratePredicatesMap<'gcx> { - fn hash_stable(&self, - hcx: &mut StableHashingContext<'a>, - hasher: &mut StableHasher) { - let ty::CratePredicatesMap { - ref predicates, - // This is just an irrelevant helper value. - empty_predicate: _, - } = *self; - - predicates.hash_stable(hcx, hasher); - } -} +impl_stable_hash_for!(struct ty::CratePredicatesMap<'tcx> { + predicates, + // This is just an irrelevant helper value. + empty_predicate -> _, +}); impl_stable_hash_for!(struct ty::AssociatedItem { def_id, diff --git a/src/librustc/macros.rs b/src/librustc/macros.rs index 4178fdb352d5..ccd9024f4aaa 100644 --- a/src/librustc/macros.rs +++ b/src/librustc/macros.rs @@ -64,15 +64,14 @@ macro_rules! span_bug { #[macro_export] macro_rules! __impl_stable_hash_field { - (DECL IGNORED) => (_); - (DECL $name:ident) => (ref $name); - (USE IGNORED $ctx:expr, $hasher:expr) => ({}); - (USE $name:ident, $ctx:expr, $hasher:expr) => ($name.hash_stable($ctx, $hasher)); + ($field:ident, $ctx:expr, $hasher:expr) => ($field.hash_stable($ctx, $hasher)); + ($field:ident, $ctx:expr, $hasher:expr, _) => ({ let _ = $field; }); + ($field:ident, $ctx:expr, $hasher:expr, $delegate:expr) => ($delegate.hash_stable($ctx, $hasher)); } #[macro_export] macro_rules! impl_stable_hash_for { - (enum $enum_name:path { $( $variant:ident $( ( $($arg:ident),* ) )* ),* $(,)* }) => { + (enum $enum_name:path { $( $variant:ident $( ( $($field:ident $(-> $delegate:tt)?),* ) )* ),* $(,)? }) => { impl<'a, 'tcx> ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a>> for $enum_name { #[inline] fn hash_stable(&self, @@ -83,15 +82,15 @@ macro_rules! impl_stable_hash_for { match *self { $( - $variant $( ( $( __impl_stable_hash_field!(DECL $arg) ),* ) )* => { - $($( __impl_stable_hash_field!(USE $arg, __ctx, __hasher) );*)* + $variant $( ( $(ref $field),* ) )* => { + $($( __impl_stable_hash_field!($field, __ctx, __hasher $(, $delegate)?) );*)* } )* } } } }; - (struct $struct_name:path { $($field:ident),* }) => { + (struct $struct_name:path { $($field:ident $(-> $delegate:tt)?),* $(,)? }) => { impl<'a, 'tcx> ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a>> for $struct_name { #[inline] fn hash_stable(&self, @@ -101,11 +100,11 @@ macro_rules! impl_stable_hash_for { $(ref $field),* } = *self; - $( $field.hash_stable(__ctx, __hasher));* + $( __impl_stable_hash_field!($field, __ctx, __hasher $(, $delegate)?) );* } } }; - (tuple_struct $struct_name:path { $($field:ident),* }) => { + (tuple_struct $struct_name:path { $($field:ident $(-> $delegate:tt)?),* $(,)? }) => { impl<'a, 'tcx> ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a>> for $struct_name { #[inline] fn hash_stable(&self, @@ -115,7 +114,7 @@ macro_rules! impl_stable_hash_for { $(ref $field),* ) = *self; - $( $field.hash_stable(__ctx, __hasher));* + $( __impl_stable_hash_field!($field, __ctx, __hasher $(, $delegate)?) );* } } }; diff --git a/src/librustc_metadata/lib.rs b/src/librustc_metadata/lib.rs index d76ca5bdf271..0fbedcaff6ee 100644 --- a/src/librustc_metadata/lib.rs +++ b/src/librustc_metadata/lib.rs @@ -15,6 +15,7 @@ #![feature(box_patterns)] #![feature(fs_read_write)] #![feature(libc)] +#![feature(macro_at_most_once_rep)] #![cfg_attr(stage0, feature(macro_lifetime_matcher))] #![feature(proc_macro_internals)] #![feature(quote)]