Skip to content

Commit 4a68373

Browse files
committed
Introduce TypeErrCtxt
TypeErrCtxt optionally has a TypeckResults so that InferCtxt doesn't need to.
1 parent 5854680 commit 4a68373

File tree

42 files changed

+634
-568
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+634
-568
lines changed

compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs

+28-32
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl<'tcx> UniverseInfo<'tcx> {
5656
) {
5757
match self.0 {
5858
UniverseInfoInner::RelateTys { expected, found } => {
59-
let err = mbcx.infcx.report_mismatched_types(
59+
let err = mbcx.infcx.err_ctxt().report_mismatched_types(
6060
&cause,
6161
expected,
6262
found,
@@ -449,42 +449,38 @@ fn try_extract_error_from_region_constraints<'tcx>(
449449
})?;
450450

451451
debug!(?sub_region, "cause = {:#?}", cause);
452-
let nice_error = match (error_region, *sub_region) {
453-
(Some(error_region), ty::ReVar(vid)) => NiceRegionError::new(
454-
infcx,
455-
RegionResolutionError::SubSupConflict(
456-
vid,
457-
region_var_origin(vid),
458-
cause.clone(),
459-
error_region,
460-
cause.clone(),
461-
placeholder_region,
462-
vec![],
463-
),
464-
),
465-
(Some(error_region), _) => NiceRegionError::new(
466-
infcx,
467-
RegionResolutionError::ConcreteFailure(cause.clone(), error_region, placeholder_region),
452+
let error = match (error_region, *sub_region) {
453+
(Some(error_region), ty::ReVar(vid)) => RegionResolutionError::SubSupConflict(
454+
vid,
455+
region_var_origin(vid),
456+
cause.clone(),
457+
error_region,
458+
cause.clone(),
459+
placeholder_region,
460+
vec![],
468461
),
462+
(Some(error_region), _) => {
463+
RegionResolutionError::ConcreteFailure(cause.clone(), error_region, placeholder_region)
464+
}
469465
// Note universe here is wrong...
470-
(None, ty::ReVar(vid)) => NiceRegionError::new(
471-
infcx,
472-
RegionResolutionError::UpperBoundUniverseConflict(
473-
vid,
474-
region_var_origin(vid),
475-
universe_of_region(vid),
476-
cause.clone(),
477-
placeholder_region,
478-
),
479-
),
480-
(None, _) => NiceRegionError::new(
481-
infcx,
482-
RegionResolutionError::ConcreteFailure(cause.clone(), sub_region, placeholder_region),
466+
(None, ty::ReVar(vid)) => RegionResolutionError::UpperBoundUniverseConflict(
467+
vid,
468+
region_var_origin(vid),
469+
universe_of_region(vid),
470+
cause.clone(),
471+
placeholder_region,
483472
),
473+
(None, _) => {
474+
RegionResolutionError::ConcreteFailure(cause.clone(), sub_region, placeholder_region)
475+
}
484476
};
485-
nice_error.try_report_from_nll().or_else(|| {
477+
NiceRegionError::new(&infcx.err_ctxt(), error).try_report_from_nll().or_else(|| {
486478
if let SubregionOrigin::Subtype(trace) = cause {
487-
Some(infcx.report_and_explain_type_error(*trace, TypeError::RegionsPlaceholderMismatch))
479+
Some(
480+
infcx
481+
.err_ctxt()
482+
.report_and_explain_type_error(*trace, TypeError::RegionsPlaceholderMismatch),
483+
)
488484
} else {
489485
None
490486
}

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
186186
if let Some(lower_bound_region) = lower_bound_region {
187187
let generic_ty = type_test.generic_kind.to_ty(self.infcx.tcx);
188188
let origin = RelateParamBound(type_test_span, generic_ty, None);
189-
self.buffer_error(self.infcx.construct_generic_bound_failure(
189+
self.buffer_error(self.infcx.err_ctxt().construct_generic_bound_failure(
190190
self.body.source.def_id().expect_local(),
191191
type_test_span,
192192
Some(origin),
@@ -365,7 +365,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
365365

366366
// Check if we can use one of the "nice region errors".
367367
if let (Some(f), Some(o)) = (self.to_error_region(fr), self.to_error_region(outlived_fr)) {
368-
let nice = NiceRegionError::new_from_span(self.infcx, cause.span, o, f);
368+
let infer_err = self.infcx.err_ctxt();
369+
let nice = NiceRegionError::new_from_span(&infer_err, cause.span, o, f);
369370
if let Some(diag) = nice.try_report_from_nll() {
370371
self.buffer_error(diag);
371372
return;

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_middle::ty::{
1313
self, OpaqueHiddenType, OpaqueTypeKey, ToPredicate, Ty, TyCtxt, TypeFoldable,
1414
};
1515
use rustc_span::Span;
16-
use rustc_trait_selection::traits::error_reporting::InferCtxtExt as _;
16+
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _;
1717
use rustc_trait_selection::traits::TraitEngineExt as _;
1818

1919
use crate::session_diagnostics::ConstNotUsedTraitAlias;
@@ -299,6 +299,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
299299
}
300300
Err(err) => {
301301
infcx
302+
.err_ctxt()
302303
.report_mismatched_types(
303304
&ObligationCause::misc(instantiated_ty.span, body_id),
304305
self.tcx.mk_opaque(def_id.to_def_id(), id_substs),
@@ -325,7 +326,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
325326
if errors.is_empty() {
326327
definition_ty
327328
} else {
328-
infcx.report_fulfillment_errors(&errors, None, false);
329+
infcx.err_ctxt().report_fulfillment_errors(&errors, None, false);
329330
self.tcx.ty_error()
330331
}
331332
},

compiler/rustc_const_eval/src/transform/check_consts/check.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_middle::ty::{Binder, TraitPredicate, TraitRef, TypeVisitable};
1414
use rustc_mir_dataflow::{self, Analysis};
1515
use rustc_span::{sym, Span, Symbol};
1616
use rustc_trait_selection::infer::InferCtxtExt;
17-
use rustc_trait_selection::traits::error_reporting::InferCtxtExt as _;
17+
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _;
1818
use rustc_trait_selection::traits::{
1919
self, ObligationCauseCode, SelectionContext, TraitEngine, TraitEngineExt,
2020
};
@@ -775,7 +775,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
775775
}
776776
let errors = fulfill_cx.select_all_or_error(&infcx);
777777
if !errors.is_empty() {
778-
infcx.report_fulfillment_errors(&errors, None, false);
778+
infcx.err_ctxt().report_fulfillment_errors(&errors, None, false);
779779
}
780780
});
781781

@@ -837,7 +837,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
837837
// as we are going to error again anyways.
838838
tcx.infer_ctxt().enter(|infcx| {
839839
if let Err(e) = implsrc {
840-
infcx.report_selection_error(
840+
infcx.err_ctxt().report_selection_error(
841841
obligation.clone(),
842842
&obligation,
843843
&e,

compiler/rustc_hir_analysis/src/check/check.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use rustc_session::lint::builtin::{UNINHABITED_STATIC, UNSUPPORTED_CALLING_CONVE
2929
use rustc_span::symbol::sym;
3030
use rustc_span::{self, Span};
3131
use rustc_target::spec::abi::Abi;
32-
use rustc_trait_selection::traits::error_reporting::InferCtxtExt as _;
32+
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _;
3333
use rustc_trait_selection::traits::{self, ObligationCtxt};
3434
use rustc_ty_utils::representability::{self, Representability};
3535

@@ -760,7 +760,7 @@ fn check_opaque_meets_bounds<'tcx>(
760760
// version.
761761
let errors = ocx.select_all_or_error();
762762
if !errors.is_empty() {
763-
infcx.report_fulfillment_errors(&errors, None, false);
763+
infcx.err_ctxt().report_fulfillment_errors(&errors, None, false);
764764
}
765765
match origin {
766766
// Checked when type checking the function containing them.

compiler/rustc_hir_analysis/src/check/coercion.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ use rustc_span::symbol::sym;
6161
use rustc_span::{self, BytePos, DesugaringKind, Span};
6262
use rustc_target::spec::abi::Abi;
6363
use rustc_trait_selection::infer::InferCtxtExt as _;
64-
use rustc_trait_selection::traits::error_reporting::InferCtxtExt as _;
64+
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _;
6565
use rustc_trait_selection::traits::{self, ObligationCause, ObligationCauseCode};
6666

6767
use smallvec::{smallvec, SmallVec};
@@ -702,7 +702,12 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
702702

703703
// Object safety violations or miscellaneous.
704704
Err(err) => {
705-
self.report_selection_error(obligation.clone(), &obligation, &err, false);
705+
self.err_ctxt().report_selection_error(
706+
obligation.clone(),
707+
&obligation,
708+
&err,
709+
false,
710+
);
706711
// Treat this like an obligation and follow through
707712
// with the unsizing - the lack of a coercion should
708713
// be silent, as it causes a type mismatch later.
@@ -1549,7 +1554,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
15491554
}
15501555
}
15511556
_ => {
1552-
err = fcx.report_mismatched_types(
1557+
err = fcx.err_ctxt().report_mismatched_types(
15531558
cause,
15541559
expected,
15551560
found,
@@ -1629,7 +1634,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
16291634
expression: Option<&'tcx hir::Expr<'tcx>>,
16301635
blk_id: Option<hir::HirId>,
16311636
) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
1632-
let mut err = fcx.report_mismatched_types(cause, expected, found, ty_err);
1637+
let mut err = fcx.err_ctxt().report_mismatched_types(cause, expected, found, ty_err);
16331638

16341639
let mut pointing_at_return_type = false;
16351640
let mut fn_output = None;

compiler/rustc_hir_analysis/src/check/compare_method.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_middle::ty::{
1919
};
2020
use rustc_middle::ty::{GenericParamDefKind, ToPredicate, TyCtxt};
2121
use rustc_span::Span;
22-
use rustc_trait_selection::traits::error_reporting::InferCtxtExt;
22+
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt;
2323
use rustc_trait_selection::traits::outlives_bounds::InferCtxtExt as _;
2424
use rustc_trait_selection::traits::{
2525
self, ObligationCause, ObligationCauseCode, ObligationCtxt, Reveal,
@@ -395,7 +395,7 @@ fn compare_predicate_entailment<'tcx>(
395395
_ => {}
396396
}
397397

398-
infcx.note_type_err(
398+
infcx.err_ctxt().note_type_err(
399399
&mut diag,
400400
&cause,
401401
trait_err_span.map(|sp| (sp, "type in trait".to_owned())),
@@ -415,7 +415,7 @@ fn compare_predicate_entailment<'tcx>(
415415
// version.
416416
let errors = ocx.select_all_or_error();
417417
if !errors.is_empty() {
418-
let reported = infcx.report_fulfillment_errors(&errors, None, false);
418+
let reported = infcx.err_ctxt().report_fulfillment_errors(&errors, None, false);
419419
return Err(reported);
420420
}
421421

@@ -508,7 +508,7 @@ pub fn collect_trait_impl_trait_tys<'tcx>(
508508
trait_m.name
509509
);
510510
let hir = tcx.hir();
511-
infcx.note_type_err(
511+
infcx.err_ctxt().note_type_err(
512512
&mut diag,
513513
&cause,
514514
hir.get_if_local(impl_m.def_id)
@@ -530,7 +530,7 @@ pub fn collect_trait_impl_trait_tys<'tcx>(
530530
// RPITs.
531531
let errors = ocx.select_all_or_error();
532532
if !errors.is_empty() {
533-
let reported = infcx.report_fulfillment_errors(&errors, None, false);
533+
let reported = infcx.err_ctxt().report_fulfillment_errors(&errors, None, false);
534534
return Err(reported);
535535
}
536536

@@ -1382,7 +1382,7 @@ pub(crate) fn raw_compare_const_impl<'tcx>(
13821382
}
13831383
});
13841384

1385-
infcx.note_type_err(
1385+
infcx.err_ctxt().note_type_err(
13861386
&mut diag,
13871387
&cause,
13881388
trait_c_span.map(|span| (span, "type in trait".to_owned())),
@@ -1401,7 +1401,7 @@ pub(crate) fn raw_compare_const_impl<'tcx>(
14011401
// version.
14021402
let errors = ocx.select_all_or_error();
14031403
if !errors.is_empty() {
1404-
return Err(infcx.report_fulfillment_errors(&errors, None, false));
1404+
return Err(infcx.err_ctxt().report_fulfillment_errors(&errors, None, false));
14051405
}
14061406

14071407
// FIXME return `ErrorReported` if region obligations error?
@@ -1522,7 +1522,7 @@ fn compare_type_predicate_entailment<'tcx>(
15221522
// version.
15231523
let errors = ocx.select_all_or_error();
15241524
if !errors.is_empty() {
1525-
let reported = infcx.report_fulfillment_errors(&errors, None, false);
1525+
let reported = infcx.err_ctxt().report_fulfillment_errors(&errors, None, false);
15261526
return Err(reported);
15271527
}
15281528

@@ -1751,7 +1751,7 @@ pub fn check_type_bounds<'tcx>(
17511751
// version.
17521752
let errors = ocx.select_all_or_error();
17531753
if !errors.is_empty() {
1754-
let reported = infcx.report_fulfillment_errors(&errors, None, false);
1754+
let reported = infcx.err_ctxt().report_fulfillment_errors(&errors, None, false);
17551755
return Err(reported);
17561756
}
17571757

@@ -1769,6 +1769,7 @@ pub fn check_type_bounds<'tcx>(
17691769
let constraints = infcx.inner.borrow_mut().opaque_type_storage.take_opaque_types();
17701770
for (key, value) in constraints {
17711771
infcx
1772+
.err_ctxt()
17721773
.report_mismatched_types(
17731774
&ObligationCause::misc(
17741775
value.hidden_type.span,

compiler/rustc_hir_analysis/src/check/demand.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
7979
self.register_predicates(obligations);
8080
None
8181
}
82-
Err(e) => Some(self.report_mismatched_types(&cause, expected, actual, e)),
82+
Err(e) => Some(self.err_ctxt().report_mismatched_types(&cause, expected, actual, e)),
8383
}
8484
}
8585

@@ -109,7 +109,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
109109
self.register_predicates(obligations);
110110
None
111111
}
112-
Err(e) => Some(self.report_mismatched_types(cause, expected, actual, e)),
112+
Err(e) => Some(self.err_ctxt().report_mismatched_types(cause, expected, actual, e)),
113113
}
114114
}
115115

@@ -153,7 +153,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
153153
let expr = expr.peel_drop_temps();
154154
let cause = self.misc(expr.span);
155155
let expr_ty = self.resolve_vars_with_obligations(checked_ty);
156-
let mut err = self.report_mismatched_types(&cause, expected, expr_ty, e.clone());
156+
let mut err = self.err_ctxt().report_mismatched_types(&cause, expected, expr_ty, e.clone());
157157

158158
let is_insufficiently_polymorphic =
159159
matches!(e, TypeError::RegionsInsufficientlyPolymorphic(..));

compiler/rustc_hir_analysis/src/check/expr.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -1649,13 +1649,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
16491649
Err(_) => {
16501650
// This should never happen, since we're just subtyping the
16511651
// remaining_fields, but it's fine to emit this, I guess.
1652-
self.report_mismatched_types(
1653-
&cause,
1654-
target_ty,
1655-
fru_ty,
1656-
FieldMisMatch(variant.name, ident.name),
1657-
)
1658-
.emit();
1652+
self.err_ctxt()
1653+
.report_mismatched_types(
1654+
&cause,
1655+
target_ty,
1656+
fru_ty,
1657+
FieldMisMatch(variant.name, ident.name),
1658+
)
1659+
.emit();
16591660
}
16601661
}
16611662
}
@@ -1942,7 +1943,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
19421943
self.set_tainted_by_errors();
19431944
return;
19441945
}
1945-
let mut err = self.type_error_struct_with_diag(
1946+
let mut err = self.err_ctxt().type_error_struct_with_diag(
19461947
field.ident.span,
19471948
|actual| match ty.kind() {
19481949
ty::Adt(adt, ..) if adt.is_enum() => struct_span_err!(

compiler/rustc_hir_analysis/src/check/fn_ctxt/_impl.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use rustc_span::hygiene::DesugaringKind;
3232
use rustc_span::symbol::{kw, sym, Ident};
3333
use rustc_span::{Span, DUMMY_SP};
3434
use rustc_trait_selection::infer::InferCtxtExt as _;
35-
use rustc_trait_selection::traits::error_reporting::InferCtxtExt as _;
35+
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _;
3636
use rustc_trait_selection::traits::{
3737
self, ObligationCause, ObligationCauseCode, TraitEngine, TraitEngineExt,
3838
};
@@ -615,7 +615,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
615615

616616
if !errors.is_empty() {
617617
self.adjust_fulfillment_errors_for_expr_obligation(&mut errors);
618-
self.report_fulfillment_errors(&errors, self.inh.body_id, false);
618+
self.err_ctxt().report_fulfillment_errors(&errors, self.inh.body_id, false);
619619
}
620620
}
621621

@@ -629,7 +629,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
629629
if !result.is_empty() {
630630
mutate_fulfillment_errors(&mut result);
631631
self.adjust_fulfillment_errors_for_expr_obligation(&mut result);
632-
self.report_fulfillment_errors(&result, self.inh.body_id, fallback_has_occurred);
632+
self.err_ctxt().report_fulfillment_errors(
633+
&result,
634+
self.inh.body_id,
635+
fallback_has_occurred,
636+
);
633637
}
634638
}
635639

@@ -1466,7 +1470,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14661470
ty
14671471
} else {
14681472
if !self.is_tainted_by_errors() {
1469-
self.emit_inference_failure_err((**self).body_id, sp, ty.into(), E0282, true)
1473+
self.err_ctxt()
1474+
.emit_inference_failure_err((**self).body_id, sp, ty.into(), E0282, true)
14701475
.emit();
14711476
}
14721477
let err = self.tcx.ty_error();

0 commit comments

Comments
 (0)