2017-03-30 15:27:27 +02:00
|
|
|
//! This module contains `HashStable` implementations for various data types
|
2021-04-07 14:47:01 -05:00
|
|
|
//! from `rustc_ast` in no particular order.
|
2017-03-30 15:27:27 +02:00
|
|
|
|
2019-02-05 11:20:45 -06:00
|
|
|
use std::assert_matches::assert_matches;
|
2017-03-30 15:27:27 +02:00
|
|
|
|
2020-04-27 23:26:11 +05:30
|
|
|
use rustc_ast as ast;
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
2023-09-03 10:15:35 +00:00
|
|
|
use rustc_span::SourceFile;
|
2018-08-24 13:51:32 +10:00
|
|
|
use smallvec::SmallVec;
|
2017-09-12 17:07:09 +02:00
|
|
|
|
2019-02-05 11:20:45 -06:00
|
|
|
use crate::ich::StableHashingContext;
|
2024-07-29 08:13:50 +10:00
|
|
|
|
2019-11-23 13:58:17 +01:00
|
|
|
impl<'ctx> rustc_target::HashStableContext for StableHashingContext<'ctx> {}
|
2019-11-10 17:19:08 +01:00
|
|
|
|
2018-01-16 10:16:38 +01:00
|
|
|
impl<'a> HashStable<StableHashingContext<'a>> for [ast::Attribute] {
|
2019-09-26 18:54:39 -04:00
|
|
|
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
|
2020-02-28 14:20:33 +01:00
|
|
|
if self.is_empty() {
|
2017-09-14 12:29:16 +02:00
|
|
|
self.len().hash_stable(hcx, hasher);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-30 15:27:27 +02:00
|
|
|
// Some attributes are always ignored during hashing.
|
2018-08-24 13:51:32 +10:00
|
|
|
let filtered: SmallVec<[&ast::Attribute; 8]> = self
|
2017-03-30 15:27:27 +02:00
|
|
|
.iter()
|
|
|
|
|
.filter(|attr| {
|
2019-10-24 06:33:12 +11:00
|
|
|
!attr.is_doc_comment()
|
2023-05-24 14:19:22 +00:00
|
|
|
&& !attr.ident().is_some_and(|ident| hcx.is_ignored_attr(ident.name))
|
2017-03-30 15:27:27 +02:00
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
filtered.len().hash_stable(hcx, hasher);
|
|
|
|
|
for attr in filtered {
|
|
|
|
|
attr.hash_stable(hcx, hasher);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-29 20:37:32 +03:00
|
|
|
impl<'ctx> rustc_ast::HashStableContext for StableHashingContext<'ctx> {
|
2020-01-02 05:18:45 +01:00
|
|
|
fn hash_attr(&mut self, attr: &ast::Attribute, hasher: &mut StableHasher) {
|
2017-03-30 15:27:27 +02:00
|
|
|
// Make sure that these have been filtered out.
|
2023-05-24 14:19:22 +00:00
|
|
|
debug_assert!(!attr.ident().is_some_and(|ident| self.is_ignored_attr(ident.name)));
|
2020-01-02 05:18:45 +01:00
|
|
|
debug_assert!(!attr.is_doc_comment());
|
2019-10-24 06:33:12 +11:00
|
|
|
|
2020-11-05 20:27:48 +03:00
|
|
|
let ast::Attribute { kind, id: _, style, span } = attr;
|
2022-08-11 21:06:11 +10:00
|
|
|
if let ast::AttrKind::Normal(normal) = kind {
|
|
|
|
|
normal.item.hash_stable(self, hasher);
|
2020-01-02 05:18:45 +01:00
|
|
|
style.hash_stable(self, hasher);
|
|
|
|
|
span.hash_stable(self, hasher);
|
2021-03-04 13:06:01 +01:00
|
|
|
assert_matches!(
|
2022-08-11 21:06:11 +10:00
|
|
|
normal.tokens.as_ref(),
|
2021-03-04 13:06:01 +01:00
|
|
|
None,
|
|
|
|
|
"Tokens should have been removed during lowering!"
|
|
|
|
|
);
|
2019-10-24 06:33:12 +11:00
|
|
|
} else {
|
|
|
|
|
unreachable!();
|
|
|
|
|
}
|
2017-03-30 15:27:27 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-16 23:23:31 +00:00
|
|
|
impl<'ctx> rustc_hir::HashStableContext for StableHashingContext<'ctx> {}
|
|
|
|
|
|
2018-08-18 12:13:52 +02:00
|
|
|
impl<'a> HashStable<StableHashingContext<'a>> for SourceFile {
|
2019-09-26 18:54:39 -04:00
|
|
|
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
|
2018-08-18 12:13:52 +02:00
|
|
|
let SourceFile {
|
2023-12-19 22:34:26 +01:00
|
|
|
name: _, // We hash the smaller stable_id instead of this
|
|
|
|
|
stable_id,
|
2020-02-07 14:02:24 -05:00
|
|
|
cnum,
|
2017-04-27 16:12:57 +02:00
|
|
|
// Do not hash the source as it is not encoded
|
|
|
|
|
src: _,
|
2020-03-30 22:17:15 -07:00
|
|
|
ref src_hash,
|
2024-06-22 01:27:59 -06:00
|
|
|
// Already includes src_hash, this is redundant
|
|
|
|
|
checksum_hash: _,
|
2017-06-10 21:08:32 +02:00
|
|
|
external_src: _,
|
2023-09-03 10:15:35 +00:00
|
|
|
start_pos: _,
|
|
|
|
|
source_len: _,
|
2022-06-02 11:43:14 +10:00
|
|
|
lines: _,
|
2017-04-27 16:12:57 +02:00
|
|
|
ref multibyte_chars,
|
2019-10-03 03:55:31 +03:00
|
|
|
ref normalized_pos,
|
2017-04-27 16:12:57 +02:00
|
|
|
} = *self;
|
|
|
|
|
|
2023-12-19 22:34:26 +01:00
|
|
|
stable_id.hash_stable(hcx, hasher);
|
2017-04-27 16:12:57 +02:00
|
|
|
|
2017-06-10 13:39:39 +02:00
|
|
|
src_hash.hash_stable(hcx, hasher);
|
|
|
|
|
|
2023-08-31 22:12:47 +02:00
|
|
|
{
|
|
|
|
|
// We are always in `Lines` form by the time we reach here.
|
|
|
|
|
assert!(self.lines.read().is_lines());
|
|
|
|
|
let lines = self.lines();
|
2022-06-02 11:43:14 +10:00
|
|
|
// We only hash the relative position within this source_file
|
|
|
|
|
lines.len().hash_stable(hcx, hasher);
|
|
|
|
|
for &line in lines.iter() {
|
2023-09-03 10:15:35 +00:00
|
|
|
line.hash_stable(hcx, hasher);
|
2022-05-30 15:59:45 +10:00
|
|
|
}
|
2023-08-31 22:12:47 +02:00
|
|
|
}
|
2017-04-27 16:12:57 +02:00
|
|
|
|
2018-08-18 12:13:56 +02:00
|
|
|
// We only hash the relative position within this source_file
|
2018-05-23 15:59:42 +02:00
|
|
|
multibyte_chars.len().hash_stable(hcx, hasher);
|
|
|
|
|
for &char_pos in multibyte_chars.iter() {
|
2023-09-03 10:15:35 +00:00
|
|
|
char_pos.hash_stable(hcx, hasher);
|
2018-05-23 15:59:42 +02:00
|
|
|
}
|
2017-11-02 10:25:54 +09:00
|
|
|
|
2019-10-03 03:55:31 +03:00
|
|
|
normalized_pos.len().hash_stable(hcx, hasher);
|
|
|
|
|
for &char_pos in normalized_pos.iter() {
|
2023-09-03 10:15:35 +00:00
|
|
|
char_pos.hash_stable(hcx, hasher);
|
2019-10-03 03:55:31 +03:00
|
|
|
}
|
2020-02-07 14:02:24 -05:00
|
|
|
|
|
|
|
|
cnum.hash_stable(hcx, hasher);
|
2017-04-27 16:12:57 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-30 00:23:38 +01:00
|
|
|
impl<'tcx> HashStable<StableHashingContext<'tcx>> for rustc_feature::Features {
|
2019-09-26 18:54:39 -04:00
|
|
|
fn hash_stable(&self, hcx: &mut StableHashingContext<'tcx>, hasher: &mut StableHasher) {
|
2018-02-14 16:11:02 +01:00
|
|
|
// Unfortunately we cannot exhaustively list fields here, since the
|
|
|
|
|
// struct is macro generated.
|
2024-10-09 08:30:43 +02:00
|
|
|
self.enabled_lang_features().hash_stable(hcx, hasher);
|
|
|
|
|
self.enabled_lib_features().hash_stable(hcx, hasher);
|
2018-02-14 16:11:02 +01:00
|
|
|
|
2024-10-09 09:01:57 +02:00
|
|
|
// FIXME: why do we hash something that is a compile-time constant?
|
2024-10-23 08:20:02 +01:00
|
|
|
for feature in rustc_feature::UNSTABLE_LANG_FEATURES.iter() {
|
2024-10-23 08:18:19 +01:00
|
|
|
feature.name.hash_stable(hcx, hasher);
|
2023-11-26 22:32:41 -05:00
|
|
|
}
|
2018-02-14 16:11:02 +01:00
|
|
|
}
|
|
|
|
|
}
|