Move some stuff to TypeErrCtxt

This commit is contained in:
Michael Goulet 2024-07-21 14:06:24 -04:00
parent 0f8534e79e
commit fc1e7ceb4b
8 changed files with 31 additions and 27 deletions

View File

@ -457,8 +457,11 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, '_, 'tcx> {
) -> RegionNameHighlight { ) -> RegionNameHighlight {
let mut highlight = RegionHighlightMode::default(); let mut highlight = RegionHighlightMode::default();
highlight.highlighting_region_vid(self.infcx.tcx, needle_fr, counter); highlight.highlighting_region_vid(self.infcx.tcx, needle_fr, counter);
let type_name = let type_name = self
self.infcx.extract_inference_diagnostics_data(ty.into(), Some(highlight)).name; .infcx
.err_ctxt()
.extract_inference_diagnostics_data(ty.into(), Some(highlight))
.name;
debug!( debug!(
"highlight_if_we_cannot_match_hir_ty: type_name={:?} needle_fr={:?}", "highlight_if_we_cannot_match_hir_ty: type_name={:?} needle_fr={:?}",
@ -872,8 +875,11 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, '_, 'tcx> {
let mut highlight = RegionHighlightMode::default(); let mut highlight = RegionHighlightMode::default();
highlight.highlighting_region_vid(tcx, fr, *self.next_region_name.try_borrow().unwrap()); highlight.highlighting_region_vid(tcx, fr, *self.next_region_name.try_borrow().unwrap());
let type_name = let type_name = self
self.infcx.extract_inference_diagnostics_data(yield_ty.into(), Some(highlight)).name; .infcx
.err_ctxt()
.extract_inference_diagnostics_data(yield_ty.into(), Some(highlight))
.name;
let yield_span = match tcx.hir_node(self.mir_hir_id()) { let yield_span = match tcx.hir_node(self.mir_hir_id()) {
hir::Node::Expr(hir::Expr { hir::Node::Expr(hir::Expr {

View File

@ -2574,7 +2574,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
base: &'tcx hir::Expr<'tcx>, base: &'tcx hir::Expr<'tcx>,
ty: Ty<'tcx>, ty: Ty<'tcx>,
) { ) {
let Some(output_ty) = self.get_impl_future_output_ty(ty) else { let Some(output_ty) = self.err_ctxt().get_impl_future_output_ty(ty) else {
err.span_label(field_ident.span, "unknown field"); err.span_label(field_ident.span, "unknown field");
return; return;
}; };

View File

@ -1107,12 +1107,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.tcx .tcx
.instantiate_bound_regions_with_erased(Binder::bind_with_vars(ty, bound_vars)); .instantiate_bound_regions_with_erased(Binder::bind_with_vars(ty, bound_vars));
let ty = match self.tcx.asyncness(fn_id) { let ty = match self.tcx.asyncness(fn_id) {
ty::Asyncness::Yes => self.get_impl_future_output_ty(ty).unwrap_or_else(|| { ty::Asyncness::Yes => {
span_bug!( self.err_ctxt().get_impl_future_output_ty(ty).unwrap_or_else(|| {
fn_decl.output.span(), span_bug!(
"failed to get output type of async function" fn_decl.output.span(),
) "failed to get output type of async function"
}), )
})
}
ty::Asyncness::No => ty, ty::Asyncness::No => ty,
}; };
let ty = self.normalize(expr.span, ty); let ty = self.normalize(expr.span, ty);

View File

@ -3276,7 +3276,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
span: Span, span: Span,
return_type: Option<Ty<'tcx>>, return_type: Option<Ty<'tcx>>,
) { ) {
let output_ty = match self.get_impl_future_output_ty(ty) { let output_ty = match self.err_ctxt().get_impl_future_output_ty(ty) {
Some(output_ty) => self.resolve_vars_if_possible(output_ty), Some(output_ty) => self.resolve_vars_if_possible(output_ty),
_ => return, _ => return,
}; };

View File

@ -152,7 +152,7 @@ impl<'tcx> Deref for TypeErrCtxt<'_, 'tcx> {
} }
} }
impl<'tcx> InferCtxt<'tcx> { impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
pub fn get_impl_future_output_ty(&self, ty: Ty<'tcx>) -> Option<Ty<'tcx>> { pub fn get_impl_future_output_ty(&self, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
let (def_id, args) = match *ty.kind() { let (def_id, args) = match *ty.kind() {
ty::Alias(_, ty::AliasTy { def_id, args, .. }) ty::Alias(_, ty::AliasTy { def_id, args, .. })
@ -189,9 +189,7 @@ impl<'tcx> InferCtxt<'tcx> {
.flatten() .flatten()
}) })
} }
}
impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
/// Adds a note if the types come from similarly named crates /// Adds a note if the types come from similarly named crates
fn check_and_note_conflicting_crates(&self, err: &mut Diag<'_>, terr: TypeError<'tcx>) { fn check_and_note_conflicting_crates(&self, err: &mut Diag<'_>, terr: TypeError<'tcx>) {
use hir::def_id::CrateNum; use hir::def_id::CrateNum;

View File

@ -445,7 +445,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
}) })
} }
infer::CompareImplItemObligation { span, impl_item_def_id, trait_item_def_id } => { infer::CompareImplItemObligation { span, impl_item_def_id, trait_item_def_id } => {
let mut err = self.infcx.report_extra_impl_obligation( let mut err = self.report_extra_impl_obligation(
span, span,
impl_item_def_id, impl_item_def_id,
trait_item_def_id, trait_item_def_id,
@ -645,7 +645,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
trait_item_def_id, trait_item_def_id,
}) = origin }) = origin
{ {
return self.infcx.report_extra_impl_obligation( return self.report_extra_impl_obligation(
span, span,
impl_item_def_id, impl_item_def_id,
trait_item_def_id, trait_item_def_id,

View File

@ -289,7 +289,7 @@ fn closure_as_fn_str<'tcx>(infcx: &InferCtxt<'tcx>, ty: Ty<'tcx>) -> String {
format!("fn({args}){ret}") format!("fn({args}){ret}")
} }
impl<'tcx> InferCtxt<'tcx> { impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
/// Extracts data used by diagnostic for either types or constants /// Extracts data used by diagnostic for either types or constants
/// which were stuck during inference. /// which were stuck during inference.
pub fn extract_inference_diagnostics_data( pub fn extract_inference_diagnostics_data(
@ -391,7 +391,7 @@ impl<'tcx> InferCtxt<'tcx> {
span: Span, span: Span,
arg_data: InferenceDiagnosticsData, arg_data: InferenceDiagnosticsData,
error_code: TypeAnnotationNeeded, error_code: TypeAnnotationNeeded,
) -> Diag<'_> { ) -> Diag<'a> {
let source_kind = "other"; let source_kind = "other";
let source_name = ""; let source_name = "";
let failure_span = None; let failure_span = None;
@ -434,9 +434,7 @@ impl<'tcx> InferCtxt<'tcx> {
}), }),
} }
} }
}
impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
#[instrument(level = "debug", skip(self, error_code))] #[instrument(level = "debug", skip(self, error_code))]
pub fn emit_inference_failure_err( pub fn emit_inference_failure_err(
&self, &self,
@ -453,7 +451,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
// If we don't have any typeck results we're outside // If we don't have any typeck results we're outside
// of a body, so we won't be able to get better info // of a body, so we won't be able to get better info
// here. // here.
return self.infcx.bad_inference_failure_err(failure_span, arg_data, error_code); return self.bad_inference_failure_err(failure_span, arg_data, error_code);
}; };
let mut local_visitor = FindInferSourceVisitor::new(self, typeck_results, arg); let mut local_visitor = FindInferSourceVisitor::new(self, typeck_results, arg);
@ -465,7 +463,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
} }
let Some(InferSource { span, kind }) = local_visitor.infer_source else { let Some(InferSource { span, kind }) = local_visitor.infer_source else {
return self.infcx.bad_inference_failure_err(failure_span, arg_data, error_code); return self.bad_inference_failure_err(failure_span, arg_data, error_code);
}; };
let (source_kind, name, path) = kind.ty_localized_msg(self); let (source_kind, name, path) = kind.ty_localized_msg(self);

View File

@ -1,6 +1,6 @@
use super::ObjectSafetyViolation; use super::ObjectSafetyViolation;
use crate::infer::InferCtxt; use crate::error_reporting::infer::TypeErrCtxt;
use rustc_data_structures::fx::FxIndexSet; use rustc_data_structures::fx::FxIndexSet;
use rustc_errors::{codes::*, struct_span_code_err, Applicability, Diag, MultiSpan}; use rustc_errors::{codes::*, struct_span_code_err, Applicability, Diag, MultiSpan};
use rustc_hir as hir; use rustc_hir as hir;
@ -11,9 +11,9 @@ use rustc_span::Span;
use std::fmt; use std::fmt;
use std::iter; use std::iter;
impl<'tcx> InferCtxt<'tcx> { impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
pub fn report_extra_impl_obligation<'a>( pub fn report_extra_impl_obligation(
&'a self, &self,
error_span: Span, error_span: Span,
impl_item_def_id: LocalDefId, impl_item_def_id: LocalDefId,
trait_item_def_id: DefId, trait_item_def_id: DefId,