From 25bdc8965e7a80cb3a72da79ca568953738fe433 Mon Sep 17 00:00:00 2001 From: Miguel Guarniz Date: Fri, 15 Jul 2022 23:13:04 -0400 Subject: [PATCH 1/4] Change maybe_body_owned_by to take local def id Signed-off-by: Miguel Guarniz --- .../src/diagnostics/conflict_errors.rs | 3 +-- compiler/rustc_driver/src/pretty.rs | 2 +- .../error_reporting/nice_region_error/util.rs | 9 ++++++--- compiler/rustc_metadata/src/rmeta/encoder.rs | 16 ++++++++-------- compiler/rustc_middle/src/hir/map/mod.rs | 15 ++++++++------- compiler/rustc_middle/src/hir/mod.rs | 5 +++-- compiler/rustc_mir_build/src/thir/cx/mod.rs | 2 +- .../src/thir/pattern/check_match.rs | 2 +- .../rustc_mir_transform/src/check_unsafety.rs | 6 +++--- compiler/rustc_passes/src/upvars.rs | 4 ++-- .../src/traits/error_reporting/suggestions.rs | 7 ++++--- compiler/rustc_ty_utils/src/ty.rs | 7 ++++--- compiler/rustc_typeck/src/check/expr.rs | 3 ++- compiler/rustc_typeck/src/check/inherited.rs | 3 +-- compiler/rustc_typeck/src/check/region.rs | 3 +-- src/librustdoc/clean/utils.rs | 3 +-- src/librustdoc/core.rs | 2 +- src/librustdoc/scrape_examples.rs | 3 +-- .../clippy/clippy_lints/src/utils/author.rs | 2 +- 19 files changed, 50 insertions(+), 47 deletions(-) diff --git a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs index 1b3c6cac9c42f..255c31d0b1a7a 100644 --- a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs @@ -353,9 +353,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { // We use the statements were the binding was initialized, and inspect the HIR to look // for the branching codepaths that aren't covered, to point at them. - let hir_id = self.mir_hir_id(); let map = self.infcx.tcx.hir(); - let body_id = map.body_owned_by(hir_id); + let body_id = map.body_owned_by(self.mir_def_id()); let body = map.body(body_id); let mut visitor = ConditionVisitor { spans: &spans, name: &name, errors: vec![] }; diff --git a/compiler/rustc_driver/src/pretty.rs b/compiler/rustc_driver/src/pretty.rs index 12bac956adb55..a514f1a9489f9 100644 --- a/compiler/rustc_driver/src/pretty.rs +++ b/compiler/rustc_driver/src/pretty.rs @@ -328,7 +328,7 @@ impl<'tcx> pprust_hir::PpAnn for TypedAnnotation<'tcx> { let typeck_results = self.maybe_typeck_results.get().or_else(|| { self.tcx .hir() - .maybe_body_owned_by(self.tcx.hir().local_def_id_to_hir_id(expr.hir_id.owner)) + .maybe_body_owned_by(self.tcx.hir().local_def_id(expr.hir_id)) .map(|body_id| self.tcx.typeck_body(body_id)) }); diff --git a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/util.rs b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/util.rs index b356da0be554e..4c63024367c6c 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/util.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/util.rs @@ -49,10 +49,10 @@ pub fn find_param_with_region<'tcx>( }; let hir = &tcx.hir(); - let hir_id = hir.local_def_id_to_hir_id(id.as_local()?); - let body_id = hir.maybe_body_owned_by(hir_id)?; - let body = hir.body(body_id); + let local_did = id.as_local()?; + let hir_id = hir.local_def_id_to_hir_id(local_did); + // FIXME: use def_kind // Don't perform this on closures match hir.get(hir_id) { hir::Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure { .. }, .. }) => { @@ -61,11 +61,14 @@ pub fn find_param_with_region<'tcx>( _ => {} } + let body_id = hir.maybe_body_owned_by(local_did)?; + let owner_id = hir.body_owner(body_id); let fn_decl = hir.fn_decl_by_hir_id(owner_id).unwrap(); let poly_fn_sig = tcx.fn_sig(id); let fn_sig = tcx.liberate_late_bound_regions(id, poly_fn_sig); + let body = hir.body(body_id); body.params .iter() .take(if fn_sig.c_variadic { diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 50d983754e89c..d2ed6e08ff713 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1614,16 +1614,16 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } 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 body_id = self.tcx.hir().body_owned_by(id); + let local_did = self.tcx.hir().local_def_id(id); + debug!("EncodeContext::encode_info_for_anon_const({:?})", local_did); + let body_id = self.tcx.hir().body_owned_by(local_did); let const_data = self.encode_rendered_const_for_body(body_id); - let qualifs = self.tcx.mir_const_qualif(def_id); + let qualifs = self.tcx.mir_const_qualif(local_did); - record!(self.tables.kind[def_id.to_def_id()] <- EntryKind::AnonConst); - record!(self.tables.mir_const_qualif[def_id.to_def_id()] <- qualifs); - record!(self.tables.rendered_const[def_id.to_def_id()] <- const_data); - self.encode_item_type(def_id.to_def_id()); + record!(self.tables.kind[local_did.to_def_id()] <- EntryKind::AnonConst); + record!(self.tables.mir_const_qualif[local_did.to_def_id()] <- qualifs); + record!(self.tables.rendered_const[local_did.to_def_id()] <- const_data); + self.encode_item_type(local_did.to_def_id()); } fn encode_native_libraries(&mut self) -> LazyArray { diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index 0001e1aa53efc..adb591fe09c5a 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -398,7 +398,7 @@ impl<'hir> Map<'hir> { pub fn enclosing_body_owner(self, hir_id: HirId) -> HirId { for (parent, _) in self.parent_iter(hir_id) { - if let Some(body) = self.maybe_body_owned_by(parent) { + if let Some(local_did) = parent.as_owner() && let Some(body) = self.maybe_body_owned_by(local_did) { return self.body_owner(body); } } @@ -419,19 +419,20 @@ impl<'hir> Map<'hir> { self.local_def_id(self.body_owner(id)) } - /// Given a `HirId`, returns the `BodyId` associated with it, + /// Given a `LocalDefId`, returns the `BodyId` associated with it, /// if the node is a body owner, otherwise returns `None`. - pub fn maybe_body_owned_by(self, hir_id: HirId) -> Option { - self.find(hir_id).map(associated_body).flatten() + pub fn maybe_body_owned_by(self, id: LocalDefId) -> Option { + self.get_if_local(id.to_def_id()).map(associated_body).flatten() } /// Given a body owner's id, returns the `BodyId` associated with it. - pub fn body_owned_by(self, id: HirId) -> BodyId { + pub fn body_owned_by(self, id: LocalDefId) -> BodyId { self.maybe_body_owned_by(id).unwrap_or_else(|| { + let hir_id = self.local_def_id_to_hir_id(id); span_bug!( - self.span(id), + self.span(hir_id), "body_owned_by: {} has no associated body", - self.node_to_string(id) + self.node_to_string(hir_id) ); }) } diff --git a/compiler/rustc_middle/src/hir/mod.rs b/compiler/rustc_middle/src/hir/mod.rs index a605e234be95f..3c0087493582f 100644 --- a/compiler/rustc_middle/src/hir/mod.rs +++ b/compiler/rustc_middle/src/hir/mod.rs @@ -157,8 +157,9 @@ pub fn provide(providers: &mut Providers) { }; providers.fn_arg_names = |tcx, id| { let hir = tcx.hir(); - let hir_id = hir.local_def_id_to_hir_id(id.expect_local()); - if let Some(body_id) = hir.maybe_body_owned_by(hir_id) { + let local_did = id.expect_local(); + let hir_id = hir.local_def_id_to_hir_id(local_did); + if let Some(body_id) = hir.maybe_body_owned_by(local_did) { tcx.arena.alloc_from_iter(hir.body_param_names(body_id)) } else if let Node::TraitItem(&TraitItem { kind: TraitItemKind::Fn(_, TraitFn::Required(idents)), diff --git a/compiler/rustc_mir_build/src/thir/cx/mod.rs b/compiler/rustc_mir_build/src/thir/cx/mod.rs index 35a0afd6813a8..f7351a4caa954 100644 --- a/compiler/rustc_mir_build/src/thir/cx/mod.rs +++ b/compiler/rustc_mir_build/src/thir/cx/mod.rs @@ -21,7 +21,7 @@ pub(crate) fn thir_body<'tcx>( owner_def: ty::WithOptConstParam, ) -> Result<(&'tcx Steal>, ExprId), ErrorGuaranteed> { let hir = tcx.hir(); - let body = hir.body(hir.body_owned_by(hir.local_def_id_to_hir_id(owner_def.did))); + let body = hir.body(hir.body_owned_by(owner_def.did)); let mut cx = Cx::new(tcx, owner_def); if let Some(reported) = cx.typeck_results.tainted_by_errors { return Err(reported); diff --git a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs index 3435f127c72e2..c6e38520f640d 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs @@ -26,7 +26,7 @@ use rustc_span::{BytePos, Span}; pub(crate) fn check_match(tcx: TyCtxt<'_>, def_id: DefId) { let body_id = match def_id.as_local() { None => return, - Some(id) => tcx.hir().body_owned_by(tcx.hir().local_def_id_to_hir_id(id)), + Some(did) => tcx.hir().body_owned_by(did), }; let pattern_arena = TypedArena::default(); diff --git a/compiler/rustc_mir_transform/src/check_unsafety.rs b/compiler/rustc_mir_transform/src/check_unsafety.rs index a2ad96cfc16d2..13ae8a1cd3e20 100644 --- a/compiler/rustc_mir_transform/src/check_unsafety.rs +++ b/compiler/rustc_mir_transform/src/check_unsafety.rs @@ -464,15 +464,15 @@ fn check_unused_unsafe( def_id: LocalDefId, used_unsafe_blocks: &FxHashMap, ) -> Vec<(HirId, UnusedUnsafe)> { - let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); - let body_id = tcx.hir().maybe_body_owned_by(hir_id); + let body_id = tcx.hir().maybe_body_owned_by(def_id); let Some(body_id) = body_id else { debug!("check_unused_unsafe({:?}) - no body found", def_id); return vec![]; }; - let body = tcx.hir().body(body_id); + let body = tcx.hir().body(body_id); + let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); let context = match tcx.hir().fn_sig_by_hir_id(hir_id) { Some(sig) if sig.header.unsafety == hir::Unsafety::Unsafe => Context::UnsafeFn(hir_id), _ => Context::Safe, diff --git a/compiler/rustc_passes/src/upvars.rs b/compiler/rustc_passes/src/upvars.rs index 97a461272b468..6f3b0eea4b59d 100644 --- a/compiler/rustc_passes/src/upvars.rs +++ b/compiler/rustc_passes/src/upvars.rs @@ -15,8 +15,8 @@ pub fn provide(providers: &mut Providers) { return None; } - let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); - let body = tcx.hir().body(tcx.hir().maybe_body_owned_by(hir_id)?); + let local_did = def_id.expect_local(); + let body = tcx.hir().body(tcx.hir().maybe_body_owned_by(local_did)?); let mut local_collector = LocalCollector::default(); local_collector.visit_body(body); 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 6c8faed0df486..b000774ed2a29 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -1783,8 +1783,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { let generator_body = generator_did .as_local() - .map(|def_id| hir.local_def_id_to_hir_id(def_id)) - .and_then(|hir_id| hir.maybe_body_owned_by(hir_id)) + .and_then(|local_did| hir.maybe_body_owned_by(local_did)) .map(|body_id| hir.body(body_id)); let is_async = match generator_did.as_local() { Some(_) => generator_body @@ -2752,7 +2751,9 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { let body_hir_id = obligation.cause.body_id; let item_id = self.tcx.hir().get_parent_node(body_hir_id); - if let Some(body_id) = self.tcx.hir().maybe_body_owned_by(item_id) { + if let Some(body_id) = + self.tcx.hir().maybe_body_owned_by(self.tcx.hir().local_def_id(item_id)) + { let body = self.tcx.hir().body(body_id); if let Some(hir::GeneratorKind::Async(_)) = body.generator_kind { let future_trait = self.tcx.require_lang_item(LangItem::Future, None); diff --git a/compiler/rustc_ty_utils/src/ty.rs b/compiler/rustc_ty_utils/src/ty.rs index d542a9b599724..f437eb98942a5 100644 --- a/compiler/rustc_ty_utils/src/ty.rs +++ b/compiler/rustc_ty_utils/src/ty.rs @@ -207,9 +207,10 @@ fn param_env(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ParamEnv<'_> { constness, ); - let body_id = hir_id.map_or(hir::CRATE_HIR_ID, |id| { - tcx.hir().maybe_body_owned_by(id).map_or(id, |body| body.hir_id) - }); + let body_id = local_did + .and_then(|id| tcx.hir().maybe_body_owned_by(id).map(|body| body.hir_id)) + .or(hir_id) + .map_or(hir::CRATE_HIR_ID, |did| did); let cause = traits::ObligationCause::misc(tcx.def_span(def_id), body_id); traits::normalize_param_env_or_error(tcx, unnormalized_env, cause) } diff --git a/compiler/rustc_typeck/src/check/expr.rs b/compiler/rustc_typeck/src/check/expr.rs index ba5ef5edc8630..2116e2db5f569 100644 --- a/compiler/rustc_typeck/src/check/expr.rs +++ b/compiler/rustc_typeck/src/check/expr.rs @@ -768,7 +768,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // the first place. 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_id = + self.tcx.hir().body_owned_by(self.tcx.hir().local_def_id(encl_body_owner_id)); let encl_body = self.tcx.hir().body(encl_body_id); err.encl_body_span = Some(encl_body.value.span); diff --git a/compiler/rustc_typeck/src/check/inherited.rs b/compiler/rustc_typeck/src/check/inherited.rs index a499179b95f10..99895dc122452 100644 --- a/compiler/rustc_typeck/src/check/inherited.rs +++ b/compiler/rustc_typeck/src/check/inherited.rs @@ -107,8 +107,7 @@ impl<'tcx> InheritedBuilder<'tcx> { impl<'a, 'tcx> Inherited<'a, 'tcx> { fn new(infcx: InferCtxt<'a, 'tcx>, def_id: LocalDefId) -> Self { let tcx = infcx.tcx; - let item_id = tcx.hir().local_def_id_to_hir_id(def_id); - let body_id = tcx.hir().maybe_body_owned_by(item_id); + let body_id = tcx.hir().maybe_body_owned_by(def_id); let typeck_results = infcx.in_progress_typeck_results.expect("building `FnCtxt` without typeck results"); diff --git a/compiler/rustc_typeck/src/check/region.rs b/compiler/rustc_typeck/src/check/region.rs index 0c33a243e108b..0081e9049eeca 100644 --- a/compiler/rustc_typeck/src/check/region.rs +++ b/compiler/rustc_typeck/src/check/region.rs @@ -814,8 +814,7 @@ pub fn region_scope_tree(tcx: TyCtxt<'_>, def_id: DefId) -> &ScopeTree { return tcx.region_scope_tree(typeck_root_def_id); } - let id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); - let scope_tree = if let Some(body_id) = tcx.hir().maybe_body_owned_by(id) { + let scope_tree = if let Some(body_id) = tcx.hir().maybe_body_owned_by(def_id.expect_local()) { let mut visitor = RegionResolutionVisitor { tcx, scope_tree: ScopeTree::default(), diff --git a/src/librustdoc/clean/utils.rs b/src/librustdoc/clean/utils.rs index 27ecea5cc40bd..621f70f0da986 100644 --- a/src/librustdoc/clean/utils.rs +++ b/src/librustdoc/clean/utils.rs @@ -236,8 +236,7 @@ pub(crate) fn print_const(cx: &DocContext<'_>, n: ty::Const<'_>) -> String { match n.kind() { ty::ConstKind::Unevaluated(ty::Unevaluated { def, substs: _, promoted }) => { let mut s = if let Some(def) = def.as_local() { - let hir_id = cx.tcx.hir().local_def_id_to_hir_id(def.did); - print_const_expr(cx.tcx, cx.tcx.hir().body_owned_by(hir_id)) + print_const_expr(cx.tcx, cx.tcx.hir().body_owned_by(def.did)) } else { inline::print_inlined_const(cx.tcx, def.did) }; diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index f46fde7b35a6d..c48b25aea4a37 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -313,7 +313,7 @@ pub(crate) fn create_config( } let hir = tcx.hir(); - let body = hir.body(hir.body_owned_by(hir.local_def_id_to_hir_id(def_id))); + let body = hir.body(hir.body_owned_by(def_id)); debug!("visiting body for {:?}", def_id); EmitIgnoredResolutionErrors::new(tcx).visit_body(body); (rustc_interface::DEFAULT_QUERY_PROVIDERS.typeck)(tcx, def_id) diff --git a/src/librustdoc/scrape_examples.rs b/src/librustdoc/scrape_examples.rs index c0fe8b49cfd1b..fd0b19034a259 100644 --- a/src/librustdoc/scrape_examples.rs +++ b/src/librustdoc/scrape_examples.rs @@ -143,8 +143,7 @@ where // then we need to exit before calling typeck (which will panic). See // test/run-make/rustdoc-scrape-examples-invalid-expr for an example. let hir = tcx.hir(); - let owner = hir.local_def_id_to_hir_id(ex.hir_id.owner); - if hir.maybe_body_owned_by(owner).is_none() { + if hir.maybe_body_owned_by(ex.hir_id.owner).is_none() { return; } diff --git a/src/tools/clippy/clippy_lints/src/utils/author.rs b/src/tools/clippy/clippy_lints/src/utils/author.rs index bbb04c9945a09..59e07313f549e 100644 --- a/src/tools/clippy/clippy_lints/src/utils/author.rs +++ b/src/tools/clippy/clippy_lints/src/utils/author.rs @@ -138,7 +138,7 @@ impl<'tcx> LateLintPass<'tcx> for Author { fn check_item(cx: &LateContext<'_>, hir_id: HirId) { let hir = cx.tcx.hir(); - if let Some(body_id) = hir.maybe_body_owned_by(hir_id) { + if let Some(body_id) = hir.maybe_body_owned_by(hir.local_def_id(hir_id)) { check_node(cx, hir_id, |v| { v.expr(&v.bind("expr", &hir.body(body_id).value)); }); From 16513d689e4fe30e58a37dd773af5be9210ebcbd Mon Sep 17 00:00:00 2001 From: Miguel Guarniz Date: Tue, 19 Jul 2022 17:06:52 -0400 Subject: [PATCH 2/4] Rename local_did to def_id Signed-off-by: Miguel Guarniz --- compiler/rustc_driver/src/pretty.rs | 2 +- .../error_reporting/nice_region_error/util.rs | 6 +++--- compiler/rustc_metadata/src/rmeta/encoder.rs | 16 ++++++++-------- compiler/rustc_middle/src/hir/mod.rs | 6 +++--- .../src/thir/pattern/check_match.rs | 2 +- compiler/rustc_passes/src/upvars.rs | 4 ++-- .../src/traits/error_reporting/suggestions.rs | 2 +- compiler/rustc_ty_utils/src/ty.rs | 12 ++++++++---- .../clippy/clippy_lints/src/utils/author.rs | 2 +- 9 files changed, 28 insertions(+), 24 deletions(-) diff --git a/compiler/rustc_driver/src/pretty.rs b/compiler/rustc_driver/src/pretty.rs index a514f1a9489f9..f66b1a2976f1c 100644 --- a/compiler/rustc_driver/src/pretty.rs +++ b/compiler/rustc_driver/src/pretty.rs @@ -328,7 +328,7 @@ impl<'tcx> pprust_hir::PpAnn for TypedAnnotation<'tcx> { let typeck_results = self.maybe_typeck_results.get().or_else(|| { self.tcx .hir() - .maybe_body_owned_by(self.tcx.hir().local_def_id(expr.hir_id)) + .maybe_body_owned_by(expr.hir_id.owner) .map(|body_id| self.tcx.typeck_body(body_id)) }); diff --git a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/util.rs b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/util.rs index 4c63024367c6c..3e9d491af62d3 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/util.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/util.rs @@ -49,8 +49,8 @@ pub fn find_param_with_region<'tcx>( }; let hir = &tcx.hir(); - let local_did = id.as_local()?; - let hir_id = hir.local_def_id_to_hir_id(local_did); + let def_id = id.as_local()?; + let hir_id = hir.local_def_id_to_hir_id(def_id); // FIXME: use def_kind // Don't perform this on closures @@ -61,7 +61,7 @@ pub fn find_param_with_region<'tcx>( _ => {} } - let body_id = hir.maybe_body_owned_by(local_did)?; + let body_id = hir.maybe_body_owned_by(def_id)?; let owner_id = hir.body_owner(body_id); let fn_decl = hir.fn_decl_by_hir_id(owner_id).unwrap(); diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index d2ed6e08ff713..f0886036899a2 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1614,16 +1614,16 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } fn encode_info_for_anon_const(&mut self, id: hir::HirId) { - let local_did = self.tcx.hir().local_def_id(id); - debug!("EncodeContext::encode_info_for_anon_const({:?})", local_did); - let body_id = self.tcx.hir().body_owned_by(local_did); + let def_id = self.tcx.hir().local_def_id(id); + debug!("EncodeContext::encode_info_for_anon_const({:?})", def_id); + let body_id = self.tcx.hir().body_owned_by(def_id); let const_data = self.encode_rendered_const_for_body(body_id); - let qualifs = self.tcx.mir_const_qualif(local_did); + let qualifs = self.tcx.mir_const_qualif(def_id); - record!(self.tables.kind[local_did.to_def_id()] <- EntryKind::AnonConst); - record!(self.tables.mir_const_qualif[local_did.to_def_id()] <- qualifs); - record!(self.tables.rendered_const[local_did.to_def_id()] <- const_data); - self.encode_item_type(local_did.to_def_id()); + record!(self.tables.kind[def_id.to_def_id()] <- EntryKind::AnonConst); + record!(self.tables.mir_const_qualif[def_id.to_def_id()] <- qualifs); + record!(self.tables.rendered_const[def_id.to_def_id()] <- const_data); + self.encode_item_type(def_id.to_def_id()); } fn encode_native_libraries(&mut self) -> LazyArray { diff --git a/compiler/rustc_middle/src/hir/mod.rs b/compiler/rustc_middle/src/hir/mod.rs index 3c0087493582f..211a614717f8b 100644 --- a/compiler/rustc_middle/src/hir/mod.rs +++ b/compiler/rustc_middle/src/hir/mod.rs @@ -157,9 +157,9 @@ pub fn provide(providers: &mut Providers) { }; providers.fn_arg_names = |tcx, id| { let hir = tcx.hir(); - let local_did = id.expect_local(); - let hir_id = hir.local_def_id_to_hir_id(local_did); - if let Some(body_id) = hir.maybe_body_owned_by(local_did) { + let def_id = id.expect_local(); + let hir_id = hir.local_def_id_to_hir_id(def_id); + if let Some(body_id) = hir.maybe_body_owned_by(def_id) { tcx.arena.alloc_from_iter(hir.body_param_names(body_id)) } else if let Node::TraitItem(&TraitItem { kind: TraitItemKind::Fn(_, TraitFn::Required(idents)), diff --git a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs index c6e38520f640d..063c076474e90 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs @@ -26,7 +26,7 @@ use rustc_span::{BytePos, Span}; pub(crate) fn check_match(tcx: TyCtxt<'_>, def_id: DefId) { let body_id = match def_id.as_local() { None => return, - Some(did) => tcx.hir().body_owned_by(did), + Some(def_id) => tcx.hir().body_owned_by(def_id), }; let pattern_arena = TypedArena::default(); diff --git a/compiler/rustc_passes/src/upvars.rs b/compiler/rustc_passes/src/upvars.rs index 6f3b0eea4b59d..68d9bf22bf9c0 100644 --- a/compiler/rustc_passes/src/upvars.rs +++ b/compiler/rustc_passes/src/upvars.rs @@ -15,8 +15,8 @@ pub fn provide(providers: &mut Providers) { return None; } - let local_did = def_id.expect_local(); - let body = tcx.hir().body(tcx.hir().maybe_body_owned_by(local_did)?); + let local_def_id = def_id.expect_local(); + let body = tcx.hir().body(tcx.hir().maybe_body_owned_by(local_def_id)?); let mut local_collector = LocalCollector::default(); local_collector.visit_body(body); 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 b000774ed2a29..03a8de7c760f6 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -1783,7 +1783,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { let generator_body = generator_did .as_local() - .and_then(|local_did| hir.maybe_body_owned_by(local_did)) + .and_then(|def_id| hir.maybe_body_owned_by(def_id)) .map(|body_id| hir.body(body_id)); let is_async = match generator_did.as_local() { Some(_) => generator_body diff --git a/compiler/rustc_ty_utils/src/ty.rs b/compiler/rustc_ty_utils/src/ty.rs index f437eb98942a5..b1af3051719e8 100644 --- a/compiler/rustc_ty_utils/src/ty.rs +++ b/compiler/rustc_ty_utils/src/ty.rs @@ -207,10 +207,14 @@ fn param_env(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ParamEnv<'_> { constness, ); - let body_id = local_did - .and_then(|id| tcx.hir().maybe_body_owned_by(id).map(|body| body.hir_id)) - .or(hir_id) - .map_or(hir::CRATE_HIR_ID, |did| did); + let body_id = + local_did.and_then(|id| tcx.hir().maybe_body_owned_by(id).map(|body| body.hir_id)); + let body_id = match body_id { + Some(id) => id, + None if hir_id.is_some() => hir_id.unwrap(), + _ => hir::CRATE_HIR_ID, + }; + let cause = traits::ObligationCause::misc(tcx.def_span(def_id), body_id); traits::normalize_param_env_or_error(tcx, unnormalized_env, cause) } diff --git a/src/tools/clippy/clippy_lints/src/utils/author.rs b/src/tools/clippy/clippy_lints/src/utils/author.rs index 59e07313f549e..c0726868f77e2 100644 --- a/src/tools/clippy/clippy_lints/src/utils/author.rs +++ b/src/tools/clippy/clippy_lints/src/utils/author.rs @@ -138,7 +138,7 @@ impl<'tcx> LateLintPass<'tcx> for Author { fn check_item(cx: &LateContext<'_>, hir_id: HirId) { let hir = cx.tcx.hir(); - if let Some(body_id) = hir.maybe_body_owned_by(hir.local_def_id(hir_id)) { + if let Some(body_id) = hir.maybe_body_owned_by(hir_id.expect_owner()) { check_node(cx, hir_id, |v| { v.expr(&v.bind("expr", &hir.body(body_id).value)); }); From 0c609a4c1fc182298b920c61d1b32c9a1deb5776 Mon Sep 17 00:00:00 2001 From: Miguel Guarniz Date: Tue, 19 Jul 2022 17:47:49 -0400 Subject: [PATCH 3/4] Change enclosing_body_owner to return LocalDefId Signed-off-by: Miguel Guarniz --- .../rustc_borrowck/src/diagnostics/mutability_errors.rs | 3 +-- compiler/rustc_middle/src/hir/map/mod.rs | 8 ++++---- compiler/rustc_mir_build/src/check_unsafety.rs | 2 +- compiler/rustc_typeck/src/check/expr.rs | 5 ++--- compiler/rustc_typeck/src/check/fn_ctxt/checks.rs | 3 ++- compiler/rustc_typeck/src/collect/type_of.rs | 4 ++-- src/tools/clippy/clippy_utils/src/lib.rs | 2 +- 7 files changed, 13 insertions(+), 14 deletions(-) diff --git a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs index 8134e1226628f..ac26bc9c7f44f 100644 --- a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs @@ -853,7 +853,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { let closure_id = self.mir_hir_id(); let fn_call_id = hir.get_parent_node(closure_id); let node = hir.get(fn_call_id); - let item_id = hir.enclosing_body_owner(fn_call_id); + let def_id = hir.enclosing_body_owner(fn_call_id); let mut look_at_return = true; // If we can detect the expression to be an `fn` call where the closure was an argument, // we point at the `fn` definition argument... @@ -864,7 +864,6 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { .filter(|(_, arg)| arg.hir_id == closure_id) .map(|(pos, _)| pos) .next(); - let def_id = hir.local_def_id(item_id); let tables = self.infcx.tcx.typeck(def_id); if let Some(ty::FnDef(def_id, _)) = tables.node_type_opt(func.hir_id).as_ref().map(|ty| ty.kind()) diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index adb591fe09c5a..47b04c33ec1cd 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -396,10 +396,10 @@ impl<'hir> Map<'hir> { } } - pub fn enclosing_body_owner(self, hir_id: HirId) -> HirId { + pub fn enclosing_body_owner(self, hir_id: HirId) -> LocalDefId { for (parent, _) in self.parent_iter(hir_id) { - if let Some(local_did) = parent.as_owner() && let Some(body) = self.maybe_body_owned_by(local_did) { - return self.body_owner(body); + if let Some(body) = self.find(parent).map(associated_body).flatten() { + return self.body_owner_def_id(body); } } @@ -671,7 +671,7 @@ impl<'hir> Map<'hir> { /// Whether the expression pointed at by `hir_id` belongs to a `const` evaluation context. /// Used exclusively for diagnostics, to avoid suggestion function calls. pub fn is_inside_const_context(self, hir_id: HirId) -> bool { - self.body_const_context(self.local_def_id(self.enclosing_body_owner(hir_id))).is_some() + self.body_const_context(self.enclosing_body_owner(hir_id)).is_some() } /// Retrieves the `HirId` for `id`'s enclosing method, unless there's a diff --git a/compiler/rustc_mir_build/src/check_unsafety.rs b/compiler/rustc_mir_build/src/check_unsafety.rs index 1f0d0ce04aaa3..55ad83f897584 100644 --- a/compiler/rustc_mir_build/src/check_unsafety.rs +++ b/compiler/rustc_mir_build/src/check_unsafety.rs @@ -626,7 +626,7 @@ pub fn check_unsafety<'tcx>(tcx: TyCtxt<'tcx>, def: ty::WithOptConstParam FnCtxt<'a, 'tcx> { // If this didn't hold, we would not have to report an error in // the first place. - assert_ne!(hir::HirId::make_owner(encl_item_id), encl_body_owner_id); + assert_ne!(encl_item_id, encl_body_owner_id); - let encl_body_id = - self.tcx.hir().body_owned_by(self.tcx.hir().local_def_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); err.encl_body_span = Some(encl_body.value.span); diff --git a/compiler/rustc_typeck/src/check/fn_ctxt/checks.rs b/compiler/rustc_typeck/src/check/fn_ctxt/checks.rs index eb22938fb61c4..c1c021a6d7d9e 100644 --- a/compiler/rustc_typeck/src/check/fn_ctxt/checks.rs +++ b/compiler/rustc_typeck/src/check/fn_ctxt/checks.rs @@ -58,7 +58,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { debug!("FnCtxt::check_asm: {} deferred checks", deferred_asm_checks.len()); for (asm, hir_id) in deferred_asm_checks.drain(..) { let enclosing_id = self.tcx.hir().enclosing_body_owner(hir_id); - InlineAsmCtxt::new_in_fn(self).check_asm(asm, enclosing_id); + InlineAsmCtxt::new_in_fn(self) + .check_asm(asm, self.tcx.hir().local_def_id_to_hir_id(enclosing_id)); } } diff --git a/compiler/rustc_typeck/src/collect/type_of.rs b/compiler/rustc_typeck/src/collect/type_of.rs index 64ac655e0c393..534ddfa9531c1 100644 --- a/compiler/rustc_typeck/src/collect/type_of.rs +++ b/compiler/rustc_typeck/src/collect/type_of.rs @@ -100,7 +100,7 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option< ExprKind::MethodCall(segment, ..) | ExprKind::Path(QPath::TypeRelative(_, segment)), .. }) => { - let body_owner = tcx.hir().local_def_id(tcx.hir().enclosing_body_owner(hir_id)); + let body_owner = tcx.hir().enclosing_body_owner(hir_id); let tables = tcx.typeck(body_owner); // This may fail in case the method/path does not actually exist. // As there is no relevant param for `def_id`, we simply return @@ -134,7 +134,7 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option< | ExprKind::Struct(&QPath::Resolved(_, path), ..), .. }) => { - let body_owner = tcx.hir().local_def_id(tcx.hir().enclosing_body_owner(hir_id)); + let body_owner = tcx.hir().enclosing_body_owner(hir_id); let _tables = tcx.typeck(body_owner); &*path } diff --git a/src/tools/clippy/clippy_utils/src/lib.rs b/src/tools/clippy/clippy_utils/src/lib.rs index 34a1cdaf1d52a..50bb008098dcb 100644 --- a/src/tools/clippy/clippy_utils/src/lib.rs +++ b/src/tools/clippy/clippy_utils/src/lib.rs @@ -1353,7 +1353,7 @@ pub fn is_integer_const(cx: &LateContext<'_>, e: &Expr<'_>, value: u128) -> bool if is_integer_literal(e, value) { return true; } - let enclosing_body = cx.tcx.hir().local_def_id(cx.tcx.hir().enclosing_body_owner(e.hir_id)); + let enclosing_body = cx.tcx.hir().enclosing_body_owner(e.hir_id); if let Some((Constant::Int(v), _)) = constant(cx, cx.tcx.typeck(enclosing_body), e) { return value == v; } From 94611b81c7f002032baabb526b587d208c886f8e Mon Sep 17 00:00:00 2001 From: Miguel Guarniz Date: Tue, 19 Jul 2022 22:51:52 -0400 Subject: [PATCH 4/4] Avoid ICE when fetching LocalDefId Signed-off-by: Miguel Guarniz --- src/tools/clippy/clippy_lints/src/methods/suspicious_map.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/tools/clippy/clippy_lints/src/methods/suspicious_map.rs b/src/tools/clippy/clippy_lints/src/methods/suspicious_map.rs index 18ded291915e1..9c3375bf35e7d 100644 --- a/src/tools/clippy/clippy_lints/src/methods/suspicious_map.rs +++ b/src/tools/clippy/clippy_lints/src/methods/suspicious_map.rs @@ -12,7 +12,8 @@ pub fn check<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>, count_recv: &hi if_chain! { if is_trait_method(cx, count_recv, sym::Iterator); let closure = expr_or_init(cx, map_arg); - if let Some(body_id) = cx.tcx.hir().maybe_body_owned_by(closure.hir_id); + if let Some(def_id) = cx.tcx.hir().opt_local_def_id(closure.hir_id); + if let Some(body_id) = cx.tcx.hir().maybe_body_owned_by(def_id); let closure_body = cx.tcx.hir().body(body_id); if !cx.typeck_results().expr_ty(&closure_body.value).is_unit(); then {