Skip to content

Commit ff86ff4

Browse files
committed
Derive Hash for TokenStream
1 parent 2f37ce2 commit ff86ff4

File tree

4 files changed

+17
-114
lines changed

4 files changed

+17
-114
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3243,7 +3243,7 @@ impl UseTree {
32433243
/// Distinguishes between `Attribute`s that decorate items and Attributes that
32443244
/// are contained as statements within items. These two cases need to be
32453245
/// distinguished for pretty-printing.
3246-
#[derive(Clone, PartialEq, Encodable, Decodable, Debug, Copy, HashStable_Generic, Walkable)]
3246+
#[derive(Clone, PartialEq, Encodable, Decodable, Hash, Debug, Copy, HashStable_Generic, Walkable)]
32473247
pub enum AttrStyle {
32483248
Outer,
32493249
Inner,

compiler/rustc_ast/src/token.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ use rustc_span::{Ident, Symbol};
1515
use crate::ast;
1616
use crate::util::case::Case;
1717

18-
#[derive(Clone, Copy, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
18+
#[derive(Clone, Copy, PartialEq, Hash, Encodable, Decodable, Debug, HashStable_Generic)]
1919
pub enum CommentKind {
2020
Line,
2121
Block,
2222
}
2323

2424
// This type must not implement `Hash` due to the unusual `PartialEq` impl below.
25-
#[derive(Copy, Clone, Debug, Encodable, Decodable, HashStable_Generic)]
25+
#[derive(Copy, Clone, Debug, Hash, Encodable, Decodable, HashStable_Generic)]
2626
pub enum InvisibleOrigin {
2727
// From the expansion of a metavariable in a declarative macro.
2828
MetaVar(MetaVarKind),
@@ -113,7 +113,7 @@ impl fmt::Display for MetaVarKind {
113113
/// Describes how a sequence of token trees is delimited.
114114
/// Cannot use `proc_macro::Delimiter` directly because this
115115
/// structure should implement some additional traits.
116-
#[derive(Copy, Clone, Debug, PartialEq, Encodable, Decodable, HashStable_Generic)]
116+
#[derive(Copy, Clone, Debug, PartialEq, Hash, Encodable, Decodable, HashStable_Generic)]
117117
pub enum Delimiter {
118118
/// `( ... )`
119119
Parenthesis,
@@ -175,7 +175,7 @@ impl Delimiter {
175175
// type. This means that float literals like `1f32` are classified by this type
176176
// as `Int`. Only upon conversion to `ast::LitKind` will such a literal be
177177
// given the `Float` kind.
178-
#[derive(Clone, Copy, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
178+
#[derive(Clone, Copy, PartialEq, Hash, Encodable, Decodable, Debug, HashStable_Generic)]
179179
pub enum LitKind {
180180
Bool, // AST only, must never appear in a `Token`
181181
Byte,
@@ -192,7 +192,7 @@ pub enum LitKind {
192192
}
193193

194194
/// A literal token.
195-
#[derive(Clone, Copy, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
195+
#[derive(Clone, Copy, PartialEq, Hash, Encodable, Decodable, Debug, HashStable_Generic)]
196196
pub struct Lit {
197197
pub kind: LitKind,
198198
pub symbol: Symbol,
@@ -338,7 +338,7 @@ fn ident_can_begin_type(name: Symbol, span: Span, is_raw: IdentIsRaw) -> bool {
338338
.contains(&name)
339339
}
340340

341-
#[derive(PartialEq, Encodable, Decodable, Debug, Copy, Clone, HashStable_Generic)]
341+
#[derive(PartialEq, Encodable, Decodable, Hash, Debug, Copy, Clone, HashStable_Generic)]
342342
pub enum IdentIsRaw {
343343
No,
344344
Yes,
@@ -356,7 +356,7 @@ impl From<IdentIsRaw> for bool {
356356
}
357357
}
358358

359-
#[derive(Clone, Copy, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
359+
#[derive(Clone, Copy, PartialEq, Hash, Encodable, Decodable, Debug, HashStable_Generic)]
360360
pub enum TokenKind {
361361
/* Expression-operator symbols. */
362362
/// `=`
@@ -506,7 +506,7 @@ pub enum TokenKind {
506506
Eof,
507507
}
508508

509-
#[derive(Clone, Copy, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
509+
#[derive(Clone, Copy, PartialEq, Hash, Encodable, Decodable, Debug, HashStable_Generic)]
510510
pub struct Token {
511511
pub kind: TokenKind,
512512
pub span: Span,

compiler/rustc_ast/src/tokenstream.rs

Lines changed: 7 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ use std::{cmp, fmt, iter, mem};
2222
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
2323
use rustc_data_structures::sync;
2424
use rustc_macros::{Decodable, Encodable, HashStable_Generic, Walkable};
25-
use rustc_serialize::{Decodable, Encodable, Encoder};
26-
use rustc_span::def_id::{CrateNum, DefIndex};
27-
use rustc_span::{ByteSymbol, DUMMY_SP, Span, SpanDecoder, SpanEncoder, Symbol, sym};
25+
use rustc_serialize::{Decodable, Encodable};
26+
use rustc_span::{DUMMY_SP, Span, SpanDecoder, SpanEncoder, Symbol, sym};
2827
use thin_vec::ThinVec;
2928

3029
use crate::ast::AttrStyle;
@@ -33,7 +32,7 @@ use crate::token::{self, Delimiter, Token, TokenKind};
3332
use crate::{AttrVec, Attribute};
3433

3534
/// Part of a `TokenStream`.
36-
#[derive(Debug, Clone, PartialEq, Encodable, Decodable, HashStable_Generic)]
35+
#[derive(Debug, Clone, PartialEq, Hash, Encodable, Decodable, HashStable_Generic)]
3736
pub enum TokenTree {
3837
/// A single token. Should never be `OpenDelim` or `CloseDelim`, because
3938
/// delimiters are implicitly represented by `Delimited`.
@@ -559,110 +558,14 @@ pub struct AttrsTarget {
559558
}
560559

561560
/// A `TokenStream` is an abstract sequence of tokens, organized into [`TokenTree`]s.
562-
#[derive(Clone, Debug, Default, Encodable, Decodable)]
561+
#[derive(Clone, Debug, Default, Hash, Encodable, Decodable)]
563562
pub struct TokenStream(pub(crate) Arc<Vec<TokenTree>>);
564563

565-
struct HashEncoder<H: std::hash::Hasher> {
566-
hasher: H,
567-
}
568-
569-
impl<H: std::hash::Hasher> Encoder for HashEncoder<H> {
570-
fn emit_usize(&mut self, v: usize) {
571-
self.hasher.write_usize(v)
572-
}
573-
574-
fn emit_u128(&mut self, v: u128) {
575-
self.hasher.write_u128(v)
576-
}
577-
578-
fn emit_u64(&mut self, v: u64) {
579-
self.hasher.write_u64(v)
580-
}
581-
582-
fn emit_u32(&mut self, v: u32) {
583-
self.hasher.write_u32(v)
584-
}
585-
586-
fn emit_u16(&mut self, v: u16) {
587-
self.hasher.write_u16(v)
588-
}
589-
590-
fn emit_u8(&mut self, v: u8) {
591-
self.hasher.write_u8(v)
592-
}
593-
594-
fn emit_isize(&mut self, v: isize) {
595-
self.hasher.write_isize(v)
596-
}
597-
598-
fn emit_i128(&mut self, v: i128) {
599-
self.hasher.write_i128(v)
600-
}
601-
602-
fn emit_i64(&mut self, v: i64) {
603-
self.hasher.write_i64(v)
604-
}
605-
606-
fn emit_i32(&mut self, v: i32) {
607-
self.hasher.write_i32(v)
608-
}
609-
610-
fn emit_i16(&mut self, v: i16) {
611-
self.hasher.write_i16(v)
612-
}
613-
614-
fn emit_raw_bytes(&mut self, s: &[u8]) {
615-
self.hasher.write(s)
616-
}
617-
}
618-
619-
impl<H: std::hash::Hasher> SpanEncoder for HashEncoder<H> {
620-
fn encode_span(&mut self, span: Span) {
621-
span.hash(&mut self.hasher)
622-
}
623-
624-
fn encode_symbol(&mut self, symbol: Symbol) {
625-
symbol.hash(&mut self.hasher)
626-
}
627-
628-
fn encode_byte_symbol(&mut self, byte_sym: ByteSymbol) {
629-
byte_sym.hash(&mut self.hasher);
630-
}
631-
632-
fn encode_expn_id(&mut self, expn_id: rustc_span::ExpnId) {
633-
expn_id.hash(&mut self.hasher)
634-
}
635-
636-
fn encode_syntax_context(&mut self, syntax_context: rustc_span::SyntaxContext) {
637-
syntax_context.hash(&mut self.hasher)
638-
}
639-
640-
fn encode_crate_num(&mut self, crate_num: CrateNum) {
641-
crate_num.hash(&mut self.hasher)
642-
}
643-
644-
fn encode_def_index(&mut self, def_index: DefIndex) {
645-
def_index.hash(&mut self.hasher)
646-
}
647-
648-
fn encode_def_id(&mut self, def_id: rustc_span::def_id::DefId) {
649-
def_id.hash(&mut self.hasher)
650-
}
651-
}
652-
653-
/// TokenStream needs to be hashable because it is used as a query key for caching derive macro
654-
/// expansions.
655-
impl Hash for TokenStream {
656-
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
657-
Encodable::encode(self, &mut HashEncoder { hasher: state });
658-
}
659-
}
660-
661564
/// Indicates whether a token can join with the following token to form a
662565
/// compound token. Used for conversions to `proc_macro::Spacing`. Also used to
663566
/// guide pretty-printing, which is where the `JointHidden` value (which isn't
664567
/// part of `proc_macro::Spacing`) comes in useful.
665-
#[derive(Clone, Copy, Debug, PartialEq, Encodable, Decodable, HashStable_Generic)]
568+
#[derive(Clone, Copy, Debug, PartialEq, Hash, Encodable, Decodable, HashStable_Generic)]
666569
pub enum Spacing {
667570
/// The token cannot join with the following token to form a compound
668571
/// token.
@@ -1075,7 +978,7 @@ impl TokenCursor {
1075978
}
1076979
}
1077980

1078-
#[derive(Debug, Copy, Clone, PartialEq, Encodable, Decodable, HashStable_Generic, Walkable)]
981+
#[derive(Debug, Copy, Clone, PartialEq, Hash, Encodable, Decodable, HashStable_Generic, Walkable)]
1079982
pub struct DelimSpan {
1080983
pub open: Span,
1081984
pub close: Span,
@@ -1099,7 +1002,7 @@ impl DelimSpan {
10991002
}
11001003
}
11011004

1102-
#[derive(Copy, Clone, Debug, PartialEq, Encodable, Decodable, HashStable_Generic)]
1005+
#[derive(Copy, Clone, Debug, PartialEq, Hash, Encodable, Decodable, HashStable_Generic)]
11031006
pub struct DelimSpacing {
11041007
pub open: Spacing,
11051008
pub close: Spacing,

compiler/rustc_middle/src/query/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ rustc_queries! {
168168
/// Caches the expansion of a derive proc macro, e.g. `#[derive(Serialize)]`.
169169
/// The key is:
170170
/// - A unique key corresponding to the invocation of a macro.
171-
/// - Strict Version Hash of a crate.
171+
/// - Strict Version Hash of the crate defining the proc macro.
172172
/// - Token stream which serves as an input to the macro.
173173
///
174174
/// The output is the token stream generated by the proc macro.

0 commit comments

Comments
 (0)