1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! This module contains `HashStable` implementations for various data types
12 //! from libsyntax in no particular order.
14 use ich::StableHashingContext;
16 use std::hash as std_hash;
20 use syntax::feature_gate;
21 use syntax::parse::token;
22 use syntax::symbol::{InternedString, LocalInternedString};
23 use syntax::tokenstream;
24 use syntax_pos::FileMap;
26 use hir::def_id::{DefId, CrateNum, CRATE_DEF_INDEX};
28 use rustc_data_structures::stable_hasher::{HashStable, ToStableHashKey,
29 StableHasher, StableHasherResult};
30 use rustc_data_structures::accumulate_vec::AccumulateVec;
32 impl<'a> HashStable<StableHashingContext<'a>> for InternedString {
34 fn hash_stable<W: StableHasherResult>(&self,
35 hcx: &mut StableHashingContext<'a>,
36 hasher: &mut StableHasher<W>) {
37 self.with(|s| s.hash_stable(hcx, hasher))
41 impl<'a> ToStableHashKey<StableHashingContext<'a>> for InternedString {
42 type KeyType = InternedString;
45 fn to_stable_hash_key(&self,
46 _: &StableHashingContext<'a>)
52 impl<'a> HashStable<StableHashingContext<'a>> for LocalInternedString {
54 fn hash_stable<W: StableHasherResult>(&self,
55 hcx: &mut StableHashingContext<'a>,
56 hasher: &mut StableHasher<W>) {
57 let s: &str = &**self;
58 s.hash_stable(hcx, hasher);
62 impl<'a> ToStableHashKey<StableHashingContext<'a>> for LocalInternedString {
63 type KeyType = LocalInternedString;
66 fn to_stable_hash_key(&self,
67 _: &StableHashingContext<'a>)
68 -> LocalInternedString {
73 impl<'a> HashStable<StableHashingContext<'a>> for ast::Name {
75 fn hash_stable<W: StableHasherResult>(&self,
76 hcx: &mut StableHashingContext<'a>,
77 hasher: &mut StableHasher<W>) {
78 self.as_str().hash_stable(hcx, hasher);
82 impl<'a> ToStableHashKey<StableHashingContext<'a>> for ast::Name {
83 type KeyType = InternedString;
86 fn to_stable_hash_key(&self,
87 _: &StableHashingContext<'a>)
89 self.as_interned_str()
93 impl_stable_hash_for!(enum ::syntax::ast::AsmDialect {
98 impl_stable_hash_for!(enum ::syntax::ext::base::MacroKind {
106 impl_stable_hash_for!(enum ::rustc_target::spec::abi::Abi {
128 impl_stable_hash_for!(struct ::syntax::attr::Deprecation { since, note });
129 impl_stable_hash_for!(struct ::syntax::attr::Stability {
136 impl<'a> HashStable<StableHashingContext<'a>>
137 for ::syntax::edition::Edition {
138 fn hash_stable<W: StableHasherResult>(&self,
139 hcx: &mut StableHashingContext<'a>,
140 hasher: &mut StableHasher<W>) {
141 mem::discriminant(self).hash_stable(hcx, hasher);
145 impl<'a> HashStable<StableHashingContext<'a>>
146 for ::syntax::attr::StabilityLevel {
147 fn hash_stable<W: StableHasherResult>(&self,
148 hcx: &mut StableHashingContext<'a>,
149 hasher: &mut StableHasher<W>) {
150 mem::discriminant(self).hash_stable(hcx, hasher);
152 ::syntax::attr::StabilityLevel::Unstable { ref reason, ref issue } => {
153 reason.hash_stable(hcx, hasher);
154 issue.hash_stable(hcx, hasher);
156 ::syntax::attr::StabilityLevel::Stable { ref since } => {
157 since.hash_stable(hcx, hasher);
163 impl_stable_hash_for!(struct ::syntax::attr::RustcDeprecation { since, reason });
164 impl_stable_hash_for!(struct ::syntax::attr::RustcConstUnstable { feature });
167 impl_stable_hash_for!(enum ::syntax::attr::IntType {
172 impl_stable_hash_for!(enum ::syntax::ast::LitIntType {
178 impl_stable_hash_for_spanned!(::syntax::ast::LitKind);
179 impl_stable_hash_for!(enum ::syntax::ast::LitKind {
184 Int(value, lit_int_type),
185 Float(value, float_ty),
186 FloatUnsuffixed(value),
190 impl_stable_hash_for!(enum ::syntax::ast::IntTy { Isize, I8, I16, I32, I64, I128 });
191 impl_stable_hash_for!(enum ::syntax::ast::UintTy { Usize, U8, U16, U32, U64, U128 });
192 impl_stable_hash_for!(enum ::syntax::ast::FloatTy { F32, F64 });
193 impl_stable_hash_for!(enum ::syntax::ast::Unsafety { Unsafe, Normal });
194 impl_stable_hash_for!(enum ::syntax::ast::Constness { Const, NotConst });
195 impl_stable_hash_for!(enum ::syntax::ast::Defaultness { Default, Final });
196 impl_stable_hash_for!(struct ::syntax::ast::Lifetime { id, ident });
197 impl_stable_hash_for!(enum ::syntax::ast::StrStyle { Cooked, Raw(pounds) });
198 impl_stable_hash_for!(enum ::syntax::ast::AttrStyle { Outer, Inner });
200 impl<'a> HashStable<StableHashingContext<'a>> for [ast::Attribute] {
201 fn hash_stable<W: StableHasherResult>(&self,
202 hcx: &mut StableHashingContext<'a>,
203 hasher: &mut StableHasher<W>) {
205 self.len().hash_stable(hcx, hasher);
209 // Some attributes are always ignored during hashing.
210 let filtered: AccumulateVec<[&ast::Attribute; 8]> = self
213 !attr.is_sugared_doc && !hcx.is_ignored_attr(attr.name())
217 filtered.len().hash_stable(hcx, hasher);
218 for attr in filtered {
219 attr.hash_stable(hcx, hasher);
224 impl<'a> HashStable<StableHashingContext<'a>> for ast::Path {
225 fn hash_stable<W: StableHasherResult>(&self,
226 hcx: &mut StableHashingContext<'a>,
227 hasher: &mut StableHasher<W>) {
228 self.segments.len().hash_stable(hcx, hasher);
229 for segment in &self.segments {
230 segment.ident.name.hash_stable(hcx, hasher);
235 impl<'a> HashStable<StableHashingContext<'a>> for ast::Attribute {
236 fn hash_stable<W: StableHasherResult>(&self,
237 hcx: &mut StableHashingContext<'a>,
238 hasher: &mut StableHasher<W>) {
239 // Make sure that these have been filtered out.
240 debug_assert!(!hcx.is_ignored_attr(self.name()));
241 debug_assert!(!self.is_sugared_doc);
252 style.hash_stable(hcx, hasher);
253 path.hash_stable(hcx, hasher);
254 for tt in tokens.trees() {
255 tt.hash_stable(hcx, hasher);
257 span.hash_stable(hcx, hasher);
261 impl<'a> HashStable<StableHashingContext<'a>>
262 for tokenstream::TokenTree {
263 fn hash_stable<W: StableHasherResult>(&self,
264 hcx: &mut StableHashingContext<'a>,
265 hasher: &mut StableHasher<W>) {
266 mem::discriminant(self).hash_stable(hcx, hasher);
268 tokenstream::TokenTree::Token(span, ref token) => {
269 span.hash_stable(hcx, hasher);
270 hash_token(token, hcx, hasher);
272 tokenstream::TokenTree::Delimited(span, ref delimited) => {
273 span.hash_stable(hcx, hasher);
274 std_hash::Hash::hash(&delimited.delim, hasher);
275 for sub_tt in delimited.stream().trees() {
276 sub_tt.hash_stable(hcx, hasher);
283 impl<'a> HashStable<StableHashingContext<'a>>
284 for tokenstream::TokenStream {
285 fn hash_stable<W: StableHasherResult>(&self,
286 hcx: &mut StableHashingContext<'a>,
287 hasher: &mut StableHasher<W>) {
288 for sub_tt in self.trees() {
289 sub_tt.hash_stable(hcx, hasher);
294 fn hash_token<'a, 'gcx, W: StableHasherResult>(
295 token: &token::Token,
296 hcx: &mut StableHashingContext<'a>,
297 hasher: &mut StableHasher<W>,
299 mem::discriminant(token).hash_stable(hcx, hasher);
308 token::Token::AndAnd |
311 token::Token::Tilde |
314 token::Token::DotDot |
315 token::Token::DotDotDot |
316 token::Token::DotDotEq |
317 token::Token::DotEq |
318 token::Token::Comma |
320 token::Token::Colon |
321 token::Token::ModSep |
322 token::Token::RArrow |
323 token::Token::LArrow |
324 token::Token::FatArrow |
325 token::Token::Pound |
326 token::Token::Dollar |
327 token::Token::Question |
328 token::Token::SingleQuote |
329 token::Token::Whitespace |
330 token::Token::Comment |
331 token::Token::Eof => {}
333 token::Token::BinOp(bin_op_token) |
334 token::Token::BinOpEq(bin_op_token) => {
335 std_hash::Hash::hash(&bin_op_token, hasher);
338 token::Token::OpenDelim(delim_token) |
339 token::Token::CloseDelim(delim_token) => {
340 std_hash::Hash::hash(&delim_token, hasher);
342 token::Token::Literal(ref lit, ref opt_name) => {
343 mem::discriminant(lit).hash_stable(hcx, hasher);
345 token::Lit::Byte(val) |
346 token::Lit::Char(val) |
347 token::Lit::Integer(val) |
348 token::Lit::Float(val) |
349 token::Lit::Str_(val) |
350 token::Lit::ByteStr(val) => val.hash_stable(hcx, hasher),
351 token::Lit::StrRaw(val, n) |
352 token::Lit::ByteStrRaw(val, n) => {
353 val.hash_stable(hcx, hasher);
354 n.hash_stable(hcx, hasher);
357 opt_name.hash_stable(hcx, hasher);
360 token::Token::Ident(ident, is_raw) => {
361 ident.name.hash_stable(hcx, hasher);
362 is_raw.hash_stable(hcx, hasher);
364 token::Token::Lifetime(ident) => ident.name.hash_stable(hcx, hasher),
366 token::Token::Interpolated(_) => {
367 bug!("interpolated tokens should not be present in the HIR")
370 token::Token::DocComment(val) |
371 token::Token::Shebang(val) => val.hash_stable(hcx, hasher),
375 impl_stable_hash_for_spanned!(::syntax::ast::NestedMetaItemKind);
377 impl_stable_hash_for!(enum ::syntax::ast::NestedMetaItemKind {
382 impl_stable_hash_for!(struct ::syntax::ast::MetaItem {
388 impl_stable_hash_for!(enum ::syntax::ast::MetaItemKind {
394 impl_stable_hash_for!(struct ::syntax_pos::hygiene::ExpnInfo {
398 allow_internal_unstable,
399 allow_internal_unsafe,
404 impl_stable_hash_for!(enum ::syntax_pos::hygiene::ExpnFormat {
407 CompilerDesugaring(kind)
410 impl_stable_hash_for!(enum ::syntax_pos::hygiene::CompilerDesugaringKind {
413 ExistentialReturnType,
418 impl_stable_hash_for!(enum ::syntax_pos::FileName {
430 impl<'a> HashStable<StableHashingContext<'a>> for FileMap {
431 fn hash_stable<W: StableHasherResult>(&self,
432 hcx: &mut StableHashingContext<'a>,
433 hasher: &mut StableHasher<W>) {
435 name: _, // We hash the smaller name_hash instead of this
440 // Do not hash the source as it is not encoded
448 ref non_narrow_chars,
451 (name_hash as u64).hash_stable(hcx, hasher);
452 name_was_remapped.hash_stable(hcx, hasher);
455 krate: CrateNum::from_u32(crate_of_origin),
456 index: CRATE_DEF_INDEX,
457 }.hash_stable(hcx, hasher);
459 src_hash.hash_stable(hcx, hasher);
461 // We only hash the relative position within this filemap
462 lines.len().hash_stable(hcx, hasher);
463 for &line in lines.iter() {
464 stable_byte_pos(line, start_pos).hash_stable(hcx, hasher);
467 // We only hash the relative position within this filemap
468 multibyte_chars.len().hash_stable(hcx, hasher);
469 for &char_pos in multibyte_chars.iter() {
470 stable_multibyte_char(char_pos, start_pos).hash_stable(hcx, hasher);
473 non_narrow_chars.len().hash_stable(hcx, hasher);
474 for &char_pos in non_narrow_chars.iter() {
475 stable_non_narrow_char(char_pos, start_pos).hash_stable(hcx, hasher);
480 fn stable_byte_pos(pos: ::syntax_pos::BytePos,
481 filemap_start: ::syntax_pos::BytePos)
483 pos.0 - filemap_start.0
486 fn stable_multibyte_char(mbc: ::syntax_pos::MultiByteChar,
487 filemap_start: ::syntax_pos::BytePos)
489 let ::syntax_pos::MultiByteChar {
494 (pos.0 - filemap_start.0, bytes as u32)
497 fn stable_non_narrow_char(swc: ::syntax_pos::NonNarrowChar,
498 filemap_start: ::syntax_pos::BytePos)
501 let width = swc.width();
503 (pos.0 - filemap_start.0, width as u32)
508 impl<'gcx> HashStable<StableHashingContext<'gcx>> for feature_gate::Features {
509 fn hash_stable<W: StableHasherResult>(&self,
510 hcx: &mut StableHashingContext<'gcx>,
511 hasher: &mut StableHasher<W>) {
512 // Unfortunately we cannot exhaustively list fields here, since the
513 // struct is macro generated.
514 self.declared_stable_lang_features.hash_stable(hcx, hasher);
515 self.declared_lib_features.hash_stable(hcx, hasher);
517 self.walk_feature_fields(|feature_name, value| {
518 feature_name.hash_stable(hcx, hasher);
519 value.hash_stable(hcx, hasher);