diff options
| author | bors <bors@rust-lang.org> | 2024-02-12 00:34:22 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-02-12 00:34:22 +0000 |
| commit | 084ce5bdb5f7dc1c725f6770a8de281165ba3b0a (patch) | |
| tree | 19b02783a13bce0a281f7d29274a102afde2c629 /compiler/rustc_middle/src | |
| parent | 520b0b20aa8c218f84cefc6260f52406b84fa55f (diff) | |
| parent | 9bd630af97e609cd9579703777a2d8d67f8fc2da (diff) | |
| download | rust-084ce5bdb5f7dc1c725f6770a8de281165ba3b0a.tar.gz rust-084ce5bdb5f7dc1c725f6770a8de281165ba3b0a.zip | |
Auto merge of #120951 - matthiaskrgr:rollup-0nnm7dv, r=matthiaskrgr
Rollup of 8 pull requests Successful merges: - #110483 (Create try_new function for ThinBox) - #120740 (Make cmath.rs a single file) - #120872 (hir: Refactor getters for HIR parents) - #120880 (add note on comparing vtables / function pointers) - #120885 (interpret/visitor: ensure we only see normalized types) - #120888 (assert_unsafe_precondition cleanup) - #120897 (Encode `coroutine_for_closure` for foreign crates) - #120937 ([docs] Update armv6k-nintendo-3ds platform docs for outdated info) r? `@ghost` `@rustbot` modify labels: rollup
Diffstat (limited to 'compiler/rustc_middle/src')
| -rw-r--r-- | compiler/rustc_middle/src/hir/map/mod.rs | 76 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/hir/mod.rs | 15 |
2 files changed, 40 insertions, 51 deletions
diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index bf72aac1033..50817dd0a80 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -82,7 +82,7 @@ impl<'hir> Iterator for ParentHirIterator<'hir> { } // There are nodes that do not have entries, so we need to skip them. - let parent_id = self.map.parent_id(self.current_id); + let parent_id = self.map.tcx.parent_hir_id(self.current_id); if parent_id == self.current_id { self.current_id = CRATE_HIR_ID; @@ -175,6 +175,28 @@ impl<'tcx> TyCtxt<'tcx> { self.opt_hir_node_by_def_id(id) .unwrap_or_else(|| bug!("couldn't find HIR node for def id {id:?}")) } + + /// Returns `HirId` of the parent HIR node of node with this `hir_id`. + /// Returns the same `hir_id` if and only if `hir_id == CRATE_HIR_ID`. + /// + /// If calling repeatedly and iterating over parents, prefer [`Map::parent_iter`]. + pub fn parent_hir_id(self, hir_id: HirId) -> HirId { + let HirId { owner, local_id } = hir_id; + if local_id == ItemLocalId::from_u32(0) { + self.hir_owner_parent(owner) + } else { + let parent_local_id = self.hir_owner_nodes(owner).nodes[local_id].parent; + // HIR indexing should have checked that. + debug_assert_ne!(parent_local_id, local_id); + HirId { owner, local_id: parent_local_id } + } + } + + /// Returns parent HIR node of node with this `hir_id`. + /// Returns HIR node of the same `hir_id` if and only if `hir_id == CRATE_HIR_ID`. + pub fn parent_hir_node(self, hir_id: HirId) -> Node<'tcx> { + self.hir_node(self.parent_hir_id(hir_id)) + } } impl<'hir> Map<'hir> { @@ -217,39 +239,6 @@ impl<'hir> Map<'hir> { self.tcx.definitions_untracked().def_path_hash(def_id) } - /// Finds the id of the parent node to this one. - /// - /// If calling repeatedly and iterating over parents, prefer [`Map::parent_iter`]. - pub fn opt_parent_id(self, id: HirId) -> Option<HirId> { - if id.local_id == ItemLocalId::from_u32(0) { - // FIXME: This function never returns `None` right now, and the parent chain end is - // determined by checking for `parent(id) == id`. This function should return `None` - // for the crate root instead. - Some(self.tcx.hir_owner_parent(id.owner)) - } else { - let owner = self.tcx.hir_owner_nodes(id.owner); - let node = &owner.nodes[id.local_id]; - 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) - } - } - - #[track_caller] - pub fn parent_id(self, hir_id: HirId) -> HirId { - self.opt_parent_id(hir_id) - .unwrap_or_else(|| bug!("No parent for node {}", self.node_to_string(hir_id))) - } - - pub fn get_parent(self, hir_id: HirId) -> Node<'hir> { - self.tcx.hir_node(self.parent_id(hir_id)) - } - - pub fn find_parent(self, hir_id: HirId) -> Option<Node<'hir>> { - Some(self.tcx.hir_node(self.opt_parent_id(hir_id)?)) - } - pub fn get_if_local(self, id: DefId) -> Option<Node<'hir>> { id.as_local() .and_then(|id| Some(self.tcx.hir_node(self.tcx.opt_local_def_id_to_hir_id(id)?))) @@ -304,14 +293,13 @@ impl<'hir> Map<'hir> { /// which this is the body of, i.e., a `fn`, `const` or `static` /// item (possibly associated), a closure, or a `hir::AnonConst`. pub fn body_owner(self, BodyId { hir_id }: BodyId) -> HirId { - let parent = self.parent_id(hir_id); + let parent = self.tcx.parent_hir_id(hir_id); assert!(is_body_owner(self.tcx.hir_node(parent), hir_id), "{hir_id:?}"); parent } pub fn body_owner_def_id(self, BodyId { hir_id }: BodyId) -> LocalDefId { - let parent = self.parent_id(hir_id); - associated_body(self.tcx.hir_node(parent)).unwrap().0 + associated_body(self.tcx.parent_hir_node(hir_id)).unwrap().0 } /// Given a `LocalDefId`, returns the `BodyId` associated with it, @@ -569,8 +557,8 @@ impl<'hir> Map<'hir> { /// Checks if the node is left-hand side of an assignment. pub fn is_lhs(self, id: HirId) -> bool { - match self.find_parent(id) { - Some(Node::Expr(expr)) => match expr.kind { + match self.tcx.parent_hir_node(id) { + Node::Expr(expr) => match expr.kind { ExprKind::Assign(lhs, _rhs, _span) => lhs.hir_id == id, _ => false, }, @@ -793,7 +781,7 @@ impl<'hir> Map<'hir> { Node::Pat(&Pat { kind: PatKind::Binding(_, _, ident, _), .. }) => Some(ident), // A `Ctor` doesn't have an identifier itself, but its parent // struct/variant does. Compare with `hir::Map::span`. - Node::Ctor(..) => match self.find_parent(id)? { + Node::Ctor(..) => match self.tcx.parent_hir_node(id) { Node::Item(item) => Some(item.ident), Node::Variant(variant) => Some(variant.ident), _ => unreachable!(), @@ -925,7 +913,7 @@ impl<'hir> Map<'hir> { ForeignItemKind::Fn(decl, _, _) => until_within(item.span, decl.output.span()), _ => named_span(item.span, item.ident, None), }, - Node::Ctor(_) => return self.span(self.parent_id(hir_id)), + Node::Ctor(_) => return self.span(self.tcx.parent_hir_id(hir_id)), Node::Expr(Expr { kind: ExprKind::Closure(Closure { fn_decl_span, .. }), span, @@ -968,7 +956,7 @@ impl<'hir> Map<'hir> { Node::PatField(field) => field.span, Node::Arm(arm) => arm.span, Node::Block(block) => block.span, - Node::Ctor(..) => self.span_with_body(self.parent_id(hir_id)), + Node::Ctor(..) => self.span_with_body(self.tcx.parent_hir_id(hir_id)), Node::Lifetime(lifetime) => lifetime.ident.span, Node::GenericParam(param) => param.span, Node::Infer(i) => i.span, @@ -1001,7 +989,7 @@ impl<'hir> Map<'hir> { /// Returns the HirId of `N` in `struct Foo<const N: usize = { ... }>` when /// called with the HirId for the `{ ... }` anon const pub fn opt_const_param_default_param_def_id(self, anon_const: HirId) -> Option<LocalDefId> { - match self.get_parent(anon_const) { + match self.tcx.parent_hir_node(anon_const) { Node::GenericParam(GenericParam { def_id: param_id, kind: GenericParamKind::Const { .. }, @@ -1026,7 +1014,7 @@ impl<'hir> Map<'hir> { _ => None, }?; - match self.find_parent(expr.hir_id)? { + match self.tcx.parent_hir_node(expr.hir_id) { Node::ExprField(field) => { if field.ident.name == local.name && field.is_shorthand { return Some(local.name); diff --git a/compiler/rustc_middle/src/hir/mod.rs b/compiler/rustc_middle/src/hir/mod.rs index f66cd2370e3..4ef9bc16221 100644 --- a/compiler/rustc_middle/src/hir/mod.rs +++ b/compiler/rustc_middle/src/hir/mod.rs @@ -136,13 +136,14 @@ pub fn provide(providers: &mut Providers) { }; providers.opt_hir_owner_nodes = |tcx, id| tcx.hir_crate(()).owners.get(id)?.as_owner().map(|i| &i.nodes); - providers.hir_owner_parent = |tcx, id| { - // Accessing the local_parent is ok since its value is hashed as part of `id`'s DefPathHash. - tcx.opt_local_parent(id.def_id).map_or(CRATE_HIR_ID, |parent| { - let mut parent_hir_id = tcx.local_def_id_to_hir_id(parent); - parent_hir_id.local_id = - tcx.hir_crate(()).owners[parent_hir_id.owner.def_id].unwrap().parenting[&id.def_id]; - parent_hir_id + providers.hir_owner_parent = |tcx, owner_id| { + tcx.opt_local_parent(owner_id.def_id).map_or(CRATE_HIR_ID, |parent_def_id| { + let parent_owner_id = tcx.local_def_id_to_hir_id(parent_def_id).owner; + HirId { + owner: parent_owner_id, + local_id: tcx.hir_crate(()).owners[parent_owner_id.def_id].unwrap().parenting + [&owner_id.def_id], + } }) }; providers.hir_attrs = |tcx, id| { |
