Skip to main content

rustc_middle/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_ast as ast;
5use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
6use rustc_hir as hir;
7use rustc_span::{Symbol, sym};
8use smallvec::SmallVec;
9
10use super::StableHashingContext;
11
12impl<'a> HashStable<StableHashingContext<'a>> for ast::NodeId {
13    #[inline]
14    fn hash_stable(&self, _: &mut StableHashingContext<'a>, _: &mut StableHasher) {
15        {
    ::core::panicking::panic_fmt(format_args!("Node IDs should not appear in incremental state"));
};panic!("Node IDs should not appear in incremental state");
16    }
17}
18
19impl<'a> HashStable<StableHashingContext<'a>> for [hir::Attribute] {
20    fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
21        if self.is_empty() {
22            self.len().hash_stable(hcx, hasher);
23            return;
24        }
25
26        // Some attributes are always ignored during hashing.
27        let filtered: SmallVec<[&hir::Attribute; 8]> = self
28            .iter()
29            .filter(|attr| {
30                attr.is_doc_comment().is_none()
31                    // FIXME(jdonszelmann) have a better way to handle ignored attrs
32                    && !attr.name().is_some_and(|ident| is_ignored_attr(ident))
33            })
34            .collect();
35
36        filtered.len().hash_stable(hcx, hasher);
37        for attr in filtered {
38            attr.hash_stable(hcx, hasher);
39        }
40    }
41}
42
43#[inline]
44fn is_ignored_attr(name: Symbol) -> bool {
45    const IGNORED_ATTRIBUTES: &[Symbol] = &[
46        sym::cfg_trace, // FIXME(#138844) should this really be ignored?
47        sym::rustc_if_this_changed,
48        sym::rustc_then_this_would_need,
49        sym::rustc_clean,
50        sym::rustc_partition_reused,
51        sym::rustc_partition_codegened,
52        sym::rustc_expected_cgu_reuse,
53    ];
54    IGNORED_ATTRIBUTES.contains(&name)
55}
56
57impl<'tcx> HashStable<StableHashingContext<'tcx>> for rustc_feature::Features {
58    fn hash_stable(&self, hcx: &mut StableHashingContext<'tcx>, hasher: &mut StableHasher) {
59        // Unfortunately we cannot exhaustively list fields here, since the
60        // struct has private fields (to ensure its invariant is maintained)
61        self.enabled_lang_features().hash_stable(hcx, hasher);
62        self.enabled_lib_features().hash_stable(hcx, hasher);
63    }
64}
65
66impl<'tcx> HashStable<StableHashingContext<'tcx>> for rustc_feature::EnabledLangFeature {
67    fn hash_stable(&self, hcx: &mut StableHashingContext<'tcx>, hasher: &mut StableHasher) {
68        let rustc_feature::EnabledLangFeature { gate_name, attr_sp, stable_since } = self;
69        gate_name.hash_stable(hcx, hasher);
70        attr_sp.hash_stable(hcx, hasher);
71        stable_since.hash_stable(hcx, hasher);
72    }
73}
74
75impl<'tcx> HashStable<StableHashingContext<'tcx>> for rustc_feature::EnabledLibFeature {
76    fn hash_stable(&self, hcx: &mut StableHashingContext<'tcx>, hasher: &mut StableHasher) {
77        let rustc_feature::EnabledLibFeature { gate_name, attr_sp } = self;
78        gate_name.hash_stable(hcx, hasher);
79        attr_sp.hash_stable(hcx, hasher);
80    }
81}