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 {
105 impl_stable_hash_for!(enum ::rustc_target::spec::abi::Abi {
126 impl_stable_hash_for!(struct ::syntax::attr::Deprecation { since, note });
127 impl_stable_hash_for!(struct ::syntax::attr::Stability {
134 impl<'a> HashStable<StableHashingContext<'a>>
135 for ::syntax::attr::StabilityLevel {
136 fn hash_stable<W: StableHasherResult>(&self,
137 hcx: &mut StableHashingContext<'a>,
138 hasher: &mut StableHasher<W>) {
139 mem::discriminant(self).hash_stable(hcx, hasher);
141 ::syntax::attr::StabilityLevel::Unstable { ref reason, ref issue } => {
142 reason.hash_stable(hcx, hasher);
143 issue.hash_stable(hcx, hasher);
145 ::syntax::attr::StabilityLevel::Stable { ref since } => {
146 since.hash_stable(hcx, hasher);
152 impl_stable_hash_for!(struct ::syntax::attr::RustcDeprecation { since, reason });
153 impl_stable_hash_for!(struct ::syntax::attr::RustcConstUnstable { feature });
156 impl_stable_hash_for!(enum ::syntax::attr::IntType {
161 impl_stable_hash_for!(enum ::syntax::ast::LitIntType {
167 impl_stable_hash_for_spanned!(::syntax::ast::LitKind);
168 impl_stable_hash_for!(enum ::syntax::ast::LitKind {
173 Int(value, lit_int_type),
174 Float(value, float_ty),
175 FloatUnsuffixed(value),
179 impl_stable_hash_for!(enum ::syntax::ast::IntTy { Isize, I8, I16, I32, I64, I128 });
180 impl_stable_hash_for!(enum ::syntax::ast::UintTy { Usize, U8, U16, U32, U64, U128 });
181 impl_stable_hash_for!(enum ::syntax::ast::FloatTy { F32, F64 });
182 impl_stable_hash_for!(enum ::syntax::ast::Unsafety { Unsafe, Normal });
183 impl_stable_hash_for!(enum ::syntax::ast::Constness { Const, NotConst });
184 impl_stable_hash_for!(enum ::syntax::ast::Defaultness { Default, Final });
185 impl_stable_hash_for!(struct ::syntax::ast::Lifetime { id, ident });
186 impl_stable_hash_for!(enum ::syntax::ast::StrStyle { Cooked, Raw(pounds) });
187 impl_stable_hash_for!(enum ::syntax::ast::AttrStyle { Outer, Inner });
189 impl<'a> HashStable<StableHashingContext<'a>> for [ast::Attribute] {
190 fn hash_stable<W: StableHasherResult>(&self,
191 hcx: &mut StableHashingContext<'a>,
192 hasher: &mut StableHasher<W>) {
194 self.len().hash_stable(hcx, hasher);
198 // Some attributes are always ignored during hashing.
199 let filtered: AccumulateVec<[&ast::Attribute; 8]> = self
202 !attr.is_sugared_doc && !hcx.is_ignored_attr(attr.name())
206 filtered.len().hash_stable(hcx, hasher);
207 for attr in filtered {
208 attr.hash_stable(hcx, hasher);
213 impl<'a> HashStable<StableHashingContext<'a>> for ast::Path {
214 fn hash_stable<W: StableHasherResult>(&self,
215 hcx: &mut StableHashingContext<'a>,
216 hasher: &mut StableHasher<W>) {
217 self.segments.len().hash_stable(hcx, hasher);
218 for segment in &self.segments {
219 segment.identifier.name.hash_stable(hcx, hasher);
224 impl<'a> HashStable<StableHashingContext<'a>> for ast::Attribute {
225 fn hash_stable<W: StableHasherResult>(&self,
226 hcx: &mut StableHashingContext<'a>,
227 hasher: &mut StableHasher<W>) {
228 // Make sure that these have been filtered out.
229 debug_assert!(!hcx.is_ignored_attr(self.name()));
230 debug_assert!(!self.is_sugared_doc);
241 style.hash_stable(hcx, hasher);
242 path.hash_stable(hcx, hasher);
243 for tt in tokens.trees() {
244 tt.hash_stable(hcx, hasher);
246 span.hash_stable(hcx, hasher);
250 impl<'a> HashStable<StableHashingContext<'a>>
251 for tokenstream::TokenTree {
252 fn hash_stable<W: StableHasherResult>(&self,
253 hcx: &mut StableHashingContext<'a>,
254 hasher: &mut StableHasher<W>) {
255 mem::discriminant(self).hash_stable(hcx, hasher);
257 tokenstream::TokenTree::Token(span, ref token) => {
258 span.hash_stable(hcx, hasher);
259 hash_token(token, hcx, hasher);
261 tokenstream::TokenTree::Delimited(span, ref delimited) => {
262 span.hash_stable(hcx, hasher);
263 std_hash::Hash::hash(&delimited.delim, hasher);
264 for sub_tt in delimited.stream().trees() {
265 sub_tt.hash_stable(hcx, hasher);
272 impl<'a> HashStable<StableHashingContext<'a>>
273 for tokenstream::TokenStream {
274 fn hash_stable<W: StableHasherResult>(&self,
275 hcx: &mut StableHashingContext<'a>,
276 hasher: &mut StableHasher<W>) {
277 for sub_tt in self.trees() {
278 sub_tt.hash_stable(hcx, hasher);
283 fn hash_token<'a, 'gcx, W: StableHasherResult>(
284 token: &token::Token,
285 hcx: &mut StableHashingContext<'a>,
286 hasher: &mut StableHasher<W>,
288 mem::discriminant(token).hash_stable(hcx, hasher);
297 token::Token::AndAnd |
300 token::Token::Tilde |
303 token::Token::DotDot |
304 token::Token::DotDotDot |
305 token::Token::DotDotEq |
306 token::Token::DotEq |
307 token::Token::Comma |
309 token::Token::Colon |
310 token::Token::ModSep |
311 token::Token::RArrow |
312 token::Token::LArrow |
313 token::Token::FatArrow |
314 token::Token::Pound |
315 token::Token::Dollar |
316 token::Token::Question |
317 token::Token::Whitespace |
318 token::Token::Comment |
319 token::Token::Eof => {}
321 token::Token::BinOp(bin_op_token) |
322 token::Token::BinOpEq(bin_op_token) => {
323 std_hash::Hash::hash(&bin_op_token, hasher);
326 token::Token::OpenDelim(delim_token) |
327 token::Token::CloseDelim(delim_token) => {
328 std_hash::Hash::hash(&delim_token, hasher);
330 token::Token::Literal(ref lit, ref opt_name) => {
331 mem::discriminant(lit).hash_stable(hcx, hasher);
333 token::Lit::Byte(val) |
334 token::Lit::Char(val) |
335 token::Lit::Integer(val) |
336 token::Lit::Float(val) |
337 token::Lit::Str_(val) |
338 token::Lit::ByteStr(val) => val.hash_stable(hcx, hasher),
339 token::Lit::StrRaw(val, n) |
340 token::Lit::ByteStrRaw(val, n) => {
341 val.hash_stable(hcx, hasher);
342 n.hash_stable(hcx, hasher);
345 opt_name.hash_stable(hcx, hasher);
348 token::Token::Ident(ident, is_raw) => {
349 ident.name.hash_stable(hcx, hasher);
350 is_raw.hash_stable(hcx, hasher);
352 token::Token::Lifetime(ident) => ident.name.hash_stable(hcx, hasher),
354 token::Token::Interpolated(_) => {
355 bug!("interpolated tokens should not be present in the HIR")
358 token::Token::DocComment(val) |
359 token::Token::Shebang(val) => val.hash_stable(hcx, hasher),
363 impl_stable_hash_for_spanned!(::syntax::ast::NestedMetaItemKind);
365 impl_stable_hash_for!(enum ::syntax::ast::NestedMetaItemKind {
370 impl_stable_hash_for!(struct ::syntax::ast::MetaItem {
376 impl_stable_hash_for!(enum ::syntax::ast::MetaItemKind {
382 impl_stable_hash_for!(struct ::syntax_pos::hygiene::ExpnInfo {
387 impl_stable_hash_for!(struct ::syntax_pos::hygiene::NameAndSpan {
389 allow_internal_unstable,
390 allow_internal_unsafe,
394 impl_stable_hash_for!(enum ::syntax_pos::hygiene::ExpnFormat {
397 CompilerDesugaring(kind)
400 impl_stable_hash_for!(enum ::syntax_pos::hygiene::CompilerDesugaringKind {
406 impl_stable_hash_for!(enum ::syntax_pos::FileName {
417 impl<'a> HashStable<StableHashingContext<'a>> for FileMap {
418 fn hash_stable<W: StableHasherResult>(&self,
419 hcx: &mut StableHashingContext<'a>,
420 hasher: &mut StableHasher<W>) {
422 name: _, // We hash the smaller name_hash instead of this
427 // Do not hash the source as it is not encoded
435 ref non_narrow_chars,
438 (name_hash as u64).hash_stable(hcx, hasher);
439 name_was_remapped.hash_stable(hcx, hasher);
442 krate: CrateNum::from_u32(crate_of_origin),
443 index: CRATE_DEF_INDEX,
444 }.hash_stable(hcx, hasher);
446 src_hash.hash_stable(hcx, hasher);
448 // We only hash the relative position within this filemap
449 lines.with_lock(|lines| {
450 lines.len().hash_stable(hcx, hasher);
451 for &line in lines.iter() {
452 stable_byte_pos(line, start_pos).hash_stable(hcx, hasher);
456 // We only hash the relative position within this filemap
457 multibyte_chars.with_lock(|multibyte_chars| {
458 multibyte_chars.len().hash_stable(hcx, hasher);
459 for &char_pos in multibyte_chars.iter() {
460 stable_multibyte_char(char_pos, start_pos).hash_stable(hcx, hasher);
464 non_narrow_chars.with_lock(|non_narrow_chars| {
465 non_narrow_chars.len().hash_stable(hcx, hasher);
466 for &char_pos in non_narrow_chars.iter() {
467 stable_non_narrow_char(char_pos, start_pos).hash_stable(hcx, hasher);
473 fn stable_byte_pos(pos: ::syntax_pos::BytePos,
474 filemap_start: ::syntax_pos::BytePos)
476 pos.0 - filemap_start.0
479 fn stable_multibyte_char(mbc: ::syntax_pos::MultiByteChar,
480 filemap_start: ::syntax_pos::BytePos)
482 let ::syntax_pos::MultiByteChar {
487 (pos.0 - filemap_start.0, bytes as u32)
490 fn stable_non_narrow_char(swc: ::syntax_pos::NonNarrowChar,
491 filemap_start: ::syntax_pos::BytePos)
494 let width = swc.width();
496 (pos.0 - filemap_start.0, width as u32)
501 impl<'gcx> HashStable<StableHashingContext<'gcx>> for feature_gate::Features {
502 fn hash_stable<W: StableHasherResult>(&self,
503 hcx: &mut StableHashingContext<'gcx>,
504 hasher: &mut StableHasher<W>) {
505 // Unfortunately we cannot exhaustively list fields here, since the
506 // struct is macro generated.
507 self.declared_stable_lang_features.hash_stable(hcx, hasher);
508 self.declared_lib_features.hash_stable(hcx, hasher);
510 self.walk_feature_fields(|feature_name, value| {
511 feature_name.hash_stable(hcx, hasher);
512 value.hash_stable(hcx, hasher);