From 67727aa7c31a24ea73a91a9134c3653fae8209ab Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Wed, 20 Oct 2021 20:59:15 +0200 Subject: [PATCH] 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.