1 //! This module contains `HashStable` implementations for various data types
2 //! from `rustc_ast` in no particular order.
4 use crate::ich::StableHashingContext;
7 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
8 use rustc_span::{BytePos, NormalizedPos, SourceFile};
10 use smallvec::SmallVec;
12 impl<'ctx> rustc_target::HashStableContext for StableHashingContext<'ctx> {}
14 impl<'a> HashStable<StableHashingContext<'a>> for [ast::Attribute] {
15 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
17 self.len().hash_stable(hcx, hasher);
21 // Some attributes are always ignored during hashing.
22 let filtered: SmallVec<[&ast::Attribute; 8]> = self
25 !attr.is_doc_comment()
26 && !attr.ident().map_or(false, |ident| hcx.is_ignored_attr(ident.name))
30 filtered.len().hash_stable(hcx, hasher);
31 for attr in filtered {
32 attr.hash_stable(hcx, hasher);
37 impl<'ctx> rustc_ast::HashStableContext for StableHashingContext<'ctx> {
38 fn hash_attr(&mut self, attr: &ast::Attribute, hasher: &mut StableHasher) {
39 // Make sure that these have been filtered out.
40 debug_assert!(!attr.ident().map_or(false, |ident| self.is_ignored_attr(ident.name)));
41 debug_assert!(!attr.is_doc_comment());
43 let ast::Attribute { kind, id: _, style, span } = attr;
44 if let ast::AttrKind::Normal(item, tokens) = kind {
45 item.hash_stable(self, hasher);
46 style.hash_stable(self, hasher);
47 span.hash_stable(self, hasher);
51 "Tokens should have been removed during lowering!"
59 impl<'a> HashStable<StableHashingContext<'a>> for SourceFile {
60 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
62 name: _, // We hash the smaller name_hash instead of this
65 // Do not hash the source as it is not encoded
77 (name_hash as u64).hash_stable(hcx, hasher);
79 src_hash.hash_stable(hcx, hasher);
81 // We only hash the relative position within this source_file
82 lines.len().hash_stable(hcx, hasher);
83 for &line in lines.iter() {
84 stable_byte_pos(line, start_pos).hash_stable(hcx, hasher);
87 // We only hash the relative position within this source_file
88 multibyte_chars.len().hash_stable(hcx, hasher);
89 for &char_pos in multibyte_chars.iter() {
90 stable_multibyte_char(char_pos, start_pos).hash_stable(hcx, hasher);
93 non_narrow_chars.len().hash_stable(hcx, hasher);
94 for &char_pos in non_narrow_chars.iter() {
95 stable_non_narrow_char(char_pos, start_pos).hash_stable(hcx, hasher);
98 normalized_pos.len().hash_stable(hcx, hasher);
99 for &char_pos in normalized_pos.iter() {
100 stable_normalized_pos(char_pos, start_pos).hash_stable(hcx, hasher);
103 cnum.hash_stable(hcx, hasher);
107 fn stable_byte_pos(pos: BytePos, source_file_start: BytePos) -> u32 {
108 pos.0 - source_file_start.0
111 fn stable_multibyte_char(mbc: rustc_span::MultiByteChar, source_file_start: BytePos) -> (u32, u32) {
112 let rustc_span::MultiByteChar { pos, bytes } = mbc;
114 (pos.0 - source_file_start.0, bytes as u32)
117 fn stable_non_narrow_char(
118 swc: rustc_span::NonNarrowChar,
119 source_file_start: BytePos,
122 let width = swc.width();
124 (pos.0 - source_file_start.0, width as u32)
127 fn stable_normalized_pos(np: NormalizedPos, source_file_start: BytePos) -> (u32, u32) {
128 let NormalizedPos { pos, diff } = np;
130 (pos.0 - source_file_start.0, diff)
133 impl<'tcx> HashStable<StableHashingContext<'tcx>> for rustc_feature::Features {
134 fn hash_stable(&self, hcx: &mut StableHashingContext<'tcx>, hasher: &mut StableHasher) {
135 // Unfortunately we cannot exhaustively list fields here, since the
136 // struct is macro generated.
137 self.declared_lang_features.hash_stable(hcx, hasher);
138 self.declared_lib_features.hash_stable(hcx, hasher);
140 self.walk_feature_fields(|feature_name, value| {
141 feature_name.hash_stable(hcx, hasher);
142 value.hash_stable(hcx, hasher);