Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 41e80b8

Browse files
committed
Directly use AttributeMap inside OwnerInfo.
1 parent 1c7f85f commit 41e80b8

File tree

8 files changed

+42
-28
lines changed

8 files changed

+42
-28
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
476476
let (nodes, parenting) =
477477
index::index_hir(self.sess, self.resolver.definitions(), node, &bodies);
478478
let nodes = hir::OwnerNodes { hash, node_hash, nodes, bodies };
479+
let attrs = {
480+
let mut hcx = self.resolver.create_stable_hashing_context();
481+
let mut stable_hasher = StableHasher::new();
482+
attrs.hash_stable(&mut hcx, &mut stable_hasher);
483+
let hash = stable_hasher.finish();
484+
hir::AttributeMap { map: attrs, hash }
485+
};
479486

480487
hir::OwnerInfo { nodes, parenting, attrs, trait_map }
481488
}

compiler/rustc_hir/src/hir.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,23 @@ pub struct ParentedNode<'tcx> {
672672
pub node: Node<'tcx>,
673673
}
674674

675+
/// Attributes owner by a HIR owner.
676+
#[derive(Debug)]
677+
pub struct AttributeMap<'tcx> {
678+
pub map: BTreeMap<ItemLocalId, &'tcx [Attribute]>,
679+
pub hash: Fingerprint,
680+
}
681+
682+
impl<'tcx> AttributeMap<'tcx> {
683+
pub const EMPTY: &'static AttributeMap<'static> =
684+
&AttributeMap { map: BTreeMap::new(), hash: Fingerprint::ZERO };
685+
686+
#[inline]
687+
pub fn get(&self, id: ItemLocalId) -> &'tcx [Attribute] {
688+
self.map.get(&id).copied().unwrap_or(&[])
689+
}
690+
}
691+
675692
#[derive(Debug)]
676693
pub struct OwnerNodes<'tcx> {
677694
/// Pre-computed hash of the full HIR.
@@ -691,8 +708,8 @@ pub struct OwnerInfo<'hir> {
691708
pub nodes: OwnerNodes<'hir>,
692709
/// Map from each nested owner to its parent's local id.
693710
pub parenting: FxHashMap<LocalDefId, ItemLocalId>,
694-
695-
pub attrs: BTreeMap<ItemLocalId, &'hir [Attribute]>,
711+
/// Collected attributes of the HIR nodes.
712+
pub attrs: AttributeMap<'hir>,
696713
/// Map indicating what traits are in scope for places where this
697714
/// is relevant; generated by resolve.
698715
pub trait_map: FxHashMap<ItemLocalId, Box<[TraitCandidate]>>,

compiler/rustc_hir/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//!
33
//! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/hir.html
44
5+
#![feature(const_btree_new)]
56
#![feature(crate_visibility_modifier)]
67
#![feature(in_band_lifetimes)]
78
#![feature(once_cell)]

compiler/rustc_hir/src/stable_hash_impls.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
22

33
use crate::hir::{
4-
BodyId, Expr, ForeignItem, ForeignItemId, ImplItem, ImplItemId, Item, ItemId, Mod, OwnerNodes,
5-
TraitItem, TraitItemId, Ty, VisibilityKind,
4+
AttributeMap, BodyId, Expr, ForeignItem, ForeignItemId, ImplItem, ImplItemId, Item, ItemId,
5+
Mod, OwnerNodes, TraitItem, TraitItemId, Ty, VisibilityKind,
66
};
77
use crate::hir_id::{HirId, ItemLocalId};
88
use rustc_span::def_id::DefPathHash;
@@ -218,3 +218,12 @@ impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for OwnerNodes<'tcx> {
218218
hash.hash_stable(hcx, hasher);
219219
}
220220
}
221+
222+
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for AttributeMap<'tcx> {
223+
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
224+
// We ignore the `map` since it refers to information included in `hash` which is hashed in
225+
// the collector and used for the crate hash.
226+
let AttributeMap { hash, map: _ } = *self;
227+
hash.hash_stable(hcx, hasher);
228+
}
229+
}

