From 1de5fdb5babf74f729b008585a8aaf16110bb1fd Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 10 Nov 2019 17:19:08 +0100 Subject: Add StableHashingContextLike to HashStable_Generic derive. --- src/libsyntax/lib.rs | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/libsyntax') diff --git a/src/libsyntax/lib.rs b/src/libsyntax/lib.rs index e3eca75dfe7..8f091c06b4b 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 {} -- cgit 1.4.1-3-g733a5 From 0073d3be97707b34f747c2633ac02e8c9ea89452 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 10 Nov 2019 18:10:15 +0100 Subject: 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(-) (limited to 'src/libsyntax') diff --git a/src/librustc/ich/hcx.rs b/src/librustc/ich/hcx.rs index da45f93b40c..c67e1a7da7b 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 83852bbbfc4..e1281d67703 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 a9f03e4af5b..01abd09aa91 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 8f091c06b4b..7865323f842 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 2201f1ed6ca..40f00aaa5d8 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, -- cgit 1.4.1-3-g733a5 From 31298b41d65093b2ea260d8fe8820da6db6dac94 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 10 Nov 2019 18:24:37 +0100 Subject: 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(-) (limited to 'src/libsyntax') diff --git a/src/librustc/ich/impls_syntax.rs b/src/librustc/ich/impls_syntax.rs index e1281d67703..a37e014d2bd 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 01abd09aa91..3cc6a043e3b 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 7865323f842..939e1877b4a 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 fd1623384a4..305db739399 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 40f00aaa5d8..6de9a0fb070 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 -- cgit 1.4.1-3-g733a5 From e85c195174bb13616c155fd9871006c56aef0bc7 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 10 Nov 2019 18:32:43 +0100 Subject: 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(-) (limited to 'src/libsyntax') diff --git a/src/librustc/ich/impls_hir.rs b/src/librustc/ich/impls_hir.rs index 48c9cb619cc..d849c2d6857 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 a37e014d2bd..a30804c76e6 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 3cc6a043e3b..57f2104e4a5 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. /// -- cgit 1.4.1-3-g733a5 From 9efd3205b57aaa8908a1eca337ab8336cdb16bc2 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Fri, 22 Nov 2019 20:17:22 +0100 Subject: Fix rebase fallout. --- src/libsyntax/tokenstream.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'src/libsyntax') diff --git a/src/libsyntax/tokenstream.rs b/src/libsyntax/tokenstream.rs index 6de9a0fb070..eb99e549a7f 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}; -- cgit 1.4.1-3-g733a5 From ce301075c0de3b69914cc11744558dabd6d2e731 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 23 Nov 2019 13:58:17 +0100 Subject: 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(-) (limited to 'src/libsyntax') diff --git a/src/librustc/ich/hcx.rs b/src/librustc/ich/hcx.rs index c67e1a7da7b..55c02074fce 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 6e030562832..2d7db94bf8c 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 9003517d715..103fcd0e8e7 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 274f5ec7f1d..5582eaf47c4 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 939e1877b4a..579d2657939 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 305db739399..08afca921fc 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 eb99e549a7f..5ec99fa2f08 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 25391ad5ce6..1c66858a915 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); } -- cgit 1.4.1-3-g733a5 From 4d1674f62007053ddeba44e27459e18128cc97cf Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 23 Nov 2019 14:41:56 +0100 Subject: Use proc-macro for TokenTree. --- src/libsyntax/token.rs | 1 + src/libsyntax/tokenstream.rs | 22 +--------------------- 2 files changed, 2 insertions(+), 21 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/token.rs b/src/libsyntax/token.rs index 08afca921fc..045e9f8689f 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 5ec99fa2f08..6a0523dd655 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 { -- cgit 1.4.1-3-g733a5 From 782cc9f65c0c19ef79bd009074e09bf0394674f4 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 23 Nov 2019 14:47:31 +0100 Subject: 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(-) (limited to 'src/libsyntax') diff --git a/src/librustc/ich/impls_syntax.rs b/src/librustc/ich/impls_syntax.rs index 2d7db94bf8c..144980c53eb 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 579d2657939..22b49862f49 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 045e9f8689f..6f45211ac5f 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") + } +} -- cgit 1.4.1-3-g733a5