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};
9 use std::assert_matches::assert_matches;
11 use smallvec::SmallVec;
13 impl<'ctx> rustc_target::HashStableContext for StableHashingContext<'ctx> {}
15 impl<'a> HashStable<StableHashingContext<'a>> for [ast::Attribute] {
16 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
18 self.len().hash_stable(hcx, hasher);
22 // Some attributes are always ignored during hashing.
23 let filtered: SmallVec<[&ast::Attribute; 8]> = self
26 !attr.is_doc_comment()
27 && !attr.ident().map_or(false, |ident| hcx.is_ignored_attr(ident.name))
31 filtered.len().hash_stable(hcx, hasher);
32 for attr in filtered {
33 attr.hash_stable(hcx, hasher);
38 impl<'ctx> rustc_ast::HashStableContext for StableHashingContext<'ctx> {
39 fn hash_attr(&mut self, attr: &ast::Attribute, hasher: &mut StableHasher) {
40 // Make sure that these have been filtered out.
41 debug_assert!(!attr.ident().map_or(false, |ident| self.is_ignored_attr(ident.name)));
42 debug_assert!(!attr.is_doc_comment());
44 let ast::Attribute { kind, id: _, style, span } = attr;
45 if let ast::AttrKind::Normal(item, tokens) = kind {
46 item.hash_stable(self, hasher);
47 style.hash_stable(self, hasher);
48 span.hash_stable(self, hasher);
52 "Tokens should have been removed during lowering!"
60 impl<'a> HashStable<StableHashingContext<'a>> for SourceFile {
61 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
63 name: _, // We hash the smaller name_hash instead of this
66 // Do not hash the source as it is not encoded
78 (name_hash as u64).hash_stable(hcx, hasher);
80 src_hash.hash_stable(hcx, hasher);
82 // We only hash the relative position within this source_file
83 lines.len().hash_stable(hcx, hasher);
84 for &line in lines.iter() {
85 stable_byte_pos(line, start_pos).hash_stable(hcx, hasher);
88 // We only hash the relative position within this source_file
89 multibyte_chars.len().hash_stable(hcx, hasher);
90 for &char_pos in multibyte_chars.iter() {
91 stable_multibyte_char(char_pos, start_pos).hash_stable(hcx, hasher);
94 non_narrow_chars.len().hash_stable(hcx, hasher);
95 for &char_pos in non_narrow_chars.iter() {
96 stable_non_narrow_char(char_pos, start_pos).hash_stable(hcx, hasher);
99 normalized_pos.len().hash_stable(hcx, hasher);
100 for &char_pos in normalized_pos.iter() {
101 stable_normalized_pos(char_pos, start_pos).hash_stable(hcx, hasher);
104 cnum.hash_stable(hcx, hasher);
108 fn stable_byte_pos(pos: BytePos, source_file_start: BytePos) -> u32 {
109 pos.0 - source_file_start.0
112 fn stable_multibyte_char(mbc: rustc_span::MultiByteChar, source_file_start: BytePos) -> (u32, u32) {
113 let rustc_span::MultiByteChar { pos, bytes } = mbc;
115 (pos.0 - source_file_start.0, bytes as u32)
118 fn stable_non_narrow_char(
119 swc: rustc_span::NonNarrowChar,
120 source_file_start: BytePos,
123 let width = swc.width();
125 (pos.0 - source_file_start.0, width as u32)
128 fn stable_normalized_pos(np: NormalizedPos, source_file_start: BytePos) -> (u32, u32) {
129 let NormalizedPos { pos, diff } = np;
131 (pos.0 - source_file_start.0, diff)
134 impl<'tcx> HashStable<StableHashingContext<'tcx>> for rustc_feature::Features {
135 fn hash_stable(&self, hcx: &mut StableHashingContext<'tcx>, hasher: &mut StableHasher) {
136 // Unfortunately we cannot exhaustively list fields here, since the
137 // struct is macro generated.
138 self.declared_lang_features.hash_stable(hcx, hasher);
139 self.declared_lib_features.hash_stable(hcx, hasher);
141 self.walk_feature_fields(|feature_name, value| {
142 feature_name.hash_stable(hcx, hasher);
143 value.hash_stable(hcx, hasher);