1 //! This module contains `HashStable` implementations for various data types
2 //! from libsyntax in no particular order.
4 use crate::ich::StableHashingContext;
6 use std::hash as std_hash;
10 use syntax::feature_gate;
11 use syntax::parse::token;
12 use syntax::symbol::LocalInternedString;
13 use syntax::tokenstream;
14 use syntax_pos::SourceFile;
16 use crate::hir::def_id::{DefId, CrateNum, CRATE_DEF_INDEX};
18 use smallvec::SmallVec;
19 use rustc_data_structures::stable_hasher::{HashStable, ToStableHashKey, StableHasher};
21 impl<'a> HashStable<StableHashingContext<'a>> for LocalInternedString {
23 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
24 let str = self as &str;
25 str.hash_stable(hcx, hasher)
29 impl<'a> ToStableHashKey<StableHashingContext<'a>> for LocalInternedString {
30 type KeyType = LocalInternedString;
33 fn to_stable_hash_key(&self,
34 _: &StableHashingContext<'a>)
35 -> LocalInternedString {
40 impl<'a> HashStable<StableHashingContext<'a>> for ast::Name {
42 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
43 self.as_str().hash_stable(hcx, hasher);
47 impl<'a> ToStableHashKey<StableHashingContext<'a>> for ast::Name {
48 type KeyType = LocalInternedString;
51 fn to_stable_hash_key(&self,
52 _: &StableHashingContext<'a>)
53 -> LocalInternedString {
58 impl_stable_hash_for!(enum ::syntax::ast::AsmDialect {
63 impl_stable_hash_for!(enum ::syntax_pos::hygiene::MacroKind {
70 impl_stable_hash_for!(enum ::rustc_target::spec::abi::Abi {
92 impl_stable_hash_for!(struct ::syntax::attr::Deprecation { since, note });
93 impl_stable_hash_for!(struct ::syntax::attr::Stability {
102 impl_stable_hash_for!(enum ::syntax::edition::Edition {
107 impl<'a> HashStable<StableHashingContext<'a>>
108 for ::syntax::attr::StabilityLevel {
109 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
110 mem::discriminant(self).hash_stable(hcx, hasher);
112 ::syntax::attr::StabilityLevel::Unstable { ref reason, ref issue, ref is_soft } => {
113 reason.hash_stable(hcx, hasher);
114 issue.hash_stable(hcx, hasher);
115 is_soft.hash_stable(hcx, hasher);
117 ::syntax::attr::StabilityLevel::Stable { ref since } => {
118 since.hash_stable(hcx, hasher);
124 impl_stable_hash_for!(struct ::syntax::attr::RustcDeprecation { since, reason, suggestion });
127 impl_stable_hash_for!(enum ::syntax::attr::IntType {
132 impl_stable_hash_for!(enum ::syntax::ast::LitIntType {
138 impl_stable_hash_for!(struct ::syntax::ast::Lit {
144 impl_stable_hash_for!(enum ::syntax::ast::LitKind {
149 Int(value, lit_int_type),
150 Float(value, float_ty),
151 FloatUnsuffixed(value),
156 impl_stable_hash_for_spanned!(::syntax::ast::LitKind);
158 impl_stable_hash_for!(enum ::syntax::ast::IntTy { Isize, I8, I16, I32, I64, I128 });
159 impl_stable_hash_for!(enum ::syntax::ast::UintTy { Usize, U8, U16, U32, U64, U128 });
160 impl_stable_hash_for!(enum ::syntax::ast::FloatTy { F32, F64 });
161 impl_stable_hash_for!(enum ::syntax::ast::Unsafety { Unsafe, Normal });
162 impl_stable_hash_for!(enum ::syntax::ast::Constness { Const, NotConst });
163 impl_stable_hash_for!(enum ::syntax::ast::Defaultness { Default, Final });
164 impl_stable_hash_for!(struct ::syntax::ast::Lifetime { id, ident });
165 impl_stable_hash_for!(enum ::syntax::ast::StrStyle { Cooked, Raw(pounds) });
166 impl_stable_hash_for!(enum ::syntax::ast::AttrStyle { Outer, Inner });
168 impl<'a> HashStable<StableHashingContext<'a>> for [ast::Attribute] {
169 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
171 self.len().hash_stable(hcx, hasher);
175 // Some attributes are always ignored during hashing.
176 let filtered: SmallVec<[&ast::Attribute; 8]> = self
179 !attr.is_sugared_doc &&
180 !attr.ident().map_or(false, |ident| hcx.is_ignored_attr(ident.name))
184 filtered.len().hash_stable(hcx, hasher);
185 for attr in filtered {
186 attr.hash_stable(hcx, hasher);
191 impl<'a> HashStable<StableHashingContext<'a>> for ast::Path {
192 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
193 self.segments.len().hash_stable(hcx, hasher);
194 for segment in &self.segments {
195 segment.ident.name.hash_stable(hcx, hasher);
200 impl_stable_hash_for!(struct ::syntax::ast::AttrItem {
205 impl<'a> HashStable<StableHashingContext<'a>> for ast::Attribute {
206 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
207 // Make sure that these have been filtered out.
208 debug_assert!(!self.ident().map_or(false, |ident| hcx.is_ignored_attr(ident.name)));
209 debug_assert!(!self.is_sugared_doc);
219 item.hash_stable(hcx, hasher);
220 style.hash_stable(hcx, hasher);
221 span.hash_stable(hcx, hasher);
225 impl<'a> HashStable<StableHashingContext<'a>>
226 for tokenstream::TokenTree {
227 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
228 mem::discriminant(self).hash_stable(hcx, hasher);
230 tokenstream::TokenTree::Token(ref token) => {
231 token.hash_stable(hcx, hasher);
233 tokenstream::TokenTree::Delimited(span, delim, ref tts) => {
234 span.hash_stable(hcx, hasher);
235 std_hash::Hash::hash(&delim, hasher);
236 for sub_tt in tts.trees() {
237 sub_tt.hash_stable(hcx, hasher);
244 impl<'a> HashStable<StableHashingContext<'a>>
245 for tokenstream::TokenStream {
246 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
247 for sub_tt in self.trees() {
248 sub_tt.hash_stable(hcx, hasher);
253 impl_stable_hash_for!(enum token::LitKind {
266 impl_stable_hash_for!(struct token::Lit {
272 impl<'a> HashStable<StableHashingContext<'a>> for token::TokenKind {
273 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
274 mem::discriminant(self).hash_stable(hcx, hasher);
307 token::BinOp(bin_op_token) |
308 token::BinOpEq(bin_op_token) => {
309 std_hash::Hash::hash(&bin_op_token, hasher);
312 token::OpenDelim(delim_token) |
313 token::CloseDelim(delim_token) => {
314 std_hash::Hash::hash(&delim_token, hasher);
316 token::Literal(lit) => lit.hash_stable(hcx, hasher),
318 token::Ident(name, is_raw) => {
319 name.hash_stable(hcx, hasher);
320 is_raw.hash_stable(hcx, hasher);
322 token::Lifetime(name) => name.hash_stable(hcx, hasher),
324 token::Interpolated(_) => {
325 bug!("interpolated tokens should not be present in the HIR")
328 token::DocComment(val) |
329 token::Shebang(val) |
330 token::Unknown(val) => val.hash_stable(hcx, hasher),
335 impl_stable_hash_for!(struct token::Token {
340 impl_stable_hash_for!(enum ::syntax::ast::NestedMetaItem {
345 impl_stable_hash_for!(struct ::syntax::ast::MetaItem {
351 impl_stable_hash_for!(enum ::syntax::ast::MetaItemKind {
357 impl_stable_hash_for!(enum ::syntax_pos::hygiene::Transparency {
363 impl_stable_hash_for!(struct ::syntax_pos::hygiene::ExpnData {
368 allow_internal_unstable,
369 allow_internal_unsafe,
374 impl_stable_hash_for!(enum ::syntax_pos::hygiene::ExpnKind {
381 impl_stable_hash_for!(enum ::syntax_pos::hygiene::AstPass {
388 impl_stable_hash_for!(enum ::syntax_pos::hygiene::DesugaringKind {
398 impl_stable_hash_for!(enum ::syntax_pos::FileName {
404 ProcMacroSourceCode(s),
411 impl<'a> HashStable<StableHashingContext<'a>> for SourceFile {
412 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
414 name: _, // We hash the smaller name_hash instead of this
419 // Do not hash the source as it is not encoded
427 ref non_narrow_chars,
431 (name_hash as u64).hash_stable(hcx, hasher);
432 name_was_remapped.hash_stable(hcx, hasher);
435 krate: CrateNum::from_u32(crate_of_origin),
436 index: CRATE_DEF_INDEX,
437 }.hash_stable(hcx, hasher);
439 src_hash.hash_stable(hcx, hasher);
441 // We only hash the relative position within this source_file
442 lines.len().hash_stable(hcx, hasher);
443 for &line in lines.iter() {
444 stable_byte_pos(line, start_pos).hash_stable(hcx, hasher);
447 // We only hash the relative position within this source_file
448 multibyte_chars.len().hash_stable(hcx, hasher);
449 for &char_pos in multibyte_chars.iter() {
450 stable_multibyte_char(char_pos, start_pos).hash_stable(hcx, hasher);
453 non_narrow_chars.len().hash_stable(hcx, hasher);
454 for &char_pos in non_narrow_chars.iter() {
455 stable_non_narrow_char(char_pos, start_pos).hash_stable(hcx, hasher);
458 normalized_pos.len().hash_stable(hcx, hasher);
459 for &char_pos in normalized_pos.iter() {
460 stable_normalized_pos(char_pos, start_pos).hash_stable(hcx, hasher);
466 fn stable_byte_pos(pos: ::syntax_pos::BytePos,
467 source_file_start: ::syntax_pos::BytePos)
469 pos.0 - source_file_start.0
472 fn stable_multibyte_char(mbc: ::syntax_pos::MultiByteChar,
473 source_file_start: ::syntax_pos::BytePos)
475 let ::syntax_pos::MultiByteChar {
480 (pos.0 - source_file_start.0, bytes as u32)
483 fn stable_non_narrow_char(swc: ::syntax_pos::NonNarrowChar,
484 source_file_start: ::syntax_pos::BytePos)
487 let width = swc.width();
489 (pos.0 - source_file_start.0, width as u32)
492 fn stable_normalized_pos(np: ::syntax_pos::NormalizedPos,
493 source_file_start: ::syntax_pos::BytePos)
495 let ::syntax_pos::NormalizedPos {
500 (pos.0 - source_file_start.0, diff)
504 impl<'tcx> HashStable<StableHashingContext<'tcx>> for feature_gate::Features {
505 fn hash_stable(&self, hcx: &mut StableHashingContext<'tcx>, hasher: &mut StableHasher) {
506 // Unfortunately we cannot exhaustively list fields here, since the
507 // struct is macro generated.
508 self.declared_lang_features.hash_stable(hcx, hasher);
509 self.declared_lib_features.hash_stable(hcx, hasher);
511 self.walk_feature_fields(|feature_name, value| {
512 feature_name.hash_stable(hcx, hasher);
513 value.hash_stable(hcx, hasher);