Skip to content

Commit f8e2b9a

Browse files
committed
Return a LocalDefId in get_parent_item.
1 parent 27f5e93 commit f8e2b9a

File tree

50 files changed

+153
-139
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+153
-139
lines changed

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
874874
if look_at_return && hir.get_return_block(closure_id).is_some() {
875875
// ...otherwise we are probably in the tail expression of the function, point at the
876876
// return type.
877-
match hir.get(hir.get_parent_item(fn_call_id)) {
877+
match hir.get_def(hir.get_parent_item(fn_call_id)) {
878878
hir::Node::Item(hir::Item { ident, kind: hir::ItemKind::Fn(sig, ..), .. })
879879
| hir::Node::TraitItem(hir::TraitItem {
880880
ident,

compiler/rustc_borrowck/src/diagnostics/region_name.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
704704
hir::AsyncGeneratorKind::Block => " of async block",
705705
hir::AsyncGeneratorKind::Closure => " of async closure",
706706
hir::AsyncGeneratorKind::Fn => {
707-
let parent_item = hir.get(hir.get_parent_item(mir_hir_id));
707+
let parent_item = hir.get_def(hir.get_parent_item(mir_hir_id));
708708
let output = &parent_item
709709
.fn_decl()
710710
.expect("generator lowered from async fn should be in fn")

compiler/rustc_infer/src/infer/error_reporting/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2105,9 +2105,9 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
21052105
if let Some(Node::Item(Item {
21062106
kind: ItemKind::Trait(..) | ItemKind::Impl { .. },
21072107
..
2108-
})) = hir.find(parent_id)
2108+
})) = hir.find_def(parent_id)
21092109
{
2110-
Some(self.tcx.generics_of(hir.local_def_id(parent_id).to_def_id()))
2110+
Some(self.tcx.generics_of(parent_id))
21112111
} else {
21122112
None
21132113
},

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
376376
let tcx = self.tcx();
377377
match tcx.hir().get_if_local(def_id) {
378378
Some(Node::ImplItem(impl_item)) => {
379-
match tcx.hir().find(tcx.hir().get_parent_item(impl_item.hir_id())) {
379+
match tcx.hir().find_def(tcx.hir().get_parent_item(impl_item.hir_id())) {
380380
Some(Node::Item(Item {
381381
kind: ItemKind::Impl(hir::Impl { self_ty, .. }),
382382
..
@@ -385,13 +385,13 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
385385
}
386386
}
387387
Some(Node::TraitItem(trait_item)) => {
388-
let parent_id = tcx.hir().get_parent_item(trait_item.hir_id());
389-
match tcx.hir().find(parent_id) {
388+
let trait_did = tcx.hir().get_parent_item(trait_item.hir_id());
389+
match tcx.hir().find_def(trait_did) {
390390
Some(Node::Item(Item { kind: ItemKind::Trait(..), .. })) => {
391391
// The method being called is defined in the `trait`, but the `'static`
392392
// obligation comes from the `impl`. Find that `impl` so that we can point
393393
// at it in the suggestion.
394-
let trait_did = tcx.hir().local_def_id(parent_id).to_def_id();
394+
let trait_did = trait_did.to_def_id();
395395
match tcx
396396
.hir()
397397
.trait_impls(trait_did)

compiler/rustc_lint/src/builtin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
659659

660660
// If the method is an impl for an item with docs_hidden, don't doc.
661661
if method_context(cx, impl_item.hir_id()) == MethodLateContext::PlainImpl {
662-
let parent = cx.tcx.hir().get_parent_did(impl_item.hir_id());
662+
let parent = cx.tcx.hir().get_parent_item(impl_item.hir_id());
663663
let impl_ty = cx.tcx.type_of(parent);
664664
let outerdef = match impl_ty.kind() {
665665
ty::Adt(def, _) => Some(def.did),

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

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,13 @@ pub struct ParentOwnerIterator<'hir> {
117117
}
118118

119119
impl<'hir> Iterator for ParentOwnerIterator<'hir> {
120-
type Item = (HirId, OwnerNode<'hir>);
120+
type Item = (LocalDefId, OwnerNode<'hir>);
121121

122122
fn next(&mut self) -> Option<Self::Item> {
123123
if self.current_id.local_id.index() != 0 {
124124
self.current_id.local_id = ItemLocalId::new(0);
125125
if let Some(node) = self.map.tcx.hir_owner(self.current_id.owner) {
126-
return Some((self.current_id, node.node));
126+
return Some((self.current_id.owner, node.node));
127127
}
128128
}
129129
if self.current_id == CRATE_HIR_ID {
@@ -141,7 +141,7 @@ impl<'hir> Iterator for ParentOwnerIterator<'hir> {
141141

142142
// If this `HirId` doesn't have an entry, skip it and look for its `parent_id`.
143143
if let Some(node) = self.map.tcx.hir_owner(self.current_id.owner) {
144-
return Some((self.current_id, node.node));
144+
return Some((self.current_id.owner, node.node));
145145
}
146146
}
147147
}
@@ -328,11 +328,32 @@ impl<'hir> Map<'hir> {
328328
}
329329
}
330330

331+
/// Retrieves the `Node` corresponding to `id`, returning `None` if cannot be found.
332+
pub fn find_def(&self, id: LocalDefId) -> Option<Node<'hir>> {
333+
if let Some(owner) = self.tcx.hir_owner(id) {
334+
return Some(owner.node.into());
335+
}
336+
337+
let id = self.local_def_id_to_hir_id(id);
338+
if id.local_id == ItemLocalId::from_u32(0) {
339+
None
340+
} else {
341+
let owner = self.tcx.hir_owner_nodes(id.owner)?;
342+
let node = owner.nodes[id.local_id].as_ref()?;
343+
Some(node.node)
344+
}
345+
}
346+
331347
/// Retrieves the `Node` corresponding to `id`, panicking if it cannot be found.
332348
pub fn get(&self, id: HirId) -> Node<'hir> {
333349
self.find(id).unwrap_or_else(|| bug!("couldn't find hir id {} in the HIR map", id))
334350
}
335351

352+
/// Retrieves the `Node` corresponding to `id`, panicking if it cannot be found.
353+
pub fn get_def(&self, id: LocalDefId) -> Node<'hir> {
354+
self.find_def(id).unwrap_or_else(|| bug!("couldn't find {:?} in the HIR map", id))
355+
}
356+
336357
pub fn get_if_local(&self, id: DefId) -> Option<Node<'hir>> {
337358
id.as_local().and_then(|id| self.find(self.local_def_id_to_hir_id(id)))
338359
}
@@ -773,23 +794,23 @@ impl<'hir> Map<'hir> {
773794
/// parent item is in this map. The "parent item" is the closest parent node
774795
/// in the HIR which is recorded by the map and is an item, either an item
775796
/// in a module, trait, or impl.
776-
pub fn get_parent_item(&self, hir_id: HirId) -> HirId {
777-
if let Some((hir_id, _node)) = self.parent_owner_iter(hir_id).next() {
778-
hir_id
797+
pub fn get_parent_item(&self, hir_id: HirId) -> LocalDefId {
798+
if let Some((def_id, _node)) = self.parent_owner_iter(hir_id).next() {
799+
def_id
779800
} else {
780-
CRATE_HIR_ID
801+
CRATE_DEF_ID
781802
}
782803
}
783804

784805
/// Returns the `HirId` of `id`'s nearest module parent, or `id` itself if no
785806
/// module parent is in this map.
786-
pub(super) fn get_module_parent_node(&self, hir_id: HirId) -> HirId {
787-
for (hir_id, node) in self.parent_owner_iter(hir_id) {
807+
pub(super) fn get_module_parent_node(&self, hir_id: HirId) -> LocalDefId {
808+
for (def_id, node) in self.parent_owner_iter(hir_id) {
788809
if let OwnerNode::Item(&Item { kind: ItemKind::Mod(_), .. }) = node {
789-
return hir_id;
810+
return def_id;
790811
}
791812
}
792-
CRATE_HIR_ID
813+
CRATE_DEF_ID
793814
}
794815

795816
/// When on an if expression, a match arm tail expression or a match arm, give back
@@ -852,19 +873,18 @@ impl<'hir> Map<'hir> {
852873
}
853874
}
854875

855-
pub fn get_parent_did(&self, id: HirId) -> LocalDefId {
856-
self.local_def_id(self.get_parent_item(id))
857-
}
858-
859876
pub fn get_foreign_abi(&self, hir_id: HirId) -> Abi {
860877
let parent = self.get_parent_item(hir_id);
861-
if let Some(node) = self.tcx.hir_owner(self.local_def_id(parent)) {
878+
if let Some(node) = self.tcx.hir_owner(parent) {
862879
if let OwnerNode::Item(Item { kind: ItemKind::ForeignMod { abi, .. }, .. }) = node.node
863880
{
864881
return *abi;
865882
}
866883
}
867-
bug!("expected foreign mod or inlined parent, found {}", self.node_to_string(parent))
884+
bug!(
885+
"expected foreign mod or inlined parent, found {}",
886+
self.node_to_string(HirId::make_owner(parent))
887+
)
868888
}
869889

870890
pub fn expect_item(&self, id: LocalDefId) -> &'hir Item<'hir> {
@@ -922,7 +942,7 @@ impl<'hir> Map<'hir> {
922942
Node::Lifetime(lt) => lt.name.ident().name,
923943
Node::GenericParam(param) => param.name.ident().name,
924944
Node::Binding(&Pat { kind: PatKind::Binding(_, _, l, _), .. }) => l.name,
925-
Node::Ctor(..) => self.name(self.get_parent_item(id)),
945+
Node::Ctor(..) => self.name(HirId::make_owner(self.get_parent_item(id))),
926946
_ => return None,
927947
})
928948
}

compiler/rustc_middle/src/hir/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl<'tcx> TyCtxt<'tcx> {
5959
pub fn provide(providers: &mut Providers) {
6060
providers.parent_module_from_def_id = |tcx, id| {
6161
let hir = tcx.hir();
62-
hir.local_def_id(hir.get_module_parent_node(hir.local_def_id_to_hir_id(id)))
62+
hir.get_module_parent_node(hir.local_def_id_to_hir_id(id))
6363
};
6464
providers.hir_crate = |tcx, ()| tcx.untracked_crate;
6565
providers.crate_hash = map::crate_hash;

compiler/rustc_middle/src/middle/stability.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ impl<'tcx> TyCtxt<'tcx> {
312312
// Deprecated attributes apply in-crate and cross-crate.
313313
if let Some(id) = id {
314314
if let Some(depr_entry) = self.lookup_deprecation_entry(def_id) {
315-
let parent_def_id = self.hir().local_def_id(self.hir().get_parent_item(id));
315+
let parent_def_id = self.hir().get_parent_item(id);
316316
let skip = self
317317
.lookup_deprecation_entry(parent_def_id.to_def_id())
318318
.map_or(false, |parent_depr| parent_depr.same_origin(&depr_entry));

compiler/rustc_middle/src/ty/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@ fn foo(&self) -> Self::T { String::new() }
868868
// When `body_owner` is an `impl` or `trait` item, look in its associated types for
869869
// `expected` and point at it.
870870
let parent_id = self.hir().get_parent_item(hir_id);
871-
let item = self.hir().find(parent_id);
871+
let item = self.hir().find_def(parent_id);
872872
debug!("expected_projection parent item {:?}", item);
873873
match item {
874874
Some(hir::Node::Item(hir::Item { kind: hir::ItemKind::Trait(.., items), .. })) => {

compiler/rustc_passes/src/check_attr.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1313
use rustc_errors::{pluralize, struct_span_err, Applicability};
1414
use rustc_feature::{AttributeType, BUILTIN_ATTRIBUTE_MAP};
1515
use rustc_hir as hir;
16-
use rustc_hir::def_id::LocalDefId;
16+
use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID};
1717
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
1818
use rustc_hir::{self, FnSig, ForeignItem, HirId, Item, ItemKind, TraitItem, CRATE_HIR_ID};
1919
use rustc_hir::{MethodKind, Target};
@@ -31,7 +31,7 @@ pub(crate) fn target_from_impl_item<'tcx>(
3131
match impl_item.kind {
3232
hir::ImplItemKind::Const(..) => Target::AssocConst,
3333
hir::ImplItemKind::Fn(..) => {
34-
let parent_hir_id = tcx.hir().get_parent_item(impl_item.hir_id()).expect_owner();
34+
let parent_hir_id = tcx.hir().get_parent_item(impl_item.hir_id());
3535
let containing_item = tcx.hir().expect_item(parent_hir_id);
3636
let containing_impl_is_for_trait = match &containing_item.kind {
3737
hir::ItemKind::Impl(impl_) => impl_.of_trait.is_some(),
@@ -565,7 +565,7 @@ impl CheckAttrVisitor<'tcx> {
565565
Target::Impl => Some("implementation block"),
566566
Target::ForeignMod => Some("extern block"),
567567
Target::AssocTy => {
568-
let parent_hir_id = self.tcx.hir().get_parent_item(hir_id).expect_owner();
568+
let parent_hir_id = self.tcx.hir().get_parent_item(hir_id);
569569
let containing_item = self.tcx.hir().expect_item(parent_hir_id);
570570
if Target::from_item(containing_item) == Target::Impl {
571571
Some("type alias in implementation block")
@@ -574,7 +574,7 @@ impl CheckAttrVisitor<'tcx> {
574574
}
575575
}
576576
Target::AssocConst => {
577-
let parent_hir_id = self.tcx.hir().get_parent_item(hir_id).expect_owner();
577+
let parent_hir_id = self.tcx.hir().get_parent_item(hir_id);
578578
let containing_item = self.tcx.hir().expect_item(parent_hir_id);
579579
// We can't link to trait impl's consts.
580580
let err = "associated constant in trait implementation block";
@@ -815,7 +815,7 @@ impl CheckAttrVisitor<'tcx> {
815815
let mut err = lint.build(
816816
"this attribute can only be applied at the crate level",
817817
);
818-
if attr.style == AttrStyle::Outer && self.tcx.hir().get_parent_item(hir_id) == CRATE_HIR_ID {
818+
if attr.style == AttrStyle::Outer && self.tcx.hir().get_parent_item(hir_id) == CRATE_DEF_ID {
819819
if let Ok(mut src) =
820820
self.tcx.sess.source_map().span_to_snippet(attr.span)
821821
{

0 commit comments

Comments
 (0)