diff options
| author | Camille GILLOT <gillot.camille@gmail.com> | 2019-11-10 18:24:37 +0100 |
|---|---|---|
| committer | Camille GILLOT <gillot.camille@gmail.com> | 2019-11-22 20:01:45 +0100 |
| commit | 31298b41d65093b2ea260d8fe8820da6db6dac94 (patch) | |
| tree | 2a792a21b0dcb3834272399e6827ddb2443c8d9f | |
| parent | 0073d3be97707b34f747c2633ac02e8c9ea89452 (diff) | |
| download | rust-31298b41d65093b2ea260d8fe8820da6db6dac94.tar.gz rust-31298b41d65093b2ea260d8fe8820da6db6dac94.zip | |
Invert implementations for TokenKind.
Also export a bunch of Token-related impls.
| -rw-r--r-- | src/librustc/ich/impls_syntax.rs | 58 | ||||
| -rw-r--r-- | src/libsyntax/ast.rs | 2 | ||||
| -rw-r--r-- | src/libsyntax/lib.rs | 5 | ||||
| -rw-r--r-- | src/libsyntax/token.rs | 11 | ||||
| -rw-r--r-- | 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 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<StableHashingContext<'a>> for [ast::Attribute] { } } -impl_stable_hash_for!(struct ::syntax::ast::AttrItem { - path, - tokens, -}); - impl<'a> HashStable<StableHashingContext<'a>> 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<StableHashingContext<'a>> for ast::Attribute { } } -impl<'a> HashStable<StableHashingContext<'a>> -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<StableHashingContext<'a>> -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<StableHashingContext<'a>> 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<StableHashingContext<'a>> 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<StableHashingContext<'a>> 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<CTX> HashStable<CTX> 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<CTX> HashStable<CTX> 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<CTX> HashStable<CTX> 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 |
