1
Fork 0

Avoid creating a fn sig type just to unwrap it again to get at its signature

This commit is contained in:
Oli Scherer 2024-12-16 14:59:05 +00:00
parent 83ab648e00
commit bdb88c9d4a

View file

@ -9,7 +9,7 @@ use rustc_middle::hir::nested_filter;
use rustc_middle::traits::ObligationCauseCode; use rustc_middle::traits::ObligationCauseCode;
use rustc_middle::ty::error::ExpectedFound; use rustc_middle::ty::error::ExpectedFound;
use rustc_middle::ty::print::RegionHighlightMode; use rustc_middle::ty::print::RegionHighlightMode;
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitor}; use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitable};
use rustc_span::Span; use rustc_span::Span;
use tracing::debug; use tracing::debug;
@ -39,12 +39,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
{ {
// FIXME(compiler-errors): Don't like that this needs `Ty`s, but // FIXME(compiler-errors): Don't like that this needs `Ty`s, but
// all of the region highlighting machinery only deals with those. // all of the region highlighting machinery only deals with those.
let guar = self.emit_err( let guar = self.emit_err(var_origin.span(), expected, found, trait_item_def_id);
var_origin.span(),
Ty::new_fn_ptr(self.cx.tcx, expected),
Ty::new_fn_ptr(self.cx.tcx, found),
*trait_item_def_id,
);
return Some(guar); return Some(guar);
} }
None None
@ -53,8 +48,8 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
fn emit_err( fn emit_err(
&self, &self,
sp: Span, sp: Span,
expected: Ty<'tcx>, expected: ty::PolyFnSig<'tcx>,
found: Ty<'tcx>, found: ty::PolyFnSig<'tcx>,
trait_def_id: DefId, trait_def_id: DefId,
) -> ErrorGuaranteed { ) -> ErrorGuaranteed {
let trait_sp = self.tcx().def_span(trait_def_id); let trait_sp = self.tcx().def_span(trait_def_id);
@ -67,10 +62,10 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
} }
impl<'tcx> HighlightBuilder<'tcx> { impl<'tcx> HighlightBuilder<'tcx> {
fn build(ty: Ty<'tcx>) -> RegionHighlightMode<'tcx> { fn build(sig: ty::PolyFnSig<'tcx>) -> RegionHighlightMode<'tcx> {
let mut builder = let mut builder =
HighlightBuilder { highlight: RegionHighlightMode::default(), counter: 1 }; HighlightBuilder { highlight: RegionHighlightMode::default(), counter: 1 };
builder.visit_ty(ty); sig.visit_with(&mut builder);
builder.highlight builder.highlight
} }
} }
@ -85,13 +80,22 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
} }
let expected_highlight = HighlightBuilder::build(expected); let expected_highlight = HighlightBuilder::build(expected);
let tcx = self.cx.tcx;
let expected = self let expected = self
.cx .cx
.extract_inference_diagnostics_data(expected.into(), Some(expected_highlight)) .extract_inference_diagnostics_data(
Ty::new_fn_ptr(tcx, expected).into(),
Some(expected_highlight),
)
.name; .name;
let found_highlight = HighlightBuilder::build(found); let found_highlight = HighlightBuilder::build(found);
let found = let found = self
self.cx.extract_inference_diagnostics_data(found.into(), Some(found_highlight)).name; .cx
.extract_inference_diagnostics_data(
Ty::new_fn_ptr(tcx, found).into(),
Some(found_highlight),
)
.name;
// Get the span of all the used type parameters in the method. // Get the span of all the used type parameters in the method.
let assoc_item = self.tcx().associated_item(trait_def_id); let assoc_item = self.tcx().associated_item(trait_def_id);