From fcfb3e92a016283516d6472eda0a2485453fcbdf Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Fri, 15 Jul 2022 05:23:47 +0000 Subject: [PATCH] Remove some more usages of guess_head_span --- .../src/traits/error_reporting/mod.rs | 9 +--- .../src/traits/error_reporting/suggestions.rs | 1 - .../rustc_typeck/src/check/compare_method.rs | 41 ++++++++----------- 3 files changed, 19 insertions(+), 32 deletions(-) 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 247c34379ec7d..39fce3cf76935 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs @@ -823,10 +823,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind) => { let found_kind = self.closure_kind(closure_substs).unwrap(); - let closure_span = - self.tcx.sess.source_map().guess_head_span( - self.tcx.hir().span_if_local(closure_def_id).unwrap(), - ); + let closure_span = self.tcx.def_span(closure_def_id); let mut err = struct_span_err!( self.tcx.sess, closure_span, @@ -951,9 +948,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { _ => None, }; - let found_span = found_did - .and_then(|did| self.tcx.hir().span_if_local(did)) - .map(|sp| self.tcx.sess.source_map().guess_head_span(sp)); // the sp could be a closure + let found_span = found_did.and_then(|did| self.tcx.hir().span_if_local(did)); if self.reported_closure_mismatch.borrow().contains(&(span, found_span)) { // We check closures twice, with obligations flowing in different directions, 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 ff4ef92f42e71..57d5e5436a691 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -1543,7 +1543,6 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { ty::Generator(..) => "generator", _ => "function", }; - let span = self.tcx.sess.source_map().guess_head_span(span); let mut err = struct_span_err!( self.tcx.sess, span, diff --git a/compiler/rustc_typeck/src/check/compare_method.rs b/compiler/rustc_typeck/src/check/compare_method.rs index 6020898dbe21c..3a31df32298d7 100644 --- a/compiler/rustc_typeck/src/check/compare_method.rs +++ b/compiler/rustc_typeck/src/check/compare_method.rs @@ -171,14 +171,7 @@ fn compare_predicate_entailment<'tcx>( let trait_m_predicates = tcx.predicates_of(trait_m.def_id); // Check region bounds. - check_region_bounds_on_impl_item( - tcx, - impl_m_span, - impl_m, - trait_m, - &trait_m_generics, - &impl_m_generics, - )?; + check_region_bounds_on_impl_item(tcx, impl_m, trait_m, &trait_m_generics, &impl_m_generics)?; // Create obligations for each predicate declared by the impl // definition in the context of the trait's parameter @@ -410,7 +403,6 @@ fn compare_predicate_entailment<'tcx>( fn check_region_bounds_on_impl_item<'tcx>( tcx: TyCtxt<'tcx>, - span: Span, impl_m: &ty::AssocItem, trait_m: &ty::AssocItem, trait_generics: &ty::Generics, @@ -436,23 +428,25 @@ fn check_region_bounds_on_impl_item<'tcx>( // are zero. Since I don't quite know how to phrase things at // the moment, give a kind of vague error message. if trait_params != impl_params { - let item_kind = assoc_item_kind_str(impl_m); - let span = impl_m - .def_id - .as_local() - .and_then(|did| tcx.hir().get_generics(did)) - .map_or(span, |g| g.span); - let generics_span = tcx.hir().span_if_local(trait_m.def_id).map(|sp| { - trait_m - .def_id - .as_local() - .and_then(|did| tcx.hir().get_generics(did)) - .map_or(sp, |g| g.span) - }); + let span = tcx + .hir() + .get_generics(impl_m.def_id.expect_local()) + .expect("expected impl item to have generics or else we can't compare them") + .span; + let generics_span = if let Some(local_def_id) = trait_m.def_id.as_local() { + Some( + tcx.hir() + .get_generics(local_def_id) + .expect("expected trait item to have generics or else we can't compare them") + .span, + ) + } else { + None + }; let reported = tcx.sess.emit_err(LifetimesOrBoundsMismatchOnTrait { span, - item_kind, + item_kind: assoc_item_kind_str(impl_m), ident: impl_m.ident(tcx), generics_span, }); @@ -1201,7 +1195,6 @@ fn compare_type_predicate_entailment<'tcx>( check_region_bounds_on_impl_item( tcx, - impl_ty_span, impl_ty, trait_ty, &trait_ty_generics,