Skip to content

Commit

Permalink
change hashing to hash hir::Attribute instead of ast::Attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
jdonszelmann committed Oct 17, 2024
1 parent 1bfc0b5 commit d2dcaaa
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 23 deletions.
12 changes: 1 addition & 11 deletions compiler/rustc_ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,10 @@ pub mod token;
pub mod tokenstream;
pub mod visit;

use rustc_data_structures::stable_hasher::{HashStable, StableHasher};

pub use self::ast::*;
pub use self::ast_traits::{AstDeref, AstNodeWrapper, HasAttrs, HasNodeId, HasTokens};

/// Requirements for a `StableHashingContext` to be used in this crate.
/// This is a hack to allow using the `HashStable_Generic` derive macro
/// instead of implementing everything in `rustc_middle`.
pub trait HashStableContext: rustc_span::HashStableContext {
fn hash_attr(&mut self, _: &ast::Attribute, hasher: &mut StableHasher);
}

impl<AstCtx: crate::HashStableContext> HashStable<AstCtx> for ast::Attribute {
fn hash_stable(&self, hcx: &mut AstCtx, hasher: &mut StableHasher) {
hcx.hash_attr(self, hasher)
}
}
pub trait HashStableContext: rustc_span::HashStableContext {}
7 changes: 7 additions & 0 deletions compiler/rustc_hir/src/stable_hash_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::hir_id::{HirId, ItemLocalId};
pub trait HashStableContext:
rustc_ast::HashStableContext + rustc_target::HashStableContext
{
fn hash_attr(&mut self, _: &Attribute, hasher: &mut StableHasher);
}

impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for HirId {
Expand Down Expand Up @@ -114,3 +115,9 @@ impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Crate<'_> {
opt_hir_hash.unwrap().hash_stable(hcx, hasher)
}
}

impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Attribute {
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
hcx.hash_attr(self, hasher)
}
}
17 changes: 5 additions & 12 deletions compiler/rustc_query_system/src/ich/impls_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,23 @@ impl<'ctx> HashStable<StableHashingContext<'ctx>> for [hir::Attribute] {
}
}

impl<'ctx> rustc_ast::HashStableContext for StableHashingContext<'ctx> {
fn hash_attr(&mut self, attr: &ast::Attribute, hasher: &mut StableHasher) {
impl<'ctx> rustc_hir::HashStableContext for StableHashingContext<'ctx> {
fn hash_attr(&mut self, attr: &hir::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());

let ast::Attribute { kind, id: _, style, span } = attr;
if let ast::AttrKind::Normal(normal) = kind {
normal.item.hash_stable(self, hasher);
let hir::Attribute { kind, id: _, style, span } = attr;
if let hir::AttrKind::Normal(item) = kind {
item.hash_stable(self, hasher);
style.hash_stable(self, hasher);
span.hash_stable(self, hasher);
assert_matches!(
normal.tokens.as_ref(),
None,
"Tokens should have been removed during lowering!"
);
} else {
unreachable!();
}
}
}

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

0 comments on commit d2dcaaa

Please sign in to comment.