From 3e969e070fcb0d8fab5d2c812edad12490fe8c1d Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Fri, 22 Nov 2019 19:23:08 +0100 Subject: [PATCH 01/16] Revert "Revert expansion of impl HashStable for Frame." This reverts commit 579625b9e738e606b91fa315c75ab4909fa090be. --- src/librustc_mir/interpret/eval_context.rs | 20 ++++++++++++++++++++ src/librustc_mir/interpret/snapshot.rs | 12 ------------ 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/librustc_mir/interpret/eval_context.rs b/src/librustc_mir/interpret/eval_context.rs index 08640476f7ab7..471227f74037e 100644 --- a/src/librustc_mir/interpret/eval_context.rs +++ b/src/librustc_mir/interpret/eval_context.rs @@ -3,6 +3,7 @@ use std::fmt::Write; use std::mem; use syntax::source_map::{self, Span, DUMMY_SP}; +use rustc::ich::StableHashingContext; use rustc::hir::def_id::DefId; use rustc::hir::def::DefKind; use rustc::mir; @@ -18,6 +19,7 @@ use rustc::mir::interpret::{ InterpResult, truncate, sign_extend, }; use rustc_data_structures::fx::FxHashMap; +use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_macros::HashStable; use super::{ @@ -829,3 +831,21 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { frames } } + +impl<'ctx, 'mir, 'tcx, Tag, Extra> HashStable> +for Frame<'mir, 'tcx, Tag, Extra> + where Extra: HashStable>, + Tag: HashStable> +{ + fn hash_stable(&self, hcx: &mut StableHashingContext<'ctx>, hasher: &mut StableHasher) { + self.body.hash_stable(hcx, hasher); + self.instance.hash_stable(hcx, hasher); + self.span.hash_stable(hcx, hasher); + self.return_to_block.hash_stable(hcx, hasher); + self.return_place.as_ref().map(|r| &**r).hash_stable(hcx, hasher); + self.locals.hash_stable(hcx, hasher); + self.block.hash_stable(hcx, hasher); + self.stmt.hash_stable(hcx, hasher); + self.extra.hash_stable(hcx, hasher); + } +} diff --git a/src/librustc_mir/interpret/snapshot.rs b/src/librustc_mir/interpret/snapshot.rs index b460badd1fdac..3ea00d6922186 100644 --- a/src/librustc_mir/interpret/snapshot.rs +++ b/src/librustc_mir/interpret/snapshot.rs @@ -304,18 +304,6 @@ struct FrameSnapshot<'a, 'tcx> { stmt: usize, } -impl_stable_hash_for!(impl<> for struct Frame<'mir, 'tcx> { - body, - instance, - span, - return_to_block, - return_place -> (return_place.as_ref().map(|r| &**r)), - locals, - block, - stmt, - extra, -}); - impl<'a, 'mir, 'tcx, Ctx> Snapshot<'a, Ctx> for &'a Frame<'mir, 'tcx> where Ctx: SnapshotContext<'a>, { From 1de5fdb5babf74f729b008585a8aaf16110bb1fd Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 10 Nov 2019 17:19:08 +0100 Subject: [PATCH 02/16] Add StableHashingContextLike to HashStable_Generic derive. --- src/librustc/ich/impls_syntax.rs | 4 ++++ src/librustc_macros/src/hash_stable.rs | 1 + src/librustc_target/lib.rs | 5 +++++ src/libsyntax/lib.rs | 5 +++++ src/libsyntax_pos/lib.rs | 5 +++++ 5 files changed, 20 insertions(+) diff --git a/src/librustc/ich/impls_syntax.rs b/src/librustc/ich/impls_syntax.rs index f8bf8f4ab8a2f..b4800796b1458 100644 --- a/src/librustc/ich/impls_syntax.rs +++ b/src/librustc/ich/impls_syntax.rs @@ -17,6 +17,10 @@ use crate::hir::def_id::{DefId, CrateNum, CRATE_DEF_INDEX}; use smallvec::SmallVec; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; +impl<'ctx> syntax_pos::StableHashingContextLike for StableHashingContext<'ctx> {} +impl<'ctx> syntax::StableHashingContextLike for StableHashingContext<'ctx> {} +impl<'ctx> rustc_target::StableHashingContextLike for StableHashingContext<'ctx> {} + impl_stable_hash_for!(struct ::syntax::ast::Lit { kind, token, diff --git a/src/librustc_macros/src/hash_stable.rs b/src/librustc_macros/src/hash_stable.rs index 3fb252cbf8d9c..9003517d715a5 100644 --- a/src/librustc_macros/src/hash_stable.rs +++ b/src/librustc_macros/src/hash_stable.rs @@ -51,6 +51,7 @@ pub fn hash_stable_generic_derive(mut s: synstructure::Structure<'_>) -> proc_ma let generic: syn::GenericParam = parse_quote!(__CTX); s.add_bounds(synstructure::AddBounds::Generics); s.add_impl_generic(generic); + s.add_where_predicate(parse_quote!{ __CTX: crate::StableHashingContextLike }); let body = s.each(|bi| { let attrs = parse_attributes(bi.ast()); if attrs.ignore { diff --git a/src/librustc_target/lib.rs b/src/librustc_target/lib.rs index a349dc26e834c..274f5ec7f1d0a 100644 --- a/src/librustc_target/lib.rs +++ b/src/librustc_target/lib.rs @@ -17,3 +17,8 @@ pub mod abi; pub mod spec; + +/// Requirements for a `StableHashingContext` to be used in this crate. +/// This is a hack to allow using the `HashStable_Generic` derive macro +/// instead of implementing everything in librustc. +pub trait StableHashingContextLike {} diff --git a/src/libsyntax/lib.rs b/src/libsyntax/lib.rs index e3eca75dfe7e7..8f091c06b4b8a 100644 --- a/src/libsyntax/lib.rs +++ b/src/libsyntax/lib.rs @@ -110,3 +110,8 @@ pub mod print { } pub mod early_buffered_lints; + +/// Requirements for a `StableHashingContext` to be used in this crate. +/// This is a hack to allow using the `HashStable_Generic` derive macro +/// instead of implementing everything in librustc. +pub trait StableHashingContextLike {} diff --git a/src/libsyntax_pos/lib.rs b/src/libsyntax_pos/lib.rs index 720ace90324b9..d11ef5fcab2e0 100644 --- a/src/libsyntax_pos/lib.rs +++ b/src/libsyntax_pos/lib.rs @@ -1562,3 +1562,8 @@ fn lookup_line(lines: &[BytePos], pos: BytePos) -> isize { Err(line) => line as isize - 1 } } + +/// Requirements for a `StableHashingContext` to be used in this crate. +/// This is a hack to allow using the `HashStable_Generic` derive macro +/// instead of implementing everything in librustc. +pub trait StableHashingContextLike {} From 640797fdd700ab30addcd3497621422989f9826f Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 10 Nov 2019 17:31:21 +0100 Subject: [PATCH 03/16] Invert flow in impl HashStable of Span. --- src/librustc/ich/hcx.rs | 20 ++++++++++---------- src/librustc/ich/impls_syntax.rs | 1 - src/libsyntax_pos/lib.rs | 14 ++++++++++++-- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/librustc/ich/hcx.rs b/src/librustc/ich/hcx.rs index a5b131520c243..da45f93b40c63 100644 --- a/src/librustc/ich/hcx.rs +++ b/src/librustc/ich/hcx.rs @@ -281,7 +281,7 @@ impl<'a> ToStableHashKey> for ast::NodeId { } } -impl<'a> HashStable> for Span { +impl<'a> syntax_pos::StableHashingContextLike for StableHashingContext<'a> { /// Hashes a span in a stable way. We can't directly hash the span's `BytePos` /// fields (that would be similar to hashing pointers, since those are just /// offsets into the `SourceMap`). Instead, we hash the (file name, line, column) @@ -291,25 +291,25 @@ impl<'a> HashStable> for Span { /// codepoint offsets. For the purpose of the hash that's sufficient. /// Also, hashing filenames is expensive so we avoid doing it twice when the /// span starts and ends in the same file, which is almost always the case. - fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { + fn hash_stable_span(&mut self, span: &Span, hasher: &mut StableHasher) { const TAG_VALID_SPAN: u8 = 0; const TAG_INVALID_SPAN: u8 = 1; const TAG_EXPANSION: u8 = 0; const TAG_NO_EXPANSION: u8 = 1; - if !hcx.hash_spans { + if !self.hash_spans { return } - if *self == DUMMY_SP { + if *span == DUMMY_SP { return std_hash::Hash::hash(&TAG_INVALID_SPAN, hasher); } // If this is not an empty or invalid span, we want to hash the last // position that belongs to it, as opposed to hashing the first // position past it. - let span = self.data(); - let (file_lo, line_lo, col_lo) = match hcx.source_map() + let span = span.data(); + let (file_lo, line_lo, col_lo) = match self.source_map() .byte_pos_to_line_and_col(span.lo) { Some(pos) => pos, None => { @@ -333,9 +333,9 @@ impl<'a> HashStable> for Span { std_hash::Hash::hash(&line_col_len, hasher); if span.ctxt == SyntaxContext::root() { - TAG_NO_EXPANSION.hash_stable(hcx, hasher); + TAG_NO_EXPANSION.hash_stable(self, hasher); } else { - TAG_EXPANSION.hash_stable(hcx, hasher); + TAG_EXPANSION.hash_stable(self, hasher); // Since the same expansion context is usually referenced many // times, we cache a stable hash of it and hash that instead of @@ -352,14 +352,14 @@ impl<'a> HashStable> for Span { } let mut hasher = StableHasher::new(); - expn_id.expn_data().hash_stable(hcx, &mut hasher); + expn_id.expn_data().hash_stable(self, &mut hasher); let sub_hash: Fingerprint = hasher.finish(); let sub_hash = sub_hash.to_smaller_hash(); cache.borrow_mut().insert(expn_id, sub_hash); sub_hash }); - sub_hash.hash_stable(hcx, hasher); + sub_hash.hash_stable(self, hasher); } } } diff --git a/src/librustc/ich/impls_syntax.rs b/src/librustc/ich/impls_syntax.rs index b4800796b1458..581653f5bbc81 100644 --- a/src/librustc/ich/impls_syntax.rs +++ b/src/librustc/ich/impls_syntax.rs @@ -17,7 +17,6 @@ use crate::hir::def_id::{DefId, CrateNum, CRATE_DEF_INDEX}; use smallvec::SmallVec; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; -impl<'ctx> syntax_pos::StableHashingContextLike for StableHashingContext<'ctx> {} impl<'ctx> syntax::StableHashingContextLike for StableHashingContext<'ctx> {} impl<'ctx> rustc_target::StableHashingContextLike for StableHashingContext<'ctx> {} diff --git a/src/libsyntax_pos/lib.rs b/src/libsyntax_pos/lib.rs index d11ef5fcab2e0..25391ad5ce6f4 100644 --- a/src/libsyntax_pos/lib.rs +++ b/src/libsyntax_pos/lib.rs @@ -34,7 +34,7 @@ pub use symbol::{Symbol, sym}; mod analyze_source_file; pub mod fatal_error; -use rustc_data_structures::stable_hasher::StableHasher; +use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::sync::{Lrc, Lock}; use std::borrow::Cow; @@ -245,6 +245,14 @@ impl Ord for Span { } } +impl HashStable for Span + where CTX: StableHashingContextLike +{ + fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { + ctx.hash_stable_span(self, hasher) + } +} + /// A collection of spans. Spans have two orthogonal attributes: /// /// - They can be *primary spans*. In this case they are the locus of @@ -1566,4 +1574,6 @@ fn lookup_line(lines: &[BytePos], pos: BytePos) -> isize { /// Requirements for a `StableHashingContext` to be used in this crate. /// This is a hack to allow using the `HashStable_Generic` derive macro /// instead of implementing everything in librustc. -pub trait StableHashingContextLike {} +pub trait StableHashingContextLike { + fn hash_stable_span(&mut self, span: &Span, hasher: &mut StableHasher); +} From bf7c9ba7ced9a2c092192d5d10a78ab858f9faf6 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 10 Nov 2019 17:40:43 +0100 Subject: [PATCH 04/16] Derive HashStable_Generic for ExpnData. --- src/librustc/ich/impls_syntax.rs | 11 ----------- src/libsyntax_pos/hygiene.rs | 3 ++- 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/src/librustc/ich/impls_syntax.rs b/src/librustc/ich/impls_syntax.rs index 581653f5bbc81..83852bbbfc45b 100644 --- a/src/librustc/ich/impls_syntax.rs +++ b/src/librustc/ich/impls_syntax.rs @@ -197,17 +197,6 @@ impl_stable_hash_for!(enum ::syntax::ast::MetaItemKind { NameValue(lit) }); -impl_stable_hash_for!(struct ::syntax_pos::hygiene::ExpnData { - kind, - parent -> _, - call_site, - def_site, - allow_internal_unstable, - allow_internal_unsafe, - local_inner_macros, - edition -}); - impl<'a> HashStable> for SourceFile { fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { let SourceFile { diff --git a/src/libsyntax_pos/hygiene.rs b/src/libsyntax_pos/hygiene.rs index eb420454f03d3..cd3c9e308ab52 100644 --- a/src/libsyntax_pos/hygiene.rs +++ b/src/libsyntax_pos/hygiene.rs @@ -616,12 +616,13 @@ impl Span { /// A subset of properties from both macro definition and macro call available through global data. /// Avoid using this if you have access to the original definition or call structures. -#[derive(Clone, Debug, RustcEncodable, RustcDecodable)] +#[derive(Clone, Debug, RustcEncodable, RustcDecodable, HashStable_Generic)] pub struct ExpnData { // --- The part unique to each expansion. /// The kind of this expansion - macro or compiler desugaring. pub kind: ExpnKind, /// The expansion that produced this expansion. + #[stable_hasher(ignore)] pub parent: ExpnId, /// The location of the actual macro invocation or syntax sugar , e.g. /// `let x = foo!();` or `if let Some(y) = x {}` From 3c5ddfdd57812bec0446e791d8e8bd74ff3949b3 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 10 Nov 2019 17:46:49 +0100 Subject: [PATCH 05/16] Derive HashStable_Generic for Ident. --- src/librustc/ich/impls_hir.rs | 5 ----- src/libsyntax_pos/symbol.rs | 4 ++-- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/src/librustc/ich/impls_hir.rs b/src/librustc/ich/impls_hir.rs index 39d1f850f45ef..48c9cb619cc48 100644 --- a/src/librustc/ich/impls_hir.rs +++ b/src/librustc/ich/impls_hir.rs @@ -161,11 +161,6 @@ impl<'a> HashStable> for hir::Expr { impl_stable_hash_for_spanned!(usize); -impl_stable_hash_for!(struct ast::Ident { - name, - span, -}); - impl<'a> HashStable> for hir::TraitItem { fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { let hir::TraitItem { diff --git a/src/libsyntax_pos/symbol.rs b/src/libsyntax_pos/symbol.rs index 23ee972529a70..c285c30d7615b 100644 --- a/src/libsyntax_pos/symbol.rs +++ b/src/libsyntax_pos/symbol.rs @@ -5,7 +5,7 @@ use arena::DroplessArena; use rustc_data_structures::fx::FxHashMap; use rustc_index::vec::Idx; -use rustc_macros::symbols; +use rustc_macros::{symbols, HashStable_Generic}; use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; use rustc_serialize::{UseSpecializedDecodable, UseSpecializedEncodable}; use rustc_data_structures::stable_hasher::{HashStable, ToStableHashKey, StableHasher}; @@ -752,7 +752,7 @@ symbols! { } } -#[derive(Copy, Clone, Eq)] +#[derive(Copy, Clone, Eq, HashStable_Generic)] pub struct Ident { pub name: Symbol, pub span: Span, From 0073d3be97707b34f747c2633ac02e8c9ea89452 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 10 Nov 2019 18:10:15 +0100 Subject: [PATCH 06/16] Export HashStable for DelimSpan, Lit and Path. --- src/librustc/ich/hcx.rs | 8 -------- src/librustc/ich/impls_syntax.rs | 15 --------------- src/libsyntax/ast.rs | 12 +++++++++++- src/libsyntax/lib.rs | 2 +- src/libsyntax/tokenstream.rs | 2 +- 5 files changed, 13 insertions(+), 26 deletions(-) diff --git a/src/librustc/ich/hcx.rs b/src/librustc/ich/hcx.rs index da45f93b40c63..c67e1a7da7b47 100644 --- a/src/librustc/ich/hcx.rs +++ b/src/librustc/ich/hcx.rs @@ -14,7 +14,6 @@ use std::cell::RefCell; use syntax::ast; use syntax::source_map::SourceMap; use syntax::symbol::Symbol; -use syntax::tokenstream::DelimSpan; use syntax_pos::{Span, DUMMY_SP}; use syntax_pos::hygiene::{self, SyntaxContext}; @@ -364,13 +363,6 @@ impl<'a> syntax_pos::StableHashingContextLike for StableHashingContext<'a> { } } -impl<'a> HashStable> for DelimSpan { - fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { - self.open.hash_stable(hcx, hasher); - self.close.hash_stable(hcx, hasher); - } -} - pub fn hash_stable_trait_impls<'a>( hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher, diff --git a/src/librustc/ich/impls_syntax.rs b/src/librustc/ich/impls_syntax.rs index 83852bbbfc45b..e1281d6770347 100644 --- a/src/librustc/ich/impls_syntax.rs +++ b/src/librustc/ich/impls_syntax.rs @@ -20,12 +20,6 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; impl<'ctx> syntax::StableHashingContextLike for StableHashingContext<'ctx> {} impl<'ctx> rustc_target::StableHashingContextLike for StableHashingContext<'ctx> {} -impl_stable_hash_for!(struct ::syntax::ast::Lit { - kind, - token, - span -}); - impl_stable_hash_for_spanned!(::syntax::ast::LitKind); impl_stable_hash_for!(struct ::syntax::ast::Lifetime { id, ident }); @@ -53,15 +47,6 @@ impl<'a> HashStable> for [ast::Attribute] { } } -impl<'a> HashStable> for ast::Path { - fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { - self.segments.len().hash_stable(hcx, hasher); - for segment in &self.segments { - segment.ident.name.hash_stable(hcx, hasher); - } - } -} - impl_stable_hash_for!(struct ::syntax::ast::AttrItem { path, tokens, diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index a9f03e4af5b65..01abd09aa9195 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -33,6 +33,7 @@ use syntax_pos::symbol::{kw, sym, Symbol}; use syntax_pos::{Span, DUMMY_SP, ExpnId}; use rustc_data_structures::fx::FxHashSet; +use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::sync::Lrc; use rustc_data_structures::thin_vec::ThinVec; use rustc_index::vec::Idx; @@ -112,6 +113,15 @@ impl PartialEq for Path { } } +impl HashStable for Path { + fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) { + self.segments.len().hash_stable(hcx, hasher); + for segment in &self.segments { + segment.ident.name.hash_stable(hcx, hasher); + } + } +} + impl Path { // Convert a span and an identifier to the corresponding // one-segment path. @@ -1411,7 +1421,7 @@ pub enum StrStyle { } /// An AST literal. -#[derive(Clone, RustcEncodable, RustcDecodable, Debug)] +#[derive(Clone, RustcEncodable, RustcDecodable, Debug, HashStable_Generic)] pub struct Lit { /// The original literal token as written in source code. pub token: token::Lit, diff --git a/src/libsyntax/lib.rs b/src/libsyntax/lib.rs index 8f091c06b4b8a..7865323f8425e 100644 --- a/src/libsyntax/lib.rs +++ b/src/libsyntax/lib.rs @@ -114,4 +114,4 @@ pub mod early_buffered_lints; /// Requirements for a `StableHashingContext` to be used in this crate. /// This is a hack to allow using the `HashStable_Generic` derive macro /// instead of implementing everything in librustc. -pub trait StableHashingContextLike {} +pub trait StableHashingContextLike: syntax_pos::StableHashingContextLike {} diff --git a/src/libsyntax/tokenstream.rs b/src/libsyntax/tokenstream.rs index 2201f1ed6caca..40f00aaa5d81b 100644 --- a/src/libsyntax/tokenstream.rs +++ b/src/libsyntax/tokenstream.rs @@ -444,7 +444,7 @@ impl Cursor { } } -#[derive(Debug, Copy, Clone, PartialEq, RustcEncodable, RustcDecodable)] +#[derive(Debug, Copy, Clone, PartialEq, RustcEncodable, RustcDecodable, HashStable_Generic)] pub struct DelimSpan { pub open: Span, pub close: Span, From 31298b41d65093b2ea260d8fe8820da6db6dac94 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 10 Nov 2019 18:24:37 +0100 Subject: [PATCH 07/16] Invert implementations for TokenKind. Also export a bunch of Token-related impls. --- src/librustc/ich/impls_syntax.rs | 58 +++++--------------------------- src/libsyntax/ast.rs | 2 +- src/libsyntax/lib.rs | 5 ++- src/libsyntax/token.rs | 11 +++++- src/libsyntax/tokenstream.rs | 31 +++++++++++++++++ 5 files changed, 55 insertions(+), 52 deletions(-) diff --git a/src/librustc/ich/impls_syntax.rs b/src/librustc/ich/impls_syntax.rs index e1281d6770347..a37e014d2bd9f 100644 --- a/src/librustc/ich/impls_syntax.rs +++ b/src/librustc/ich/impls_syntax.rs @@ -9,7 +9,6 @@ use std::mem; use syntax::ast; use syntax::feature_gate; use syntax::token; -use syntax::tokenstream; use syntax_pos::SourceFile; use crate::hir::def_id::{DefId, CrateNum, CRATE_DEF_INDEX}; @@ -17,7 +16,6 @@ use crate::hir::def_id::{DefId, CrateNum, CRATE_DEF_INDEX}; use smallvec::SmallVec; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; -impl<'ctx> syntax::StableHashingContextLike for StableHashingContext<'ctx> {} impl<'ctx> rustc_target::StableHashingContextLike for StableHashingContext<'ctx> {} impl_stable_hash_for_spanned!(::syntax::ast::LitKind); @@ -47,11 +45,6 @@ impl<'a> HashStable> for [ast::Attribute] { } } -impl_stable_hash_for!(struct ::syntax::ast::AttrItem { - path, - tokens, -}); - impl<'a> HashStable> for ast::Attribute { fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { // Make sure that these have been filtered out. @@ -69,38 +62,10 @@ impl<'a> HashStable> for ast::Attribute { } } -impl<'a> HashStable> -for tokenstream::TokenTree { - fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { - mem::discriminant(self).hash_stable(hcx, hasher); - match *self { - tokenstream::TokenTree::Token(ref token) => { - token.hash_stable(hcx, hasher); - } - tokenstream::TokenTree::Delimited(span, delim, ref tts) => { - span.hash_stable(hcx, hasher); - std_hash::Hash::hash(&delim, hasher); - for sub_tt in tts.trees() { - sub_tt.hash_stable(hcx, hasher); - } - } - } - } -} - -impl<'a> HashStable> -for tokenstream::TokenStream { - fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { - for sub_tt in self.trees() { - sub_tt.hash_stable(hcx, hasher); - } - } -} - -impl<'a> HashStable> for token::TokenKind { - fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { - mem::discriminant(self).hash_stable(hcx, hasher); - match *self { +impl<'ctx> syntax::StableHashingContextLike for StableHashingContext<'ctx> { + fn hash_stable_tokenkind(&mut self, tokenkind: &token::TokenKind, hasher: &mut StableHasher) { + mem::discriminant(tokenkind).hash_stable(self, hasher); + match *tokenkind { token::Eq | token::Lt | token::Le | @@ -141,13 +106,13 @@ impl<'a> HashStable> for token::TokenKind { token::CloseDelim(delim_token) => { std_hash::Hash::hash(&delim_token, hasher); } - token::Literal(lit) => lit.hash_stable(hcx, hasher), + token::Literal(lit) => lit.hash_stable(self, hasher), token::Ident(name, is_raw) => { - name.hash_stable(hcx, hasher); - is_raw.hash_stable(hcx, hasher); + name.hash_stable(self, hasher); + is_raw.hash_stable(self, hasher); } - token::Lifetime(name) => name.hash_stable(hcx, hasher), + token::Lifetime(name) => name.hash_stable(self, hasher), token::Interpolated(_) => { bug!("interpolated tokens should not be present in the HIR") @@ -155,16 +120,11 @@ impl<'a> HashStable> for token::TokenKind { token::DocComment(val) | token::Shebang(val) | - token::Unknown(val) => val.hash_stable(hcx, hasher), + token::Unknown(val) => val.hash_stable(self, hasher), } } } -impl_stable_hash_for!(struct token::Token { - kind, - span -}); - impl_stable_hash_for!(enum ::syntax::ast::NestedMetaItem { MetaItem(meta_item), Literal(lit) diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 01abd09aa9195..3cc6a043e3b24 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -2276,7 +2276,7 @@ impl rustc_serialize::Decodable for AttrId { } } -#[derive(Clone, RustcEncodable, RustcDecodable, Debug)] +#[derive(Clone, RustcEncodable, RustcDecodable, Debug, HashStable_Generic)] pub struct AttrItem { pub path: Path, pub tokens: TokenStream, diff --git a/src/libsyntax/lib.rs b/src/libsyntax/lib.rs index 7865323f8425e..939e1877b4a45 100644 --- a/src/libsyntax/lib.rs +++ b/src/libsyntax/lib.rs @@ -20,6 +20,7 @@ #![recursion_limit="256"] pub use errors; +use rustc_data_structures::stable_hasher::StableHasher; use rustc_data_structures::sync::Lock; use rustc_index::bit_set::GrowableBitSet; pub use rustc_data_structures::thin_vec::ThinVec; @@ -114,4 +115,6 @@ pub mod early_buffered_lints; /// Requirements for a `StableHashingContext` to be used in this crate. /// This is a hack to allow using the `HashStable_Generic` derive macro /// instead of implementing everything in librustc. -pub trait StableHashingContextLike: syntax_pos::StableHashingContextLike {} +pub trait StableHashingContextLike: syntax_pos::StableHashingContextLike { + fn hash_stable_tokenkind(&mut self, tokenkind: &token::TokenKind, hasher: &mut StableHasher); +} diff --git a/src/libsyntax/token.rs b/src/libsyntax/token.rs index fd1623384a443..305db739399f5 100644 --- a/src/libsyntax/token.rs +++ b/src/libsyntax/token.rs @@ -14,6 +14,7 @@ use syntax_pos::{self, Span, DUMMY_SP}; use std::fmt; use std::mem; +use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::sync::Lrc; use rustc_macros::HashStable_Generic; @@ -262,7 +263,15 @@ pub enum TokenKind { #[cfg(target_arch = "x86_64")] rustc_data_structures::static_assert_size!(TokenKind, 16); -#[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Debug)] +impl HashStable for TokenKind + where CTX: crate::StableHashingContextLike +{ + fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) { + hcx.hash_stable_tokenkind(self, hasher) + } +} + +#[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Debug, HashStable_Generic)] pub struct Token { pub kind: TokenKind, pub span: Span, diff --git a/src/libsyntax/tokenstream.rs b/src/libsyntax/tokenstream.rs index 40f00aaa5d81b..6de9a0fb0701f 100644 --- a/src/libsyntax/tokenstream.rs +++ b/src/libsyntax/tokenstream.rs @@ -16,6 +16,7 @@ use crate::token::{self, DelimToken, Token, TokenKind}; use syntax_pos::{Span, DUMMY_SP}; +use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::sync::Lrc; use smallvec::{SmallVec, smallvec}; @@ -51,6 +52,26 @@ where TokenStream: Send + Sync, {} +impl HashStable for TokenTree + where CTX: crate::StableHashingContextLike +{ + fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) { + mem::discriminant(self).hash_stable(hcx, hasher); + match *self { + TokenTree::Token(ref token) => { + token.hash_stable(hcx, hasher); + } + TokenTree::Delimited(span, delim, ref tts) => { + span.hash_stable(hcx, hasher); + std::hash::Hash::hash(&delim, hasher); + for sub_tt in tts.trees() { + sub_tt.hash_stable(hcx, hasher); + } + } + } + } +} + impl TokenTree { /// Checks if this TokenTree is equal to the other, regardless of span information. pub fn eq_unspanned(&self, other: &TokenTree) -> bool { @@ -115,6 +136,16 @@ impl TokenTree { } } +impl HashStable for TokenStream + where CTX: crate::StableHashingContextLike +{ + fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) { + for sub_tt in self.trees() { + sub_tt.hash_stable(hcx, hasher); + } + } +} + /// A `TokenStream` is an abstract sequence of tokens, organized into `TokenTree`s. /// /// The goal is for procedural macros to work with `TokenStream`s and `TokenTree`s From e85c195174bb13616c155fd9871006c56aef0bc7 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 10 Nov 2019 18:32:43 +0100 Subject: [PATCH 08/16] Derives for ast. --- src/librustc/ich/impls_hir.rs | 4 ---- src/librustc/ich/impls_syntax.rs | 17 ----------------- src/libsyntax/ast.rs | 8 ++++---- 3 files changed, 4 insertions(+), 25 deletions(-) diff --git a/src/librustc/ich/impls_hir.rs b/src/librustc/ich/impls_hir.rs index 48c9cb619cc48..d849c2d685763 100644 --- a/src/librustc/ich/impls_hir.rs +++ b/src/librustc/ich/impls_hir.rs @@ -119,10 +119,6 @@ impl<'a> HashStable> for hir::ImplItemId { } } -impl_stable_hash_for!(struct ast::Label { - ident -}); - impl<'a> HashStable> for hir::Ty { fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { hcx.while_hashing_hir_bodies(true, |hcx| { diff --git a/src/librustc/ich/impls_syntax.rs b/src/librustc/ich/impls_syntax.rs index a37e014d2bd9f..a30804c76e60c 100644 --- a/src/librustc/ich/impls_syntax.rs +++ b/src/librustc/ich/impls_syntax.rs @@ -125,23 +125,6 @@ impl<'ctx> syntax::StableHashingContextLike for StableHashingContext<'ctx> { } } -impl_stable_hash_for!(enum ::syntax::ast::NestedMetaItem { - MetaItem(meta_item), - Literal(lit) -}); - -impl_stable_hash_for!(struct ::syntax::ast::MetaItem { - path, - kind, - span -}); - -impl_stable_hash_for!(enum ::syntax::ast::MetaItemKind { - Word, - List(nested_items), - NameValue(lit) -}); - impl<'a> HashStable> for SourceFile { fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { let SourceFile { diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 3cc6a043e3b24..57f2104e4a50f 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -55,7 +55,7 @@ mod tests; /// ``` /// /// `'outer` is a label. -#[derive(Clone, RustcEncodable, RustcDecodable, Copy)] +#[derive(Clone, RustcEncodable, RustcDecodable, Copy, HashStable_Generic)] pub struct Label { pub ident: Ident, } @@ -483,7 +483,7 @@ pub struct Crate { /// Possible values inside of compile-time attribute lists. /// /// E.g., the '..' in `#[name(..)]`. -#[derive(Clone, RustcEncodable, RustcDecodable, Debug)] +#[derive(Clone, RustcEncodable, RustcDecodable, Debug, HashStable_Generic)] pub enum NestedMetaItem { /// A full MetaItem, for recursive meta items. MetaItem(MetaItem), @@ -496,7 +496,7 @@ pub enum NestedMetaItem { /// A spanned compile-time attribute item. /// /// E.g., `#[test]`, `#[derive(..)]`, `#[rustfmt::skip]` or `#[feature = "foo"]`. -#[derive(Clone, RustcEncodable, RustcDecodable, Debug)] +#[derive(Clone, RustcEncodable, RustcDecodable, Debug, HashStable_Generic)] pub struct MetaItem { pub path: Path, pub kind: MetaItemKind, @@ -506,7 +506,7 @@ pub struct MetaItem { /// A compile-time attribute item. /// /// E.g., `#[test]`, `#[derive(..)]` or `#[feature = "foo"]`. -#[derive(Clone, RustcEncodable, RustcDecodable, Debug)] +#[derive(Clone, RustcEncodable, RustcDecodable, Debug, HashStable_Generic)] pub enum MetaItemKind { /// Word meta item. /// From 8c86a7994733db6d405e33404afa09c63c0bef4d Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 10 Nov 2019 18:50:11 +0100 Subject: [PATCH 09/16] Retire impl_stable_hash_for_spanned. --- src/librustc/ich/impls_hir.rs | 12 ------------ src/librustc/ich/impls_syntax.rs | 2 -- src/librustc/macros.rs | 17 ----------------- src/libsyntax_pos/source_map.rs | 2 +- 4 files changed, 1 insertion(+), 32 deletions(-) diff --git a/src/librustc/ich/impls_hir.rs b/src/librustc/ich/impls_hir.rs index d849c2d685763..066359bd4e036 100644 --- a/src/librustc/ich/impls_hir.rs +++ b/src/librustc/ich/impls_hir.rs @@ -9,7 +9,6 @@ use crate::ich::{StableHashingContext, NodeIdHashingMode, Fingerprint}; use rustc_data_structures::stable_hasher::{HashStable, ToStableHashKey, StableHasher}; use smallvec::SmallVec; use std::mem; -use syntax::ast; use syntax::attr; impl<'a> HashStable> for DefId { @@ -134,10 +133,6 @@ impl<'a> HashStable> for hir::Ty { } } -impl_stable_hash_for_spanned!(hir::BinOpKind); - -impl_stable_hash_for_spanned!(ast::Name); - impl<'a> HashStable> for hir::Expr { fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { hcx.while_hashing_hir_bodies(true, |hcx| { @@ -155,8 +150,6 @@ impl<'a> HashStable> for hir::Expr { } } -impl_stable_hash_for_spanned!(usize); - impl<'a> HashStable> for hir::TraitItem { fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { let hir::TraitItem { @@ -225,8 +218,6 @@ impl<'a> HashStable> for hir::VisibilityKind { } } -impl_stable_hash_for_spanned!(hir::VisibilityKind); - impl<'a> HashStable> for hir::Mod { fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { let hir::Mod { @@ -254,9 +245,6 @@ impl<'a> HashStable> for hir::Mod { } } -impl_stable_hash_for_spanned!(hir::Variant); - - impl<'a> HashStable> for hir::Item { fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { let hir::Item { diff --git a/src/librustc/ich/impls_syntax.rs b/src/librustc/ich/impls_syntax.rs index a30804c76e60c..aae175e964731 100644 --- a/src/librustc/ich/impls_syntax.rs +++ b/src/librustc/ich/impls_syntax.rs @@ -18,8 +18,6 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; impl<'ctx> rustc_target::StableHashingContextLike for StableHashingContext<'ctx> {} -impl_stable_hash_for_spanned!(::syntax::ast::LitKind); - impl_stable_hash_for!(struct ::syntax::ast::Lifetime { id, ident }); impl<'a> HashStable> for [ast::Attribute] { diff --git a/src/librustc/macros.rs b/src/librustc/macros.rs index aae1c7a299272..59612a0ee5cfa 100644 --- a/src/librustc/macros.rs +++ b/src/librustc/macros.rs @@ -166,23 +166,6 @@ macro_rules! impl_stable_hash_for { }; } -#[macro_export] -macro_rules! impl_stable_hash_for_spanned { - ($T:path) => ( - - impl HashStable> for ::syntax::source_map::Spanned<$T> - { - #[inline] - fn hash_stable(&self, - hcx: &mut StableHashingContext<'a>, - hasher: &mut StableHasher) { - self.node.hash_stable(hcx, hasher); - self.span.hash_stable(hcx, hasher); - } - } - ); -} - /////////////////////////////////////////////////////////////////////////// // Lift and TypeFoldable macros // diff --git a/src/libsyntax_pos/source_map.rs b/src/libsyntax_pos/source_map.rs index 77d9807225ec8..b597fad080fa0 100644 --- a/src/libsyntax_pos/source_map.rs +++ b/src/libsyntax_pos/source_map.rs @@ -39,7 +39,7 @@ pub fn original_sp(sp: Span, enclosing_sp: Span) -> Span { } } -#[derive(Clone, RustcEncodable, RustcDecodable, Debug, Copy)] +#[derive(Clone, RustcEncodable, RustcDecodable, Debug, Copy, HashStable_Generic)] pub struct Spanned { pub node: T, pub span: Span, From edc5232a4d1234d26015cd053db53f5a82c94a9d Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 10 Nov 2019 19:32:04 +0100 Subject: [PATCH 10/16] Retire impl_stable_hash_for. --- src/librustc/ich/impls_syntax.rs | 7 +- src/librustc/macros.rs | 116 ------------------------------- 2 files changed, 6 insertions(+), 117 deletions(-) diff --git a/src/librustc/ich/impls_syntax.rs b/src/librustc/ich/impls_syntax.rs index aae175e964731..6e03056283235 100644 --- a/src/librustc/ich/impls_syntax.rs +++ b/src/librustc/ich/impls_syntax.rs @@ -18,7 +18,12 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; impl<'ctx> rustc_target::StableHashingContextLike for StableHashingContext<'ctx> {} -impl_stable_hash_for!(struct ::syntax::ast::Lifetime { id, ident }); +impl<'a> HashStable> for ast::Lifetime { + fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { + self.id.hash_stable(hcx, hasher); + self.ident.hash_stable(hcx, hasher); + } +} impl<'a> HashStable> for [ast::Attribute] { fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { diff --git a/src/librustc/macros.rs b/src/librustc/macros.rs index 59612a0ee5cfa..2bda0c0bef02d 100644 --- a/src/librustc/macros.rs +++ b/src/librustc/macros.rs @@ -1,5 +1,3 @@ -// ignore-tidy-linelength - macro_rules! enum_from_u32 { ($(#[$attr:meta])* pub enum $name:ident { $($variant:ident = $e:expr,)* @@ -52,120 +50,6 @@ macro_rules! span_bug { }) } -#[macro_export] -macro_rules! __impl_stable_hash_field { - ($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 { - // Enums - (enum $enum_name:path { - $( $variant:ident - // this incorrectly allows specifying both tuple-like and struct-like fields, as in `Variant(a,b){c,d}`, - // when it should be only one or the other - $( ( $($field:ident $(-> $delegate:tt)?),* ) )? - $( { $($named_field:ident $(-> $named_delegate:tt)?),* } )? - ),* $(,)? - }) => { - impl_stable_hash_for!( - impl<> for enum $enum_name [ $enum_name ] { $( $variant - $( ( $($field $(-> $delegate)?),* ) )? - $( { $($named_field $(-> $named_delegate)?),* } )? - ),* } - ); - }; - // We want to use the enum name both in the `impl ... for $enum_name` as well as for - // importing all the variants. Unfortunately it seems we have to take the name - // twice for this purpose - (impl<$($T:ident),* $(,)?> - for enum $enum_name:path - [ $enum_path:path ] - { - $( $variant:ident - // this incorrectly allows specifying both tuple-like and struct-like fields, as in `Variant(a,b){c,d}`, - // when it should be only one or the other - $( ( $($field:ident $(-> $delegate:tt)?),* ) )? - $( { $($named_field:ident $(-> $named_delegate:tt)?),* } )? - ),* $(,)? - }) => { - impl<$($T,)*> - ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a>> - for $enum_name - where $($T: ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a>>),* - { - #[inline] - fn hash_stable(&self, - __ctx: &mut $crate::ich::StableHashingContext<'a>, - __hasher: &mut ::rustc_data_structures::stable_hasher::StableHasher) { - use $enum_path::*; - ::std::mem::discriminant(self).hash_stable(__ctx, __hasher); - - match *self { - $( - $variant $( ( $(ref $field),* ) )? $( { $(ref $named_field),* } )? => { - $($( __impl_stable_hash_field!($field, __ctx, __hasher $(, $delegate)?) );*)? - $($( __impl_stable_hash_field!($named_field, __ctx, __hasher $(, $named_delegate)?) );*)? - } - )* - } - } - } - }; - // Structs - (struct $struct_name:path { $($field:ident $(-> $delegate:tt)?),* $(,)? }) => { - impl_stable_hash_for!( - impl<> for struct $struct_name { $($field $(-> $delegate)?),* } - ); - }; - (impl<$($T:ident),* $(,)?> for struct $struct_name:path { - $($field:ident $(-> $delegate:tt)?),* $(,)? - }) => { - impl<$($T,)*> - ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a>> for $struct_name - where $($T: ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a>>),* - { - #[inline] - fn hash_stable(&self, - __ctx: &mut $crate::ich::StableHashingContext<'a>, - __hasher: &mut ::rustc_data_structures::stable_hasher::StableHasher) { - let $struct_name { - $(ref $field),* - } = *self; - - $( __impl_stable_hash_field!($field, __ctx, __hasher $(, $delegate)?) );* - } - } - }; - // Tuple structs - // We cannot use normal parentheses here, the parser won't allow it - (tuple_struct $struct_name:path { $($field:ident $(-> $delegate:tt)?),* $(,)? }) => { - impl_stable_hash_for!( - impl<> for tuple_struct $struct_name { $($field $(-> $delegate)?),* } - ); - }; - (impl<$($T:ident),* $(,)?> - for tuple_struct $struct_name:path { $($field:ident $(-> $delegate:tt)?),* $(,)? }) => { - impl<$($T,)*> - ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a>> for $struct_name - where $($T: ::rustc_data_structures::stable_hasher::HashStable<$crate::ich::StableHashingContext<'a>>),* - { - #[inline] - fn hash_stable(&self, - __ctx: &mut $crate::ich::StableHashingContext<'a>, - __hasher: &mut ::rustc_data_structures::stable_hasher::StableHasher) { - let $struct_name ( - $(ref $field),* - ) = *self; - - $( __impl_stable_hash_field!($field, __ctx, __hasher $(, $delegate)?) );* - } - } - }; -} - /////////////////////////////////////////////////////////////////////////// // Lift and TypeFoldable macros // From 9efd3205b57aaa8908a1eca337ab8336cdb16bc2 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Fri, 22 Nov 2019 20:17:22 +0100 Subject: [PATCH 11/16] Fix rebase fallout. --- src/libsyntax/tokenstream.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libsyntax/tokenstream.rs b/src/libsyntax/tokenstream.rs index 6de9a0fb0701f..eb99e549a7f6c 100644 --- a/src/libsyntax/tokenstream.rs +++ b/src/libsyntax/tokenstream.rs @@ -17,6 +17,7 @@ use crate::token::{self, DelimToken, Token, TokenKind}; use syntax_pos::{Span, DUMMY_SP}; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; +use rustc_macros::HashStable_Generic; use rustc_data_structures::sync::Lrc; use smallvec::{SmallVec, smallvec}; From ce301075c0de3b69914cc11744558dabd6d2e731 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 23 Nov 2019 13:58:17 +0100 Subject: [PATCH 12/16] Rename StableHashingContextLike to HashStableContext. --- src/librustc/ich/hcx.rs | 2 +- src/librustc/ich/impls_syntax.rs | 4 ++-- src/librustc_macros/src/hash_stable.rs | 2 +- src/librustc_target/lib.rs | 2 +- src/libsyntax/lib.rs | 2 +- src/libsyntax/token.rs | 2 +- src/libsyntax/tokenstream.rs | 4 ++-- src/libsyntax_pos/lib.rs | 4 ++-- 8 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/librustc/ich/hcx.rs b/src/librustc/ich/hcx.rs index c67e1a7da7b47..55c02074fce7c 100644 --- a/src/librustc/ich/hcx.rs +++ b/src/librustc/ich/hcx.rs @@ -280,7 +280,7 @@ impl<'a> ToStableHashKey> for ast::NodeId { } } -impl<'a> syntax_pos::StableHashingContextLike for StableHashingContext<'a> { +impl<'a> syntax_pos::HashStableContext for StableHashingContext<'a> { /// Hashes a span in a stable way. We can't directly hash the span's `BytePos` /// fields (that would be similar to hashing pointers, since those are just /// offsets into the `SourceMap`). Instead, we hash the (file name, line, column) diff --git a/src/librustc/ich/impls_syntax.rs b/src/librustc/ich/impls_syntax.rs index 6e03056283235..2d7db94bf8c65 100644 --- a/src/librustc/ich/impls_syntax.rs +++ b/src/librustc/ich/impls_syntax.rs @@ -16,7 +16,7 @@ use crate::hir::def_id::{DefId, CrateNum, CRATE_DEF_INDEX}; use smallvec::SmallVec; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; -impl<'ctx> rustc_target::StableHashingContextLike for StableHashingContext<'ctx> {} +impl<'ctx> rustc_target::HashStableContext for StableHashingContext<'ctx> {} impl<'a> HashStable> for ast::Lifetime { fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { @@ -65,7 +65,7 @@ impl<'a> HashStable> for ast::Attribute { } } -impl<'ctx> syntax::StableHashingContextLike for StableHashingContext<'ctx> { +impl<'ctx> syntax::HashStableContext for StableHashingContext<'ctx> { fn hash_stable_tokenkind(&mut self, tokenkind: &token::TokenKind, hasher: &mut StableHasher) { mem::discriminant(tokenkind).hash_stable(self, hasher); match *tokenkind { diff --git a/src/librustc_macros/src/hash_stable.rs b/src/librustc_macros/src/hash_stable.rs index 9003517d715a5..103fcd0e8e7bd 100644 --- a/src/librustc_macros/src/hash_stable.rs +++ b/src/librustc_macros/src/hash_stable.rs @@ -51,7 +51,7 @@ pub fn hash_stable_generic_derive(mut s: synstructure::Structure<'_>) -> proc_ma let generic: syn::GenericParam = parse_quote!(__CTX); s.add_bounds(synstructure::AddBounds::Generics); s.add_impl_generic(generic); - s.add_where_predicate(parse_quote!{ __CTX: crate::StableHashingContextLike }); + s.add_where_predicate(parse_quote!{ __CTX: crate::HashStableContext }); let body = s.each(|bi| { let attrs = parse_attributes(bi.ast()); if attrs.ignore { diff --git a/src/librustc_target/lib.rs b/src/librustc_target/lib.rs index 274f5ec7f1d0a..5582eaf47c426 100644 --- a/src/librustc_target/lib.rs +++ b/src/librustc_target/lib.rs @@ -21,4 +21,4 @@ pub mod spec; /// Requirements for a `StableHashingContext` to be used in this crate. /// This is a hack to allow using the `HashStable_Generic` derive macro /// instead of implementing everything in librustc. -pub trait StableHashingContextLike {} +pub trait HashStableContext {} diff --git a/src/libsyntax/lib.rs b/src/libsyntax/lib.rs index 939e1877b4a45..579d26579392e 100644 --- a/src/libsyntax/lib.rs +++ b/src/libsyntax/lib.rs @@ -115,6 +115,6 @@ pub mod early_buffered_lints; /// Requirements for a `StableHashingContext` to be used in this crate. /// This is a hack to allow using the `HashStable_Generic` derive macro /// instead of implementing everything in librustc. -pub trait StableHashingContextLike: syntax_pos::StableHashingContextLike { +pub trait HashStableContext: syntax_pos::HashStableContext { fn hash_stable_tokenkind(&mut self, tokenkind: &token::TokenKind, hasher: &mut StableHasher); } diff --git a/src/libsyntax/token.rs b/src/libsyntax/token.rs index 305db739399f5..08afca921fc92 100644 --- a/src/libsyntax/token.rs +++ b/src/libsyntax/token.rs @@ -264,7 +264,7 @@ pub enum TokenKind { rustc_data_structures::static_assert_size!(TokenKind, 16); impl HashStable for TokenKind - where CTX: crate::StableHashingContextLike + where CTX: crate::HashStableContext { fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) { hcx.hash_stable_tokenkind(self, hasher) diff --git a/src/libsyntax/tokenstream.rs b/src/libsyntax/tokenstream.rs index eb99e549a7f6c..5ec99fa2f08e6 100644 --- a/src/libsyntax/tokenstream.rs +++ b/src/libsyntax/tokenstream.rs @@ -54,7 +54,7 @@ where {} impl HashStable for TokenTree - where CTX: crate::StableHashingContextLike + where CTX: crate::HashStableContext { fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) { mem::discriminant(self).hash_stable(hcx, hasher); @@ -138,7 +138,7 @@ impl TokenTree { } impl HashStable for TokenStream - where CTX: crate::StableHashingContextLike + where CTX: crate::HashStableContext { fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) { for sub_tt in self.trees() { diff --git a/src/libsyntax_pos/lib.rs b/src/libsyntax_pos/lib.rs index 25391ad5ce6f4..1c66858a915c6 100644 --- a/src/libsyntax_pos/lib.rs +++ b/src/libsyntax_pos/lib.rs @@ -246,7 +246,7 @@ impl Ord for Span { } impl HashStable for Span - where CTX: StableHashingContextLike + where CTX: HashStableContext { fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { ctx.hash_stable_span(self, hasher) @@ -1574,6 +1574,6 @@ fn lookup_line(lines: &[BytePos], pos: BytePos) -> isize { /// Requirements for a `StableHashingContext` to be used in this crate. /// This is a hack to allow using the `HashStable_Generic` derive macro /// instead of implementing everything in librustc. -pub trait StableHashingContextLike { +pub trait HashStableContext { fn hash_stable_span(&mut self, span: &Span, hasher: &mut StableHasher); } From ea0c354758b334985169b35575fcbb8ca0a6f2f3 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 23 Nov 2019 14:17:37 +0100 Subject: [PATCH 13/16] Move CachingSourceMapView to libsyntax_pos. --- src/librustc/ich/mod.rs | 3 +-- .../ich => libsyntax_pos}/caching_source_map_view.rs | 4 ++-- src/libsyntax_pos/lib.rs | 2 ++ 3 files changed, 5 insertions(+), 4 deletions(-) rename src/{librustc/ich => libsyntax_pos}/caching_source_map_view.rs (97%) diff --git a/src/librustc/ich/mod.rs b/src/librustc/ich/mod.rs index 9e985ffb14ca7..ece438266c0c2 100644 --- a/src/librustc/ich/mod.rs +++ b/src/librustc/ich/mod.rs @@ -1,12 +1,11 @@ //! ICH - Incremental Compilation Hash crate use rustc_data_structures::fingerprint::Fingerprint; -pub use self::caching_source_map_view::CachingSourceMapView; +pub use syntax_pos::CachingSourceMapView; pub use self::hcx::{StableHashingContextProvider, StableHashingContext, NodeIdHashingMode, hash_stable_trait_impls}; use syntax::symbol::{Symbol, sym}; -mod caching_source_map_view; mod hcx; mod impls_hir; diff --git a/src/librustc/ich/caching_source_map_view.rs b/src/libsyntax_pos/caching_source_map_view.rs similarity index 97% rename from src/librustc/ich/caching_source_map_view.rs rename to src/libsyntax_pos/caching_source_map_view.rs index bfe2ca6dd09d1..8237173087610 100644 --- a/src/librustc/ich/caching_source_map_view.rs +++ b/src/libsyntax_pos/caching_source_map_view.rs @@ -1,6 +1,6 @@ use rustc_data_structures::sync::Lrc; -use syntax::source_map::SourceMap; -use syntax_pos::{BytePos, SourceFile}; +use crate::source_map::SourceMap; +use crate::{BytePos, SourceFile}; #[derive(Clone)] struct CacheEntry { diff --git a/src/libsyntax_pos/lib.rs b/src/libsyntax_pos/lib.rs index 1c66858a915c6..a24a2555bffae 100644 --- a/src/libsyntax_pos/lib.rs +++ b/src/libsyntax_pos/lib.rs @@ -18,6 +18,8 @@ use rustc_serialize::{Encodable, Decodable, Encoder, Decoder}; use rustc_macros::HashStable_Generic; pub mod source_map; +mod caching_source_map_view; +pub use self::caching_source_map_view::CachingSourceMapView; pub mod edition; use edition::Edition; From 7e411e7f55a6050fb690f2c4e46b002a46502031 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 23 Nov 2019 14:39:00 +0100 Subject: [PATCH 14/16] Implement HashStable for Span in libsyntax_pos. --- src/librustc/ich/hcx.rs | 93 ++++-------------------------------- src/libsyntax_pos/lib.rs | 100 +++++++++++++++++++++++++++++++++++---- 2 files changed, 100 insertions(+), 93 deletions(-) diff --git a/src/librustc/ich/hcx.rs b/src/librustc/ich/hcx.rs index 55c02074fce7c..8b35839c182ac 100644 --- a/src/librustc/ich/hcx.rs +++ b/src/librustc/ich/hcx.rs @@ -2,25 +2,23 @@ use crate::hir; use crate::hir::def_id::{DefId, DefIndex}; use crate::hir::map::DefPathHash; use crate::hir::map::definitions::Definitions; -use crate::ich::{self, CachingSourceMapView, Fingerprint}; +use crate::ich::{self, CachingSourceMapView}; use crate::middle::cstore::CrateStore; use crate::ty::{TyCtxt, fast_reject}; use crate::session::Session; use std::cmp::Ord; -use std::hash as std_hash; -use std::cell::RefCell; use syntax::ast; use syntax::source_map::SourceMap; use syntax::symbol::Symbol; -use syntax_pos::{Span, DUMMY_SP}; -use syntax_pos::hygiene::{self, SyntaxContext}; +use syntax_pos::{SourceFile, BytePos}; use rustc_data_structures::stable_hasher::{ HashStable, StableHasher, ToStableHashKey, }; use rustc_data_structures::fx::{FxHashSet, FxHashMap}; +use rustc_data_structures::sync::Lrc; use smallvec::SmallVec; fn compute_ignored_attr_names() -> FxHashSet { @@ -281,85 +279,14 @@ impl<'a> ToStableHashKey> for ast::NodeId { } impl<'a> syntax_pos::HashStableContext for StableHashingContext<'a> { - /// Hashes a span in a stable way. We can't directly hash the span's `BytePos` - /// fields (that would be similar to hashing pointers, since those are just - /// offsets into the `SourceMap`). Instead, we hash the (file name, line, column) - /// triple, which stays the same even if the containing `SourceFile` has moved - /// within the `SourceMap`. - /// Also note that we are hashing byte offsets for the column, not unicode - /// codepoint offsets. For the purpose of the hash that's sufficient. - /// Also, hashing filenames is expensive so we avoid doing it twice when the - /// span starts and ends in the same file, which is almost always the case. - fn hash_stable_span(&mut self, span: &Span, hasher: &mut StableHasher) { - const TAG_VALID_SPAN: u8 = 0; - const TAG_INVALID_SPAN: u8 = 1; - const TAG_EXPANSION: u8 = 0; - const TAG_NO_EXPANSION: u8 = 1; - - if !self.hash_spans { - return - } - - if *span == DUMMY_SP { - return std_hash::Hash::hash(&TAG_INVALID_SPAN, hasher); - } - - // If this is not an empty or invalid span, we want to hash the last - // position that belongs to it, as opposed to hashing the first - // position past it. - let span = span.data(); - let (file_lo, line_lo, col_lo) = match self.source_map() - .byte_pos_to_line_and_col(span.lo) { - Some(pos) => pos, - None => { - return std_hash::Hash::hash(&TAG_INVALID_SPAN, hasher); - } - }; - - if !file_lo.contains(span.hi) { - return std_hash::Hash::hash(&TAG_INVALID_SPAN, hasher); - } - - std_hash::Hash::hash(&TAG_VALID_SPAN, hasher); - // We truncate the stable ID hash and line and column numbers. The chances - // of causing a collision this way should be minimal. - std_hash::Hash::hash(&(file_lo.name_hash as u64), hasher); - - let col = (col_lo.0 as u64) & 0xFF; - let line = ((line_lo as u64) & 0xFF_FF_FF) << 8; - let len = ((span.hi - span.lo).0 as u64) << 32; - let line_col_len = col | line | len; - std_hash::Hash::hash(&line_col_len, hasher); - - if span.ctxt == SyntaxContext::root() { - TAG_NO_EXPANSION.hash_stable(self, hasher); - } else { - TAG_EXPANSION.hash_stable(self, hasher); - - // Since the same expansion context is usually referenced many - // times, we cache a stable hash of it and hash that instead of - // recursing every time. - thread_local! { - static CACHE: RefCell> = Default::default(); - } - - let sub_hash: u64 = CACHE.with(|cache| { - let expn_id = span.ctxt.outer_expn(); - - if let Some(&sub_hash) = cache.borrow().get(&expn_id) { - return sub_hash; - } - - let mut hasher = StableHasher::new(); - expn_id.expn_data().hash_stable(self, &mut hasher); - let sub_hash: Fingerprint = hasher.finish(); - let sub_hash = sub_hash.to_smaller_hash(); - cache.borrow_mut().insert(expn_id, sub_hash); - sub_hash - }); + fn hash_spans(&self) -> bool { + self.hash_spans + } - sub_hash.hash_stable(self, hasher); - } + fn byte_pos_to_line_and_col(&mut self, byte: BytePos) + -> Option<(Lrc, usize, BytePos)> + { + self.source_map().byte_pos_to_line_and_col(byte) } } diff --git a/src/libsyntax_pos/lib.rs b/src/libsyntax_pos/lib.rs index a24a2555bffae..66f25770722b2 100644 --- a/src/libsyntax_pos/lib.rs +++ b/src/libsyntax_pos/lib.rs @@ -37,10 +37,12 @@ mod analyze_source_file; pub mod fatal_error; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; +use rustc_data_structures::fingerprint::Fingerprint; use rustc_data_structures::sync::{Lrc, Lock}; +use rustc_data_structures::fx::FxHashMap; use std::borrow::Cow; -use std::cell::Cell; +use std::cell::{Cell, RefCell}; use std::cmp::{self, Ordering}; use std::fmt; use std::hash::{Hasher, Hash}; @@ -247,14 +249,6 @@ impl Ord for Span { } } -impl HashStable for Span - where CTX: HashStableContext -{ - fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { - ctx.hash_stable_span(self, hasher) - } -} - /// A collection of spans. Spans have two orthogonal attributes: /// /// - They can be *primary spans*. In this case they are the locus of @@ -1577,5 +1571,91 @@ fn lookup_line(lines: &[BytePos], pos: BytePos) -> isize { /// This is a hack to allow using the `HashStable_Generic` derive macro /// instead of implementing everything in librustc. pub trait HashStableContext { - fn hash_stable_span(&mut self, span: &Span, hasher: &mut StableHasher); + fn hash_spans(&self) -> bool; + fn byte_pos_to_line_and_col(&mut self, byte: BytePos) + -> Option<(Lrc, usize, BytePos)>; +} + +impl HashStable for Span + where CTX: HashStableContext +{ + /// Hashes a span in a stable way. We can't directly hash the span's `BytePos` + /// fields (that would be similar to hashing pointers, since those are just + /// offsets into the `SourceMap`). Instead, we hash the (file name, line, column) + /// triple, which stays the same even if the containing `SourceFile` has moved + /// within the `SourceMap`. + /// Also note that we are hashing byte offsets for the column, not unicode + /// codepoint offsets. For the purpose of the hash that's sufficient. + /// Also, hashing filenames is expensive so we avoid doing it twice when the + /// span starts and ends in the same file, which is almost always the case. + fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { + const TAG_VALID_SPAN: u8 = 0; + const TAG_INVALID_SPAN: u8 = 1; + const TAG_EXPANSION: u8 = 0; + const TAG_NO_EXPANSION: u8 = 1; + + if !ctx.hash_spans() { + return + } + + if *self == DUMMY_SP { + return std::hash::Hash::hash(&TAG_INVALID_SPAN, hasher); + } + + // If this is not an empty or invalid span, we want to hash the last + // position that belongs to it, as opposed to hashing the first + // position past it. + let span = self.data(); + let (file_lo, line_lo, col_lo) = match ctx.byte_pos_to_line_and_col(span.lo) { + Some(pos) => pos, + None => { + return std::hash::Hash::hash(&TAG_INVALID_SPAN, hasher); + } + }; + + if !file_lo.contains(span.hi) { + return std::hash::Hash::hash(&TAG_INVALID_SPAN, hasher); + } + + std::hash::Hash::hash(&TAG_VALID_SPAN, hasher); + // We truncate the stable ID hash and line and column numbers. The chances + // of causing a collision this way should be minimal. + std::hash::Hash::hash(&(file_lo.name_hash as u64), hasher); + + let col = (col_lo.0 as u64) & 0xFF; + let line = ((line_lo as u64) & 0xFF_FF_FF) << 8; + let len = ((span.hi - span.lo).0 as u64) << 32; + let line_col_len = col | line | len; + std::hash::Hash::hash(&line_col_len, hasher); + + if span.ctxt == SyntaxContext::root() { + TAG_NO_EXPANSION.hash_stable(ctx, hasher); + } else { + TAG_EXPANSION.hash_stable(ctx, hasher); + + // Since the same expansion context is usually referenced many + // times, we cache a stable hash of it and hash that instead of + // recursing every time. + thread_local! { + static CACHE: RefCell> = Default::default(); + } + + let sub_hash: u64 = CACHE.with(|cache| { + let expn_id = span.ctxt.outer_expn(); + + if let Some(&sub_hash) = cache.borrow().get(&expn_id) { + return sub_hash; + } + + let mut hasher = StableHasher::new(); + expn_id.expn_data().hash_stable(ctx, &mut hasher); + let sub_hash: Fingerprint = hasher.finish(); + let sub_hash = sub_hash.to_smaller_hash(); + cache.borrow_mut().insert(expn_id, sub_hash); + sub_hash + }); + + sub_hash.hash_stable(ctx, hasher); + } + } } From 4d1674f62007053ddeba44e27459e18128cc97cf Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 23 Nov 2019 14:41:56 +0100 Subject: [PATCH 15/16] Use proc-macro for TokenTree. --- src/libsyntax/token.rs | 1 + src/libsyntax/tokenstream.rs | 22 +--------------------- 2 files changed, 2 insertions(+), 21 deletions(-) diff --git a/src/libsyntax/token.rs b/src/libsyntax/token.rs index 08afca921fc92..045e9f8689f12 100644 --- a/src/libsyntax/token.rs +++ b/src/libsyntax/token.rs @@ -34,6 +34,7 @@ pub enum BinOpToken { /// A delimiter token. #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)] +#[derive(HashStable_Generic)] pub enum DelimToken { /// A round parenthesis (i.e., `(` or `)`). Paren, diff --git a/src/libsyntax/tokenstream.rs b/src/libsyntax/tokenstream.rs index 5ec99fa2f08e6..6a0523dd655b8 100644 --- a/src/libsyntax/tokenstream.rs +++ b/src/libsyntax/tokenstream.rs @@ -35,7 +35,7 @@ use std::{iter, mem}; /// /// The RHS of an MBE macro is the only place `SubstNt`s are substituted. /// Nothing special happens to misnamed or misplaced `SubstNt`s. -#[derive(Debug, Clone, PartialEq, RustcEncodable, RustcDecodable)] +#[derive(Debug, Clone, PartialEq, RustcEncodable, RustcDecodable, HashStable_Generic)] pub enum TokenTree { /// A single token Token(Token), @@ -53,26 +53,6 @@ where TokenStream: Send + Sync, {} -impl HashStable for TokenTree - where CTX: crate::HashStableContext -{ - fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) { - mem::discriminant(self).hash_stable(hcx, hasher); - match *self { - TokenTree::Token(ref token) => { - token.hash_stable(hcx, hasher); - } - TokenTree::Delimited(span, delim, ref tts) => { - span.hash_stable(hcx, hasher); - std::hash::Hash::hash(&delim, hasher); - for sub_tt in tts.trees() { - sub_tt.hash_stable(hcx, hasher); - } - } - } - } -} - impl TokenTree { /// Checks if this TokenTree is equal to the other, regardless of span information. pub fn eq_unspanned(&self, other: &TokenTree) -> bool { From 782cc9f65c0c19ef79bd009074e09bf0394674f4 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 23 Nov 2019 14:47:31 +0100 Subject: [PATCH 16/16] Derive HashStable for TokenKind. --- src/librustc/ich/impls_syntax.rs | 67 +------------------------------- src/libsyntax/lib.rs | 5 +-- src/libsyntax/token.rs | 19 ++++----- 3 files changed, 12 insertions(+), 79 deletions(-) diff --git a/src/librustc/ich/impls_syntax.rs b/src/librustc/ich/impls_syntax.rs index 2d7db94bf8c65..144980c53eb5f 100644 --- a/src/librustc/ich/impls_syntax.rs +++ b/src/librustc/ich/impls_syntax.rs @@ -3,12 +3,8 @@ use crate::ich::StableHashingContext; -use std::hash as std_hash; -use std::mem; - use syntax::ast; use syntax::feature_gate; -use syntax::token; use syntax_pos::SourceFile; use crate::hir::def_id::{DefId, CrateNum, CRATE_DEF_INDEX}; @@ -65,68 +61,7 @@ impl<'a> HashStable> for ast::Attribute { } } -impl<'ctx> syntax::HashStableContext for StableHashingContext<'ctx> { - fn hash_stable_tokenkind(&mut self, tokenkind: &token::TokenKind, hasher: &mut StableHasher) { - mem::discriminant(tokenkind).hash_stable(self, hasher); - match *tokenkind { - token::Eq | - token::Lt | - token::Le | - token::EqEq | - token::Ne | - token::Ge | - token::Gt | - token::AndAnd | - token::OrOr | - token::Not | - token::Tilde | - token::At | - token::Dot | - token::DotDot | - token::DotDotDot | - token::DotDotEq | - token::Comma | - token::Semi | - token::Colon | - token::ModSep | - token::RArrow | - token::LArrow | - token::FatArrow | - token::Pound | - token::Dollar | - token::Question | - token::SingleQuote | - token::Whitespace | - token::Comment | - token::Eof => {} - - token::BinOp(bin_op_token) | - token::BinOpEq(bin_op_token) => { - std_hash::Hash::hash(&bin_op_token, hasher); - } - - token::OpenDelim(delim_token) | - token::CloseDelim(delim_token) => { - std_hash::Hash::hash(&delim_token, hasher); - } - token::Literal(lit) => lit.hash_stable(self, hasher), - - token::Ident(name, is_raw) => { - name.hash_stable(self, hasher); - is_raw.hash_stable(self, hasher); - } - token::Lifetime(name) => name.hash_stable(self, hasher), - - token::Interpolated(_) => { - bug!("interpolated tokens should not be present in the HIR") - } - - token::DocComment(val) | - token::Shebang(val) | - token::Unknown(val) => val.hash_stable(self, hasher), - } - } -} +impl<'ctx> syntax::HashStableContext for StableHashingContext<'ctx> {} impl<'a> HashStable> for SourceFile { fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { diff --git a/src/libsyntax/lib.rs b/src/libsyntax/lib.rs index 579d26579392e..22b49862f4965 100644 --- a/src/libsyntax/lib.rs +++ b/src/libsyntax/lib.rs @@ -20,7 +20,6 @@ #![recursion_limit="256"] pub use errors; -use rustc_data_structures::stable_hasher::StableHasher; use rustc_data_structures::sync::Lock; use rustc_index::bit_set::GrowableBitSet; pub use rustc_data_structures::thin_vec::ThinVec; @@ -115,6 +114,4 @@ pub mod early_buffered_lints; /// Requirements for a `StableHashingContext` to be used in this crate. /// This is a hack to allow using the `HashStable_Generic` derive macro /// instead of implementing everything in librustc. -pub trait HashStableContext: syntax_pos::HashStableContext { - fn hash_stable_tokenkind(&mut self, tokenkind: &token::TokenKind, hasher: &mut StableHasher); -} +pub trait HashStableContext: syntax_pos::HashStableContext {} diff --git a/src/libsyntax/token.rs b/src/libsyntax/token.rs index 045e9f8689f12..6f45211ac5f29 100644 --- a/src/libsyntax/token.rs +++ b/src/libsyntax/token.rs @@ -19,6 +19,7 @@ use rustc_data_structures::sync::Lrc; use rustc_macros::HashStable_Generic; #[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)] +#[derive(HashStable_Generic)] pub enum BinOpToken { Plus, Minus, @@ -192,7 +193,7 @@ fn ident_can_begin_type(name: ast::Name, span: Span, is_raw: bool) -> bool { ].contains(&name) } -#[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Debug)] +#[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Debug, HashStable_Generic)] pub enum TokenKind { /* Expression-operator symbols. */ Eq, @@ -264,14 +265,6 @@ pub enum TokenKind { #[cfg(target_arch = "x86_64")] rustc_data_structures::static_assert_size!(TokenKind, 16); -impl HashStable for TokenKind - where CTX: crate::HashStableContext -{ - fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) { - hcx.hash_stable_tokenkind(self, hasher) - } -} - #[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Debug, HashStable_Generic)] pub struct Token { pub kind: TokenKind, @@ -735,3 +728,11 @@ impl fmt::Debug for Nonterminal { } } } + +impl HashStable for Nonterminal + where CTX: crate::HashStableContext +{ + fn hash_stable(&self, _hcx: &mut CTX, _hasher: &mut StableHasher) { + panic!("interpolated tokens should not be present in the HIR") + } +}