]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/ich/impls_syntax.rs
Auto merge of #82958 - camelid:res-docs, r=petrochenkov
[rust.git] / compiler / rustc_middle / src / ich / impls_syntax.rs
1 //! This module contains `HashStable` implementations for various data types
2 //! from librustc_ast in no particular order.
3
4 use crate::ich::StableHashingContext;
5
6 use rustc_ast as ast;
7 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
8 use rustc_span::{BytePos, NormalizedPos, SourceFile};
9
10 use smallvec::SmallVec;
11
12 impl<'ctx> rustc_target::HashStableContext for StableHashingContext<'ctx> {}
13
14 impl<'a> HashStable<StableHashingContext<'a>> for [ast::Attribute] {
15     fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
16         if self.is_empty() {
17             self.len().hash_stable(hcx, hasher);
18             return;
19         }
20
21         // Some attributes are always ignored during hashing.
22         let filtered: SmallVec<[&ast::Attribute; 8]> = self
23             .iter()
24             .filter(|attr| {
25                 !attr.is_doc_comment()
26                     && !attr.ident().map_or(false, |ident| hcx.is_ignored_attr(ident.name))
27             })
28             .collect();
29
30         filtered.len().hash_stable(hcx, hasher);
31         for attr in filtered {
32             attr.hash_stable(hcx, hasher);
33         }
34     }
35 }
36
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());
42
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);
48             assert_matches!(
49                 tokens.as_ref(),
50                 None,
51                 "Tokens should have been removed during lowering!"
52             );
53         } else {
54             unreachable!();
55         }
56     }
57 }
58
59 impl<'a> HashStable<StableHashingContext<'a>> for SourceFile {
60     fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
61         let SourceFile {
62             name: _, // We hash the smaller name_hash instead of this
63             name_hash,
64             name_was_remapped,
65             unmapped_path: _,
66             cnum,
67             // Do not hash the source as it is not encoded
68             src: _,
69             ref src_hash,
70             external_src: _,
71             start_pos,
72             end_pos: _,
73             ref lines,
74             ref multibyte_chars,
75             ref non_narrow_chars,
76             ref normalized_pos,
77         } = *self;
78
79         (name_hash as u64).hash_stable(hcx, hasher);
80         name_was_remapped.hash_stable(hcx, hasher);
81
82         src_hash.hash_stable(hcx, hasher);
83
84         // We only hash the relative position within this source_file
85         lines.len().hash_stable(hcx, hasher);
86         for &line in lines.iter() {
87             stable_byte_pos(line, start_pos).hash_stable(hcx, hasher);
88         }
89
90         // We only hash the relative position within this source_file
91         multibyte_chars.len().hash_stable(hcx, hasher);
92         for &char_pos in multibyte_chars.iter() {
93             stable_multibyte_char(char_pos, start_pos).hash_stable(hcx, hasher);
94         }
95
96         non_narrow_chars.len().hash_stable(hcx, hasher);
97         for &char_pos in non_narrow_chars.iter() {
98             stable_non_narrow_char(char_pos, start_pos).hash_stable(hcx, hasher);
99         }
100
101         normalized_pos.len().hash_stable(hcx, hasher);
102         for &char_pos in normalized_pos.iter() {
103             stable_normalized_pos(char_pos, start_pos).hash_stable(hcx, hasher);
104         }
105
106         cnum.hash_stable(hcx, hasher);
107     }
108 }
109
110 fn stable_byte_pos(pos: BytePos, source_file_start: BytePos) -> u32 {
111     pos.0 - source_file_start.0
112 }
113
114 fn stable_multibyte_char(mbc: rustc_span::MultiByteChar, source_file_start: BytePos) -> (u32, u32) {
115     let rustc_span::MultiByteChar { pos, bytes } = mbc;
116
117     (pos.0 - source_file_start.0, bytes as u32)
118 }
119
120 fn stable_non_narrow_char(
121     swc: rustc_span::NonNarrowChar,
122     source_file_start: BytePos,
123 ) -> (u32, u32) {
124     let pos = swc.pos();
125     let width = swc.width();
126
127     (pos.0 - source_file_start.0, width as u32)
128 }
129
130 fn stable_normalized_pos(np: NormalizedPos, source_file_start: BytePos) -> (u32, u32) {
131     let NormalizedPos { pos, diff } = np;
132
133     (pos.0 - source_file_start.0, diff)
134 }
135
136 impl<'tcx> HashStable<StableHashingContext<'tcx>> for rustc_feature::Features {
137     fn hash_stable(&self, hcx: &mut StableHashingContext<'tcx>, hasher: &mut StableHasher) {
138         // Unfortunately we cannot exhaustively list fields here, since the
139         // struct is macro generated.
140         self.declared_lang_features.hash_stable(hcx, hasher);
141         self.declared_lib_features.hash_stable(hcx, hasher);
142
143         self.walk_feature_fields(|feature_name, value| {
144             feature_name.hash_stable(hcx, hasher);
145             value.hash_stable(hcx, hasher);
146         });
147     }
148 }