compiler/rustc_middle/src/hir/map/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ impl<'hir> Map<'hir> {
580580
let krate = self.krate();
581581
for (owner, info) in krate.owners.iter_enumerated() {
582582
if let Some(info) = info {
583-
for (&local_id, attrs) in info.attrs.iter() {
583+
for (&local_id, attrs) in info.attrs.map.iter() {
584584
let id = HirId { owner, local_id };
585585
for a in *attrs {
586586
visitor.visit_attribute(id, a)

compiler/rustc_middle/src/hir/mod.rs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@ pub mod place;
88

99
use crate::ty::query::Providers;
1010
use crate::ty::TyCtxt;
11-
use rustc_ast::Attribute;
1211
use rustc_data_structures::fingerprint::Fingerprint;
1312
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
1413
use rustc_hir::def_id::LocalDefId;
1514
use rustc_hir::*;
1615
use rustc_query_system::ich::StableHashingContext;
1716
use rustc_span::DUMMY_SP;
18-
use std::collections::BTreeMap;
1917

2018
/// Top-level HIR node for current owner. This only contains the node for which
2119
/// `HirId::local_id == 0`, and excludes bodies.
@@ -36,24 +34,6 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for Owner<'tcx> {
3634
}
3735
}
3836

39-
/// Attributes owner by a HIR owner.
40-
#[derive(Copy, Clone, Debug, HashStable)]
41-
pub struct AttributeMap<'tcx> {
42-
map: &'tcx BTreeMap<ItemLocalId, &'tcx [Attribute]>,
43-
}
44-
45-
impl<'tcx> AttributeMap<'tcx> {
46-
fn new(owner_info: &'tcx Option<OwnerInfo<'tcx>>) -> AttributeMap<'tcx> {
47-
const FALLBACK: &'static BTreeMap<ItemLocalId, &'static [Attribute]> = &BTreeMap::new();
48-
let map = owner_info.as_ref().map_or(FALLBACK, |info| &info.attrs);
49-
AttributeMap { map }
50-
}
51-
52-
fn get(&self, id: ItemLocalId) -> &'tcx [Attribute] {
53-
self.map.get(&id).copied().unwrap_or(&[])
54-
}
55-
}
56-
5737
/// Gather the LocalDefId for each item-like within a module, including items contained within
5838
/// bodies. The Ids are in visitor order. This is used to partition a pass between modules.
5939
#[derive(Debug, HashStable)]
@@ -105,7 +85,8 @@ pub fn provide(providers: &mut Providers) {
10585
});
10686
parent
10787
};
108-
providers.hir_attrs = |tcx, id| AttributeMap::new(&tcx.hir_crate(()).owners[id]);
88+
providers.hir_attrs =
89+
|tcx, id| tcx.hir_crate(()).owners[id].as_ref().map_or(AttributeMap::EMPTY, |o| &o.attrs);
10990
providers.source_span = |tcx, def_id| tcx.resolutions(()).definitions.def_span(def_id);
11091
providers.def_span = |tcx, def_id| tcx.hir().span_if_local(def_id).unwrap_or(DUMMY_SP);
11192
providers.fn_arg_names = |tcx, id| {

compiler/rustc_middle/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
#![feature(bool_to_option)]
3131
#![feature(box_patterns)]
3232
#![feature(core_intrinsics)]
33-
#![feature(const_btree_new)]
3433
#![feature(discriminant_kind)]
3534
#![feature(exhaustive_patterns)]
3635
#![feature(if_let_guard)]

compiler/rustc_middle/src/query/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ rustc_queries! {
7777
///
7878
/// This can be conveniently accessed by methods on `tcx.hir()`.
7979
/// Avoid calling this query directly.
80-
query hir_attrs(key: LocalDefId) -> rustc_middle::hir::AttributeMap<'tcx> {
80+
query hir_attrs(key: LocalDefId) -> &'tcx hir::AttributeMap<'tcx> {
8181
desc { |tcx| "HIR owner attributes in `{}`", tcx.def_path_str(key.to_def_id()) }
8282
}
8383

0 commit comments

Comments
 (0)