From 5a123c265b7bf47fb7894c9e338f531fb463b4d8 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 5 Dec 2021 11:18:16 +0100 Subject: [PATCH 1/9] Add fast path to `opt_local_def_id`. --- compiler/rustc_middle/src/hir/map/mod.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index 394a1fc227095..30c75f35fc3cc 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -200,8 +200,12 @@ impl<'hir> Map<'hir> { #[inline] pub fn opt_local_def_id(&self, hir_id: HirId) -> Option { - // FIXME(#85914) is this access safe for incr. comp.? - self.tcx.untracked_resolutions.definitions.opt_hir_id_to_local_def_id(hir_id) + if hir_id.local_id == ItemLocalId::new(0) { + Some(hir_id.owner) + } else { + // FIXME(#85914) is this access safe for incr. comp.? + self.tcx.untracked_resolutions.definitions.opt_hir_id_to_local_def_id(hir_id) + } } #[inline] From 60064726aee0d05c41bf6ff0eac4699f47762b05 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Thu, 21 Oct 2021 19:41:47 +0200 Subject: [PATCH 2/9] 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 From c9de7d7a20c11d2df26a6580a964f5755a08ad15 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 5 Dec 2021 13:07:51 +0100 Subject: [PATCH 3/9] Use LocalDefId in rustc_passes::entry. --- compiler/rustc_passes/src/entry.rs | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/compiler/rustc_passes/src/entry.rs b/compiler/rustc_passes/src/entry.rs index 63f9b3ed6b15c..8ab6c4eeae500 100644 --- a/compiler/rustc_passes/src/entry.rs +++ b/compiler/rustc_passes/src/entry.rs @@ -1,8 +1,8 @@ use rustc_ast::entry::EntryPointType; use rustc_errors::struct_span_err; -use rustc_hir::def_id::{DefId, CRATE_DEF_ID, CRATE_DEF_INDEX, LOCAL_CRATE}; +use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID, CRATE_DEF_INDEX, LOCAL_CRATE}; use rustc_hir::itemlikevisit::ItemLikeVisitor; -use rustc_hir::{ForeignItem, HirId, ImplItem, Item, ItemKind, Node, TraitItem, CRATE_HIR_ID}; +use rustc_hir::{ForeignItem, ImplItem, Item, ItemKind, Node, TraitItem, CRATE_HIR_ID}; use rustc_middle::hir::map::Map; use rustc_middle::ty::query::Providers; use rustc_middle::ty::TyCtxt; @@ -18,14 +18,14 @@ struct EntryContext<'a, 'tcx> { map: Map<'tcx>, /// The function that has attribute named `main`. - attr_main_fn: Option<(HirId, Span)>, + attr_main_fn: Option<(LocalDefId, Span)>, /// The function that has the attribute 'start' on it. - start_fn: Option<(HirId, Span)>, + start_fn: Option<(LocalDefId, Span)>, /// The functions that one might think are `main` but aren't, e.g. /// main functions not defined at the top level. For diagnostics. - non_main_fns: Vec<(HirId, Span)>, + non_main_fns: Vec, } impl<'a, 'tcx> ItemLikeVisitor<'tcx> for EntryContext<'a, 'tcx> { @@ -112,11 +112,11 @@ fn find_item(item: &Item<'_>, ctxt: &mut EntryContext<'_, '_>, at_root: bool) { } EntryPointType::MainNamed => (), EntryPointType::OtherMain => { - ctxt.non_main_fns.push((item.hir_id(), item.span)); + ctxt.non_main_fns.push(item.span); } EntryPointType::MainAttr => { if ctxt.attr_main_fn.is_none() { - ctxt.attr_main_fn = Some((item.hir_id(), item.span)); + ctxt.attr_main_fn = Some((item.def_id, item.span)); } else { struct_span_err!( ctxt.session, @@ -131,7 +131,7 @@ fn find_item(item: &Item<'_>, ctxt: &mut EntryContext<'_, '_>, at_root: bool) { } EntryPointType::Start => { if ctxt.start_fn.is_none() { - ctxt.start_fn = Some((item.hir_id(), item.span)); + ctxt.start_fn = Some((item.def_id, item.span)); } else { struct_span_err!(ctxt.session, item.span, E0138, "multiple `start` functions") .span_label(ctxt.start_fn.unwrap().1, "previous `#[start]` function here") @@ -143,16 +143,16 @@ fn find_item(item: &Item<'_>, ctxt: &mut EntryContext<'_, '_>, at_root: bool) { } fn configure_main(tcx: TyCtxt<'_>, visitor: &EntryContext<'_, '_>) -> Option<(DefId, EntryFnType)> { - if let Some((hir_id, _)) = visitor.start_fn { - Some((tcx.hir().local_def_id(hir_id).to_def_id(), EntryFnType::Start)) - } else if let Some((hir_id, _)) = visitor.attr_main_fn { - Some((tcx.hir().local_def_id(hir_id).to_def_id(), EntryFnType::Main)) + if let Some((def_id, _)) = visitor.start_fn { + Some((def_id.to_def_id(), EntryFnType::Start)) + } else if let Some((def_id, _)) = visitor.attr_main_fn { + Some((def_id.to_def_id(), EntryFnType::Main)) } else { if let Some(main_def) = tcx.resolutions(()).main_def { if let Some(def_id) = main_def.opt_fn_def_id() { // non-local main imports are handled below - if def_id.is_local() { - let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); + if let Some(def_id) = def_id.as_local() { + let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); if matches!(tcx.hir().find(hir_id), Some(Node::ForeignItem(_))) { tcx.sess .struct_span_err( @@ -201,7 +201,7 @@ fn no_main_err(tcx: TyCtxt<'_>, visitor: &EntryContext<'_, '_>) { ); let filename = &tcx.sess.local_crate_source_file; let note = if !visitor.non_main_fns.is_empty() { - for &(_, span) in &visitor.non_main_fns { + for &span in &visitor.non_main_fns { err.span_note(span, "here is a function named `main`"); } err.note("you have one or more functions named `main` not defined at the crate level"); From a5b7e235d3b6e3bb8406d182cc0280de1844c6d9 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 5 Dec 2021 13:08:24 +0100 Subject: [PATCH 4/9] Simplify DefIdForest. --- .../rustc_middle/src/ty/inhabitedness/def_id_forest.rs | 8 ++++---- compiler/rustc_middle/src/ty/inhabitedness/mod.rs | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_middle/src/ty/inhabitedness/def_id_forest.rs b/compiler/rustc_middle/src/ty/inhabitedness/def_id_forest.rs index f31c7dd743d82..55807874705f6 100644 --- a/compiler/rustc_middle/src/ty/inhabitedness/def_id_forest.rs +++ b/compiler/rustc_middle/src/ty/inhabitedness/def_id_forest.rs @@ -1,6 +1,6 @@ use crate::ty::context::TyCtxt; use crate::ty::{DefId, DefIdTree}; -use rustc_hir::CRATE_HIR_ID; +use rustc_span::def_id::CRATE_DEF_ID; use smallvec::SmallVec; use std::mem; use std::sync::Arc; @@ -43,8 +43,8 @@ impl<'tcx> DefIdForest { /// Creates a forest consisting of a single tree representing the entire /// crate. #[inline] - pub fn full(tcx: TyCtxt<'tcx>) -> DefIdForest { - DefIdForest::from_id(tcx.hir().local_def_id(CRATE_HIR_ID).to_def_id()) + pub fn full() -> DefIdForest { + DefIdForest::from_id(CRATE_DEF_ID.to_def_id()) } /// Creates a forest containing a `DefId` and all its descendants. @@ -96,7 +96,7 @@ impl<'tcx> DefIdForest { let mut ret: SmallVec<[_; 1]> = if let Some(first) = iter.next() { SmallVec::from_slice(first.as_slice()) } else { - return DefIdForest::full(tcx); + return DefIdForest::full(); }; let mut next_ret: SmallVec<[_; 1]> = SmallVec::new(); diff --git a/compiler/rustc_middle/src/ty/inhabitedness/mod.rs b/compiler/rustc_middle/src/ty/inhabitedness/mod.rs index 77d82ee6eae6f..167a54e42a015 100644 --- a/compiler/rustc_middle/src/ty/inhabitedness/mod.rs +++ b/compiler/rustc_middle/src/ty/inhabitedness/mod.rs @@ -205,7 +205,7 @@ pub(crate) fn type_uninhabited_from<'tcx>( match *ty.kind() { Adt(def, substs) => def.uninhabited_from(tcx, substs, param_env), - Never => DefIdForest::full(tcx), + Never => DefIdForest::full(), Tuple(ref tys) => DefIdForest::union( tcx, From 79afe99973f15bb0daee8c8554cd3d8049b602b2 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 5 Dec 2021 13:08:52 +0100 Subject: [PATCH 5/9] Use LocalDefId in rustc_passes::hir_id_validator. --- compiler/rustc_passes/src/hir_id_validator.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_passes/src/hir_id_validator.rs b/compiler/rustc_passes/src/hir_id_validator.rs index 0e60ca9f90010..d8c984c06764e 100644 --- a/compiler/rustc_passes/src/hir_id_validator.rs +++ b/compiler/rustc_passes/src/hir_id_validator.rs @@ -57,22 +57,22 @@ impl<'a, 'hir> OuterVisitor<'a, 'hir> { impl<'a, 'hir> ItemLikeVisitor<'hir> for OuterVisitor<'a, 'hir> { fn visit_item(&mut self, i: &'hir hir::Item<'hir>) { let mut inner_visitor = self.new_inner_visitor(self.hir_map); - inner_visitor.check(i.hir_id(), |this| intravisit::walk_item(this, i)); + inner_visitor.check(i.def_id, |this| intravisit::walk_item(this, i)); } fn visit_trait_item(&mut self, i: &'hir hir::TraitItem<'hir>) { let mut inner_visitor = self.new_inner_visitor(self.hir_map); - inner_visitor.check(i.hir_id(), |this| intravisit::walk_trait_item(this, i)); + inner_visitor.check(i.def_id, |this| intravisit::walk_trait_item(this, i)); } fn visit_impl_item(&mut self, i: &'hir hir::ImplItem<'hir>) { let mut inner_visitor = self.new_inner_visitor(self.hir_map); - inner_visitor.check(i.hir_id(), |this| intravisit::walk_impl_item(this, i)); + inner_visitor.check(i.def_id, |this| intravisit::walk_impl_item(this, i)); } fn visit_foreign_item(&mut self, i: &'hir hir::ForeignItem<'hir>) { let mut inner_visitor = self.new_inner_visitor(self.hir_map); - inner_visitor.check(i.hir_id(), |this| intravisit::walk_foreign_item(this, i)); + inner_visitor.check(i.def_id, |this| intravisit::walk_foreign_item(this, i)); } } @@ -83,9 +83,8 @@ impl<'a, 'hir> HirIdValidator<'a, 'hir> { self.errors.lock().push(f()); } - fn check)>(&mut self, hir_id: HirId, walk: F) { + fn check)>(&mut self, owner: LocalDefId, walk: F) { assert!(self.owner.is_none()); - let owner = self.hir_map.local_def_id(hir_id); self.owner = Some(owner); walk(self); From ebcc847369d5b050cdde870b2261e34afed8679f Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 5 Dec 2021 13:09:56 +0100 Subject: [PATCH 6/9] Make ty_param_owner return a LocalDefId. --- compiler/rustc_middle/src/hir/map/mod.rs | 8 +++++--- compiler/rustc_typeck/src/check/fn_ctxt/mod.rs | 3 +-- compiler/rustc_typeck/src/collect.rs | 5 ++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index b346c2bb6364f..73829151bb1a9 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -546,10 +546,12 @@ impl<'hir> Map<'hir> { }); } - pub fn ty_param_owner(&self, id: HirId) -> HirId { + pub fn ty_param_owner(&self, id: HirId) -> LocalDefId { match self.get(id) { - Node::Item(&Item { kind: ItemKind::Trait(..) | ItemKind::TraitAlias(..), .. }) => id, - Node::GenericParam(_) => self.get_parent_node(id), + Node::Item(&Item { kind: ItemKind::Trait(..) | ItemKind::TraitAlias(..), .. }) => { + id.expect_owner() + } + Node::GenericParam(_) => self.get_parent_item(id), _ => bug!("ty_param_owner: {} not a type parameter", self.node_to_string(id)), } } diff --git a/compiler/rustc_typeck/src/check/fn_ctxt/mod.rs b/compiler/rustc_typeck/src/check/fn_ctxt/mod.rs index 9c70d2cb365b2..3a81af0316286 100644 --- a/compiler/rustc_typeck/src/check/fn_ctxt/mod.rs +++ b/compiler/rustc_typeck/src/check/fn_ctxt/mod.rs @@ -188,8 +188,7 @@ impl<'a, 'tcx> AstConv<'tcx> for FnCtxt<'a, 'tcx> { ) -> ty::GenericPredicates<'tcx> { let tcx = self.tcx; let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); - let item_id = tcx.hir().ty_param_owner(hir_id); - let item_def_id = tcx.hir().local_def_id(item_id); + let item_def_id = tcx.hir().ty_param_owner(hir_id); let generics = tcx.generics_of(item_def_id); let index = generics.param_def_id_to_index[&def_id]; ty::GenericPredicates { diff --git a/compiler/rustc_typeck/src/collect.rs b/compiler/rustc_typeck/src/collect.rs index 5c1c9c4791cc2..85d81cce100f8 100644 --- a/compiler/rustc_typeck/src/collect.rs +++ b/compiler/rustc_typeck/src/collect.rs @@ -569,13 +569,12 @@ fn type_param_predicates( let param_id = tcx.hir().local_def_id_to_hir_id(def_id); let param_owner = tcx.hir().ty_param_owner(param_id); - let param_owner_def_id = tcx.hir().local_def_id(param_owner); - let generics = tcx.generics_of(param_owner_def_id); + let generics = tcx.generics_of(param_owner); let index = generics.param_def_id_to_index[&def_id.to_def_id()]; let ty = tcx.mk_ty_param(index, tcx.hir().ty_param_name(param_id)); // Don't look for bounds where the type parameter isn't in scope. - let parent = if item_def_id == param_owner_def_id.to_def_id() { + let parent = if item_def_id == param_owner.to_def_id() { None } else { tcx.generics_of(item_def_id).parent From 67727aa7c31a24ea73a91a9134c3653fae8209ab Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Wed, 20 Oct 2021 20:59:15 +0200 Subject: [PATCH 7/9] Reduce use of local_def_id_to_hir_id. --- .../src/diagnostics/conflict_errors.rs | 5 +- .../src/diagnostics/mutability_errors.rs | 66 +++++++-------- .../src/back/symbol_export.rs | 2 +- .../src/const_eval/fn_queries.rs | 13 +-- .../src/transform/check_consts/check.rs | 3 +- .../src/transform/check_consts/qualifs.rs | 3 +- .../src/persist/dirty_clean.rs | 3 +- .../src/infer/error_reporting/mod.rs | 23 ++---- compiler/rustc_lint/src/builtin.rs | 5 +- compiler/rustc_metadata/src/rmeta/encoder.rs | 14 ++-- compiler/rustc_middle/src/hir/map/mod.rs | 3 +- compiler/rustc_middle/src/mir/mod.rs | 6 +- compiler/rustc_middle/src/mir/mono.rs | 12 +-- compiler/rustc_middle/src/mir/spanview.rs | 4 +- compiler/rustc_middle/src/ty/consts.rs | 7 +- compiler/rustc_middle/src/ty/context.rs | 7 +- compiler/rustc_middle/src/ty/mod.rs | 5 +- compiler/rustc_middle/src/ty/print/pretty.rs | 6 +- .../src/build/expr/as_place.rs | 7 +- compiler/rustc_mir_build/src/lints.rs | 3 +- .../src/thir/pattern/const_to_pat.rs | 54 ++++++------- .../rustc_mir_transform/src/const_prop.rs | 6 +- .../rustc_mir_transform/src/coverage/mod.rs | 4 +- compiler/rustc_mir_transform/src/lib.rs | 3 +- compiler/rustc_monomorphize/src/util.rs | 3 +- compiler/rustc_passes/src/dead.rs | 4 +- compiler/rustc_passes/src/entry.rs | 5 +- compiler/rustc_passes/src/lib.rs | 1 + compiler/rustc_passes/src/reachable.rs | 16 ++-- compiler/rustc_resolve/src/late/lifetimes.rs | 80 +++++++++---------- compiler/rustc_symbol_mangling/src/lib.rs | 3 +- .../src/traits/error_reporting/mod.rs | 6 +- .../src/traits/structural_match.rs | 1 - .../rustc_ty_utils/src/representability.rs | 7 +- compiler/rustc_ty_utils/src/ty.rs | 7 +- compiler/rustc_typeck/src/check/check.rs | 2 +- .../rustc_typeck/src/check/compare_method.rs | 12 ++- compiler/rustc_typeck/src/check/dropck.rs | 4 +- compiler/rustc_typeck/src/check/wfcheck.rs | 4 +- 39 files changed, 182 insertions(+), 237 deletions(-) diff --git a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs index a24b7cff9e75c..ba111d394ec26 100644 --- a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs @@ -409,8 +409,9 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { let generics = tcx.generics_of(self.mir_def_id()); let param = generics.type_param(¶m_ty, tcx); if let Some(generics) = tcx - .hir() - .get_generics(tcx.typeck_root_def_id(self.mir_def_id().to_def_id())) + .typeck_root_def_id(self.mir_def_id().to_def_id()) + .as_local() + .and_then(|def_id| tcx.hir().get_generics(def_id)) { suggest_constraining_type_param( tcx, diff --git a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs index 2b23a0475fe59..8f4e574fbd618 100644 --- a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs @@ -628,42 +628,39 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { }; ( true, - td.as_local().and_then(|tld| { - let h = hir_map.local_def_id_to_hir_id(tld); - match hir_map.find(h) { - Some(Node::Item(hir::Item { - kind: hir::ItemKind::Trait(_, _, _, _, items), - .. - })) => { - let mut f_in_trait_opt = None; - for hir::TraitItemRef { id: fi, kind: k, .. } in *items { - let hi = fi.hir_id(); - if !matches!(k, hir::AssocItemKind::Fn { .. }) { - continue; - } - if hir_map.name(hi) != hir_map.name(my_hir) { - continue; - } - f_in_trait_opt = Some(hi); - break; + td.as_local().and_then(|tld| match hir_map.find_by_def_id(tld) { + Some(Node::Item(hir::Item { + kind: hir::ItemKind::Trait(_, _, _, _, items), + .. + })) => { + let mut f_in_trait_opt = None; + for hir::TraitItemRef { id: fi, kind: k, .. } in *items { + let hi = fi.hir_id(); + if !matches!(k, hir::AssocItemKind::Fn { .. }) { + continue; } - f_in_trait_opt.and_then(|f_in_trait| match hir_map.find(f_in_trait) { - Some(Node::TraitItem(hir::TraitItem { - kind: - hir::TraitItemKind::Fn( - hir::FnSig { decl: hir::FnDecl { inputs, .. }, .. }, - _, - ), - .. - })) => { - let hir::Ty { span, .. } = inputs[local.index() - 1]; - Some(span) - } - _ => None, - }) + if hir_map.name(hi) != hir_map.name(my_hir) { + continue; + } + f_in_trait_opt = Some(hi); + break; } - _ => None, + f_in_trait_opt.and_then(|f_in_trait| match hir_map.find(f_in_trait) { + Some(Node::TraitItem(hir::TraitItem { + kind: + hir::TraitItemKind::Fn( + hir::FnSig { decl: hir::FnDecl { inputs, .. }, .. }, + _, + ), + .. + })) => { + let hir::Ty { span, .. } = inputs[local.index() - 1]; + Some(span) + } + _ => None, + }) } + _ => None, }), ) } @@ -1075,8 +1072,7 @@ fn get_mut_span_in_struct_field<'tcx>( if let ty::Adt(def, _) = ty.kind() { let field = def.all_fields().nth(field.index())?; // Use the HIR types to construct the diagnostic message. - let hir_id = tcx.hir().local_def_id_to_hir_id(field.did.as_local()?); - let node = tcx.hir().find(hir_id)?; + let node = tcx.hir().find_by_def_id(field.did.as_local()?)?; // Now we're dealing with the actual struct that we're going to suggest a change to, // we can expect a field that is an immutable reference to a type. if let hir::Node::Field(field) = node { diff --git a/compiler/rustc_codegen_ssa/src/back/symbol_export.rs b/compiler/rustc_codegen_ssa/src/back/symbol_export.rs index baafa74b13146..aeddd926896e8 100644 --- a/compiler/rustc_codegen_ssa/src/back/symbol_export.rs +++ b/compiler/rustc_codegen_ssa/src/back/symbol_export.rs @@ -76,7 +76,7 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, cnum: CrateNum) -> DefIdMap< // // As a result, if this id is an FFI item (foreign item) then we only // let it through if it's included statically. - match tcx.hir().get(tcx.hir().local_def_id_to_hir_id(def_id)) { + match tcx.hir().get_by_def_id(def_id) { Node::ForeignItem(..) => { tcx.is_statically_included_foreign_item(def_id).then_some(def_id) } diff --git a/compiler/rustc_const_eval/src/const_eval/fn_queries.rs b/compiler/rustc_const_eval/src/const_eval/fn_queries.rs index 821b048eb9bcf..05fbbf45d7c24 100644 --- a/compiler/rustc_const_eval/src/const_eval/fn_queries.rs +++ b/compiler/rustc_const_eval/src/const_eval/fn_queries.rs @@ -1,5 +1,5 @@ use rustc_hir as hir; -use rustc_hir::def_id::DefId; +use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_middle::ty::query::Providers; use rustc_middle::ty::TyCtxt; use rustc_span::symbol::Symbol; @@ -15,7 +15,8 @@ pub fn is_unstable_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> Option { } } -pub fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, hir_id: hir::HirId) -> bool { +pub fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool { + let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); let parent_id = tcx.hir().get_parent_node(hir_id); matches!( tcx.hir().get(parent_id), @@ -29,15 +30,15 @@ pub fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, hir_id: hir::HirId) -> bool { /// Checks whether the function has a `const` modifier or, in case it is an intrinsic, whether /// said intrinsic has a `rustc_const_{un,}stable` attribute. fn is_const_fn_raw(tcx: TyCtxt<'_>, def_id: DefId) -> bool { - let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); - - let node = tcx.hir().get(hir_id); + let def_id = def_id.expect_local(); + let node = tcx.hir().get_by_def_id(def_id); if let hir::Node::ForeignItem(hir::ForeignItem { kind: hir::ForeignItemKind::Fn(..), .. }) = node { // Intrinsics use `rustc_const_{un,}stable` attributes to indicate constness. All other // foreign items cannot be evaluated at compile-time. + let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); if let Abi::RustIntrinsic | Abi::PlatformIntrinsic = tcx.hir().get_foreign_abi(hir_id) { tcx.lookup_const_stability(def_id).is_some() } else { @@ -50,7 +51,7 @@ fn is_const_fn_raw(tcx: TyCtxt<'_>, def_id: DefId) -> bool { // If the function itself is not annotated with `const`, it may still be a `const fn` // if it resides in a const trait impl. - is_parent_const_impl_raw(tcx, hir_id) + is_parent_const_impl_raw(tcx, def_id) } else { matches!(node, hir::Node::Ctor(_)) } diff --git a/compiler/rustc_const_eval/src/transform/check_consts/check.rs b/compiler/rustc_const_eval/src/transform/check_consts/check.rs index de4824eb667c4..1daade8f4004b 100644 --- a/compiler/rustc_const_eval/src/transform/check_consts/check.rs +++ b/compiler/rustc_const_eval/src/transform/check_consts/check.rs @@ -221,8 +221,7 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> { // Prevent const trait methods from being annotated as `stable`. // FIXME: Do this as part of stability checking. if self.is_const_stable_const_fn() { - let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); - if crate::const_eval::is_parent_const_impl_raw(tcx, hir_id) { + if crate::const_eval::is_parent_const_impl_raw(tcx, def_id) { self.ccx .tcx .sess diff --git a/compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs b/compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs index 27f2da34262a1..a3664493e1883 100644 --- a/compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs +++ b/compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs @@ -210,8 +210,7 @@ impl Qualif for CustomEq { // because that component may be part of an enum variant (e.g., // `Option::::Some`), in which case some values of this type may be // structural-match (`Option::None`). - let id = cx.tcx.hir().local_def_id_to_hir_id(cx.def_id()); - traits::search_for_structural_match_violation(id, cx.body.span, cx.tcx, ty).is_some() + traits::search_for_structural_match_violation(cx.body.span, cx.tcx, ty).is_some() } fn in_adt_inherently<'tcx>( diff --git a/compiler/rustc_incremental/src/persist/dirty_clean.rs b/compiler/rustc_incremental/src/persist/dirty_clean.rs index 88795679943fb..7676ff3c41cc7 100644 --- a/compiler/rustc_incremental/src/persist/dirty_clean.rs +++ b/compiler/rustc_incremental/src/persist/dirty_clean.rs @@ -223,8 +223,7 @@ impl<'tcx> DirtyCleanVisitor<'tcx> { /// Return all DepNode labels that should be asserted for this item. /// index=0 is the "name" used for error messages fn auto_labels(&mut self, item_id: LocalDefId, attr: &Attribute) -> (&'static str, Labels) { - let hir_id = self.tcx.hir().local_def_id_to_hir_id(item_id); - let node = self.tcx.hir().get(hir_id); + let node = self.tcx.hir().get_by_def_id(item_id); let (name, labels) = match node { HirNode::Item(item) => { match item.kind { diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index e855ad2f02fd9..ece7ca9df18e5 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -151,11 +151,10 @@ fn msg_span_from_early_bound_and_free_regions<'tcx>( ) -> (String, Span) { let sm = tcx.sess.source_map(); - let scope = region.free_region_binding_scope(tcx); - let node = tcx.hir().local_def_id_to_hir_id(scope.expect_local()); + let scope = region.free_region_binding_scope(tcx).expect_local(); match *region { ty::ReEarlyBound(ref br) => { - let mut sp = sm.guess_head_span(tcx.hir().span(node)); + let mut sp = sm.guess_head_span(tcx.def_span(scope)); if let Some(param) = tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(br.name)) { @@ -166,7 +165,7 @@ fn msg_span_from_early_bound_and_free_regions<'tcx>( ty::ReFree(ty::FreeRegion { bound_region: ty::BoundRegionKind::BrNamed(_, name), .. }) => { - let mut sp = sm.guess_head_span(tcx.hir().span(node)); + let mut sp = sm.guess_head_span(tcx.def_span(scope)); if let Some(param) = tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(name)) { @@ -181,13 +180,13 @@ fn msg_span_from_early_bound_and_free_regions<'tcx>( } else { ( format!("the anonymous lifetime #{} defined here", idx + 1), - tcx.hir().span(node), + tcx.def_span(scope), ) } } _ => ( format!("the lifetime `{}` as defined here", region), - sm.guess_head_span(tcx.hir().span(node)), + sm.guess_head_span(tcx.def_span(scope)), ), }, _ => bug!(), @@ -1759,8 +1758,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { if let Some(ValuePairs::PolyTraitRefs(exp_found)) = values { if let ty::Closure(def_id, _) = exp_found.expected.skip_binder().self_ty().kind() { if let Some(def_id) = def_id.as_local() { - let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id); - let span = self.tcx.hir().span(hir_id); + let span = self.tcx.def_span(def_id); diag.span_note(span, "this closure does not fulfill the lifetime requirements"); } } @@ -2245,7 +2243,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { if let Node::GenericParam(param) = hir.get(id) { has_bounds = !param.bounds.is_empty(); } - let sp = hir.span(id); + let sp = self.tcx.def_span(def_id); // `sp` only covers `T`, change it so that it covers // `T:` when appropriate let is_impl_trait = bound_kind.to_string().starts_with("impl "); @@ -2291,12 +2289,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { .as_ref() .and_then(|(_, g, _)| g.params.first()) .and_then(|param| param.def_id.as_local()) - .map(|def_id| { - ( - hir.span(hir.local_def_id_to_hir_id(def_id)).shrink_to_lo(), - format!("{}, ", new_lt), - ) - }); + .map(|def_id| (self.tcx.def_span(def_id).shrink_to_lo(), format!("{}, ", new_lt))); let labeled_user_string = match bound_kind { GenericKind::Param(ref p) => format!("the parameter type `{}`", p), diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index edfff3dae65d5..b73f403360a00 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -610,8 +610,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc { // reported for missing docs. let real_trait = trait_ref.path.res.def_id(); let Some(def_id) = real_trait.as_local() else { return }; - let hir_id = cx.tcx.hir().local_def_id_to_hir_id(def_id); - let Some(Node::Item(item)) = cx.tcx.hir().find(hir_id) else { return }; + let Some(Node::Item(item)) = cx.tcx.hir().find_by_def_id(def_id) else { return }; if let hir::VisibilityKind::Inherited = item.vis.node { for impl_item_ref in items { self.private_traits.insert(impl_item_ref.id.hir_id()); @@ -1212,7 +1211,7 @@ impl<'tcx> LateLintPass<'tcx> for InvalidNoMangleItems { check_no_mangle_on_generic_fn( no_mangle_attr, Some(generics), - cx.tcx.hir().get_generics(it.id.def_id.to_def_id()).unwrap(), + cx.tcx.hir().get_generics(it.id.def_id).unwrap(), it.span, ); } diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index fa1752aaec38d..fb5bd31631903 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1579,12 +1579,12 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } } - fn encode_info_for_closure(&mut self, def_id: LocalDefId) { + fn encode_info_for_closure(&mut self, hir_id: hir::HirId) { + let def_id = self.tcx.hir().local_def_id(hir_id); debug!("EncodeContext::encode_info_for_closure({:?})", def_id); // NOTE(eddyb) `tcx.type_of(def_id)` isn't used because it's fully generic, // including on the signature, which is inferred in `typeck. - let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id); let ty = self.tcx.typeck(def_id).node_type(hir_id); match ty.kind() { @@ -1605,9 +1605,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } } - fn encode_info_for_anon_const(&mut self, def_id: LocalDefId) { + fn encode_info_for_anon_const(&mut self, id: hir::HirId) { + let def_id = self.tcx.hir().local_def_id(id); debug!("EncodeContext::encode_info_for_anon_const({:?})", def_id); - let id = self.tcx.hir().local_def_id_to_hir_id(def_id); let body_id = self.tcx.hir().body_owned_by(id); let const_data = self.encode_rendered_const_for_body(body_id); let qualifs = self.tcx.mir_const_qualif(def_id); @@ -1928,8 +1928,7 @@ impl<'a, 'tcx> Visitor<'tcx> for EncodeContext<'a, 'tcx> { } fn visit_anon_const(&mut self, c: &'tcx AnonConst) { intravisit::walk_anon_const(self, c); - let def_id = self.tcx.hir().local_def_id(c.hir_id); - self.encode_info_for_anon_const(def_id); + self.encode_info_for_anon_const(c.hir_id); } fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) { intravisit::walk_item(self, item); @@ -1983,8 +1982,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { fn encode_info_for_expr(&mut self, expr: &hir::Expr<'_>) { if let hir::ExprKind::Closure(..) = expr.kind { - let def_id = self.tcx.hir().local_def_id(expr.hir_id); - self.encode_info_for_closure(def_id); + self.encode_info_for_closure(expr.hir_id); } } diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index 73829151bb1a9..aac9595ae6bac 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -361,8 +361,7 @@ impl<'hir> Map<'hir> { id.as_local().and_then(|id| self.find(self.local_def_id_to_hir_id(id))) } - pub fn get_generics(&self, id: DefId) -> Option<&'hir Generics<'hir>> { - let id = id.as_local()?; + pub fn get_generics(&self, id: LocalDefId) -> Option<&'hir Generics<'hir>> { let node = self.tcx.hir_owner(id)?; match node.node { OwnerNode::ImplItem(impl_item) => Some(&impl_item.generics), diff --git a/compiler/rustc_middle/src/mir/mod.rs b/compiler/rustc_middle/src/mir/mod.rs index 6d1d9dd9720d4..0890ba63e5856 100644 --- a/compiler/rustc_middle/src/mir/mod.rs +++ b/compiler/rustc_middle/src/mir/mod.rs @@ -2449,7 +2449,6 @@ impl<'tcx> Debug for Rvalue<'tcx> { AggregateKind::Closure(def_id, substs) => ty::tls::with(|tcx| { if let Some(def_id) = def_id.as_local() { - let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); let name = if tcx.sess.opts.debugging_opts.span_free_formats { let substs = tcx.lift(substs).unwrap(); format!( @@ -2457,7 +2456,7 @@ impl<'tcx> Debug for Rvalue<'tcx> { tcx.def_path_str_with_substs(def_id.to_def_id(), substs), ) } else { - let span = tcx.hir().span(hir_id); + let span = tcx.def_span(def_id); format!( "[closure@{}]", tcx.sess.source_map().span_to_diagnostic_string(span) @@ -2481,8 +2480,7 @@ impl<'tcx> Debug for Rvalue<'tcx> { AggregateKind::Generator(def_id, _, _) => ty::tls::with(|tcx| { if let Some(def_id) = def_id.as_local() { - let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); - let name = format!("[generator@{:?}]", tcx.hir().span(hir_id)); + let name = format!("[generator@{:?}]", tcx.def_span(def_id)); let mut struct_fmt = fmt.debug_struct(&name); // FIXME(project-rfc-2229#48): This should be a list of capture names/places diff --git a/compiler/rustc_middle/src/mir/mono.rs b/compiler/rustc_middle/src/mir/mono.rs index 3a6c091b3313d..892808386deef 100644 --- a/compiler/rustc_middle/src/mir/mono.rs +++ b/compiler/rustc_middle/src/mir/mono.rs @@ -179,15 +179,11 @@ impl<'tcx> MonoItem<'tcx> { pub fn local_span(&self, tcx: TyCtxt<'tcx>) -> Option { match *self { - MonoItem::Fn(Instance { def, .. }) => { - def.def_id().as_local().map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id)) - } - MonoItem::Static(def_id) => { - def_id.as_local().map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id)) - } - MonoItem::GlobalAsm(item_id) => Some(item_id.hir_id()), + MonoItem::Fn(Instance { def, .. }) => def.def_id().as_local(), + MonoItem::Static(def_id) => def_id.as_local(), + MonoItem::GlobalAsm(item_id) => Some(item_id.def_id), } - .map(|hir_id| tcx.hir().span(hir_id)) + .map(|def_id| tcx.def_span(def_id)) } // Only used by rustc_codegen_cranelift diff --git a/compiler/rustc_middle/src/mir/spanview.rs b/compiler/rustc_middle/src/mir/spanview.rs index 507f9971981b0..e76cf5d94d32e 100644 --- a/compiler/rustc_middle/src/mir/spanview.rs +++ b/compiler/rustc_middle/src/mir/spanview.rs @@ -665,9 +665,7 @@ fn trim_span_hi(span: Span, to_pos: BytePos) -> Span { } fn fn_span<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> Span { - let hir_id = - tcx.hir().local_def_id_to_hir_id(def_id.as_local().expect("expected DefId is local")); - let fn_decl_span = tcx.hir().span(hir_id); + let fn_decl_span = tcx.def_span(def_id); if let Some(body_span) = hir_body(tcx, def_id).map(|hir_body| hir_body.value.span) { if fn_decl_span.ctxt() == body_span.ctxt() { fn_decl_span.to(body_span) } else { body_span } } else { diff --git a/compiler/rustc_middle/src/ty/consts.rs b/compiler/rustc_middle/src/ty/consts.rs index 27e22ccac02a7..373823087c5c8 100644 --- a/compiler/rustc_middle/src/ty/consts.rs +++ b/compiler/rustc_middle/src/ty/consts.rs @@ -42,9 +42,7 @@ impl<'tcx> Const<'tcx> { ) -> &'tcx Self { debug!("Const::from_anon_const(def={:?})", def); - let hir_id = tcx.hir().local_def_id_to_hir_id(def.did); - - let body_id = match tcx.hir().get(hir_id) { + let body_id = match tcx.hir().get_by_def_id(def.did) { hir::Node::AnonConst(ac) => ac.body, _ => span_bug!( tcx.def_span(def.did.to_def_id()), @@ -260,8 +258,7 @@ impl<'tcx> Const<'tcx> { } pub fn const_param_default<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> &'tcx Const<'tcx> { - let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); - let default_def_id = match tcx.hir().get(hir_id) { + let default_def_id = match tcx.hir().get_by_def_id(def_id.expect_local()) { hir::Node::GenericParam(hir::GenericParam { kind: hir::GenericParamKind::Const { ty: _, default: Some(ac) }, .. diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index ecc6da6da12a7..9f38421294d9e 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -1461,8 +1461,7 @@ impl<'tcx> TyCtxt<'tcx> { _ => return None, // not a free region }; - let hir_id = self.hir().local_def_id_to_hir_id(suitable_region_binding_scope); - let is_impl_item = match self.hir().find(hir_id) { + let is_impl_item = match self.hir().find_by_def_id(suitable_region_binding_scope) { Some(Node::Item(..) | Node::TraitItem(..)) => false, Some(Node::ImplItem(..)) => { self.is_bound_region_in_impl_item(suitable_region_binding_scope) @@ -1495,8 +1494,7 @@ impl<'tcx> TyCtxt<'tcx> { pub fn return_type_impl_trait(self, scope_def_id: LocalDefId) -> Option<(Ty<'tcx>, Span)> { // `type_of()` will fail on these (#55796, #86483), so only allow `fn`s or closures. - let hir_id = self.hir().local_def_id_to_hir_id(scope_def_id); - match self.hir().get(hir_id) { + match self.hir().get_by_def_id(scope_def_id) { Node::Item(&hir::Item { kind: ItemKind::Fn(..), .. }) => {} Node::TraitItem(&hir::TraitItem { kind: TraitItemKind::Fn(..), .. }) => {} Node::ImplItem(&hir::ImplItem { kind: ImplItemKind::Fn(..), .. }) => {} @@ -1510,6 +1508,7 @@ impl<'tcx> TyCtxt<'tcx> { let sig = ret_ty.fn_sig(self); let output = self.erase_late_bound_regions(sig.output()); if output.is_impl_trait() { + let hir_id = self.hir().local_def_id_to_hir_id(scope_def_id); let fn_decl = self.hir().fn_decl_by_hir_id(hir_id).unwrap(); Some((output, fn_decl.output.span())) } else { diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index f1868459f2702..c188502f34f5c 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -2082,8 +2082,7 @@ impl<'tcx> TyCtxt<'tcx> { /// with the name of the crate containing the impl. pub fn span_of_impl(self, impl_did: DefId) -> Result { if let Some(impl_did) = impl_did.as_local() { - let hir_id = self.hir().local_def_id_to_hir_id(impl_did); - Ok(self.hir().span(hir_id)) + Ok(self.def_span(impl_did)) } else { Err(self.crate_name(impl_did.krate)) } @@ -2130,7 +2129,7 @@ impl<'tcx> TyCtxt<'tcx> { /// Yields the parent function's `LocalDefId` if `def_id` is an `impl Trait` definition. pub fn is_impl_trait_defn(tcx: TyCtxt<'_>, def_id: DefId) -> Option { let def_id = def_id.as_local()?; - if let Node::Item(item) = tcx.hir().get(tcx.hir().local_def_id_to_hir_id(def_id)) { + if let Node::Item(item) = tcx.hir().get_by_def_id(def_id) { if let hir::ItemKind::OpaqueTy(ref opaque_ty) = item.kind { return match opaque_ty.origin { hir::OpaqueTyOrigin::FnReturn(parent) | hir::OpaqueTyOrigin::AsyncFn(parent) => { diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index b2ae6e6fae6fd..32977fd42bd0a 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -671,8 +671,7 @@ pub trait PrettyPrinter<'tcx>: p!("generator"); // FIXME(eddyb) should use `def_span`. if let Some(did) = did.as_local() { - let hir_id = self.tcx().hir().local_def_id_to_hir_id(did); - let span = self.tcx().hir().span(hir_id); + let span = self.tcx().def_span(did); p!(write( "@{}", // This may end up in stderr diagnostics but it may also be emitted @@ -708,11 +707,10 @@ pub trait PrettyPrinter<'tcx>: p!(write("closure")); // FIXME(eddyb) should use `def_span`. if let Some(did) = did.as_local() { - let hir_id = self.tcx().hir().local_def_id_to_hir_id(did); if self.tcx().sess.opts.debugging_opts.span_free_formats { p!("@", print_def_path(did.to_def_id(), substs)); } else { - let span = self.tcx().hir().span(hir_id); + let span = self.tcx().def_span(did); p!(write( "@{}", // This may end up in stderr diagnostics but it may also be emitted diff --git a/compiler/rustc_mir_build/src/build/expr/as_place.rs b/compiler/rustc_mir_build/src/build/expr/as_place.rs index 3c07077c2cbb8..2433a00232d7f 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_place.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_place.rs @@ -217,10 +217,6 @@ fn to_upvars_resolved_place_builder<'a, 'tcx>( ty::ClosureKind::FnOnce => {} } - // We won't be building MIR if the closure wasn't local - let closure_hir_id = tcx.hir().local_def_id_to_hir_id(closure_def_id.expect_local()); - let closure_span = tcx.hir().span(closure_hir_id); - let Some((capture_index, capture)) = find_capture_matching_projections( typeck_results, @@ -228,6 +224,7 @@ fn to_upvars_resolved_place_builder<'a, 'tcx>( closure_def_id, &from_builder.projection, ) else { + let closure_span = tcx.def_span(closure_def_id); if !enable_precise_capture(tcx, closure_span) { bug!( "No associated capture found for {:?}[{:#?}] even though \ @@ -244,6 +241,8 @@ fn to_upvars_resolved_place_builder<'a, 'tcx>( return Err(from_builder); }; + // We won't be building MIR if the closure wasn't local + let closure_hir_id = tcx.hir().local_def_id_to_hir_id(closure_def_id.expect_local()); let closure_ty = typeck_results.node_type(closure_hir_id); let substs = match closure_ty.kind() { diff --git a/compiler/rustc_mir_build/src/lints.rs b/compiler/rustc_mir_build/src/lints.rs index e4c2d2dce67c7..d348aaa899e65 100644 --- a/compiler/rustc_mir_build/src/lints.rs +++ b/compiler/rustc_mir_build/src/lints.rs @@ -11,9 +11,8 @@ use std::ops::ControlFlow; crate fn check<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>) { let def_id = body.source.def_id().expect_local(); - let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); - if let Some(fn_kind) = tcx.hir().get(hir_id).fn_kind() { + if let Some(fn_kind) = tcx.hir().get_by_def_id(def_id).fn_kind() { if let FnKind::Closure = fn_kind { // closures can't recur, so they don't matter. return; diff --git a/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs b/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs index dd16e3cde75ae..8ccf6acf7034c 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs @@ -120,34 +120,32 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> { } fn search_for_structural_match_violation(&self, ty: Ty<'tcx>) -> Option { - traits::search_for_structural_match_violation(self.id, self.span, self.tcx(), ty).map( - |non_sm_ty| { - with_no_trimmed_paths(|| match non_sm_ty { - traits::NonStructuralMatchTy::Adt(adt) => self.adt_derive_msg(adt), - traits::NonStructuralMatchTy::Dynamic => { - "trait objects cannot be used in patterns".to_string() - } - traits::NonStructuralMatchTy::Opaque => { - "opaque types cannot be used in patterns".to_string() - } - traits::NonStructuralMatchTy::Closure => { - "closures cannot be used in patterns".to_string() - } - traits::NonStructuralMatchTy::Generator => { - "generators cannot be used in patterns".to_string() - } - traits::NonStructuralMatchTy::Param => { - bug!("use of a constant whose type is a parameter inside a pattern") - } - traits::NonStructuralMatchTy::Projection => { - bug!("use of a constant whose type is a projection inside a pattern") - } - traits::NonStructuralMatchTy::Foreign => { - bug!("use of a value of a foreign type inside a pattern") - } - }) - }, - ) + traits::search_for_structural_match_violation(self.span, self.tcx(), ty).map(|non_sm_ty| { + with_no_trimmed_paths(|| match non_sm_ty { + traits::NonStructuralMatchTy::Adt(adt) => self.adt_derive_msg(adt), + traits::NonStructuralMatchTy::Dynamic => { + "trait objects cannot be used in patterns".to_string() + } + traits::NonStructuralMatchTy::Opaque => { + "opaque types cannot be used in patterns".to_string() + } + traits::NonStructuralMatchTy::Closure => { + "closures cannot be used in patterns".to_string() + } + traits::NonStructuralMatchTy::Generator => { + "generators cannot be used in patterns".to_string() + } + traits::NonStructuralMatchTy::Param => { + bug!("use of a constant whose type is a parameter inside a pattern") + } + traits::NonStructuralMatchTy::Projection => { + bug!("use of a constant whose type is a projection inside a pattern") + } + traits::NonStructuralMatchTy::Foreign => { + bug!("use of a value of a foreign type inside a pattern") + } + }) + }) } fn type_marked_structural(&self, ty: Ty<'tcx>) -> bool { diff --git a/compiler/rustc_mir_transform/src/const_prop.rs b/compiler/rustc_mir_transform/src/const_prop.rs index e3ff6ad45490d..b5888592f6295 100644 --- a/compiler/rustc_mir_transform/src/const_prop.rs +++ b/compiler/rustc_mir_transform/src/const_prop.rs @@ -76,10 +76,8 @@ impl<'tcx> MirPass<'tcx> for ConstProp { } let def_id = body.source.def_id().expect_local(); - let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); - - let is_fn_like = tcx.hir().get(hir_id).fn_kind().is_some(); - let is_assoc_const = tcx.def_kind(def_id.to_def_id()) == DefKind::AssocConst; + let is_fn_like = tcx.hir().get_by_def_id(def_id).fn_kind().is_some(); + let is_assoc_const = tcx.def_kind(def_id) == DefKind::AssocConst; // Only run const prop on functions, methods, closures and associated constants if !is_fn_like && !is_assoc_const { diff --git a/compiler/rustc_mir_transform/src/coverage/mod.rs b/compiler/rustc_mir_transform/src/coverage/mod.rs index b009e2fd0e4ad..82455654a8860 100644 --- a/compiler/rustc_mir_transform/src/coverage/mod.rs +++ b/compiler/rustc_mir_transform/src/coverage/mod.rs @@ -66,8 +66,8 @@ impl<'tcx> MirPass<'tcx> for InstrumentCoverage { return; } - let hir_id = tcx.hir().local_def_id_to_hir_id(mir_source.def_id().expect_local()); - let is_fn_like = tcx.hir().get(hir_id).fn_kind().is_some(); + let is_fn_like = + tcx.hir().get_by_def_id(mir_source.def_id().expect_local()).fn_kind().is_some(); // Only instrument functions, methods, and closures (not constants since they are evaluated // at compile time by Miri). diff --git a/compiler/rustc_mir_transform/src/lib.rs b/compiler/rustc_mir_transform/src/lib.rs index 638baa0b8d376..83e442c7891c6 100644 --- a/compiler/rustc_mir_transform/src/lib.rs +++ b/compiler/rustc_mir_transform/src/lib.rs @@ -366,8 +366,7 @@ fn mir_drops_elaborated_and_const_checked<'tcx>( tcx.ensure().mir_borrowck(def.did); } - let hir_id = tcx.hir().local_def_id_to_hir_id(def.did); - let is_fn_like = tcx.hir().get(hir_id).fn_kind().is_some(); + let is_fn_like = tcx.hir().get_by_def_id(def.did).fn_kind().is_some(); if is_fn_like { let did = def.did.to_def_id(); let def = ty::WithOptConstParam::unknown(did); diff --git a/compiler/rustc_monomorphize/src/util.rs b/compiler/rustc_monomorphize/src/util.rs index 6084cdda22768..27540395c0789 100644 --- a/compiler/rustc_monomorphize/src/util.rs +++ b/compiler/rustc_monomorphize/src/util.rs @@ -49,8 +49,7 @@ crate fn dump_closure_profile<'tcx>(tcx: TyCtxt<'tcx>, closure_instance: Instanc .map(|l| format!("{:?}", l.size.bytes())) .unwrap_or_else(|e| format!("Failed {:?}", e)); - let closure_hir_id = tcx.hir().local_def_id_to_hir_id(closure_def_id.expect_local()); - let closure_span = tcx.hir().span(closure_hir_id); + let closure_span = tcx.def_span(closure_def_id); let src_file = tcx.sess.source_map().span_to_filename(closure_span); let line_nos = tcx .sess diff --git a/compiler/rustc_passes/src/dead.rs b/compiler/rustc_passes/src/dead.rs index 3b15332c678fd..f17816eff9a74 100644 --- a/compiler/rustc_passes/src/dead.rs +++ b/compiler/rustc_passes/src/dead.rs @@ -23,7 +23,7 @@ use std::mem; // may need to be marked as live. fn should_explore(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool { matches!( - tcx.hir().find(tcx.hir().local_def_id_to_hir_id(def_id)), + tcx.hir().find_by_def_id(def_id), Some( Node::Item(..) | Node::ImplItem(..) @@ -232,7 +232,7 @@ impl<'tcx> MarkSymbolVisitor<'tcx> { // tuple struct constructor function let id = self.struct_constructors.get(&id).copied().unwrap_or(id); - if let Some(node) = self.tcx.hir().find(self.tcx.hir().local_def_id_to_hir_id(id)) { + if let Some(node) = self.tcx.hir().find_by_def_id(id) { self.live_symbols.insert(id); self.visit_node(node); } diff --git a/compiler/rustc_passes/src/entry.rs b/compiler/rustc_passes/src/entry.rs index 8ab6c4eeae500..fdabe41dafaed 100644 --- a/compiler/rustc_passes/src/entry.rs +++ b/compiler/rustc_passes/src/entry.rs @@ -152,11 +152,10 @@ fn configure_main(tcx: TyCtxt<'_>, visitor: &EntryContext<'_, '_>) -> Option<(De if let Some(def_id) = main_def.opt_fn_def_id() { // non-local main imports are handled below if let Some(def_id) = def_id.as_local() { - let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); - if matches!(tcx.hir().find(hir_id), Some(Node::ForeignItem(_))) { + if matches!(tcx.hir().find_by_def_id(def_id), Some(Node::ForeignItem(_))) { tcx.sess .struct_span_err( - tcx.hir().span(hir_id), + tcx.def_span(def_id), "the `main` function cannot be declared in an `extern` block", ) .emit(); diff --git a/compiler/rustc_passes/src/lib.rs b/compiler/rustc_passes/src/lib.rs index 8a411f01d6ee2..3596210036a9c 100644 --- a/compiler/rustc_passes/src/lib.rs +++ b/compiler/rustc_passes/src/lib.rs @@ -6,6 +6,7 @@ #![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")] #![feature(crate_visibility_modifier)] +#![feature(let_else)] #![feature(map_try_insert)] #![feature(min_specialization)] #![feature(nll)] diff --git a/compiler/rustc_passes/src/reachable.rs b/compiler/rustc_passes/src/reachable.rs index 1ce6a17303e5b..f4790c4335c12 100644 --- a/compiler/rustc_passes/src/reachable.rs +++ b/compiler/rustc_passes/src/reachable.rs @@ -52,7 +52,7 @@ fn method_might_be_inlined( return true; } } - match tcx.hir().find(tcx.hir().local_def_id_to_hir_id(impl_src)) { + match tcx.hir().find_by_def_id(impl_src) { Some(Node::Item(item)) => item_might_be_inlined(tcx, &item, codegen_fn_attrs), Some(..) | None => span_bug!(impl_item.span, "impl did is not an item"), } @@ -140,14 +140,11 @@ impl<'tcx> ReachableContext<'tcx> { // Returns true if the given def ID represents a local item that is // eligible for inlining and false otherwise. fn def_id_represents_local_inlined_item(&self, def_id: DefId) -> bool { - let hir_id = match def_id.as_local() { - Some(def_id) => self.tcx.hir().local_def_id_to_hir_id(def_id), - None => { - return false; - } + let Some(def_id) = def_id.as_local() else { + return false; }; - match self.tcx.hir().find(hir_id) { + match self.tcx.hir().find_by_def_id(def_id) { Some(Node::Item(item)) => match item.kind { hir::ItemKind::Fn(..) => { item_might_be_inlined(self.tcx, &item, self.tcx.codegen_fn_attrs(def_id)) @@ -169,6 +166,7 @@ impl<'tcx> ReachableContext<'tcx> { if generics.requires_monomorphization(self.tcx) || attrs.requests_inline() { true } else { + let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_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 @@ -198,9 +196,7 @@ impl<'tcx> ReachableContext<'tcx> { continue; } - if let Some(ref item) = - self.tcx.hir().find(self.tcx.hir().local_def_id_to_hir_id(search_item)) - { + if let Some(ref item) = self.tcx.hir().find_by_def_id(search_item) { self.propagate_node(item, search_item); } } diff --git a/compiler/rustc_resolve/src/late/lifetimes.rs b/compiler/rustc_resolve/src/late/lifetimes.rs index 52bd45f12c97c..01fc1b7012e26 100644 --- a/compiler/rustc_resolve/src/late/lifetimes.rs +++ b/compiler/rustc_resolve/src/late/lifetimes.rs @@ -376,12 +376,9 @@ pub fn provide(providers: &mut ty::query::Providers) { named_region_map: |tcx, id| resolve_lifetimes_for(tcx, id).defs.get(&id), is_late_bound_map, - object_lifetime_defaults_map: |tcx, id| { - let hir_id = tcx.hir().local_def_id_to_hir_id(id); - match tcx.hir().find(hir_id) { - Some(Node::Item(item)) => compute_object_lifetime_defaults(tcx, item), - _ => None, - } + object_lifetime_defaults_map: |tcx, id| match tcx.hir().find_by_def_id(id) { + Some(Node::Item(item)) => compute_object_lifetime_defaults(tcx, item), + _ => None, }, late_bound_vars_map: |tcx, id| resolve_lifetimes_for(tcx, id).late_bound_vars.get(&id), lifetime_scope_map: |tcx, id| { @@ -514,14 +511,14 @@ fn resolve_lifetimes_for<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> &'tcx R /// Finds the `Item` that contains the given `LocalDefId` fn item_for(tcx: TyCtxt<'_>, local_def_id: LocalDefId) -> LocalDefId { - let hir_id = tcx.hir().local_def_id_to_hir_id(local_def_id); - match tcx.hir().find(hir_id) { + match tcx.hir().find_by_def_id(local_def_id) { Some(Node::Item(item)) => { return item.def_id; } _ => {} } let item = { + let hir_id = tcx.hir().local_def_id_to_hir_id(local_def_id); let mut parent_iter = tcx.hir().parent_iter(hir_id); loop { let node = parent_iter.next().map(|n| n.1); @@ -1672,13 +1669,10 @@ fn extract_labels(ctxt: &mut LifetimeContext<'_, '_>, body: &hir::Body<'_>) { if let Some(def) = lifetimes.get(&hir::ParamName::Plain(label.normalize_to_macros_2_0())) { - let hir_id = - tcx.hir().local_def_id_to_hir_id(def.id().unwrap().expect_local()); - signal_shadowing_problem( tcx, label.name, - original_lifetime(tcx.hir().span(hir_id)), + original_lifetime(tcx.def_span(def.id().unwrap().expect_local())), shadower_label(label.span), ); return; @@ -1910,6 +1904,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { let remove_decl = self .tcx .parent(def_id) + .and_then(|parent_def_id| parent_def_id.as_local()) .and_then(|parent_def_id| self.tcx.hir().get_generics(parent_def_id)) .and_then(|generics| self.lifetime_deletion_span(name, generics)); @@ -2032,19 +2027,20 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { match lifetimeuseset { Some(LifetimeUseSet::One(lifetime)) => { - let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); - debug!("hir id first={:?}", hir_id); - if let Some((id, span, name)) = match self.tcx.hir().get(hir_id) { - Node::Lifetime(hir_lifetime) => Some(( - hir_lifetime.hir_id, - hir_lifetime.span, - hir_lifetime.name.ident(), - )), - Node::GenericParam(param) => { - Some((param.hir_id, param.span, param.name.ident())) + debug!(?def_id); + if let Some((id, span, name)) = + match self.tcx.hir().get_by_def_id(def_id.expect_local()) { + Node::Lifetime(hir_lifetime) => Some(( + hir_lifetime.hir_id, + hir_lifetime.span, + hir_lifetime.name.ident(), + )), + Node::GenericParam(param) => { + Some((param.hir_id, param.span, param.name.ident())) + } + _ => None, } - _ => None, - } { + { debug!("id = {:?} span = {:?} name = {:?}", id, span, name); if name.name == kw::UnderscoreLifetime { continue; @@ -2052,12 +2048,10 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { if let Some(parent_def_id) = self.tcx.parent(def_id) { if let Some(def_id) = parent_def_id.as_local() { - let parent_hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id); // lifetimes in `derive` expansions don't count (Issue #53738) if self .tcx - .hir() - .attrs(parent_hir_id) + .get_attrs(def_id.to_def_id()) .iter() .any(|attr| attr.has_name(sym::automatically_derived)) { @@ -2069,7 +2063,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { if let hir::Node::Item(hir::Item { kind: hir::ItemKind::OpaqueTy(ref opaque), .. - }) = self.tcx.hir().get(parent_hir_id) + }) = self.tcx.hir().get_by_def_id(def_id) { if !matches!(opaque.origin, hir::OpaqueTyOrigin::AsyncFn(..)) { continue 'lifetimes; @@ -2115,18 +2109,19 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { debug!("not one use lifetime"); } None => { - let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); - if let Some((id, span, name)) = match self.tcx.hir().get(hir_id) { - Node::Lifetime(hir_lifetime) => Some(( - hir_lifetime.hir_id, - hir_lifetime.span, - hir_lifetime.name.ident(), - )), - Node::GenericParam(param) => { - Some((param.hir_id, param.span, param.name.ident())) + if let Some((id, span, name)) = + match self.tcx.hir().get_by_def_id(def_id.expect_local()) { + Node::Lifetime(hir_lifetime) => Some(( + hir_lifetime.hir_id, + hir_lifetime.span, + hir_lifetime.name.ident(), + )), + Node::GenericParam(param) => { + Some((param.hir_id, param.span, param.name.ident())) + } + _ => None, } - _ => None, - } { + { debug!("id ={:?} span = {:?} name = {:?}", id, span, name); self.tcx.struct_span_lint_hir( lint::builtin::UNUSED_LIFETIMES, @@ -2137,7 +2132,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { .build(&format!("lifetime parameter `{}` never used", name)); if let Some(parent_def_id) = self.tcx.parent(def_id) { if let Some(generics) = - self.tcx.hir().get_generics(parent_def_id) + self.tcx.hir().get_generics(parent_def_id.expect_local()) { let unused_lt_span = self.lifetime_deletion_span(name, generics); @@ -3339,13 +3334,10 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { Scope::Binder { ref lifetimes, s, .. } => { if let Some(&def) = lifetimes.get(¶m.name.normalize_to_macros_2_0()) { - let hir_id = - self.tcx.hir().local_def_id_to_hir_id(def.id().unwrap().expect_local()); - signal_shadowing_problem( self.tcx, param.name.ident().name, - original_lifetime(self.tcx.hir().span(hir_id)), + original_lifetime(self.tcx.def_span(def.id().unwrap())), shadower_lifetime(¶m), ); return; diff --git a/compiler/rustc_symbol_mangling/src/lib.rs b/compiler/rustc_symbol_mangling/src/lib.rs index 1f38240ee4146..c5e85b14421cf 100644 --- a/compiler/rustc_symbol_mangling/src/lib.rs +++ b/compiler/rustc_symbol_mangling/src/lib.rs @@ -173,8 +173,7 @@ fn compute_symbol_name<'tcx>( let stable_crate_id = tcx.sess.local_stable_crate_id(); return tcx.sess.generate_proc_macro_decls_symbol(stable_crate_id); } - let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); - matches!(tcx.hir().get(hir_id), Node::ForeignItem(_)) + matches!(tcx.hir().get_by_def_id(def_id), Node::ForeignItem(_)) } else { tcx.is_foreign_item(def_id) }; diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs index 72878b6cb3858..b4d297a03ef21 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs @@ -616,8 +616,6 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { self.tcx.sess.source_map().guess_head_span( self.tcx.hir().span_if_local(closure_def_id).unwrap(), ); - let hir_id = - self.tcx.hir().local_def_id_to_hir_id(closure_def_id.expect_local()); let mut err = struct_span_err!( self.tcx.sess, closure_span, @@ -640,6 +638,10 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { // Additional context information explaining why the closure only implements // a particular trait. if let Some(typeck_results) = self.in_progress_typeck_results { + let hir_id = self + .tcx + .hir() + .local_def_id_to_hir_id(closure_def_id.expect_local()); let typeck_results = typeck_results.borrow(); match (found_kind, typeck_results.closure_kind_origins().get(hir_id)) { (ty::ClosureKind::FnOnce, Some((span, place))) => { diff --git a/compiler/rustc_trait_selection/src/traits/structural_match.rs b/compiler/rustc_trait_selection/src/traits/structural_match.rs index 55feb3c1de17d..45960bd365346 100644 --- a/compiler/rustc_trait_selection/src/traits/structural_match.rs +++ b/compiler/rustc_trait_selection/src/traits/structural_match.rs @@ -48,7 +48,6 @@ pub enum NonStructuralMatchTy<'tcx> { /// that arose when the requirement was not enforced completely, see /// Rust RFC 1445, rust-lang/rust#61188, and rust-lang/rust#62307. pub fn search_for_structural_match_violation<'tcx>( - _id: hir::HirId, span: Span, tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, diff --git a/compiler/rustc_ty_utils/src/representability.rs b/compiler/rustc_ty_utils/src/representability.rs index d3eb9fd955714..fb7fdacf5e687 100644 --- a/compiler/rustc_ty_utils/src/representability.rs +++ b/compiler/rustc_ty_utils/src/representability.rs @@ -99,12 +99,7 @@ fn are_inner_types_recursive<'tcx>( // Find non representable fields with their spans fold_repr(def.all_fields().map(|field| { let ty = field.ty(tcx, substs); - let span = match field - .did - .as_local() - .map(|id| tcx.hir().local_def_id_to_hir_id(id)) - .and_then(|id| tcx.hir().find(id)) - { + let span = match field.did.as_local().and_then(|id| tcx.hir().find_by_def_id(id)) { Some(hir::Node::Field(field)) => field.ty.span, _ => sp, }; diff --git a/compiler/rustc_ty_utils/src/ty.rs b/compiler/rustc_ty_utils/src/ty.rs index 8f50e3e0fe1ca..4d48cd25e362e 100644 --- a/compiler/rustc_ty_utils/src/ty.rs +++ b/compiler/rustc_ty_utils/src/ty.rs @@ -279,8 +279,7 @@ fn well_formed_types_in_env<'tcx>( if !def_id.is_local() { return ty::List::empty(); } - let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); - let node = tcx.hir().get(hir_id); + let node = tcx.hir().get_by_def_id(def_id.expect_local()); enum NodeKind { TraitImpl, @@ -436,9 +435,7 @@ fn issue33140_self_ty(tcx: TyCtxt<'_>, def_id: DefId) -> Option> { /// Check if a function is async. fn asyncness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::IsAsync { - let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); - - let node = tcx.hir().get(hir_id); + let node = tcx.hir().get_by_def_id(def_id.expect_local()); let fn_kind = node.fn_kind().unwrap_or_else(|| { bug!("asyncness: expected fn-like node but got `{:?}`", def_id); diff --git a/compiler/rustc_typeck/src/check/check.rs b/compiler/rustc_typeck/src/check/check.rs index 4e83b351b9b54..89f9cbf22562d 100644 --- a/compiler/rustc_typeck/src/check/check.rs +++ b/compiler/rustc_typeck/src/check/check.rs @@ -1314,7 +1314,7 @@ fn check_enum<'tcx>( let variant_i = tcx.hir().expect_variant(variant_i_hir_id); let i_span = match variant_i.disr_expr { Some(ref expr) => tcx.hir().span(expr.hir_id), - None => tcx.hir().span(variant_i_hir_id), + None => tcx.def_span(variant_did), }; let span = match v.disr_expr { Some(ref expr) => tcx.hir().span(expr.hir_id), diff --git a/compiler/rustc_typeck/src/check/compare_method.rs b/compiler/rustc_typeck/src/check/compare_method.rs index c942bafcf034b..74327affede48 100644 --- a/compiler/rustc_typeck/src/check/compare_method.rs +++ b/compiler/rustc_typeck/src/check/compare_method.rs @@ -435,10 +435,18 @@ fn check_region_bounds_on_impl_item<'tcx>( if trait_params != impl_params { let item_kind = assoc_item_kind_str(impl_m); let def_span = tcx.sess.source_map().guess_head_span(span); - let span = tcx.hir().get_generics(impl_m.def_id).map_or(def_span, |g| g.span); + let span = impl_m + .def_id + .as_local() + .and_then(|did| tcx.hir().get_generics(did)) + .map_or(def_span, |g| g.span); let generics_span = tcx.hir().span_if_local(trait_m.def_id).map(|sp| { let def_sp = tcx.sess.source_map().guess_head_span(sp); - tcx.hir().get_generics(trait_m.def_id).map_or(def_sp, |g| g.span) + trait_m + .def_id + .as_local() + .and_then(|did| tcx.hir().get_generics(did)) + .map_or(def_sp, |g| g.span) }); tcx.sess.emit_err(LifetimesOrBoundsMismatchOnTrait { diff --git a/compiler/rustc_typeck/src/check/dropck.rs b/compiler/rustc_typeck/src/check/dropck.rs index 3cc66aaf0d79c..c8986aa7f53b0 100644 --- a/compiler/rustc_typeck/src/check/dropck.rs +++ b/compiler/rustc_typeck/src/check/dropck.rs @@ -184,8 +184,6 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>( // absent. So we report an error that the Drop impl injected a // predicate that is not present on the struct definition. - let self_type_hir_id = tcx.hir().local_def_id_to_hir_id(self_type_did); - // We can assume the predicates attached to struct/enum definition // hold. let generic_assumptions = tcx.predicates_of(self_type_did); @@ -252,7 +250,7 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>( }; if !assumptions_in_impl_context.iter().copied().any(predicate_matches_closure) { - let item_span = tcx.hir().span(self_type_hir_id); + let item_span = tcx.def_span(self_type_did); let self_descr = tcx.def_kind(self_type_did).descr(self_type_did.to_def_id()); struct_span_err!( tcx.sess, diff --git a/compiler/rustc_typeck/src/check/wfcheck.rs b/compiler/rustc_typeck/src/check/wfcheck.rs index bf68c59fd1a20..c7b7bb7574bde 100644 --- a/compiler/rustc_typeck/src/check/wfcheck.rs +++ b/compiler/rustc_typeck/src/check/wfcheck.rs @@ -787,9 +787,7 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) { } }; - if traits::search_for_structural_match_violation(param.hir_id, param.span, tcx, ty) - .is_some() - { + if traits::search_for_structural_match_violation(param.span, tcx, ty).is_some() { // We use the same error code in both branches, because this is really the same // issue: we just special-case the message for type parameters to make it // clearer. From d9c6e70c57d16b248b65ec578ca9b77028ea0725 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Fri, 22 Oct 2021 16:43:43 +0200 Subject: [PATCH 8/9] Do not ICE when accesing large LocalDefId. --- compiler/rustc_middle/src/hir/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_middle/src/hir/mod.rs b/compiler/rustc_middle/src/hir/mod.rs index 900f8f43bb7dc..8164eefd6cb2e 100644 --- a/compiler/rustc_middle/src/hir/mod.rs +++ b/compiler/rustc_middle/src/hir/mod.rs @@ -64,7 +64,7 @@ pub fn provide(providers: &mut Providers) { providers.crate_hash = map::crate_hash; providers.hir_module_items = map::hir_module_items; providers.hir_owner = |tcx, id| { - let owner = tcx.hir_crate(()).owners[id].as_ref()?; + let owner = tcx.hir_crate(()).owners.get(id)?.as_ref()?; let node = owner.node(); Some(Owner { node, hash_without_bodies: owner.nodes.hash_without_bodies }) }; From 8617ff0f0bfc8ab8484f4ad970d63e0a787f954f Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Fri, 10 Dec 2021 13:23:48 +0100 Subject: [PATCH 9/9] Add inline. --- compiler/rustc_hir/src/hir_id.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/compiler/rustc_hir/src/hir_id.rs b/compiler/rustc_hir/src/hir_id.rs index 1482a96cae316..5b486819c35e8 100644 --- a/compiler/rustc_hir/src/hir_id.rs +++ b/compiler/rustc_hir/src/hir_id.rs @@ -19,11 +19,13 @@ pub struct HirId { } impl HirId { + #[inline] pub fn expect_owner(self) -> LocalDefId { assert_eq!(self.local_id.index(), 0); self.owner } + #[inline] pub fn as_owner(self) -> Option { if self.local_id.index() == 0 { Some(self.owner) } else { None } }