Skip to content

Move some diagnostic-relevant methods from InferCtxt to TypeErrCtxt #127503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use rustc_span::symbol::{kw, sym, Ident};
use rustc_span::{BytePos, Span, Symbol};
use rustc_trait_selection::infer::InferCtxtExt;
use rustc_trait_selection::traits::error_reporting::suggestions::TypeErrCtxtExt;
use rustc_trait_selection::traits::error_reporting::FindExprBySpan;
use rustc_trait_selection::traits::error_reporting::{FindExprBySpan, TypeErrCtxtExt as _};
use rustc_trait_selection::traits::{Obligation, ObligationCause, ObligationCtxt};
use std::iter;
use std::ops::ControlFlow;
Expand Down Expand Up @@ -1860,7 +1860,11 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
&& let ty::Ref(_, inner, _) = rcvr_ty.kind()
&& let inner = inner.peel_refs()
&& (Holds { ty: inner }).visit_ty(local_ty).is_break()
&& let None = self.infcx.type_implements_trait_shallow(clone, inner, self.param_env)
&& let None = self.infcx.err_ctxt().type_implements_trait_shallow(
clone,
inner,
self.param_env,
)
{
err.span_label(
span,
Expand Down
11 changes: 6 additions & 5 deletions compiler/rustc_borrowck/src/diagnostics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use rustc_span::{symbol::sym, Span, Symbol, DUMMY_SP};
use rustc_target::abi::{FieldIdx, VariantIdx};
use rustc_trait_selection::infer::InferCtxtExt;
use rustc_trait_selection::traits::error_reporting::suggestions::TypeErrCtxtExt as _;
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _;
use rustc_trait_selection::traits::{
type_known_to_meet_bound_modulo_regions, FulfillmentErrorCode,
};
Expand Down Expand Up @@ -1255,11 +1256,11 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, '_, 'tcx> {
} else {
vec![(move_span.shrink_to_hi(), ".clone()".to_string())]
};
if let Some(errors) = self.infcx.type_implements_trait_shallow(
clone_trait,
ty,
self.param_env,
) && !has_sugg
if let Some(errors) = self
.infcx
.err_ctxt()
.type_implements_trait_shallow(clone_trait, ty, self.param_env)
&& !has_sugg
{
let msg = match &errors[..] {
[] => "you can `clone` the value and consume it, but this \
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ use rustc_middle::{
use rustc_span::symbol::{kw, Symbol};
use rustc_span::{sym, BytePos, DesugaringKind, Span};
use rustc_target::abi::FieldIdx;
use rustc_trait_selection::infer::InferCtxtExt;
use rustc_trait_selection::traits;
use rustc_trait_selection::traits::error_reporting::suggestions::TypeErrCtxtExt;
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _;

use crate::diagnostics::BorrowedContentSource;
use crate::util::FindAssignments;
Expand Down Expand Up @@ -1233,6 +1233,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
{
match self
.infcx
.err_ctxt()
.type_implements_trait_shallow(
clone_trait,
ty.peel_refs(),
Expand Down
14 changes: 10 additions & 4 deletions compiler/rustc_borrowck/src/diagnostics/region_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,11 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, '_, 'tcx> {
) -> RegionNameHighlight {
let mut highlight = RegionHighlightMode::default();
highlight.highlighting_region_vid(self.infcx.tcx, needle_fr, counter);
let type_name =
self.infcx.extract_inference_diagnostics_data(ty.into(), Some(highlight)).name;
let type_name = self
.infcx
.err_ctxt()
.extract_inference_diagnostics_data(ty.into(), Some(highlight))
.name;

debug!(
"highlight_if_we_cannot_match_hir_ty: type_name={:?} needle_fr={:?}",
Expand Down Expand Up @@ -872,8 +875,11 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, '_, 'tcx> {

let mut highlight = RegionHighlightMode::default();
highlight.highlighting_region_vid(tcx, fr, *self.next_region_name.try_borrow().unwrap());
let type_name =
self.infcx.extract_inference_diagnostics_data(yield_ty.into(), Some(highlight)).name;
let type_name = self
.infcx
.err_ctxt()
.extract_inference_diagnostics_data(yield_ty.into(), Some(highlight))
.name;

let yield_span = match tcx.hir_node(self.mir_hir_id()) {
hir::Node::Expr(hir::Expr {
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir_typeck/src/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.return_position_impl_trait_from_match_expectation(orig_expected);

let (arm_block_id, arm_span) = if let hir::ExprKind::Block(blk, _) = arm.body.kind {
(Some(blk.hir_id), self.find_block_span(blk))
(Some(blk.hir_id), self.err_ctxt().find_block_span(blk))
} else {
(None, arm.body.span)
};
Expand Down Expand Up @@ -477,7 +477,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
*outer_span = outer_span.with_hi(cond_span.hi())
}

(self.find_block_span(block), block.hir_id)
(self.err_ctxt().find_block_span(block), block.hir_id)
} else {
(else_expr.span, else_expr.hir_id)
};
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_hir_typeck/src/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ use rustc_span::def_id::LocalDefId;
use rustc_span::Span;
use rustc_target::spec::abi::Abi;
use rustc_trait_selection::traits;
use rustc_trait_selection::traits::error_reporting::ArgKind;
use rustc_trait_selection::traits::error_reporting::InferCtxtExt as _;
use rustc_trait_selection::traits::error_reporting::{ArgKind, TypeErrCtxtExt};
use rustc_type_ir::ClosureKind;
use std::iter;
use std::ops::ControlFlow;
Expand Down Expand Up @@ -648,13 +647,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.map(|ty| ArgKind::from_expected_ty(*ty, None))
.collect();
let (closure_span, closure_arg_span, found_args) =
match self.get_fn_like_arguments(expr_map_node) {
match self.err_ctxt().get_fn_like_arguments(expr_map_node) {
Some((sp, arg_sp, args)) => (Some(sp), arg_sp, args),
None => (None, None, Vec::new()),
};
let expected_span =
expected_sig.cause_span.unwrap_or_else(|| self.tcx.def_span(expr_def_id));
let guar = self
.err_ctxt()
.report_arg_count_mismatch(
expected_span,
closure_span,
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir_typeck/src/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1665,8 +1665,8 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
found,
coercion_error,
);
let then_span = fcx.find_block_span_from_hir_id(then_id);
let else_span = fcx.find_block_span_from_hir_id(else_id);
let then_span = fcx.err_ctxt().find_block_span_from_hir_id(then_id);
let else_span = fcx.err_ctxt().find_block_span_from_hir_id(else_id);
// don't suggest wrapping either blocks in `if .. {} else {}`
let is_empty_arm = |id| {
let hir::Node::Block(blk) = fcx.tcx.hir_node(id) else {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2507,7 +2507,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
base: &'tcx hir::Expr<'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");
return;
};
Expand Down
23 changes: 14 additions & 9 deletions compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use rustc_trait_selection::infer::InferCtxtExt;
use rustc_trait_selection::traits;
use rustc_trait_selection::traits::error_reporting::suggestions::TypeErrCtxtExt;
use rustc_trait_selection::traits::error_reporting::DefIdOrName;
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _;
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _;

impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
Expand Down Expand Up @@ -1085,12 +1086,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.tcx
.instantiate_bound_regions_with_erased(Binder::bind_with_vars(ty, bound_vars));
let ty = match self.tcx.asyncness(fn_id) {
ty::Asyncness::Yes => self.get_impl_future_output_ty(ty).unwrap_or_else(|| {
span_bug!(
fn_decl.output.span(),
"failed to get output type of async function"
)
}),
ty::Asyncness::Yes => {
self.err_ctxt().get_impl_future_output_ty(ty).unwrap_or_else(|| {
span_bug!(
fn_decl.output.span(),
"failed to get output type of async function"
)
})
}
ty::Asyncness::No => ty,
};
let ty = self.normalize(expr.span, ty);
Expand Down Expand Up @@ -1715,9 +1718,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
None,
);
} else {
if let Some(errors) =
self.type_implements_trait_shallow(clone_trait_did, expected_ty, self.param_env)
{
if let Some(errors) = self.err_ctxt().type_implements_trait_shallow(
clone_trait_did,
expected_ty,
self.param_env,
) {
match &errors[..] {
[] => {}
[error] => {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3165,7 +3165,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
span: Span,
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),
_ => return,
};
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_infer/src/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ pub fn unexpected_hidden_region_diagnostic<'a, 'tcx>(
err
}

impl<'tcx> InferCtxt<'tcx> {
impl<'tcx> TypeErrCtxt<'_, 'tcx> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can merge the impl blocks here

pub fn get_impl_future_output_ty(&self, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
let (def_id, args) = match *ty.kind() {
ty::Alias(_, ty::AliasTy { def_id, args, .. })
Expand Down Expand Up @@ -2367,7 +2367,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
trait_item_def_id,
}) = origin
{
return self.infcx.report_extra_impl_obligation(
return self.report_extra_impl_obligation(
span,
impl_item_def_id,
trait_item_def_id,
Expand Down Expand Up @@ -2792,7 +2792,7 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for SameTypeModuloInfer<'_, 'tcx> {
}
}

impl<'tcx> InferCtxt<'tcx> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here 🤔

impl<'tcx> TypeErrCtxt<'_, 'tcx> {
fn report_inference_failure(&self, var_origin: RegionVariableOrigin) -> Diag<'_> {
let br_string = |br: ty::BoundRegionKind| {
let mut s = match br {
Expand Down Expand Up @@ -3031,7 +3031,7 @@ impl TyCategory {
}
}

impl<'tcx> InferCtxt<'tcx> {
impl<'tcx> TypeErrCtxt<'_, 'tcx> {
/// Given a [`hir::Block`], get the span of its last expression or
/// statement, peeling off any inner blocks.
pub fn find_block_span(&self, block: &'tcx hir::Block<'tcx>) -> Span {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ fn closure_as_fn_str<'tcx>(infcx: &InferCtxt<'tcx>, ty: Ty<'tcx>) -> String {
format!("fn({args}){ret}")
}

impl<'tcx> InferCtxt<'tcx> {
impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
/// Extracts data used by diagnostic for either types or constants
/// which were stuck during inference.
pub fn extract_inference_diagnostics_data(
Expand Down Expand Up @@ -391,7 +391,7 @@ impl<'tcx> InferCtxt<'tcx> {
span: Span,
arg_data: InferenceDiagnosticsData,
error_code: TypeAnnotationNeeded,
) -> Diag<'_> {
) -> Diag<'a> {
let source_kind = "other";
let source_name = "";
let failure_span = None;
Expand Down Expand Up @@ -453,7 +453,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
// If we don't have any typeck results we're outside
// of a body, so we won't be able to get better info
// 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);
Expand All @@ -465,7 +465,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
}

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);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_infer/src/infer/error_reporting/note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
})
}
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,
impl_item_def_id,
trait_item_def_id,
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_infer/src/traits/error_reporting/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::ObjectSafetyViolation;
use crate::infer::error_reporting::TypeErrCtxt;

use crate::infer::InferCtxt;
use rustc_data_structures::fx::FxIndexSet;
use rustc_errors::{codes::*, struct_span_code_err, Applicability, Diag, MultiSpan};
use rustc_hir as hir;
Expand All @@ -11,9 +11,9 @@ use rustc_span::Span;
use std::fmt;
use std::iter;

impl<'tcx> InferCtxt<'tcx> {
pub fn report_extra_impl_obligation<'a>(
&'a self,
impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
pub fn report_extra_impl_obligation(
&self,
error_span: Span,
impl_item_def_id: LocalDefId,
trait_item_def_id: DefId,
Expand Down
37 changes: 1 addition & 36 deletions compiler/rustc_trait_selection/src/infer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::infer::at::ToTrace;
use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
use crate::traits::{self, Obligation, ObligationCause, ObligationCtxt, SelectionContext};
use crate::traits::{self, ObligationCause, ObligationCtxt};

use rustc_hir::def_id::DefId;
use rustc_hir::lang_items::LangItem;
Expand Down Expand Up @@ -76,41 +76,6 @@ impl<'tcx> InferCtxt<'tcx> {
};
self.evaluate_obligation(&obligation).unwrap_or(traits::EvaluationResult::EvaluatedToErr)
}

/// Returns `Some` if a type implements a trait shallowly, without side-effects,
/// along with any errors that would have been reported upon further obligation
/// processing.
///
/// - If this returns `Some([])`, then the trait holds modulo regions.
/// - If this returns `Some([errors..])`, then the trait has an impl for
/// the self type, but some nested obligations do not hold.
/// - If this returns `None`, no implementation that applies could be found.
///
/// FIXME(-Znext-solver): Due to the recursive nature of the new solver,
/// this will probably only ever return `Some([])` or `None`.
fn type_implements_trait_shallow(
&self,
trait_def_id: DefId,
ty: Ty<'tcx>,
param_env: ty::ParamEnv<'tcx>,
) -> Option<Vec<traits::FulfillmentError<'tcx>>> {
self.probe(|_snapshot| {
let mut selcx = SelectionContext::new(self);
match selcx.select(&Obligation::new(
self.tcx,
ObligationCause::dummy(),
param_env,
ty::TraitRef::new(self.tcx, trait_def_id, [ty]),
)) {
Ok(Some(selection)) => {
let ocx = ObligationCtxt::new_with_diagnostics(self);
ocx.register_obligations(selection.nested_obligations());
Some(ocx.select_all_or_error())
}
Ok(None) | Err(_) => None,
}
})
}
}

#[extension(pub trait InferCtxtBuilderExt<'tcx>)]
Expand Down
Loading
Loading