Skip to content

Commit 25914ab

Browse files
committed
change hashing to hash hir::Attribute instead of ast::Attribute
1 parent 40a7fd3 commit 25914ab

File tree

5 files changed

+40
-43
lines changed

5 files changed

+40
-43
lines changed

compiler/rustc_ast/src/lib.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,10 @@ pub mod token;
4444
pub mod tokenstream;
4545
pub mod visit;
4646

47-
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
48-
4947
pub use self::ast::*;
5048
pub use self::ast_traits::{AstDeref, AstNodeWrapper, HasAttrs, HasNodeId, HasTokens};
5149

5250
/// Requirements for a `StableHashingContext` to be used in this crate.
5351
/// This is a hack to allow using the `HashStable_Generic` derive macro
5452
/// instead of implementing everything in `rustc_middle`.
55-
pub trait HashStableContext: rustc_span::HashStableContext {
56-
fn hash_attr(&mut self, _: &ast::Attribute, hasher: &mut StableHasher);
57-
}
58-
59-
impl<AstCtx: crate::HashStableContext> HashStable<AstCtx> for ast::Attribute {
60-
fn hash_stable(&self, hcx: &mut AstCtx, hasher: &mut StableHasher) {
61-
hcx.hash_attr(self, hasher)
62-
}
63-
}
53+
pub trait HashStableContext: rustc_span::HashStableContext {}

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
174174
id: NodeId,
175175
hir_id: hir::HirId,
176176
ident: &mut Ident,
177-
attrs: &'hir [Attribute],
177+
attrs: &'hir [hir::Attribute],
178178
vis_span: Span,
179179
i: &ItemKind,
180180
) -> hir::ItemKind<'hir> {
@@ -486,7 +486,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
486486
id: NodeId,
487487
vis_span: Span,
488488
ident: &mut Ident,
489-
attrs: &'hir [Attribute],
489+
attrs: &'hir [hir::Attribute],
490490
) -> hir::ItemKind<'hir> {
491491
let path = &tree.prefix;
492492
let segments = prefix.segments.iter().chain(path.segments.iter()).cloned().collect();
@@ -1447,7 +1447,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
14471447
}
14481448
}
14491449

