Skip to content

Commit 6006472

Browse files
committed
Return a LocalDefId in get_parent_item.
1 parent 5a123c2 commit 6006472

File tree

51 files changed

+151
-140
lines changed

Some content is hidden

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

51 files changed

+151
-140
lines changed

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
896896
if look_at_return && hir.get_return_block(closure_id).is_some() {
897897
// ...otherwise we are probably in the tail expression of the function, point at the
898898
// return type.
899-
match hir.get(hir.get_parent_item(fn_call_id)) {
899+
match hir.get_by_def_id(hir.get_parent_item(fn_call_id)) {
900900
hir::Node::Item(hir::Item { ident, kind: hir::ItemKind::Fn(sig, ..), .. })
901901
| hir::Node::TraitItem(hir::TraitItem {
902902
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_by_def_id(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
@@ -2212,9 +2212,9 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
22122212
if let Some(Node::Item(Item {
22132213
kind: ItemKind::Trait(..) | ItemKind::Impl { .. },
22142214
..
2215-
})) = hir.find(parent_id)
2215+
})) = hir.find_by_def_id(parent_id)
22162216
{
2217-
Some(self.tcx.generics_of(hir.local_def_id(parent_id).to_def_id()))
2217+
Some(self.tcx.generics_of(parent_id))
22182218
} else {
22192219
None
22202220
},

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
187187
| ObligationCauseCode::BlockTailExpression(hir_id) = cause.code()
188188
{
189189
let parent_id = tcx.hir().get_parent_item(*hir_id);
190+
let parent_id = tcx.hir().local_def_id_to_hir_id(parent_id);
190191
if let Some(fn_decl) = tcx.hir().fn_decl_by_hir_id(parent_id) {
191192
let mut span: MultiSpan = fn_decl.output.span().into();
192193
let mut add_label = true;
@@ -425,7 +426,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
425426
let tcx = self.tcx();
426427
match tcx.hir().get_if_local(def_id) {
427428
Some(Node::ImplItem(impl_item)) => {
428-
match tcx.hir().find(tcx.hir().get_parent_item(impl_item.hir_id())) {
429+
match tcx.hir().find_by_def_id(tcx.hir().get_parent_item(impl_item.hir_id())) {
429430
Some(Node::Item(Item {
430431
kind: ItemKind::Impl(hir::Impl { self_ty, .. }),
431432
..
@@ -434,13 +435,13 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
434435
}
435436
}
436437
Some(Node::TraitItem(trait_item)) => {
437-
let parent_id = tcx.hir().get_parent_item(trait_item.hir_id());
438-
match tcx.hir().find(parent_id) {
438+
let trait_did = tcx.hir().get_parent_item(trait_item.hir_id());
439+
match tcx.hir().find_by_def_id(trait_did) {
439440
Some(Node::Item(Item { kind: ItemKind::Trait(..), .. })) => {
440441
// The method being called is defined in the `trait`, but the `'static`
441442
// obligation comes from the `impl`. Find that `impl` so that we can point
442443
// at it in the suggestion.
443-
let trait_did = tcx.hir().local_def_id(parent_id).to_def_id();
444+
let trait_did = trait_did.to_def_id();
444445
match tcx
445446
.hir()
446447
.trait_impls(trait_did)

compiler/rustc_infer/src/infer/opaque_types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ fn may_define_opaque_type(tcx: TyCtxt<'_>, def_id: LocalDefId, opaque_hir_id: hi
635635
let scope = tcx.hir().get_defining_scope(opaque_hir_id);
636636
// We walk up the node tree until we hit the root or the scope of the opaque type.
637637
while hir_id != scope && hir_id != hir::CRATE_HIR_ID {
638-
hir_id = tcx.hir().get_parent_item(hir_id);
638+
hir_id = tcx.hir().local_def_id_to_hir_id(tcx.hir().get_parent_item(hir_id));
639639
}
640640
// Syntactically, we are allowed to define the concrete type if:
641641
let res = hir_id == scope;

compiler/rustc_lint/src/builtin.rs

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

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

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

Lines changed: 29 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
}
@@ -340,11 +340,23 @@ impl<'hir> Map<'hir> {
340340
}
341341
}
342342

343+
/// Retrieves the `Node` corresponding to `id`, returning `None` if cannot be found.
344+
#[inline]
345+
pub fn find_by_def_id(&self, id: LocalDefId) -> Option<Node<'hir>> {
346+
self.find(self.local_def_id_to_hir_id(id))
347+
}
348+
343349
/// Retrieves the `Node` corresponding to `id`, panicking if it cannot be found.
344350
pub fn get(&self, id: HirId) -> Node<'hir> {
345351
self.find(id).unwrap_or_else(|| bug!("couldn't find hir id {} in the HIR map", id))
346352
}
347353

354+
/// Retrieves the `Node` corresponding to `id`, panicking if it cannot be found.
355+
#[inline]
356+
pub fn get_by_def_id(&self, id: LocalDefId) -> Node<'hir> {
357+
self.find_by_def_id(id).unwrap_or_else(|| bug!("couldn't find {:?} in the HIR map", id))
358+
}
359+
348360
pub fn get_if_local(&self, id: DefId) -> Option<Node<'hir>> {
349361
id.as_local().and_then(|id| self.find(self.local_def_id_to_hir_id(id)))
350362
}
@@ -780,23 +792,23 @@ impl<'hir> Map<'hir> {
780792
/// parent item is in this map. The "parent item" is the closest parent node
781793
/// in the HIR which is recorded by the map and is an item, either an item
782794
/// in a module, trait, or impl.
783-
pub fn get_parent_item(&self, hir_id: HirId) -> HirId {
784-
if let Some((hir_id, _node)) = self.parent_owner_iter(hir_id).next() {
785-
hir_id
795+
pub fn get_parent_item(&self, hir_id: HirId) -> LocalDefId {
796+
if let Some((def_id, _node)) = self.parent_owner_iter(hir_id).next() {
797+
def_id
786798
} else {
787-
CRATE_HIR_ID
799+
CRATE_DEF_ID
788800
}
789801
}
790802

791803
/// Returns the `HirId` of `id`'s nearest module parent, or `id` itself if no
792804
/// module parent is in this map.
793-
pub(super) fn get_module_parent_node(&self, hir_id: HirId) -> HirId {
794-
for (hir_id, node) in self.parent_owner_iter(hir_id) {
805+
pub(super) fn get_module_parent_node(&self, hir_id: HirId) -> LocalDefId {
806+
for (def_id, node) in self.parent_owner_iter(hir_id) {
795807
if let OwnerNode::Item(&Item { kind: ItemKind::Mod(_), .. }) = node {
796-
return hir_id;
808+
return def_id;
797809
}
798810
}
799-
CRATE_HIR_ID
811+
CRATE_DEF_ID
800812
}
801813

802814
/// When on an if expression, a match arm tail expression or a match arm, give back
@@ -859,19 +871,18 @@ impl<'hir> Map<'hir> {
859871
}
860872
}
861873

862-
pub fn get_parent_did(&self, id: HirId) -> LocalDefId {
863-
self.local_def_id(self.get_parent_item(id))
864-
}
865-
866874
pub fn get_foreign_abi(&self, hir_id: HirId) -> Abi {
867875
let parent = self.get_parent_item(hir_id);
868-
if let Some(node) = self.tcx.hir_owner(self.local_def_id(parent)) {
876+
if let Some(node) = self.tcx.hir_owner(parent) {
869877
if let OwnerNode::Item(Item { kind: ItemKind::ForeignMod { abi, .. }, .. }) = node.node
870878
{
871879
return *abi;
872880
}
873881
}
874-
bug!("expected foreign mod or inlined parent, found {}", self.node_to_string(parent))
882+
bug!(
883+
"expected foreign mod or inlined parent, found {}",
884+
self.node_to_string(HirId::make_owner(parent))
885+
)
875886
}
876887

877888
pub fn expect_item(&self, id: LocalDefId) -> &'hir Item<'hir> {
@@ -929,7 +940,7 @@ impl<'hir> Map<'hir> {
929940
Node::Lifetime(lt) => lt.name.ident().name,
930941
Node::GenericParam(param) => param.name.ident().name,
931942
Node::Binding(&Pat { kind: PatKind::Binding(_, _, l, _), .. }) => l.name,
932-
Node::Ctor(..) => self.name(self.get_parent_item(id)),
943+
Node::Ctor(..) => self.name(HirId::make_owner(self.get_parent_item(id))),
933944
_ => return None,
934945
})
935946
}

compiler/rustc_middle/src/hir/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl<'tcx> TyCtxt<'tcx> {
5858
pub fn provide(providers: &mut Providers) {
5959
providers.parent_module_from_def_id = |tcx, id| {
6060
let hir = tcx.hir();
61-
hir.local_def_id(hir.get_module_parent_node(hir.local_def_id_to_hir_id(id)))
61+
hir.get_module_parent_node(hir.local_def_id_to_hir_id(id))
6262
};
6363
providers.hir_crate = |tcx, ()| tcx.untracked_crate;
6464
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
@@ -348,7 +348,7 @@ impl<'tcx> TyCtxt<'tcx> {
348348
// Deprecated attributes apply in-crate and cross-crate.
349349
if let Some(id) = id {
350350
if let Some(depr_entry) = self.lookup_deprecation_entry(def_id) {
351-
let parent_def_id = self.hir().local_def_id(self.hir().get_parent_item(id));
351+
let parent_def_id = self.hir().get_parent_item(id);
352352
let skip = self
353353
.lookup_deprecation_entry(parent_def_id.to_def_id())
354354
.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
@@ -869,7 +869,7 @@ fn foo(&self) -> Self::T { String::new() }
869869
// When `body_owner` is an `impl` or `trait` item, look in its associated types for
870870
// `expected` and point at it.
871871
let parent_id = self.hir().get_parent_item(hir_id);
872-
let item = self.hir().find(parent_id);
872+
let item = self.hir().find_by_def_id(parent_id);
873873
debug!("expected_projection parent item {:?}", item);
874874
match item {
875875
Some(hir::Node::Item(hir::Item { kind: hir::ItemKind::Trait(.., items), .. })) => {

0 commit comments

Comments
 (0)