Auto merge of #101688 - cjgillot:verify-hir-parent, r=petrochenkov

Assert that HIR nodes are not their own parent.

Fixes https://github.com/rust-lang/rust/issues/101505.
Replaces #101513

r? `@petrochenkov` `@nnethercote`
This commit is contained in:
bors 2022-09-12 00:41:56 +00:00
commit fa521a4691
4 changed files with 13 additions and 1 deletions

View File

@ -69,6 +69,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
fn insert(&mut self, span: Span, hir_id: HirId, node: Node<'hir>) {
debug_assert_eq!(self.owner, hir_id.owner);
debug_assert_ne!(hir_id.local_id.as_u32(), 0);
debug_assert_ne!(hir_id.local_id, self.parent_node);
// Make sure that the DepNode of some node coincides with the HirId
// owner of that node.

View File

@ -1289,7 +1289,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
return self.lower_path_ty(t, qself, path, ParamMode::Explicit, itctx);
}
TyKind::ImplicitSelf => {
let hir_id = self.lower_node_id(t.id);
let hir_id = self.next_id();
let res = self.expect_full_res(t.id);
let res = self.lower_res(res);
hir::TyKind::Path(hir::QPath::Resolved(

View File

@ -835,7 +835,16 @@ impl<'tcx> OwnerNodes<'tcx> {
impl fmt::Debug for OwnerNodes<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("OwnerNodes")
// Do not print all the pointers to all the nodes, as it would be unreadable.
.field("node", &self.nodes[ItemLocalId::from_u32(0)])
.field(
"parents",
&self
.nodes
.iter_enumerated()
.map(|(id, parented_node)| (id, parented_node.as_ref().map(|node| node.parent)))
.collect::<Vec<_>>(),
)
.field("bodies", &self.bodies)
.field("local_id_to_def_id", &self.local_id_to_def_id)
.field("hash_without_bodies", &self.hash_without_bodies)

View File

@ -307,6 +307,8 @@ impl<'hir> Map<'hir> {
let owner = self.tcx.hir_owner_nodes(id.owner).as_owner()?;
let node = owner.nodes[id.local_id].as_ref()?;
let hir_id = HirId { owner: id.owner, local_id: node.parent };
// HIR indexing should have checked that.
debug_assert_ne!(id.local_id, node.parent);
Some(hir_id)
}
}