Skip to content

Commit

Permalink
Derive HashStable for TokenKind.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Nov 23, 2019
1 parent 4d1674f commit 782cc9f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 79 deletions.
67 changes: 1 addition & 66 deletions src/librustc/ich/impls_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -65,68 +61,7 @@ impl<'a> HashStable<StableHashingContext<'a>> 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<StableHashingContext<'a>> for SourceFile {
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
Expand Down
5 changes: 1 addition & 4 deletions src/libsyntax/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {}
19 changes: 10 additions & 9 deletions src/libsyntax/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -264,14 +265,6 @@ pub enum TokenKind {
#[cfg(target_arch = "x86_64")]
rustc_data_structures::static_assert_size!(TokenKind, 16);

impl<CTX> HashStable<CTX> 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,
Expand Down Expand Up @@ -735,3 +728,11 @@ impl fmt::Debug for Nonterminal {
}
}
}

impl<CTX> HashStable<CTX> 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")
}
}

0 comments on commit 782cc9f

Please sign in to comment.