1450-
pub(super) fn lower_safety(&mut self, s: Safety, default: hir::Safety) -> hir::Safety {
1450+
pub(super) fn lower_safety(&self, s: Safety, default: hir::Safety) -> hir::Safety {
14511451
match s {
14521452
Safety::Unsafe(_) => hir::Safety::Unsafe,
14531453
Safety::Default => default,

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
use std::collections::hash_map::Entry;
4444

4545
use rustc_ast::node_id::NodeMap;
46-
use rustc_ast::ptr::P;
4746
use rustc_ast::{self as ast, *};
4847
use rustc_data_structures::captures::Captures;
4948
use rustc_data_structures::fingerprint::Fingerprint;
@@ -101,7 +100,7 @@ struct LoweringContext<'a, 'hir> {
101100
/// Bodies inside the owner being lowered.
102101
bodies: Vec<(hir::ItemLocalId, &'hir hir::Body<'hir>)>,
103102
/// Attributes inside the owner being lowered.
104-
attrs: SortedMap<hir::ItemLocalId, &'hir [Attribute]>,
103+
attrs: SortedMap<hir::ItemLocalId, &'hir [hir::Attribute]>,
105104
/// Collect items that were created by lowering the current owner.
106105
children: Vec<(LocalDefId, hir::MaybeOwner<'hir>)>,
107106

@@ -927,7 +926,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
927926
ret
928927
}
929928

930-
fn lower_attrs(&mut self, id: HirId, attrs: &[Attribute]) -> &'hir [Attribute] {
929+
fn lower_attrs(&mut self, id: HirId, attrs: &[Attribute]) -> &'hir [hir::Attribute] {
931930
if attrs.is_empty() {
932931
&[]
933932
} else {
@@ -939,25 +938,33 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
939938
}
940939
}
941940

942-
fn lower_attr(&self, attr: &Attribute) -> Attribute {
941+
fn lower_attr(&self, attr: &Attribute) -> hir::Attribute {
943942
// Note that we explicitly do not walk the path. Since we don't really
944943
// lower attributes (we use the AST version) there is nowhere to keep
945944
// the `HirId`s. We don't actually need HIR version of attributes anyway.
946945
// Tokens are also not needed after macro expansion and parsing.
947946
let kind = match attr.kind {
948-
AttrKind::Normal(ref normal) => AttrKind::Normal(P(NormalAttr {
949-
item: AttrItem {
950-
unsafety: normal.item.unsafety,
951-
path: normal.item.path.clone(),
952-
args: self.lower_attr_args(&normal.item.args),
953-
tokens: None,
947+
AttrKind::Normal(ref normal) => hir::AttrKind::Normal(hir::AttrItem {
948+
unsafety: self.lower_safety(normal.item.unsafety, hir::Safety::Safe),
949+
path: hir::AttrPath {
950+
segments: normal
951+
.item
952+
.path
953+
.segments
954+
.iter()
955+
.map(|i| i.ident)
956+
.collect::<Vec<_>>()
957+
.into_boxed_slice(),
958+
span: normal.item.path.span,
954959
},
955-
tokens: None,
956-
})),
957-
AttrKind::DocComment(comment_kind, data) => AttrKind::DocComment(comment_kind, data),
960+
args: self.lower_attr_args(&normal.item.args),
961+
}),
962+
AttrKind::DocComment(comment_kind, data) => {
963+
hir::AttrKind::DocComment(comment_kind, data)
964+
}
958965
};
959966

960-
Attribute { kind, id: attr.id, style: attr.style, span: self.lower_span(attr.span) }
967+
hir::Attribute { kind, id: attr.id, style: attr.style, span: self.lower_span(attr.span) }
961968
}
962969

963970
fn alias_attrs(&mut self, id: HirId, target_id: HirId) {
@@ -969,10 +976,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
969976
}
970977
}
971978

972-
fn lower_attr_args(&self, args: &AttrArgs) -> AttrArgs {
979+
fn lower_attr_args(&self, args: &AttrArgs) -> hir::AttrArgs {
973980
match args {
974-
AttrArgs::Empty => AttrArgs::Empty,
975-
AttrArgs::Delimited(args) => AttrArgs::Delimited(self.lower_delim_args(args)),
981+
AttrArgs::Empty => hir::AttrArgs::Empty,
982+
AttrArgs::Delimited(args) => hir::AttrArgs::Delimited(self.lower_delim_args(args)),
976983
// This is an inert key-value attribute - it will never be visible to macros
977984
// after it gets lowered to HIR. Therefore, we can extract literals to handle
978985
// nonterminals in `#[doc]` (e.g. `#[doc = $e]`).

compiler/rustc_hir/src/stable_hash_impls.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::hir_id::{HirId, ItemLocalId};
1313
pub trait HashStableContext:
1414
rustc_ast::HashStableContext + rustc_target::HashStableContext
1515
{
16+
fn hash_attr(&mut self, _: &Attribute, hasher: &mut StableHasher);
1617
}
1718

1819
impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for HirId {
@@ -114,3 +115,9 @@ impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Crate<'_> {
114115
opt_hir_hash.unwrap().hash_stable(hcx, hasher)
115116
}
116117
}
118+
119+
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Attribute {
120+
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
121+
hcx.hash_attr(self, hasher)
122+
}
123+
}

compiler/rustc_query_system/src/ich/impls_syntax.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,30 +35,23 @@ impl<'ctx> HashStable<StableHashingContext<'ctx>> for [hir::Attribute] {
3535
}
3636
}
3737

38-
impl<'ctx> rustc_ast::HashStableContext for StableHashingContext<'ctx> {
39-
fn hash_attr(&mut self, attr: &ast::Attribute, hasher: &mut StableHasher) {
38+
impl<'ctx> rustc_hir::HashStableContext for StableHashingContext<'ctx> {
39+
fn hash_attr(&mut self, attr: &hir::Attribute, hasher: &mut StableHasher) {
4040
// Make sure that these have been filtered out.
4141
debug_assert!(!attr.ident().is_some_and(|ident| self.is_ignored_attr(ident.name)));
4242
debug_assert!(!attr.is_doc_comment());
4343

44-
let ast::Attribute { kind, id: _, style, span } = attr;
45-
if let ast::AttrKind::Normal(normal) = kind {
46-
normal.item.hash_stable(self, hasher);
44+
let hir::Attribute { kind, id: _, style, span } = attr;
45+
if let hir::AttrKind::Normal(item) = kind {
46+
item.hash_stable(self, hasher);
4747
style.hash_stable(self, hasher);
4848
span.hash_stable(self, hasher);
49-
assert_matches!(
50-
normal.tokens.as_ref(),
51-
None,
52-
"Tokens should have been removed during lowering!"
53-
);
5449
} else {
5550
unreachable!();
5651
}
5752
}
5853
}
5954

60-
impl<'ctx> rustc_hir::HashStableContext for StableHashingContext<'ctx> {}
61-
6255
impl<'a> HashStable<StableHashingContext<'a>> for SourceFile {
6356
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
6457
let SourceFile {

0 commit comments

Comments
 (0)