Files
rust/compiler/rustc_query_system/src/ich/impls_syntax.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

153 lines
5.1 KiB
Rust
Raw Normal View History

//! This module contains `HashStable` implementations for various data types
//! from `rustc_ast` in no particular order.
2019-02-05 11:20:45 -06:00
use crate::ich::StableHashingContext;
2020-04-27 23:26:11 +05:30
use rustc_ast as ast;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
2022-06-02 11:43:14 +10:00
use rustc_span::{BytePos, NormalizedPos, SourceFile};
2021-07-16 09:18:14 -07:00
use std::assert_matches::assert_matches;
use smallvec::SmallVec;
impl<'ctx> rustc_target::HashStableContext for StableHashingContext<'ctx> {}
impl<'a> HashStable<StableHashingContext<'a>> for [ast::Attribute] {
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
if self.is_empty() {
self.len().hash_stable(hcx, hasher);
return;
}
// Some attributes are always ignored during hashing.
let filtered: SmallVec<[&ast::Attribute; 8]> = self
.iter()
.filter(|attr| {
!attr.is_doc_comment()
&& !attr.ident().is_some_and(|ident| hcx.is_ignored_attr(ident.name))
})
.collect();
filtered.len().hash_stable(hcx, hasher);
for attr in filtered {
attr.hash_stable(hcx, hasher);
}
}
}
impl<'ctx> rustc_ast::HashStableContext for StableHashingContext<'ctx> {
fn hash_attr(&mut self, attr: &ast::Attribute, hasher: &mut StableHasher) {
// Make sure that these have been filtered out.
debug_assert!(!attr.ident().is_some_and(|ident| self.is_ignored_attr(ident.name)));
debug_assert!(!attr.is_doc_comment());
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);
style.hash_stable(self, hasher);
span.hash_stable(self, hasher);
assert_matches!(
2022-08-11 21:06:11 +10:00
normal.tokens.as_ref(),
None,
"Tokens should have been removed during lowering!"
);
} else {
unreachable!();
}
}
}
2018-08-18 12:13:52 +02:00
impl<'a> HashStable<StableHashingContext<'a>> for SourceFile {
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
2018-08-18 12:13:52 +02:00
let SourceFile {
2022-06-02 11:43:14 +10:00
name: _, // We hash the smaller name_hash instead of this
name_hash,
cnum,
// Do not hash the source as it is not encoded
src: _,
ref src_hash,
external_src: _,
start_pos,
end_pos: _,
2022-06-02 11:43:14 +10:00
lines: _,
ref multibyte_chars,
ref non_narrow_chars,
ref normalized_pos,
} = *self;
name_hash.hash_stable(hcx, hasher);
src_hash.hash_stable(hcx, hasher);
2022-06-02 11:43:14 +10:00
// We are always in `Lines` form by the time we reach here.
assert!(self.lines.borrow().is_lines());
self.lines(|lines| {
// We only hash the relative position within this source_file
lines.len().hash_stable(hcx, hasher);
for &line in lines.iter() {
stable_byte_pos(line, start_pos).hash_stable(hcx, hasher);
}
2022-06-02 11:43:14 +10:00
});
2018-08-18 12:13:56 +02:00
// We only hash the relative position within this source_file
multibyte_chars.len().hash_stable(hcx, hasher);
for &char_pos in multibyte_chars.iter() {
stable_multibyte_char(char_pos, start_pos).hash_stable(hcx, hasher);
}
non_narrow_chars.len().hash_stable(hcx, hasher);
for &char_pos in non_narrow_chars.iter() {
stable_non_narrow_char(char_pos, start_pos).hash_stable(hcx, hasher);
}
normalized_pos.len().hash_stable(hcx, hasher);
for &char_pos in normalized_pos.iter() {
stable_normalized_pos(char_pos, start_pos).hash_stable(hcx, hasher);
}
cnum.hash_stable(hcx, hasher);
}
}
fn stable_byte_pos(pos: BytePos, source_file_start: BytePos) -> u32 {
2018-08-18 12:13:56 +02:00
pos.0 - source_file_start.0
}
fn stable_multibyte_char(mbc: rustc_span::MultiByteChar, source_file_start: BytePos) -> (u32, u32) {
let rustc_span::MultiByteChar { pos, bytes } = mbc;
2018-08-18 12:13:56 +02:00
(pos.0 - source_file_start.0, bytes as u32)
}
fn stable_non_narrow_char(
swc: rustc_span::NonNarrowChar,
source_file_start: BytePos,
) -> (u32, u32) {
let pos = swc.pos();
let width = swc.width();
2018-08-18 12:13:56 +02:00
(pos.0 - source_file_start.0, width as u32)
}
2018-02-14 16:11:02 +01:00
fn stable_normalized_pos(np: NormalizedPos, source_file_start: BytePos) -> (u32, u32) {
let NormalizedPos { pos, diff } = np;
(pos.0 - source_file_start.0, diff)
}
impl<'tcx> HashStable<StableHashingContext<'tcx>> for rustc_feature::Features {
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.
2018-07-23 02:03:01 +01:00
self.declared_lang_features.hash_stable(hcx, hasher);
2018-02-14 16:11:02 +01:00
self.declared_lib_features.hash_stable(hcx, hasher);
self.walk_feature_fields(|feature_name, value| {
feature_name.hash_stable(hcx, hasher);
value.hash_stable(hcx, hasher);
});
}
}
impl<'ctx> rustc_type_ir::HashStableContext for StableHashingContext<'ctx> {}