Skip to content

Commit 31298b4

Browse files
committed
Invert implementations for TokenKind.
Also export a bunch of Token-related impls.
1 parent 0073d3b commit 31298b4

File tree

5 files changed

+55
-52
lines changed

5 files changed

+55
-52
lines changed

src/librustc/ich/impls_syntax.rs

+9-49
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,13 @@ use std::mem;
99
use syntax::ast;
1010
use syntax::feature_gate;
1111
use syntax::token;
12-
use syntax::tokenstream;
1312
use syntax_pos::SourceFile;
1413

1514
use crate::hir::def_id::{DefId, CrateNum, CRATE_DEF_INDEX};
1615

1716
use smallvec::SmallVec;
1817
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
1918

20-
impl<'ctx> syntax::StableHashingContextLike for StableHashingContext<'ctx> {}
2119
impl<'ctx> rustc_target::StableHashingContextLike for StableHashingContext<'ctx> {}
2220

2321
impl_stable_hash_for_spanned!(::syntax::ast::LitKind);
@@ -47,11 +45,6 @@ impl<'a> HashStable<StableHashingContext<'a>> for [ast::Attribute] {
4745
}
4846
}
4947

50-
impl_stable_hash_for!(struct ::syntax::ast::AttrItem {
51-
path,
52-
tokens,
53-
});
54-
5548
impl<'a> HashStable<StableHashingContext<'a>> for ast::Attribute {
5649
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
5750
// Make sure that these have been filtered out.
@@ -69,38 +62,10 @@ impl<'a> HashStable<StableHashingContext<'a>> for ast::Attribute {
6962
}
7063
}
7164

