Skip to content

Commit fa521a4

Browse files
committed
Auto merge of #101688 - cjgillot:verify-hir-parent, r=petrochenkov
Assert that HIR nodes are not their own parent. Fixes #101505. Replaces #101513 r? `@petrochenkov` `@nnethercote`
2 parents 98e1f04 + 51f4869 commit fa521a4

File tree

4 files changed

+13
-1
lines changed

4 files changed

+13
-1
lines changed

compiler/rustc_ast_lowering/src/index.rs

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
6969
fn insert(&mut self, span: Span, hir_id: HirId, node: Node<'hir>) {
7070
debug_assert_eq!(self.owner, hir_id.owner);
7171
debug_assert_ne!(hir_id.local_id.as_u32(), 0);
72+
debug_assert_ne!(hir_id.local_id, self.parent_node);
7273

7374
// Make sure that the DepNode of some node coincides with the HirId
7475
// owner of that node.

compiler/rustc_ast_lowering/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1289,7 +1289,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12891289
return self.lower_path_ty(t, qself, path, ParamMode::Explicit, itctx);
12901290
}
12911291
TyKind::ImplicitSelf => {
1292-
let hir_id = self.lower_node_id(t.id);
1292+
let hir_id = self.next_id();
12931293
let res = self.expect_full_res(t.id);
12941294
let res = self.lower_res(res);
12951295
hir::TyKind::Path(hir::QPath::Resolved(

compiler/rustc_hir/src/hir.rs

+9
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,16 @@ impl<'tcx> OwnerNodes<'tcx> {
835835
impl fmt::Debug for OwnerNodes<'_> {
836836
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
837837
f.debug_struct("OwnerNodes")
838+
// Do not print all the pointers to all the nodes, as it would be unreadable.
838839
.field("node", &self.nodes[ItemLocalId::from_u32(0)])
840+
.field(
841+
"parents",
842+
&self
843+
.nodes
844+
.iter_enumerated()
845+
.map(|(id, parented_node)| (id, parented_node.as_ref().map(|node| node.parent)))
846+
.collect::<Vec<_>>(),
847+
)
839848
.field("bodies", &self.bodies)
840849
.field("local_id_to_def_id", &self.local_id_to_def_id)
841850
.field("hash_without_bodies", &self.hash_without_bodies)

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

+2
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,8 @@ impl<'hir> Map<'hir> {
307307
let owner = self.tcx.hir_owner_nodes(id.owner).as_owner()?;
308308
let node = owner.nodes[id.local_id].as_ref()?;
309309
let hir_id = HirId { owner: id.owner, local_id: node.parent };
310+
// HIR indexing should have checked that.
311+
debug_assert_ne!(id.local_id, node.parent);
310312
Some(hir_id)
311313
}
312314
}

0 commit comments

Comments
 (0)