rustc_query_system/ich/
impls_syntax.rs

1//! This module contains `HashStable` implementations for various data types
2//! from various crates in no particular order.
3
4use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
5use rustc_hir::{self as hir, HashIgnoredAttrId};
6use rustc_span::SourceFile;
7use smallvec::SmallVec;
8
9use crate::ich::StableHashingContext;
10
11impl<'ctx> rustc_abi::HashStableContext for StableHashingContext<'ctx> {}
12impl<'ctx> rustc_ast::HashStableContext for StableHashingContext<'ctx> {}
13
14impl<'a> HashStable<StableHashingContext<'a>> for [hir::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<[&hir::Attribute; 8]> = self
23            .iter()
24            .filter(|attr| {
25                !attr.is_doc_comment()
26                    // FIXME(jdonszelmann) have a better way to handle ignored attrs
27                    && !attr.ident().is_some_and(|ident| hcx.is_ignored_attr(ident.name))
28            })
29            .collect();
30
31        filtered.len().hash_stable(hcx, hasher);
32        for attr in filtered {
33            attr.hash_stable(hcx, hasher);
34        }
35    }
36}
37
38impl<'ctx> rustc_hir::HashStableContext for StableHashingContext<'ctx> {
39    fn hash_attr_id(&mut self, _id: &HashIgnoredAttrId, _hasher: &mut StableHasher) {
40        /* we don't hash HashIgnoredAttrId, we ignore them */
41    }
42}
43
44impl<'a> HashStable<StableHashingContext<'a>> for SourceFile {
45    fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
46        let SourceFile {
47            name: _, // We hash the smaller stable_id instead of this
48            stable_id,
49            cnum,
50            // Do not hash the source as it is not encoded
51            src: _,
52            ref src_hash,
53            // Already includes src_hash, this is redundant
54            checksum_hash: _,
55            external_src: _,
56            start_pos: _,
57            source_len: _,
58            lines: _,
59            ref multibyte_chars,
60            ref normalized_pos,
61        } = *self;
62
63        stable_id.hash_stable(hcx, hasher);
64
65        src_hash.hash_stable(hcx, hasher);
66
67        {
68            // We are always in `Lines` form by the time we reach here.
69            assert!(self.lines.read().is_lines());
70            let lines = self.lines();
71            // We only hash the relative position within this source_file
72            lines.len().hash_stable(hcx, hasher);
73            for &line in lines.iter() {
74                line.hash_stable(hcx, hasher);
75            }
76        }
77
78        // We only hash the relative position within this source_file
79        multibyte_chars.len().hash_stable(hcx, hasher);
80        for &char_pos in multibyte_chars.iter() {
81            char_pos.hash_stable(hcx, hasher);
82        }
83
84        normalized_pos.len().hash_stable(hcx, hasher);
85        for &char_pos in normalized_pos.iter() {
86            char_pos.hash_stable(hcx, hasher);
87        }
88
89        cnum.hash_stable(hcx, hasher);
90    }
91}
92
93impl<'tcx> HashStable<StableHashingContext<'tcx>> for rustc_feature::Features {
94    fn hash_stable(&self, hcx: &mut StableHashingContext<'tcx>, hasher: &mut StableHasher) {
95        // Unfortunately we cannot exhaustively list fields here, since the
96        // struct has private fields (to ensure its invariant is maintained)
97        self.enabled_lang_features().hash_stable(hcx, hasher);
98        self.enabled_lib_features().hash_stable(hcx, hasher);
99    }
100}
101
102impl<'tcx> HashStable<StableHashingContext<'tcx>> for rustc_feature::EnabledLangFeature {
103    fn hash_stable(&self, hcx: &mut StableHashingContext<'tcx>, hasher: &mut StableHasher) {
104        let rustc_feature::EnabledLangFeature { gate_name, attr_sp, stable_since } = self;
105        gate_name.hash_stable(hcx, hasher);
106        attr_sp.hash_stable(hcx, hasher);
107        stable_since.hash_stable(hcx, hasher);
108    }
109}
110
111impl<'tcx> HashStable<StableHashingContext<'tcx>> for rustc_feature::EnabledLibFeature {
112    fn hash_stable(&self, hcx: &mut StableHashingContext<'tcx>, hasher: &mut StableHasher) {
113        let rustc_feature::EnabledLibFeature { gate_name, attr_sp } = self;
114        gate_name.hash_stable(hcx, hasher);
115        attr_sp.hash_stable(hcx, hasher);
116    }
117}