72-
impl<'a> HashStable<StableHashingContext<'a>>
73-
for tokenstream::TokenTree {
74-
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
75-
mem::discriminant(self).hash_stable(hcx, hasher);
76-
match *self {
77-
tokenstream::TokenTree::Token(ref token) => {
78-
token.hash_stable(hcx, hasher);
79-
}
80-
tokenstream::TokenTree::Delimited(span, delim, ref tts) => {
81-
span.hash_stable(hcx, hasher);
82-
std_hash::Hash::hash(&delim, hasher);
83-
for sub_tt in tts.trees() {
84-
sub_tt.hash_stable(hcx, hasher);
85-
}
86-
}
87-
}
88-
}
89-
}
90-
91-
impl<'a> HashStable<StableHashingContext<'a>>
92-
for tokenstream::TokenStream {
93-
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
94-
for sub_tt in self.trees() {
95-
sub_tt.hash_stable(hcx, hasher);
96-
}
97-
}
98-
}
99-
100-
impl<'a> HashStable<StableHashingContext<'a>> for token::TokenKind {
101-
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
102-
mem::discriminant(self).hash_stable(hcx, hasher);
103-
match *self {
65+
impl<'ctx> syntax::StableHashingContextLike for StableHashingContext<'ctx> {
66+
fn hash_stable_tokenkind(&mut self, tokenkind: &token::TokenKind, hasher: &mut StableHasher) {
67+
mem::discriminant(tokenkind).hash_stable(self, hasher);
68+
match *tokenkind {
10469
token::Eq |
10570
token::Lt |
10671
token::Le |
@@ -141,30 +106,25 @@ impl<'a> HashStable<StableHashingContext<'a>> for token::TokenKind {
141106
token::CloseDelim(delim_token) => {
142107
std_hash::Hash::hash(&delim_token, hasher);
143108
}
144-
token::Literal(lit) => lit.hash_stable(hcx, hasher),
109+
token::Literal(lit) => lit.hash_stable(self, hasher),
145110

146111
token::Ident(name, is_raw) => {
147-
name.hash_stable(hcx, hasher);
148-
is_raw.hash_stable(hcx, hasher);
112+
name.hash_stable(self, hasher);
113+
is_raw.hash_stable(self, hasher);
149114
}
150-
token::Lifetime(name) => name.hash_stable(hcx, hasher),
115+
token::Lifetime(name) => name.hash_stable(self, hasher),
151116

152117
token::Interpolated(_) => {
153118
bug!("interpolated tokens should not be present in the HIR")
154119
}
155120

156121
token::DocComment(val) |
157122
token::Shebang(val) |
158-
token::Unknown(val) => val.hash_stable(hcx, hasher),
123+
token::Unknown(val) => val.hash_stable(self, hasher),
159124
}
160125
}
161126
}
162127

163-
impl_stable_hash_for!(struct token::Token {
164-
kind,
165-
span
166-
});
167-
168128
impl_stable_hash_for!(enum ::syntax::ast::NestedMetaItem {
169129
MetaItem(meta_item),
170130
Literal(lit)

src/libsyntax/ast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2276,7 +2276,7 @@ impl rustc_serialize::Decodable for AttrId {
22762276
}
22772277
}
22782278

2279-
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
2279+
#[derive(Clone, RustcEncodable, RustcDecodable, Debug, HashStable_Generic)]
22802280
pub struct AttrItem {
22812281
pub path: Path,
22822282
pub tokens: TokenStream,

src/libsyntax/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#![recursion_limit="256"]
2121

2222
pub use errors;
23+
use rustc_data_structures::stable_hasher::StableHasher;
2324
use rustc_data_structures::sync::Lock;
2425
use rustc_index::bit_set::GrowableBitSet;
2526
pub use rustc_data_structures::thin_vec::ThinVec;
@@ -114,4 +115,6 @@ pub mod early_buffered_lints;
114115
/// Requirements for a `StableHashingContext` to be used in this crate.
115116
/// This is a hack to allow using the `HashStable_Generic` derive macro
116117
/// instead of implementing everything in librustc.
117-
pub trait StableHashingContextLike: syntax_pos::StableHashingContextLike {}
118+
pub trait StableHashingContextLike: syntax_pos::StableHashingContextLike {
119+
fn hash_stable_tokenkind(&mut self, tokenkind: &token::TokenKind, hasher: &mut StableHasher);
120+
}

src/libsyntax/token.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use syntax_pos::{self, Span, DUMMY_SP};
1414

1515
use std::fmt;
1616
use std::mem;
17+
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
1718
use rustc_data_structures::sync::Lrc;
1819
use rustc_macros::HashStable_Generic;
1920

@@ -262,7 +263,15 @@ pub enum TokenKind {
262263
#[cfg(target_arch = "x86_64")]
263264
rustc_data_structures::static_assert_size!(TokenKind, 16);
264265

265-
#[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Debug)]
266+
impl<CTX> HashStable<CTX> for TokenKind
267+
where CTX: crate::StableHashingContextLike
268+
{
269+
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
270+
hcx.hash_stable_tokenkind(self, hasher)
271+
}
272+
}
273+
274+
#[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Debug, HashStable_Generic)]
266275
pub struct Token {
267276
pub kind: TokenKind,
268277
pub span: Span,

src/libsyntax/tokenstream.rs

+31
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use crate::token::{self, DelimToken, Token, TokenKind};
1717

1818
use syntax_pos::{Span, DUMMY_SP};
19+
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
1920
use rustc_data_structures::sync::Lrc;
2021
use smallvec::{SmallVec, smallvec};
2122

@@ -51,6 +52,26 @@ where
5152
TokenStream: Send + Sync,
5253
{}
5354

55+
impl<CTX> HashStable<CTX> for TokenTree
56+
where CTX: crate::StableHashingContextLike
57+
{
58+
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
59+
mem::discriminant(self).hash_stable(hcx, hasher);
60+
match *self {
61+
TokenTree::Token(ref token) => {
62+
token.hash_stable(hcx, hasher);
63+
}
64+
TokenTree::Delimited(span, delim, ref tts) => {
65+
span.hash_stable(hcx, hasher);
66+
std::hash::Hash::hash(&delim, hasher);
67+
for sub_tt in tts.trees() {
68+
sub_tt.hash_stable(hcx, hasher);
69+
}
70+
}
71+
}
72+
}
73+
}
74+
5475
impl TokenTree {
5576
/// Checks if this TokenTree is equal to the other, regardless of span information.
5677
pub fn eq_unspanned(&self, other: &TokenTree) -> bool {
@@ -115,6 +136,16 @@ impl TokenTree {
115136
}
116137
}
117138

139+
impl<CTX> HashStable<CTX> for TokenStream
140+
where CTX: crate::StableHashingContextLike
141+
{
142+
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
143+
for sub_tt in self.trees() {
144+
sub_tt.hash_stable(hcx, hasher);
145+
}
146+
}
147+
}
148+
118149
/// A `TokenStream` is an abstract sequence of tokens, organized into `TokenTree`s.
119150
///
120151
/// The goal is for procedural macros to work with `TokenStream`s and `TokenTree`s

0 commit comments

Comments
 (0)