Skip to content

Commit

Permalink
Simplify BodyId hashing.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Jan 16, 2024
1 parent 20a8a23 commit d59968b
Show file tree
Hide file tree
Showing 7 changed files with 6 additions and 69 deletions.
6 changes: 3 additions & 3 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,9 +662,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
let (opt_hash_including_bodies, attrs_hash) = if self.tcx.needs_crate_hash() {
self.tcx.with_stable_hashing_context(|mut hcx| {
let mut stable_hasher = StableHasher::new();
hcx.with_hir_bodies(node.def_id(), &bodies, |hcx| {
node.hash_stable(hcx, &mut stable_hasher)
});
node.hash_stable(&mut hcx, &mut stable_hasher);
// Bodies are stored out of line, so we need to pull them explicitly in the hash.
bodies.hash_stable(&mut hcx, &mut stable_hasher);
let h1 = stable_hasher.finish();

let mut stable_hasher = StableHasher::new();
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1302,7 +1302,7 @@ pub enum UnsafeSource {
UserProvided,
}

#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, HashStable_Generic)]
pub struct BodyId {
pub hir_id: HirId,
}
Expand Down
7 changes: 0 additions & 7 deletions compiler/rustc_hir/src/stable_hash_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use rustc_span::def_id::DefPathHash;
pub trait HashStableContext:
rustc_ast::HashStableContext + rustc_target::HashStableContext
{
fn hash_body_id(&mut self, _: BodyId, hasher: &mut StableHasher);
}

impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for HirId {
Expand Down Expand Up @@ -80,12 +79,6 @@ impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ForeignItemId
}
}

impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for BodyId {
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
hcx.hash_body_id(*self, hasher)
}
}

// The following implementations of HashStable for `ItemId`, `TraitItemId`, and
// `ImplItemId` deserve special attention. Normally we do not hash `NodeId`s within
// the HIR, since they just signify a HIR nodes own path. But `ItemId` et al
Expand Down
35 changes: 0 additions & 35 deletions compiler/rustc_query_system/src/ich/hcx.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use crate::ich;

use rustc_ast as ast;
use rustc_data_structures::sorted_map::SortedMap;
use rustc_data_structures::stable_hasher::{HashStable, HashingControls, StableHasher};
use rustc_data_structures::sync::Lrc;
use rustc_hir as hir;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::definitions::DefPathHash;
use rustc_session::cstore::Untracked;
Expand All @@ -23,34 +21,19 @@ pub struct StableHashingContext<'a> {
// The value of `-Z incremental-ignore-spans`.
// This field should only be used by `unstable_opts_incremental_ignore_span`
incremental_ignore_spans: bool,
pub(super) body_resolver: BodyResolver<'a>,
// Very often, we are hashing something that does not need the
// `CachingSourceMapView`, so we initialize it lazily.
raw_source_map: &'a SourceMap,
caching_source_map: Option<CachingSourceMapView<'a>>,
hashing_controls: HashingControls,
}

/// The `BodyResolver` allows mapping a `BodyId` to the corresponding `hir::Body`.
/// We could also just store a plain reference to the `hir::Crate` but we want
/// to avoid that the crate is used to get untracked access to all of the HIR.
#[derive(Clone, Copy)]
pub(super) enum BodyResolver<'tcx> {
Forbidden,
Ignore,
Traverse {
owner: hir::OwnerId,
bodies: &'tcx SortedMap<hir::ItemLocalId, &'tcx hir::Body<'tcx>>,
},
}

impl<'a> StableHashingContext<'a> {
#[inline]
pub fn new(sess: &'a Session, untracked: &'a Untracked) -> Self {
let hash_spans_initial = !sess.opts.unstable_opts.incremental_ignore_spans;

StableHashingContext {
body_resolver: BodyResolver::Forbidden,
untracked,
incremental_ignore_spans: sess.opts.unstable_opts.incremental_ignore_spans,
caching_source_map: None,
Expand All @@ -59,24 +42,6 @@ impl<'a> StableHashingContext<'a> {
}
}

#[inline]
pub fn without_hir_bodies(&mut self, f: impl FnOnce(&mut StableHashingContext<'_>)) {
f(&mut StableHashingContext { body_resolver: BodyResolver::Ignore, ..self.clone() });
}

#[inline]
pub fn with_hir_bodies(
&mut self,
owner: hir::OwnerId,
bodies: &SortedMap<hir::ItemLocalId, &hir::Body<'_>>,
f: impl FnOnce(&mut StableHashingContext<'_>),
) {
f(&mut StableHashingContext {
body_resolver: BodyResolver::Traverse { owner, bodies },
..self.clone()
});
}

#[inline]
pub fn while_hashing_spans<F: FnOnce(&mut Self)>(&mut self, hash_spans: bool, f: F) {
let prev_hash_spans = self.hashing_controls.hash_spans;
Expand Down
22 changes: 0 additions & 22 deletions compiler/rustc_query_system/src/ich/impls_hir.rs

This file was deleted.

2 changes: 2 additions & 0 deletions compiler/rustc_query_system/src/ich/impls_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ impl<'ctx> rustc_ast::HashStableContext for StableHashingContext<'ctx> {
}
}

impl<'ctx> rustc_hir::HashStableContext for StableHashingContext<'ctx> {}

impl<'a> HashStable<StableHashingContext<'a>> for SourceFile {
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
let SourceFile {
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_query_system/src/ich/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ pub use self::hcx::StableHashingContext;
use rustc_span::symbol::{sym, Symbol};

mod hcx;
mod impls_hir;
mod impls_syntax;

pub const IGNORED_ATTRIBUTES: &[Symbol] = &[
Expand Down

0 comments on commit d59968b

Please sign in to comment.