Skip to content

Commit c5628a5

Browse files
committed
Use invalid local id for zeroth node parent.
1 parent 6b79951 commit c5628a5

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

compiler/rustc_ast_lowering/src/index.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ pub(super) fn index_hir<'hir>(
4747
bodies: &IndexVec<ItemLocalId, Option<&'hir Body<'hir>>>,
4848
) -> (IndexVec<ItemLocalId, Option<ParentedNode<'hir>>>, FxHashMap<LocalDefId, ItemLocalId>) {
4949
let mut nodes = IndexVec::new();
50-
nodes.push(Some(ParentedNode { parent: ItemLocalId::new(0), node: item.into() }));
50+
// This node's parent should never be accessed: the owner's parent is computed by the
51+
// hir_owner_parent query. Make it invalid (= ItemLocalId::MAX) to force an ICE whenever it is
52+
// used.
53+
nodes.push(Some(ParentedNode { parent: ItemLocalId::INVALID, node: item.into() }));
5154
let mut collector = NodeCollector {
5255
source_map: sess.source_map(),
5356
definitions,

compiler/rustc_hir/src/hir.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,9 @@ pub struct OwnerNodes<'tcx> {
696696
/// Pre-computed hash of the item signature, sithout recursing into the body.
697697
pub hash_without_bodies: Fingerprint,
698698
/// Full HIR for the current owner.
699-
// The zeroth node's parent is trash, but is never accessed.
699+
// The zeroth node's parent should never be accessed: the owner's parent is computed by the
700+
// hir_owner_parent query. It is set to `ItemLocalId::INVALID` to force an ICE if accidentally
701+
// used.
700702
pub nodes: IndexVec<ItemLocalId, Option<ParentedNode<'tcx>>>,
701703
/// Content of local bodies.
702704
pub bodies: IndexVec<ItemLocalId, Option<&'tcx Body<'tcx>>>,

compiler/rustc_hir/src/hir_id.rs

+4
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ rustc_index::newtype_index! {
5656
pub struct ItemLocalId { .. }
5757
}
5858
rustc_data_structures::impl_stable_hash_via_hash!(ItemLocalId);
59+
impl ItemLocalId {
60+
/// Signal local id which should never be used.
61+
pub const INVALID: ItemLocalId = ItemLocalId::MAX;
62+
}
5963

6064
/// The `HirId` corresponding to `CRATE_NODE_ID` and `CRATE_DEF_INDEX`.
6165
pub const CRATE_HIR_ID: HirId = HirId {

0 commit comments

Comments
 (0)