From 60064726aee0d05c41bf6ff0eac4699f47762b05 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Thu, 21 Oct 2021 19:41:47 +0200 Subject: [PATCH] Return a LocalDefId in get_parent_item. --- .../src/diagnostics/mutability_errors.rs | 2 +- .../src/diagnostics/region_name.rs | 2 +- .../src/infer/error_reporting/mod.rs | 4 +- .../nice_region_error/static_impl_trait.rs | 9 ++-- .../rustc_infer/src/infer/opaque_types.rs | 2 +- compiler/rustc_lint/src/builtin.rs | 2 +- compiler/rustc_middle/src/hir/map/mod.rs | 47 ++++++++++++------- compiler/rustc_middle/src/hir/mod.rs | 2 +- compiler/rustc_middle/src/middle/stability.rs | 2 +- compiler/rustc_middle/src/ty/error.rs | 2 +- compiler/rustc_passes/src/check_attr.rs | 10 ++-- compiler/rustc_passes/src/reachable.rs | 2 +- compiler/rustc_passes/src/stability.rs | 2 +- compiler/rustc_privacy/src/lib.rs | 2 +- compiler/rustc_resolve/src/late/lifetimes.rs | 10 ++-- .../src/traits/error_reporting/suggestions.rs | 4 +- compiler/rustc_ty_utils/src/assoc.rs | 3 +- compiler/rustc_typeck/src/astconv/mod.rs | 2 +- compiler/rustc_typeck/src/check/check.rs | 4 +- compiler/rustc_typeck/src/check/coercion.rs | 12 +++-- compiler/rustc_typeck/src/check/expr.rs | 4 +- .../rustc_typeck/src/check/fn_ctxt/checks.rs | 4 +- compiler/rustc_typeck/src/check/mod.rs | 20 ++++---- compiler/rustc_typeck/src/check/op.rs | 2 +- compiler/rustc_typeck/src/check/wfcheck.rs | 15 +++--- compiler/rustc_typeck/src/collect.rs | 20 ++++---- compiler/rustc_typeck/src/collect/type_of.rs | 6 +-- compiler/rustc_typeck/src/lib.rs | 6 +-- compiler/rustc_typeck/src/outlives/mod.rs | 3 +- src/librustdoc/clean/mod.rs | 2 +- src/librustdoc/scrape_examples.rs | 4 +- .../auxiliary/issue-40001-plugin.rs | 2 +- src/tools/clippy/clippy_lints/src/escape.rs | 2 +- src/tools/clippy/clippy_lints/src/exit.rs | 5 +- .../clippy_lints/src/functions/must_use.rs | 2 +- .../src/functions/result_unit_err.rs | 2 +- .../clippy_lints/src/inherent_to_string.rs | 2 +- .../clippy/clippy_lints/src/lifetimes.rs | 2 +- .../src/loops/needless_range_loop.rs | 6 +-- .../clippy/clippy_lints/src/methods/mod.rs | 2 +- .../clippy_lints/src/missing_const_for_fn.rs | 2 +- src/tools/clippy/clippy_lints/src/mut_key.rs | 2 +- .../clippy_lints/src/new_without_default.rs | 2 +- .../clippy/clippy_lints/src/non_copy_const.rs | 2 +- src/tools/clippy/clippy_lints/src/ptr.rs | 2 +- .../src/self_named_constructors.rs | 2 +- .../clippy_lints/src/suspicious_trait_impl.rs | 2 +- .../clippy/clippy_lints/src/types/mod.rs | 14 +++--- .../clippy/clippy_lints/src/unused_self.rs | 2 +- .../clippy_lints/src/zero_sized_map_values.rs | 6 ++- src/tools/clippy/clippy_utils/src/lib.rs | 19 ++++---- 51 files changed, 151 insertions(+), 140 deletions(-) diff --git a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs index 02935af8314f0..2b23a0475fe59 100644 --- a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs @@ -896,7 +896,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { if look_at_return && hir.get_return_block(closure_id).is_some() { // ...otherwise we are probably in the tail expression of the function, point at the // return type. - match hir.get(hir.get_parent_item(fn_call_id)) { + match hir.get_by_def_id(hir.get_parent_item(fn_call_id)) { hir::Node::Item(hir::Item { ident, kind: hir::ItemKind::Fn(sig, ..), .. }) | hir::Node::TraitItem(hir::TraitItem { ident, diff --git a/compiler/rustc_borrowck/src/diagnostics/region_name.rs b/compiler/rustc_borrowck/src/diagnostics/region_name.rs index 80f5f77a02550..9aa58f05a8e65 100644 --- a/compiler/rustc_borrowck/src/diagnostics/region_name.rs +++ b/compiler/rustc_borrowck/src/diagnostics/region_name.rs @@ -704,7 +704,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { hir::AsyncGeneratorKind::Block => " of async block", hir::AsyncGeneratorKind::Closure => " of async closure", hir::AsyncGeneratorKind::Fn => { - let parent_item = hir.get(hir.get_parent_item(mir_hir_id)); + let parent_item = hir.get_by_def_id(hir.get_parent_item(mir_hir_id)); let output = &parent_item .fn_decl() .expect("generator lowered from async fn should be in fn") diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index 6d39dadc7ba3f..e855ad2f02fd9 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -2212,9 +2212,9 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { if let Some(Node::Item(Item { kind: ItemKind::Trait(..) | ItemKind::Impl { .. }, .. - })) = hir.find(parent_id) + })) = hir.find_by_def_id(parent_id) { - Some(self.tcx.generics_of(hir.local_def_id(parent_id).to_def_id())) + Some(self.tcx.generics_of(parent_id)) } else { None }, diff --git a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs index b6dff2e53e9cd..eecd0a5a70807 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs @@ -187,6 +187,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { | ObligationCauseCode::BlockTailExpression(hir_id) = cause.code() { let parent_id = tcx.hir().get_parent_item(*hir_id); + let parent_id = tcx.hir().local_def_id_to_hir_id(parent_id); if let Some(fn_decl) = tcx.hir().fn_decl_by_hir_id(parent_id) { let mut span: MultiSpan = fn_decl.output.span().into(); let mut add_label = true; @@ -425,7 +426,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { let tcx = self.tcx(); match tcx.hir().get_if_local(def_id) { Some(Node::ImplItem(impl_item)) => { - match tcx.hir().find(tcx.hir().get_parent_item(impl_item.hir_id())) { + match tcx.hir().find_by_def_id(tcx.hir().get_parent_item(impl_item.hir_id())) { Some(Node::Item(Item { kind: ItemKind::Impl(hir::Impl { self_ty, .. }), .. @@ -434,13 +435,13 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { } } Some(Node::TraitItem(trait_item)) => { - let parent_id = tcx.hir().get_parent_item(trait_item.hir_id()); - match tcx.hir().find(parent_id) { + let trait_did = tcx.hir().get_parent_item(trait_item.hir_id()); + match tcx.hir().find_by_def_id(trait_did) { Some(Node::Item(Item { kind: ItemKind::Trait(..), .. })) => { // The method being called is defined in the `trait`, but the `'static` // obligation comes from the `impl`. Find that `impl` so that we can point // at it in the suggestion. - let trait_did = tcx.hir().local_def_id(parent_id).to_def_id(); + let trait_did = trait_did.to_def_id(); match tcx .hir() .trait_impls(trait_did) diff --git a/compiler/rustc_infer/src/infer/opaque_types.rs b/compiler/rustc_infer/src/infer/opaque_types.rs index 04b1a42e5beef..0b1c59d092f2b 100644 --- a/compiler/rustc_infer/src/infer/opaque_types.rs +++ b/compiler/rustc_infer/src/infer/opaque_types.rs @@ -635,7 +635,7 @@ fn may_define_opaque_type(tcx: TyCtxt<'_>, def_id: LocalDefId, opaque_hir_id: hi let scope = tcx.hir().get_defining_scope(opaque_hir_id); // We walk up the node tree until we hit the root or the scope of the opaque type. while hir_id != scope && hir_id != hir::CRATE_HIR_ID { - hir_id = tcx.hir().get_parent_item(hir_id); + hir_id = tcx.hir().local_def_id_to_hir_id(tcx.hir().get_parent_item(hir_id)); } // Syntactically, we are allowed to define the concrete type if: let res = hir_id == scope; diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index c0384875a47c9..edfff3dae65d5 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -656,7 +656,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc { // If the method is an impl for an item with docs_hidden, don't doc. if method_context(cx, impl_item.hir_id()) == MethodLateContext::PlainImpl { - let parent = cx.tcx.hir().get_parent_did(impl_item.hir_id()); + let parent = cx.tcx.hir().get_parent_item(impl_item.hir_id()); let impl_ty = cx.tcx.type_of(parent); let outerdef = match impl_ty.kind() { ty::Adt(def, _) => Some(def.did), diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index 30c75f35fc3cc..b346c2bb6364f 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -117,13 +117,13 @@ pub struct ParentOwnerIterator<'hir> { } impl<'hir> Iterator for ParentOwnerIterator<'hir> { - type Item = (HirId, OwnerNode<'hir>); + type Item = (LocalDefId, OwnerNode<'hir>); fn next(&mut self) -> Option { if self.current_id.local_id.index() != 0 { self.current_id.local_id = ItemLocalId::new(0); if let Some(node) = self.map.tcx.hir_owner(self.current_id.owner) { - return Some((self.current_id, node.node)); + return Some((self.current_id.owner, node.node)); } } if self.current_id == CRATE_HIR_ID { @@ -141,7 +141,7 @@ impl<'hir> Iterator for ParentOwnerIterator<'hir> { // If this `HirId` doesn't have an entry, skip it and look for its `parent_id`. if let Some(node) = self.map.tcx.hir_owner(self.current_id.owner) { - return Some((self.current_id, node.node)); + return Some((self.current_id.owner, node.node)); } } } @@ -340,11 +340,23 @@ impl<'hir> Map<'hir> { } } + /// Retrieves the `Node` corresponding to `id`, returning `None` if cannot be found. + #[inline] + pub fn find_by_def_id(&self, id: LocalDefId) -> Option> { + self.find(self.local_def_id_to_hir_id(id)) + } + /// Retrieves the `Node` corresponding to `id`, panicking if it cannot be found. pub fn get(&self, id: HirId) -> Node<'hir> { self.find(id).unwrap_or_else(|| bug!("couldn't find hir id {} in the HIR map", id)) } + /// Retrieves the `Node` corresponding to `id`, panicking if it cannot be found. + #[inline] + pub fn get_by_def_id(&self, id: LocalDefId) -> Node<'hir> { + self.find_by_def_id(id).unwrap_or_else(|| bug!("couldn't find {:?} in the HIR map", id)) + } + pub fn get_if_local(&self, id: DefId) -> Option> { id.as_local().and_then(|id| self.find(self.local_def_id_to_hir_id(id))) } @@ -780,23 +792,23 @@ impl<'hir> Map<'hir> { /// parent item is in this map. The "parent item" is the closest parent node /// in the HIR which is recorded by the map and is an item, either an item /// in a module, trait, or impl. - pub fn get_parent_item(&self, hir_id: HirId) -> HirId { - if let Some((hir_id, _node)) = self.parent_owner_iter(hir_id).next() { - hir_id + pub fn get_parent_item(&self, hir_id: HirId) -> LocalDefId { + if let Some((def_id, _node)) = self.parent_owner_iter(hir_id).next() { + def_id } else { - CRATE_HIR_ID + CRATE_DEF_ID } } /// Returns the `HirId` of `id`'s nearest module parent, or `id` itself if no /// module parent is in this map. - pub(super) fn get_module_parent_node(&self, hir_id: HirId) -> HirId { - for (hir_id, node) in self.parent_owner_iter(hir_id) { + pub(super) fn get_module_parent_node(&self, hir_id: HirId) -> LocalDefId { + for (def_id, node) in self.parent_owner_iter(hir_id) { if let OwnerNode::Item(&Item { kind: ItemKind::Mod(_), .. }) = node { - return hir_id; + return def_id; } } - CRATE_HIR_ID + CRATE_DEF_ID } /// When on an if expression, a match arm tail expression or a match arm, give back @@ -859,19 +871,18 @@ impl<'hir> Map<'hir> { } } - pub fn get_parent_did(&self, id: HirId) -> LocalDefId { - self.local_def_id(self.get_parent_item(id)) - } - pub fn get_foreign_abi(&self, hir_id: HirId) -> Abi { let parent = self.get_parent_item(hir_id); - if let Some(node) = self.tcx.hir_owner(self.local_def_id(parent)) { + if let Some(node) = self.tcx.hir_owner(parent) { if let OwnerNode::Item(Item { kind: ItemKind::ForeignMod { abi, .. }, .. }) = node.node { return *abi; } } - bug!("expected foreign mod or inlined parent, found {}", self.node_to_string(parent)) + bug!( + "expected foreign mod or inlined parent, found {}", + self.node_to_string(HirId::make_owner(parent)) + ) } pub fn expect_item(&self, id: LocalDefId) -> &'hir Item<'hir> { @@ -929,7 +940,7 @@ impl<'hir> Map<'hir> { Node::Lifetime(lt) => lt.name.ident().name, Node::GenericParam(param) => param.name.ident().name, Node::Binding(&Pat { kind: PatKind::Binding(_, _, l, _), .. }) => l.name, - Node::Ctor(..) => self.name(self.get_parent_item(id)), + Node::Ctor(..) => self.name(HirId::make_owner(self.get_parent_item(id))), _ => return None, }) } diff --git a/compiler/rustc_middle/src/hir/mod.rs b/compiler/rustc_middle/src/hir/mod.rs index 557dc25528f13..900f8f43bb7dc 100644 --- a/compiler/rustc_middle/src/hir/mod.rs +++ b/compiler/rustc_middle/src/hir/mod.rs @@ -58,7 +58,7 @@ impl<'tcx> TyCtxt<'tcx> { pub fn provide(providers: &mut Providers) { providers.parent_module_from_def_id = |tcx, id| { let hir = tcx.hir(); - hir.local_def_id(hir.get_module_parent_node(hir.local_def_id_to_hir_id(id))) + hir.get_module_parent_node(hir.local_def_id_to_hir_id(id)) }; providers.hir_crate = |tcx, ()| tcx.untracked_crate; providers.crate_hash = map::crate_hash; diff --git a/compiler/rustc_middle/src/middle/stability.rs b/compiler/rustc_middle/src/middle/stability.rs index 175d31d69d916..6d531d3e7d620 100644 --- a/compiler/rustc_middle/src/middle/stability.rs +++ b/compiler/rustc_middle/src/middle/stability.rs @@ -348,7 +348,7 @@ impl<'tcx> TyCtxt<'tcx> { // Deprecated attributes apply in-crate and cross-crate. if let Some(id) = id { if let Some(depr_entry) = self.lookup_deprecation_entry(def_id) { - let parent_def_id = self.hir().local_def_id(self.hir().get_parent_item(id)); + let parent_def_id = self.hir().get_parent_item(id); let skip = self .lookup_deprecation_entry(parent_def_id.to_def_id()) .map_or(false, |parent_depr| parent_depr.same_origin(&depr_entry)); diff --git a/compiler/rustc_middle/src/ty/error.rs b/compiler/rustc_middle/src/ty/error.rs index df6e739dc2011..5bb687512f3cb 100644 --- a/compiler/rustc_middle/src/ty/error.rs +++ b/compiler/rustc_middle/src/ty/error.rs @@ -869,7 +869,7 @@ fn foo(&self) -> Self::T { String::new() } // When `body_owner` is an `impl` or `trait` item, look in its associated types for // `expected` and point at it. let parent_id = self.hir().get_parent_item(hir_id); - let item = self.hir().find(parent_id); + let item = self.hir().find_by_def_id(parent_id); debug!("expected_projection parent item {:?}", item); match item { Some(hir::Node::Item(hir::Item { kind: hir::ItemKind::Trait(.., items), .. })) => { diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index d7b00699491d4..eca7d84e95d8e 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -13,7 +13,7 @@ use rustc_data_structures::fx::FxHashMap; use rustc_errors::{pluralize, struct_span_err, Applicability}; use rustc_feature::{AttributeDuplicates, AttributeType, BuiltinAttribute, BUILTIN_ATTRIBUTE_MAP}; use rustc_hir as hir; -use rustc_hir::def_id::LocalDefId; +use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID}; use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor}; use rustc_hir::{self, FnSig, ForeignItem, HirId, Item, ItemKind, TraitItem, CRATE_HIR_ID}; use rustc_hir::{MethodKind, Target}; @@ -32,7 +32,7 @@ pub(crate) fn target_from_impl_item<'tcx>( match impl_item.kind { hir::ImplItemKind::Const(..) => Target::AssocConst, hir::ImplItemKind::Fn(..) => { - let parent_hir_id = tcx.hir().get_parent_item(impl_item.hir_id()).expect_owner(); + let parent_hir_id = tcx.hir().get_parent_item(impl_item.hir_id()); let containing_item = tcx.hir().expect_item(parent_hir_id); let containing_impl_is_for_trait = match &containing_item.kind { hir::ItemKind::Impl(impl_) => impl_.of_trait.is_some(), @@ -582,7 +582,7 @@ impl CheckAttrVisitor<'_> { Target::Impl => Some("implementation block"), Target::ForeignMod => Some("extern block"), Target::AssocTy => { - let parent_hir_id = self.tcx.hir().get_parent_item(hir_id).expect_owner(); + let parent_hir_id = self.tcx.hir().get_parent_item(hir_id); let containing_item = self.tcx.hir().expect_item(parent_hir_id); if Target::from_item(containing_item) == Target::Impl { Some("type alias in implementation block") @@ -591,7 +591,7 @@ impl CheckAttrVisitor<'_> { } } Target::AssocConst => { - let parent_hir_id = self.tcx.hir().get_parent_item(hir_id).expect_owner(); + let parent_hir_id = self.tcx.hir().get_parent_item(hir_id); let containing_item = self.tcx.hir().expect_item(parent_hir_id); // We can't link to trait impl's consts. let err = "associated constant in trait implementation block"; @@ -832,7 +832,7 @@ impl CheckAttrVisitor<'_> { let mut err = lint.build( "this attribute can only be applied at the crate level", ); - if attr.style == AttrStyle::Outer && self.tcx.hir().get_parent_item(hir_id) == CRATE_HIR_ID { + if attr.style == AttrStyle::Outer && self.tcx.hir().get_parent_item(hir_id) == CRATE_DEF_ID { if let Ok(mut src) = self.tcx.sess.source_map().span_to_snippet(attr.span) { diff --git a/compiler/rustc_passes/src/reachable.rs b/compiler/rustc_passes/src/reachable.rs index 707e6b123daa2..1ce6a17303e5b 100644 --- a/compiler/rustc_passes/src/reachable.rs +++ b/compiler/rustc_passes/src/reachable.rs @@ -169,7 +169,7 @@ impl<'tcx> ReachableContext<'tcx> { if generics.requires_monomorphization(self.tcx) || attrs.requests_inline() { true } else { - let impl_did = self.tcx.hir().get_parent_did(hir_id); + let impl_did = self.tcx.hir().get_parent_item(hir_id); // Check the impl. If the generics on the self // type of the impl require inlining, this method // does too. diff --git a/compiler/rustc_passes/src/stability.rs b/compiler/rustc_passes/src/stability.rs index c136411df2712..270da883b8db4 100644 --- a/compiler/rustc_passes/src/stability.rs +++ b/compiler/rustc_passes/src/stability.rs @@ -629,7 +629,7 @@ impl<'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'tcx> { } fn visit_impl_item(&mut self, ii: &'tcx hir::ImplItem<'tcx>) { - let impl_def_id = self.tcx.hir().local_def_id(self.tcx.hir().get_parent_item(ii.hir_id())); + let impl_def_id = self.tcx.hir().get_parent_item(ii.hir_id()); if self.tcx.impl_trait_ref(impl_def_id).is_none() { self.check_missing_stability(ii.def_id, ii.span); } diff --git a/compiler/rustc_privacy/src/lib.rs b/compiler/rustc_privacy/src/lib.rs index e7741ccc4e424..e5d57a889a6fb 100644 --- a/compiler/rustc_privacy/src/lib.rs +++ b/compiler/rustc_privacy/src/lib.rs @@ -2024,7 +2024,7 @@ fn visibility(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Visibility { // Visibilities of trait impl items are inherited from their traits // and are not filled in resolve. Node::ImplItem(impl_item) => { - match tcx.hir().get(tcx.hir().get_parent_item(hir_id)) { + match tcx.hir().get_by_def_id(tcx.hir().get_parent_item(hir_id)) { Node::Item(hir::Item { kind: hir::ItemKind::Impl(hir::Impl { of_trait: Some(tr), .. }), .. diff --git a/compiler/rustc_resolve/src/late/lifetimes.rs b/compiler/rustc_resolve/src/late/lifetimes.rs index 02e57109bbd83..52bd45f12c97c 100644 --- a/compiler/rustc_resolve/src/late/lifetimes.rs +++ b/compiler/rustc_resolve/src/late/lifetimes.rs @@ -1137,7 +1137,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> { self.missing_named_lifetime_spots.push((&trait_item.generics).into()); let tcx = self.tcx; self.visit_early_late( - Some(tcx.hir().get_parent_did(trait_item.hir_id())), + Some(tcx.hir().get_parent_item(trait_item.hir_id())), trait_item.hir_id(), &sig.decl, &trait_item.generics, @@ -1206,7 +1206,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> { self.missing_named_lifetime_spots.push((&impl_item.generics).into()); let tcx = self.tcx; self.visit_early_late( - Some(tcx.hir().get_parent_did(impl_item.hir_id())), + Some(tcx.hir().get_parent_item(impl_item.hir_id())), impl_item.hir_id(), &sig.decl, &impl_item.generics, @@ -1950,7 +1950,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { }; if let Node::Lifetime(hir_lifetime) = self.tcx.hir().get(lifetime.hir_id) { if let Some(parent) = - self.tcx.hir().find(self.tcx.hir().get_parent_item(hir_lifetime.hir_id)) + self.tcx.hir().find_by_def_id(self.tcx.hir().get_parent_item(hir_lifetime.hir_id)) { match parent { Node::Item(item) => { @@ -2761,7 +2761,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { Node::TraitItem(&hir::TraitItem { kind: hir::TraitItemKind::Fn(_, ref m), .. }) => { if let hir::ItemKind::Trait(.., ref trait_items) = - self.tcx.hir().expect_item(self.tcx.hir().get_parent_did(parent)).kind + self.tcx.hir().expect_item(self.tcx.hir().get_parent_item(parent)).kind { assoc_item_kind = trait_items.iter().find(|ti| ti.id.hir_id() == parent).map(|ti| ti.kind); @@ -2774,7 +2774,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { Node::ImplItem(&hir::ImplItem { kind: hir::ImplItemKind::Fn(_, body), .. }) => { if let hir::ItemKind::Impl(hir::Impl { ref self_ty, ref items, .. }) = - self.tcx.hir().expect_item(self.tcx.hir().get_parent_did(parent)).kind + self.tcx.hir().expect_item(self.tcx.hir().get_parent_item(parent)).kind { impl_self = Some(self_ty); assoc_item_kind = diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index 8704c4c74690f..b06977c3f302e 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -481,7 +481,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { _ => {} } - hir_id = self.tcx.hir().get_parent_item(hir_id); + hir_id = self.tcx.hir().local_def_id_to_hir_id(self.tcx.hir().get_parent_item(hir_id)); } } @@ -2301,7 +2301,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { { let in_progress_typeck_results = self.in_progress_typeck_results.map(|t| t.borrow()); - let parent_id = hir.local_def_id(hir.get_parent_item(arg_hir_id)); + let parent_id = hir.get_parent_item(arg_hir_id); let typeck_results: &TypeckResults<'tcx> = match &in_progress_typeck_results { Some(t) if t.hir_owner == parent_id => t, _ => self.tcx.typeck(parent_id), diff --git a/compiler/rustc_ty_utils/src/assoc.rs b/compiler/rustc_ty_utils/src/assoc.rs index 6e2ef27f10817..781a639b09ebd 100644 --- a/compiler/rustc_ty_utils/src/assoc.rs +++ b/compiler/rustc_ty_utils/src/assoc.rs @@ -52,8 +52,7 @@ fn trait_of_item(tcx: TyCtxt<'_>, def_id: DefId) -> Option { fn associated_item(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AssocItem { let id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); - let parent_id = tcx.hir().get_parent_item(id); - let parent_def_id = tcx.hir().local_def_id(parent_id); + let parent_def_id = tcx.hir().get_parent_item(id); let parent_item = tcx.hir().expect_item(parent_def_id); match parent_item.kind { hir::ItemKind::Impl(ref impl_) => { diff --git a/compiler/rustc_typeck/src/astconv/mod.rs b/compiler/rustc_typeck/src/astconv/mod.rs index 63d69d32c197e..5044829cd89ed 100644 --- a/compiler/rustc_typeck/src/astconv/mod.rs +++ b/compiler/rustc_typeck/src/astconv/mod.rs @@ -1914,7 +1914,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { .and_then(|def_id| { def_id.as_local().map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id)) }) - .map(|hir_id| tcx.hir().get_parent_did(hir_id).to_def_id()); + .map(|hir_id| tcx.hir().get_parent_item(hir_id).to_def_id()); debug!("qpath_to_ty: parent_def_id={:?}", parent_def_id); diff --git a/compiler/rustc_typeck/src/check/check.rs b/compiler/rustc_typeck/src/check/check.rs index de560d50795b0..4e83b351b9b54 100644 --- a/compiler/rustc_typeck/src/check/check.rs +++ b/compiler/rustc_typeck/src/check/check.rs @@ -1440,8 +1440,8 @@ fn opaque_type_cycle_error(tcx: TyCtxt<'_>, def_id: LocalDefId, span: Span) { let mut err = struct_span_err!(tcx.sess, span, E0720, "cannot resolve opaque type"); let mut label = false; - if let Some((hir_id, visitor)) = get_owner_return_paths(tcx, def_id) { - let typeck_results = tcx.typeck(tcx.hir().local_def_id(hir_id)); + if let Some((def_id, visitor)) = get_owner_return_paths(tcx, def_id) { + let typeck_results = tcx.typeck(def_id); if visitor .returns .iter() diff --git a/compiler/rustc_typeck/src/check/coercion.rs b/compiler/rustc_typeck/src/check/coercion.rs index 01221e5dfa975..3668ecd234c64 100644 --- a/compiler/rustc_typeck/src/check/coercion.rs +++ b/compiler/rustc_typeck/src/check/coercion.rs @@ -1575,7 +1575,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> { expected, found, can_suggest, - fcx.tcx.hir().get_parent_item(id), + fcx.tcx.hir().local_def_id_to_hir_id(fcx.tcx.hir().get_parent_item(id)), ); } if !pointing_at_return_type { @@ -1584,13 +1584,19 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> { } let parent_id = fcx.tcx.hir().get_parent_item(id); - let parent_item = fcx.tcx.hir().get(parent_id); + let parent_item = fcx.tcx.hir().get_by_def_id(parent_id); if let (Some((expr, _)), Some((fn_decl, _, _))) = (expression, fcx.get_node_fn_decl(parent_item)) { fcx.suggest_missing_break_or_return_expr( - &mut err, expr, fn_decl, expected, found, id, parent_id, + &mut err, + expr, + fn_decl, + expected, + found, + id, + fcx.tcx.hir().local_def_id_to_hir_id(parent_id), ); } diff --git a/compiler/rustc_typeck/src/check/expr.rs b/compiler/rustc_typeck/src/check/expr.rs index faf3ef1e543c9..8f9f4e82e845d 100644 --- a/compiler/rustc_typeck/src/check/expr.rs +++ b/compiler/rustc_typeck/src/check/expr.rs @@ -742,7 +742,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { kind: hir::ImplItemKind::Fn(..), span: encl_fn_span, .. - })) = self.tcx.hir().find(encl_item_id) + })) = self.tcx.hir().find_by_def_id(encl_item_id) { // We are inside a function body, so reporting "return statement // outside of function body" needs an explanation. @@ -751,7 +751,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // If this didn't hold, we would not have to report an error in // the first place. - assert_ne!(encl_item_id, encl_body_owner_id); + assert_ne!(hir::HirId::make_owner(encl_item_id), encl_body_owner_id); let encl_body_id = self.tcx.hir().body_owned_by(encl_body_owner_id); let encl_body = self.tcx.hir().body(encl_body_id); diff --git a/compiler/rustc_typeck/src/check/fn_ctxt/checks.rs b/compiler/rustc_typeck/src/check/fn_ctxt/checks.rs index e796fe58170d2..d9a597bce8a01 100644 --- a/compiler/rustc_typeck/src/check/fn_ctxt/checks.rs +++ b/compiler/rustc_typeck/src/check/fn_ctxt/checks.rs @@ -846,7 +846,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } fn parent_item_span(&self, id: hir::HirId) -> Option { - let node = self.tcx.hir().get(self.tcx.hir().get_parent_item(id)); + let node = self.tcx.hir().get_by_def_id(self.tcx.hir().get_parent_item(id)); match node { Node::Item(&hir::Item { kind: hir::ItemKind::Fn(_, _, body_id), .. }) | Node::ImplItem(&hir::ImplItem { kind: hir::ImplItemKind::Fn(_, body_id), .. }) => { @@ -862,7 +862,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { /// Given a function block's `HirId`, returns its `FnDecl` if it exists, or `None` otherwise. fn get_parent_fn_decl(&self, blk_id: hir::HirId) -> Option<(&'tcx hir::FnDecl<'tcx>, Ident)> { - let parent = self.tcx.hir().get(self.tcx.hir().get_parent_item(blk_id)); + let parent = self.tcx.hir().get_by_def_id(self.tcx.hir().get_parent_item(blk_id)); self.get_node_fn_decl(parent).map(|(fn_decl, ident, _)| (fn_decl, ident)) } diff --git a/compiler/rustc_typeck/src/check/mod.rs b/compiler/rustc_typeck/src/check/mod.rs index d576154ff9073..719266ad5a435 100644 --- a/compiler/rustc_typeck/src/check/mod.rs +++ b/compiler/rustc_typeck/src/check/mod.rs @@ -511,19 +511,15 @@ struct GeneratorTypes<'tcx> { fn get_owner_return_paths<'tcx>( tcx: TyCtxt<'tcx>, def_id: LocalDefId, -) -> Option<(hir::HirId, ReturnsVisitor<'tcx>)> { +) -> Option<(LocalDefId, ReturnsVisitor<'tcx>)> { let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); - let id = tcx.hir().get_parent_item(hir_id); - tcx.hir() - .find(id) - .map(|n| (id, n)) - .and_then(|(hir_id, node)| node.body_id().map(|b| (hir_id, b))) - .map(|(hir_id, body_id)| { - let body = tcx.hir().body(body_id); - let mut visitor = ReturnsVisitor::default(); - visitor.visit_body(body); - (hir_id, visitor) - }) + let parent_id = tcx.hir().get_parent_item(hir_id); + tcx.hir().find_by_def_id(parent_id).and_then(|node| node.body_id()).map(|body_id| { + let body = tcx.hir().body(body_id); + let mut visitor = ReturnsVisitor::default(); + visitor.visit_body(body); + (parent_id, visitor) + }) } // Forbid defining intrinsics in Rust code, diff --git a/compiler/rustc_typeck/src/check/op.rs b/compiler/rustc_typeck/src/check/op.rs index 8ebfcdd539b67..72eabab301ac7 100644 --- a/compiler/rustc_typeck/src/check/op.rs +++ b/compiler/rustc_typeck/src/check/op.rs @@ -972,7 +972,7 @@ fn suggest_constraining_param( if let Some(generics) = param_def_id .as_local() .map(|id| hir.local_def_id_to_hir_id(id)) - .and_then(|id| hir.find(hir.get_parent_item(id))) + .and_then(|id| hir.find_by_def_id(hir.get_parent_item(id))) .as_ref() .and_then(|node| node.generics()) { diff --git a/compiler/rustc_typeck/src/check/wfcheck.rs b/compiler/rustc_typeck/src/check/wfcheck.rs index fbc446e3ea42e..bf68c59fd1a20 100644 --- a/compiler/rustc_typeck/src/check/wfcheck.rs +++ b/compiler/rustc_typeck/src/check/wfcheck.rs @@ -206,7 +206,7 @@ pub fn check_trait_item(tcx: TyCtxt<'_>, def_id: LocalDefId) { check_object_unsafe_self_trait_by_name(tcx, trait_item); check_associated_item(tcx, trait_item.def_id, span, method_sig); - let encl_trait_def_id = tcx.hir().get_parent_did(hir_id); + let encl_trait_def_id = tcx.hir().get_parent_item(hir_id); let encl_trait = tcx.hir().expect_item(encl_trait_def_id); let encl_trait_def_id = encl_trait.def_id.to_def_id(); let fn_lang_item_name = if Some(encl_trait_def_id) == tcx.lang_items().fn_trait() { @@ -668,13 +668,14 @@ fn could_be_self(trait_def_id: LocalDefId, ty: &hir::Ty<'_>) -> bool { /// Detect when an object unsafe trait is referring to itself in one of its associated items. /// When this is done, suggest using `Self` instead. fn check_object_unsafe_self_trait_by_name(tcx: TyCtxt<'_>, item: &hir::TraitItem<'_>) { - let (trait_name, trait_def_id) = match tcx.hir().get(tcx.hir().get_parent_item(item.hir_id())) { - hir::Node::Item(item) => match item.kind { - hir::ItemKind::Trait(..) => (item.ident, item.def_id), + let (trait_name, trait_def_id) = + match tcx.hir().get_by_def_id(tcx.hir().get_parent_item(item.hir_id())) { + hir::Node::Item(item) => match item.kind { + hir::ItemKind::Trait(..) => (item.ident, item.def_id), + _ => return, + }, _ => return, - }, - _ => return, - }; + }; let mut trait_should_be_self = vec![]; match &item.kind { hir::TraitItemKind::Const(ty, _) | hir::TraitItemKind::Type(_, Some(ty)) diff --git a/compiler/rustc_typeck/src/collect.rs b/compiler/rustc_typeck/src/collect.rs index 3cccdb27448fd..5c1c9c4791cc2 100644 --- a/compiler/rustc_typeck/src/collect.rs +++ b/compiler/rustc_typeck/src/collect.rs @@ -28,7 +28,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet}; use rustc_errors::{struct_span_err, Applicability}; use rustc_hir as hir; use rustc_hir::def::{CtorKind, DefKind}; -use rustc_hir::def_id::{DefId, LocalDefId, LOCAL_CRATE}; +use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID, LOCAL_CRATE}; use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor}; use rustc_hir::weak_lang_items; use rustc_hir::{GenericParamKind, HirId, Node}; @@ -435,7 +435,7 @@ impl<'tcx> AstConv<'tcx> for ItemCtxt<'tcx> { match self.node() { hir::Node::Field(_) | hir::Node::Ctor(_) | hir::Node::Variant(_) => { let item = - self.tcx.hir().expect_item(self.tcx.hir().get_parent_did(self.hir_id())); + self.tcx.hir().expect_item(self.tcx.hir().get_parent_item(self.hir_id())); match &item.kind { hir::ItemKind::Enum(_, generics) | hir::ItemKind::Struct(_, generics) @@ -1396,13 +1396,12 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics { | Node::Ctor(..) | Node::Field(_) => { let parent_id = tcx.hir().get_parent_item(hir_id); - Some(tcx.hir().local_def_id(parent_id).to_def_id()) + Some(parent_id.to_def_id()) } // FIXME(#43408) always enable this once `lazy_normalization` is // stable enough and does not need a feature gate anymore. Node::AnonConst(_) => { - let parent_id = tcx.hir().get_parent_item(hir_id); - let parent_def_id = tcx.hir().local_def_id(parent_id); + let parent_def_id = tcx.hir().get_parent_item(hir_id); let mut in_param_ty = false; for (_parent, node) in tcx.hir().parent_iter(hir_id) { @@ -1512,11 +1511,11 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics { }) => Some(fn_def_id.to_def_id()), ItemKind::OpaqueTy(hir::OpaqueTy { origin: hir::OpaqueTyOrigin::TyAlias, .. }) => { let parent_id = tcx.hir().get_parent_item(hir_id); - assert!(parent_id != hir_id && parent_id != CRATE_HIR_ID); + assert_ne!(parent_id, CRATE_DEF_ID); debug!("generics_of: parent of opaque ty {:?} is {:?}", def_id, parent_id); // Opaque types are always nested within another item, and // inherit the generics of the item. - Some(tcx.hir().local_def_id(parent_id).to_def_id()) + Some(parent_id.to_def_id()) } _ => None, }, @@ -1861,7 +1860,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> { } Ctor(data) | Variant(hir::Variant { data, .. }) if data.ctor_hir_id().is_some() => { - let ty = tcx.type_of(tcx.hir().get_parent_did(hir_id).to_def_id()); + let ty = tcx.type_of(tcx.hir().get_parent_item(hir_id)); let inputs = data.fields().iter().map(|f| tcx.type_of(tcx.hir().local_def_id(f.hir_id))); ty::Binder::dummy(tcx.mk_fn_sig( @@ -2431,8 +2430,7 @@ fn explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicat // parent of generics returned by `generics_of` // // In the above code we want the anon const to have predicates in its param env for `T: Trait` - let item_id = tcx.hir().get_parent_item(hir_id); - let item_def_id = tcx.hir().local_def_id(item_id).to_def_id(); + let item_def_id = tcx.hir().get_parent_item(hir_id); // In the above code example we would be calling `explicit_predicates_of(Foo)` here return tcx.explicit_predicates_of(item_def_id); } @@ -3230,7 +3228,7 @@ fn check_target_feature_trait_unsafe(tcx: TyCtxt<'_>, id: LocalDefId, attr_span: let hir_id = tcx.hir().local_def_id_to_hir_id(id); let node = tcx.hir().get(hir_id); if let Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Fn(..), .. }) = node { - let parent_id = tcx.hir().get_parent_did(hir_id); + let parent_id = tcx.hir().get_parent_item(hir_id); let parent_item = tcx.hir().expect_item(parent_id); if let hir::ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }) = parent_item.kind { tcx.sess diff --git a/compiler/rustc_typeck/src/collect/type_of.rs b/compiler/rustc_typeck/src/collect/type_of.rs index 5ff2a74754117..a216e8c2f883c 100644 --- a/compiler/rustc_typeck/src/collect/type_of.rs +++ b/compiler/rustc_typeck/src/collect/type_of.rs @@ -348,7 +348,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> { } } ImplItemKind::TyAlias(ty) => { - if tcx.impl_trait_ref(tcx.hir().get_parent_did(hir_id).to_def_id()).is_none() { + if tcx.impl_trait_ref(tcx.hir().get_parent_item(hir_id)).is_none() { check_feature_inherent_assoc_ty(tcx, item.span); } @@ -458,7 +458,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> { Node::Ctor(&ref def) | Node::Variant(Variant { data: ref def, .. }) => match *def { VariantData::Unit(..) | VariantData::Struct(..) => { - tcx.type_of(tcx.hir().get_parent_did(hir_id).to_def_id()) + tcx.type_of(tcx.hir().get_parent_item(hir_id)) } VariantData::Tuple(..) => { let substs = InternalSubsts::identity_for_item(tcx, def_id.to_def_id()); @@ -507,7 +507,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> { } Node::Variant(Variant { disr_expr: Some(ref e), .. }) if e.hir_id == hir_id => tcx - .adt_def(tcx.hir().get_parent_did(hir_id).to_def_id()) + .adt_def(tcx.hir().get_parent_item(hir_id)) .repr .discr_type() .to_ty(tcx), diff --git a/compiler/rustc_typeck/src/lib.rs b/compiler/rustc_typeck/src/lib.rs index 24e427f4bcff9..6a9f154844a57 100644 --- a/compiler/rustc_typeck/src/lib.rs +++ b/compiler/rustc_typeck/src/lib.rs @@ -543,8 +543,7 @@ pub fn hir_ty_to_ty<'tcx>(tcx: TyCtxt<'tcx>, hir_ty: &hir::Ty<'_>) -> Ty<'tcx> { // In case there are any projections, etc., find the "environment" // def-ID that will be used to determine the traits/predicates in // scope. This is derived from the enclosing item-like thing. - let env_node_id = tcx.hir().get_parent_item(hir_ty.hir_id); - let env_def_id = tcx.hir().local_def_id(env_node_id); + let env_def_id = tcx.hir().get_parent_item(hir_ty.hir_id); let item_cx = self::collect::ItemCtxt::new(tcx, env_def_id.to_def_id()); >::ast_ty_to_ty(&item_cx, hir_ty) } @@ -557,8 +556,7 @@ pub fn hir_trait_to_predicates<'tcx>( // In case there are any projections, etc., find the "environment" // def-ID that will be used to determine the traits/predicates in // scope. This is derived from the enclosing item-like thing. - let env_hir_id = tcx.hir().get_parent_item(hir_trait.hir_ref_id); - let env_def_id = tcx.hir().local_def_id(env_hir_id); + let env_def_id = tcx.hir().get_parent_item(hir_trait.hir_ref_id); let item_cx = self::collect::ItemCtxt::new(tcx, env_def_id.to_def_id()); let mut bounds = Bounds::default(); let _ = >::instantiate_poly_trait_ref( diff --git a/compiler/rustc_typeck/src/outlives/mod.rs b/compiler/rustc_typeck/src/outlives/mod.rs index eb3853b6b3dee..78a9cb33fbac5 100644 --- a/compiler/rustc_typeck/src/outlives/mod.rs +++ b/compiler/rustc_typeck/src/outlives/mod.rs @@ -35,8 +35,7 @@ fn inferred_outlives_of(tcx: TyCtxt<'_>, item_def_id: DefId) -> &[(ty::Predicate // parent of generics returned by `generics_of` // // In the above code we want the anon const to have predicates in its param env for `'b: 'a` - let item_id = tcx.hir().get_parent_item(id); - let item_def_id = tcx.hir().local_def_id(item_id).to_def_id(); + let item_def_id = tcx.hir().get_parent_item(id); // In the above code example we would be calling `inferred_outlives_of(Foo)` here return tcx.inferred_outlives_of(item_def_id); } diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 5d1e9d6754e93..a5bc70a74ae92 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -1004,7 +1004,7 @@ impl Clean for hir::ImplItem<'_> { let what_rustc_thinks = Item::from_def_id_and_parts(local_did, Some(self.ident.name), inner, cx); - let parent_item = cx.tcx.hir().expect_item(cx.tcx.hir().get_parent_did(self.hir_id())); + let parent_item = cx.tcx.hir().expect_item(cx.tcx.hir().get_parent_item(self.hir_id())); if let hir::ItemKind::Impl(impl_) = &parent_item.kind { if impl_.of_trait.is_some() { // Trait impl items always inherit the impl's visibility -- diff --git a/src/librustdoc/scrape_examples.rs b/src/librustdoc/scrape_examples.rs index 6809551fcfd9a..3282309b9df3e 100644 --- a/src/librustdoc/scrape_examples.rs +++ b/src/librustdoc/scrape_examples.rs @@ -173,7 +173,9 @@ where // If the enclosing item has a span coming from a proc macro, then we also don't want to include // the example. - let enclosing_item_span = tcx.hir().span_with_body(tcx.hir().get_parent_item(ex.hir_id)); + let enclosing_item_span = tcx + .hir() + .span_with_body(tcx.hir().local_def_id_to_hir_id(tcx.hir().get_parent_item(ex.hir_id))); if enclosing_item_span.from_expansion() { trace!("Rejecting expr ({:?}) from macro item: {:?}", span, enclosing_item_span); return; diff --git a/src/test/ui-fulldeps/auxiliary/issue-40001-plugin.rs b/src/test/ui-fulldeps/auxiliary/issue-40001-plugin.rs index de0df0aae82d7..ee668501ae78b 100644 --- a/src/test/ui-fulldeps/auxiliary/issue-40001-plugin.rs +++ b/src/test/ui-fulldeps/auxiliary/issue-40001-plugin.rs @@ -44,7 +44,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingAllowedAttrPass { ) { let item = match cx.tcx.hir().get(id) { Node::Item(item) => item, - _ => cx.tcx.hir().expect_item(cx.tcx.hir().get_parent_item(id).expect_owner()), + _ => cx.tcx.hir().expect_item(cx.tcx.hir().get_parent_item(id)), }; let allowed = |attr| pprust::attribute_to_string(attr).contains("allowed_attr"); diff --git a/src/tools/clippy/clippy_lints/src/escape.rs b/src/tools/clippy/clippy_lints/src/escape.rs index bc5d2f6278dee..5f95333a77407 100644 --- a/src/tools/clippy/clippy_lints/src/escape.rs +++ b/src/tools/clippy/clippy_lints/src/escape.rs @@ -77,7 +77,7 @@ impl<'tcx> LateLintPass<'tcx> for BoxedLocal { } let parent_id = cx.tcx.hir().get_parent_item(hir_id); - let parent_node = cx.tcx.hir().find(parent_id); + let parent_node = cx.tcx.hir().find_by_def_id(parent_id); let mut trait_self_ty = None; if let Some(Node::Item(item)) = parent_node { diff --git a/src/tools/clippy/clippy_lints/src/exit.rs b/src/tools/clippy/clippy_lints/src/exit.rs index d64cc61916c5e..cbf52d19334c0 100644 --- a/src/tools/clippy/clippy_lints/src/exit.rs +++ b/src/tools/clippy/clippy_lints/src/exit.rs @@ -34,11 +34,10 @@ impl<'tcx> LateLintPass<'tcx> for Exit { if let Some(def_id) = cx.qpath_res(path, path_expr.hir_id).opt_def_id(); if match_def_path(cx, def_id, &paths::EXIT); let parent = cx.tcx.hir().get_parent_item(e.hir_id); - if let Some(Node::Item(Item{kind: ItemKind::Fn(..), ..})) = cx.tcx.hir().find(parent); + if let Some(Node::Item(Item{kind: ItemKind::Fn(..), ..})) = cx.tcx.hir().find_by_def_id(parent); // If the next item up is a function we check if it is an entry point // and only then emit a linter warning - let def_id = cx.tcx.hir().local_def_id(parent); - if !is_entrypoint_fn(cx, def_id.to_def_id()); + if !is_entrypoint_fn(cx, parent.to_def_id()); then { span_lint(cx, EXIT, e.span, "usage of `process::exit`"); } diff --git a/src/tools/clippy/clippy_lints/src/functions/must_use.rs b/src/tools/clippy/clippy_lints/src/functions/must_use.rs index f2b4aefaead52..bf59103e3f4d2 100644 --- a/src/tools/clippy/clippy_lints/src/functions/must_use.rs +++ b/src/tools/clippy/clippy_lints/src/functions/must_use.rs @@ -48,7 +48,7 @@ pub(super) fn check_impl_item<'tcx>(cx: &LateContext<'tcx>, item: &'tcx hir::Imp let attr = must_use_attr(attrs); if let Some(attr) = attr { check_needless_must_use(cx, sig.decl, item.hir_id(), item.span, fn_header_span, attr); - } else if is_public && !is_proc_macro(cx.sess(), attrs) && trait_ref_of_method(cx, item.hir_id()).is_none() { + } else if is_public && !is_proc_macro(cx.sess(), attrs) && trait_ref_of_method(cx, item.def_id).is_none() { check_must_use_candidate( cx, sig.decl, diff --git a/src/tools/clippy/clippy_lints/src/functions/result_unit_err.rs b/src/tools/clippy/clippy_lints/src/functions/result_unit_err.rs index 73f08a0498973..120fcb2619c7c 100644 --- a/src/tools/clippy/clippy_lints/src/functions/result_unit_err.rs +++ b/src/tools/clippy/clippy_lints/src/functions/result_unit_err.rs @@ -27,7 +27,7 @@ pub(super) fn check_impl_item(cx: &LateContext<'_>, item: &hir::ImplItem<'_>) { if let hir::ImplItemKind::Fn(ref sig, _) = item.kind { let is_public = cx.access_levels.is_exported(item.def_id); let fn_header_span = item.span.with_hi(sig.decl.output.span().hi()); - if is_public && trait_ref_of_method(cx, item.hir_id()).is_none() { + if is_public && trait_ref_of_method(cx, item.def_id).is_none() { check_result_unit_err(cx, sig.decl, item.span, fn_header_span); } } diff --git a/src/tools/clippy/clippy_lints/src/inherent_to_string.rs b/src/tools/clippy/clippy_lints/src/inherent_to_string.rs index 60d234cd6f08f..55c04a1186fc3 100644 --- a/src/tools/clippy/clippy_lints/src/inherent_to_string.rs +++ b/src/tools/clippy/clippy_lints/src/inherent_to_string.rs @@ -116,7 +116,7 @@ impl<'tcx> LateLintPass<'tcx> for InherentToString { if is_type_diagnostic_item(cx, return_ty(cx, impl_item.hir_id()), sym::String); // Filters instances of to_string which are required by a trait - if trait_ref_of_method(cx, impl_item.hir_id()).is_none(); + if trait_ref_of_method(cx, impl_item.def_id).is_none(); then { show_lint(cx, impl_item); diff --git a/src/tools/clippy/clippy_lints/src/lifetimes.rs b/src/tools/clippy/clippy_lints/src/lifetimes.rs index 0e2b78609c2c1..6dd7b22ff94b5 100644 --- a/src/tools/clippy/clippy_lints/src/lifetimes.rs +++ b/src/tools/clippy/clippy_lints/src/lifetimes.rs @@ -91,7 +91,7 @@ impl<'tcx> LateLintPass<'tcx> for Lifetimes { fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx ImplItem<'_>) { if let ImplItemKind::Fn(ref sig, id) = item.kind { - let report_extra_lifetimes = trait_ref_of_method(cx, item.hir_id()).is_none(); + let report_extra_lifetimes = trait_ref_of_method(cx, item.def_id).is_none(); check_fn_inner( cx, sig.decl, diff --git a/src/tools/clippy/clippy_lints/src/loops/needless_range_loop.rs b/src/tools/clippy/clippy_lints/src/loops/needless_range_loop.rs index 172d9fc39a29f..33abd2a72d880 100644 --- a/src/tools/clippy/clippy_lints/src/loops/needless_range_loop.rs +++ b/src/tools/clippy/clippy_lints/src/loops/needless_range_loop.rs @@ -58,8 +58,7 @@ pub(super) fn check<'tcx>( // ensure that the indexed variable was declared before the loop, see #601 if let Some(indexed_extent) = indexed_extent { - let parent_id = cx.tcx.hir().get_parent_item(expr.hir_id); - let parent_def_id = cx.tcx.hir().local_def_id(parent_id); + let parent_def_id = cx.tcx.hir().get_parent_item(expr.hir_id); let region_scope_tree = cx.tcx.region_scope_tree(parent_def_id); let pat_extent = region_scope_tree.var_scope(pat.hir_id.local_id); if region_scope_tree.is_subscope_of(indexed_extent, pat_extent) { @@ -263,8 +262,7 @@ impl<'a, 'tcx> VarVisitor<'a, 'tcx> { let res = self.cx.qpath_res(seqpath, seqexpr.hir_id); match res { Res::Local(hir_id) => { - let parent_id = self.cx.tcx.hir().get_parent_item(expr.hir_id); - let parent_def_id = self.cx.tcx.hir().local_def_id(parent_id); + let parent_def_id = self.cx.tcx.hir().get_parent_item(expr.hir_id); let extent = self.cx.tcx.region_scope_tree(parent_def_id).var_scope(hir_id.local_id); if index_used_directly { self.indexed_directly.insert( diff --git a/src/tools/clippy/clippy_lints/src/methods/mod.rs b/src/tools/clippy/clippy_lints/src/methods/mod.rs index 1041f644e32eb..cd038ecd5ce1e 100644 --- a/src/tools/clippy/clippy_lints/src/methods/mod.rs +++ b/src/tools/clippy/clippy_lints/src/methods/mod.rs @@ -2053,7 +2053,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods { return; } let name = impl_item.ident.name.as_str(); - let parent = cx.tcx.hir().get_parent_did(impl_item.hir_id()); + let parent = cx.tcx.hir().get_parent_item(impl_item.hir_id()); let item = cx.tcx.hir().expect_item(parent); let self_ty = cx.tcx.type_of(item.def_id); diff --git a/src/tools/clippy/clippy_lints/src/missing_const_for_fn.rs b/src/tools/clippy/clippy_lints/src/missing_const_for_fn.rs index a8d410508563c..77849e1800f6b 100644 --- a/src/tools/clippy/clippy_lints/src/missing_const_for_fn.rs +++ b/src/tools/clippy/clippy_lints/src/missing_const_for_fn.rs @@ -121,7 +121,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingConstForFn { } }, FnKind::Method(_, sig, ..) => { - if trait_ref_of_method(cx, hir_id).is_some() + if trait_ref_of_method(cx, def_id).is_some() || already_const(sig.header) || method_accepts_dropable(cx, sig.decl.inputs) { diff --git a/src/tools/clippy/clippy_lints/src/mut_key.rs b/src/tools/clippy/clippy_lints/src/mut_key.rs index 5fe887a4573cc..1bdd805f65854 100644 --- a/src/tools/clippy/clippy_lints/src/mut_key.rs +++ b/src/tools/clippy/clippy_lints/src/mut_key.rs @@ -89,7 +89,7 @@ impl<'tcx> LateLintPass<'tcx> for MutableKeyType { fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::ImplItem<'tcx>) { if let hir::ImplItemKind::Fn(ref sig, ..) = item.kind { - if trait_ref_of_method(cx, item.hir_id()).is_none() { + if trait_ref_of_method(cx, item.def_id).is_none() { check_sig(cx, item.hir_id(), sig.decl); } } diff --git a/src/tools/clippy/clippy_lints/src/new_without_default.rs b/src/tools/clippy/clippy_lints/src/new_without_default.rs index f0c0c89ca8f3b..aec95530bba67 100644 --- a/src/tools/clippy/clippy_lints/src/new_without_default.rs +++ b/src/tools/clippy/clippy_lints/src/new_without_default.rs @@ -101,7 +101,7 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault { if sig.decl.inputs.is_empty(); if name == sym::new; if cx.access_levels.is_reachable(impl_item.def_id); - let self_def_id = cx.tcx.hir().local_def_id(cx.tcx.hir().get_parent_item(id)); + let self_def_id = cx.tcx.hir().get_parent_item(id); let self_ty = cx.tcx.type_of(self_def_id); if TyS::same_type(self_ty, return_ty(cx, id)); if let Some(default_trait_id) = cx.tcx.get_diagnostic_item(sym::Default); diff --git a/src/tools/clippy/clippy_lints/src/non_copy_const.rs b/src/tools/clippy/clippy_lints/src/non_copy_const.rs index 7d2ff083b7e07..21ac6548b0179 100644 --- a/src/tools/clippy/clippy_lints/src/non_copy_const.rs +++ b/src/tools/clippy/clippy_lints/src/non_copy_const.rs @@ -280,7 +280,7 @@ impl<'tcx> LateLintPass<'tcx> for NonCopyConst { fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx ImplItem<'_>) { if let ImplItemKind::Const(hir_ty, body_id) = &impl_item.kind { - let item_def_id = cx.tcx.hir().get_parent_did(impl_item.hir_id()); + let item_def_id = cx.tcx.hir().get_parent_item(impl_item.hir_id()); let item = cx.tcx.hir().expect_item(item_def_id); match &item.kind { diff --git a/src/tools/clippy/clippy_lints/src/ptr.rs b/src/tools/clippy/clippy_lints/src/ptr.rs index c08a19d520b60..63de117a6f1de 100644 --- a/src/tools/clippy/clippy_lints/src/ptr.rs +++ b/src/tools/clippy/clippy_lints/src/ptr.rs @@ -164,7 +164,7 @@ impl<'tcx> LateLintPass<'tcx> for Ptr { fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx ImplItem<'_>) { if let ImplItemKind::Fn(ref sig, body_id) = item.kind { let parent_item = cx.tcx.hir().get_parent_item(item.hir_id()); - if let Some(Node::Item(it)) = cx.tcx.hir().find(parent_item) { + if let Some(Node::Item(it)) = cx.tcx.hir().find_by_def_id(parent_item) { if let ItemKind::Impl(Impl { of_trait: Some(_), .. }) = it.kind { return; // ignore trait impls } diff --git a/src/tools/clippy/clippy_lints/src/self_named_constructors.rs b/src/tools/clippy/clippy_lints/src/self_named_constructors.rs index d386663e49858..9673d975cb4d3 100644 --- a/src/tools/clippy/clippy_lints/src/self_named_constructors.rs +++ b/src/tools/clippy/clippy_lints/src/self_named_constructors.rs @@ -51,7 +51,7 @@ impl<'tcx> LateLintPass<'tcx> for SelfNamedConstructors { _ => return, } - let parent = cx.tcx.hir().get_parent_did(impl_item.hir_id()); + let parent = cx.tcx.hir().get_parent_item(impl_item.hir_id()); let item = cx.tcx.hir().expect_item(parent); let self_ty = cx.tcx.type_of(item.def_id); let ret_ty = return_ty(cx, impl_item.hir_id()); diff --git a/src/tools/clippy/clippy_lints/src/suspicious_trait_impl.rs b/src/tools/clippy/clippy_lints/src/suspicious_trait_impl.rs index a3195de81d15c..92494159deeb5 100644 --- a/src/tools/clippy/clippy_lints/src/suspicious_trait_impl.rs +++ b/src/tools/clippy/clippy_lints/src/suspicious_trait_impl.rs @@ -66,7 +66,7 @@ impl<'tcx> LateLintPass<'tcx> for SuspiciousImpl { // Check for more than one binary operation in the implemented function // Linting when multiple operations are involved can result in false positives let parent_fn = cx.tcx.hir().get_parent_item(expr.hir_id); - if let hir::Node::ImplItem(impl_item) = cx.tcx.hir().get(parent_fn); + if let hir::Node::ImplItem(impl_item) = cx.tcx.hir().get_by_def_id(parent_fn); if let hir::ImplItemKind::Fn(_, body_id) = impl_item.kind; let body = cx.tcx.hir().body(body_id); let parent_fn = cx.tcx.hir().get_parent_item(expr.hir_id); diff --git a/src/tools/clippy/clippy_lints/src/types/mod.rs b/src/tools/clippy/clippy_lints/src/types/mod.rs index 9d57505e55ed6..67cc891331896 100644 --- a/src/tools/clippy/clippy_lints/src/types/mod.rs +++ b/src/tools/clippy/clippy_lints/src/types/mod.rs @@ -312,12 +312,12 @@ impl_lint_pass!(Types => [BOX_COLLECTION, VEC_BOX, OPTION_OPTION, LINKEDLIST, BO impl<'tcx> LateLintPass<'tcx> for Types { fn check_fn(&mut self, cx: &LateContext<'_>, _: FnKind<'_>, decl: &FnDecl<'_>, _: &Body<'_>, _: Span, id: HirId) { - let is_in_trait_impl = if let Some(hir::Node::Item(item)) = cx.tcx.hir().find(cx.tcx.hir().get_parent_item(id)) - { - matches!(item.kind, ItemKind::Impl(hir::Impl { of_trait: Some(_), .. })) - } else { - false - }; + let is_in_trait_impl = + if let Some(hir::Node::Item(item)) = cx.tcx.hir().find_by_def_id(cx.tcx.hir().get_parent_item(id)) { + matches!(item.kind, ItemKind::Impl(hir::Impl { of_trait: Some(_), .. })) + } else { + false + }; let is_exported = cx.access_levels.is_exported(cx.tcx.hir().local_def_id(id)); @@ -353,7 +353,7 @@ impl<'tcx> LateLintPass<'tcx> for Types { match item.kind { ImplItemKind::Const(ty, _) => { let is_in_trait_impl = if let Some(hir::Node::Item(item)) = - cx.tcx.hir().find(cx.tcx.hir().get_parent_item(item.hir_id())) + cx.tcx.hir().find_by_def_id(cx.tcx.hir().get_parent_item(item.hir_id())) { matches!(item.kind, ItemKind::Impl(hir::Impl { of_trait: Some(_), .. })) } else { diff --git a/src/tools/clippy/clippy_lints/src/unused_self.rs b/src/tools/clippy/clippy_lints/src/unused_self.rs index aa105580ee354..fd9d5b52e501f 100644 --- a/src/tools/clippy/clippy_lints/src/unused_self.rs +++ b/src/tools/clippy/clippy_lints/src/unused_self.rs @@ -42,7 +42,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedSelf { if impl_item.span.from_expansion() { return; } - let parent = cx.tcx.hir().get_parent_did(impl_item.hir_id()); + let parent = cx.tcx.hir().get_parent_item(impl_item.hir_id()); let parent_item = cx.tcx.hir().expect_item(parent); let assoc_item = cx.tcx.associated_item(impl_item.def_id); if_chain! { diff --git a/src/tools/clippy/clippy_lints/src/zero_sized_map_values.rs b/src/tools/clippy/clippy_lints/src/zero_sized_map_values.rs index eb8436a501d54..70b0560e67604 100644 --- a/src/tools/clippy/clippy_lints/src/zero_sized_map_values.rs +++ b/src/tools/clippy/clippy_lints/src/zero_sized_map_values.rs @@ -69,7 +69,11 @@ impl LateLintPass<'_> for ZeroSizedMapValues { fn in_trait_impl(cx: &LateContext<'_>, hir_id: HirId) -> bool { let parent_id = cx.tcx.hir().get_parent_item(hir_id); - if let Some(Node::Item(item)) = cx.tcx.hir().find(cx.tcx.hir().get_parent_item(parent_id)) { + let second_parent_id = cx + .tcx + .hir() + .get_parent_item(cx.tcx.hir().local_def_id_to_hir_id(parent_id)); + if let Some(Node::Item(item)) = cx.tcx.hir().find_by_def_id(second_parent_id) { if let ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }) = item.kind { return true; } diff --git a/src/tools/clippy/clippy_utils/src/lib.rs b/src/tools/clippy/clippy_utils/src/lib.rs index 57183b58b2a11..e55817f13eb1a 100644 --- a/src/tools/clippy/clippy_utils/src/lib.rs +++ b/src/tools/clippy/clippy_utils/src/lib.rs @@ -70,7 +70,7 @@ use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::unhash::UnhashMap; use rustc_hir as hir; use rustc_hir::def::{DefKind, Res}; -use rustc_hir::def_id::{CrateNum, DefId}; +use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_ID}; use rustc_hir::hir_id::{HirIdMap, HirIdSet}; use rustc_hir::intravisit::{walk_expr, ErasedMap, FnKind, NestedVisitorMap, Visitor}; use rustc_hir::itemlikevisit::ItemLikeVisitor; @@ -90,7 +90,6 @@ use rustc_middle::ty::binding::BindingMode; use rustc_middle::ty::{layout::IntegerExt, BorrowKind, DefIdTree, Ty, TyCtxt, TypeAndMut, TypeFoldable, UpvarCapture}; use rustc_semver::RustcVersion; use rustc_session::Session; -use rustc_span::def_id::LocalDefId; use rustc_span::hygiene::{ExpnKind, MacroKind}; use rustc_span::source_map::original_sp; use rustc_span::sym; @@ -216,7 +215,7 @@ pub fn find_binding_init<'tcx>(cx: &LateContext<'tcx>, hir_id: HirId) -> Option< /// ``` pub fn in_constant(cx: &LateContext<'_>, id: HirId) -> bool { let parent_id = cx.tcx.hir().get_parent_item(id); - match cx.tcx.hir().get(parent_id) { + match cx.tcx.hir().get_by_def_id(parent_id) { Node::Item(&Item { kind: ItemKind::Const(..) | ItemKind::Static(..), .. @@ -607,12 +606,13 @@ pub fn get_trait_def_id(cx: &LateContext<'_>, path: &[&str]) -> Option { /// } /// } /// ``` -pub fn trait_ref_of_method<'tcx>(cx: &LateContext<'tcx>, hir_id: HirId) -> Option<&'tcx TraitRef<'tcx>> { +pub fn trait_ref_of_method<'tcx>(cx: &LateContext<'tcx>, def_id: LocalDefId) -> Option<&'tcx TraitRef<'tcx>> { // Get the implemented trait for the current function + let hir_id = cx.tcx.hir().local_def_id_to_hir_id(def_id); let parent_impl = cx.tcx.hir().get_parent_item(hir_id); if_chain! { - if parent_impl != hir::CRATE_HIR_ID; - if let hir::Node::Item(item) = cx.tcx.hir().get(parent_impl); + if parent_impl != CRATE_DEF_ID; + if let hir::Node::Item(item) = cx.tcx.hir().get_by_def_id(parent_impl); if let hir::ItemKind::Impl(impl_) = &item.kind; then { return impl_.of_trait.as_ref(); } } @@ -1122,14 +1122,13 @@ pub fn is_entrypoint_fn(cx: &LateContext<'_>, def_id: DefId) -> bool { /// Returns `true` if the expression is in the program's `#[panic_handler]`. pub fn is_in_panic_handler(cx: &LateContext<'_>, e: &Expr<'_>) -> bool { let parent = cx.tcx.hir().get_parent_item(e.hir_id); - let def_id = cx.tcx.hir().local_def_id(parent).to_def_id(); - Some(def_id) == cx.tcx.lang_items().panic_impl() + Some(parent.to_def_id()) == cx.tcx.lang_items().panic_impl() } /// Gets the name of the item the expression is in, if available. pub fn get_item_name(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option { let parent_id = cx.tcx.hir().get_parent_item(expr.hir_id); - match cx.tcx.hir().find(parent_id) { + match cx.tcx.hir().find_by_def_id(parent_id) { Some( Node::Item(Item { ident, .. }) | Node::TraitItem(TraitItem { ident, .. }) @@ -1639,7 +1638,7 @@ pub fn any_parent_has_attr(tcx: TyCtxt<'_>, node: HirId, symbol: Symbol) -> bool return true; } prev_enclosing_node = Some(enclosing_node); - enclosing_node = map.get_parent_item(enclosing_node); + enclosing_node = map.local_def_id_to_hir_id(map.get_parent_item(enclosing_node)); } false