Skip to content

Commit d17a52c

Browse files
Rollup merge of #157715 - cjgillot:type-err-ctxt-normalize, r=TaKO8Ki
Stop using a fn pointer to normalize_fn_sig inside TypeErrCtxt Part of #127492
2 parents aa2f08d + 561b2d8 commit d17a52c

4 files changed

Lines changed: 56 additions & 53 deletions

File tree

compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -197,25 +197,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
197197
pub(crate) fn err_ctxt(&'a self) -> TypeErrCtxt<'a, 'tcx> {
198198
TypeErrCtxt {
199199
infcx: &self.infcx,
200+
param_env: Some(self.param_env),
200201
typeck_results: Some(self.typeck_results.borrow()),
201202
diverging_fallback_has_occurred: self.diverging_fallback_has_occurred.get(),
202-
normalize_fn_sig: Box::new(|fn_sig| {
203-
if fn_sig.skip_normalization().has_escaping_bound_vars() {
204-
return fn_sig.skip_normalization();
205-
}
206-
self.probe(|_| {
207-
let ocx = ObligationCtxt::new(self);
208-
let normalized_fn_sig =
209-
ocx.normalize(&ObligationCause::dummy(), self.param_env, fn_sig);
210-
if ocx.evaluate_obligations_error_on_ambiguity().is_empty() {
211-
let normalized_fn_sig = self.resolve_vars_if_possible(normalized_fn_sig);
212-
if !normalized_fn_sig.has_infer() {
213-
return normalized_fn_sig;
214-
}
215-
}
216-
fn_sig.skip_normalization()
217-
})
218-
}),
219203
autoderef_steps: Box::new(|ty| {
220204
let mut autoderef = self.autoderef(DUMMY_SP, ty).silence_errors();
221205
let mut steps = vec![];

compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ use crate::infer::relate::{self, RelateResult, TypeRelation};
8282
use crate::infer::{InferCtxt, InferCtxtExt as _, TypeTrace, ValuePairs};
8383
use crate::solve::deeply_normalize_for_diagnostics;
8484
use crate::traits::{
85-
MatchExpressionArmCause, Obligation, ObligationCause, ObligationCauseCode, specialization_graph,
85+
MatchExpressionArmCause, Obligation, ObligationCause, ObligationCauseCode, ObligationCtxt,
86+
specialization_graph,
8687
};
8788

8889
mod note_and_explain;
@@ -115,6 +116,31 @@ fn escape_literal(s: &str) -> String {
115116
}
116117

117118
impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
119+
fn normalize_fn_sig(
120+
&self,
121+
fn_sig: Unnormalized<'tcx, ty::PolyFnSig<'tcx>>,
122+
) -> ty::PolyFnSig<'tcx> {
123+
let Some(param_env) = self.param_env else {
124+
return fn_sig.skip_normalization();
125+
};
126+
127+
if fn_sig.skip_normalization().has_escaping_bound_vars() {
128+
return fn_sig.skip_normalization();
129+
}
130+
131+
self.probe(|_| {
132+
let ocx = ObligationCtxt::new(self);
133+
let normalized_fn_sig = ocx.normalize(&ObligationCause::dummy(), param_env, fn_sig);
134+
if ocx.evaluate_obligations_error_on_ambiguity().is_empty() {
135+
let normalized_fn_sig = self.resolve_vars_if_possible(normalized_fn_sig);
136+
if !normalized_fn_sig.has_infer() {
137+
return normalized_fn_sig;
138+
}
139+
}
140+
fn_sig.skip_normalization()
141+
})
142+
}
143+
118144
// [Note-Type-error-reporting]
119145
// An invariant is that anytime the expected or actual type is Error (the special
120146
// error type, meaning that an error occurred when typechecking this expression),
@@ -760,18 +786,18 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
760786
/// Given two `fn` signatures highlight only sub-parts that are different.
761787
fn cmp_fn_sig(
762788
&self,
763-
sig1: &ty::PolyFnSig<'tcx>,
789+
sig1: ty::PolyFnSig<'tcx>,
764790
fn_def1: Option<(DefId, Option<&'tcx [ty::GenericArg<'tcx>]>)>,
765-
sig2: &ty::PolyFnSig<'tcx>,
791+
sig2: ty::PolyFnSig<'tcx>,
766792
fn_def2: Option<(DefId, Option<&'tcx [ty::GenericArg<'tcx>]>)>,
767793
) -> (DiagStyledString, DiagStyledString) {
768-
let sig1 = &(self.normalize_fn_sig)(Unnormalized::new_wip(*sig1));
769-
let sig2 = &(self.normalize_fn_sig)(Unnormalized::new_wip(*sig2));
794+
let sig1 = self.normalize_fn_sig(Unnormalized::new_wip(sig1));
795+
let sig2 = self.normalize_fn_sig(Unnormalized::new_wip(sig2));
770796

771797
let get_lifetimes = |sig| {
772798
use rustc_hir::def::Namespace;
773799
let (sig, reg) = ty::print::FmtPrinter::new(self.tcx, Namespace::TypeNS)
774-
.name_all_regions(sig, WrapBinderMode::ForAll)
800+
.name_all_regions(&sig, WrapBinderMode::ForAll)
775801
.unwrap();
776802
let lts: Vec<String> =
777803
reg.into_items().map(|(_, kind)| kind.to_string()).into_sorted_stable_ord();
@@ -1284,26 +1310,21 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
12841310
(ty::FnDef(did1, args1), ty::FnDef(did2, args2)) => {
12851311
let sig1 = self.tcx.fn_sig(*did1).instantiate(self.tcx, args1).skip_norm_wip();
12861312
let sig2 = self.tcx.fn_sig(*did2).instantiate(self.tcx, args2).skip_norm_wip();
1287-
self.cmp_fn_sig(
1288-
&sig1,
1289-
Some((*did1, Some(args1))),
1290-
&sig2,
1291-
Some((*did2, Some(args2))),
1292-
)
1313+
self.cmp_fn_sig(sig1, Some((*did1, Some(args1))), sig2, Some((*did2, Some(args2))))
12931314
}
12941315

12951316
(ty::FnDef(did1, args1), ty::FnPtr(sig_tys2, hdr2)) => {
12961317
let sig1 = self.tcx.fn_sig(*did1).instantiate(self.tcx, args1).skip_norm_wip();
1297-
self.cmp_fn_sig(&sig1, Some((*did1, Some(args1))), &sig_tys2.with(*hdr2), None)
1318+
self.cmp_fn_sig(sig1, Some((*did1, Some(args1))), sig_tys2.with(*hdr2), None)
12981319
}
12991320

13001321
(ty::FnPtr(sig_tys1, hdr1), ty::FnDef(did2, args2)) => {
13011322
let sig2 = self.tcx.fn_sig(*did2).instantiate(self.tcx, args2).skip_norm_wip();
1302-
self.cmp_fn_sig(&sig_tys1.with(*hdr1), None, &sig2, Some((*did2, Some(args2))))
1323+
self.cmp_fn_sig(sig_tys1.with(*hdr1), None, sig2, Some((*did2, Some(args2))))
13031324
}
13041325

13051326
(ty::FnPtr(sig_tys1, hdr1), ty::FnPtr(sig_tys2, hdr2)) => {
1306-
self.cmp_fn_sig(&sig_tys1.with(*hdr1), None, &sig_tys2.with(*hdr2), None)
1327+
self.cmp_fn_sig(sig_tys1.with(*hdr1), None, sig_tys2.with(*hdr2), None)
13071328
}
13081329

13091330
_ => {
@@ -2190,7 +2211,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
21902211
(None, None)
21912212
};
21922213

2193-
Some(self.cmp_fn_sig(&exp_found.expected, fn_def1, &exp_found.found, fn_def2))
2214+
Some(self.cmp_fn_sig(exp_found.expected, fn_def1, exp_found.found, fn_def2))
21942215
}
21952216
}
21962217
}

compiler/rustc_trait_selection/src/error_reporting/infer/suggest.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -412,13 +412,13 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
412412
match (expected_inner.kind(), found_inner.kind()) {
413413
(ty::FnPtr(sig_tys, hdr), ty::FnDef(did, args)) => {
414414
let sig = sig_tys.with(*hdr);
415-
let expected_sig = &(self.normalize_fn_sig)(Unnormalized::new_wip(sig));
415+
let expected_sig = self.normalize_fn_sig(Unnormalized::new_wip(sig));
416416
let found_sig =
417-
&(self.normalize_fn_sig)(self.tcx.fn_sig(*did).instantiate(self.tcx, args));
417+
self.normalize_fn_sig(self.tcx.fn_sig(*did).instantiate(self.tcx, args));
418418

419419
let fn_name = self.tcx.def_path_str_with_args(*did, args);
420420

421-
if !self.same_type_modulo_infer(*found_sig, *expected_sig)
421+
if !self.same_type_modulo_infer(found_sig, expected_sig)
422422
|| !sig.is_suggestable(self.tcx, true)
423423
|| self.tcx.intrinsic(*did).is_some()
424424
{
@@ -450,15 +450,15 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
450450
}
451451
(ty::FnDef(did1, args1), ty::FnDef(did2, args2)) => {
452452
let expected_sig =
453-
&(self.normalize_fn_sig)(self.tcx.fn_sig(*did1).instantiate(self.tcx, args1));
453+
self.normalize_fn_sig(self.tcx.fn_sig(*did1).instantiate(self.tcx, args1));
454454
let found_sig =
455-
&(self.normalize_fn_sig)(self.tcx.fn_sig(*did2).instantiate(self.tcx, args2));
455+
self.normalize_fn_sig(self.tcx.fn_sig(*did2).instantiate(self.tcx, args2));
456456

457-
if self.same_type_modulo_infer(*expected_sig, *found_sig) {
457+
if self.same_type_modulo_infer(expected_sig, found_sig) {
458458
diag.subdiagnostic(FnUniqTypes);
459459
}
460460

461-
if !self.same_type_modulo_infer(*found_sig, *expected_sig)
461+
if !self.same_type_modulo_infer(found_sig, expected_sig)
462462
|| !found_sig.is_suggestable(self.tcx, true)
463463
|| !expected_sig.is_suggestable(self.tcx, true)
464464
|| self.tcx.intrinsic(*did1).is_some()
@@ -470,33 +470,33 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
470470
let fn_name = self.tcx.def_path_str_with_args(*did2, args2);
471471

472472
let Some(span) = span else {
473-
diag.subdiagnostic(FnConsiderCastingBoth { sig: *expected_sig });
473+
diag.subdiagnostic(FnConsiderCastingBoth { sig: expected_sig });
474474
return;
475475
};
476476

477477
let sug = if found.is_ref() {
478478
FunctionPointerSuggestion::CastBothRef {
479479
span,
480480
fn_name,
481-
found_sig: *found_sig,
482-
expected_sig: *expected_sig,
481+
found_sig,
482+
expected_sig,
483483
}
484484
} else {
485485
FunctionPointerSuggestion::CastBoth {
486486
span: span.shrink_to_hi(),
487-
found_sig: *found_sig,
488-
expected_sig: *expected_sig,
487+
found_sig,
488+
expected_sig,
489489
}
490490
};
491491

492492
diag.subdiagnostic(sug);
493493
}
494494
(ty::FnDef(did, args), ty::FnPtr(sig_tys, hdr)) => {
495495
let expected_sig =
496-
&(self.normalize_fn_sig)(self.tcx.fn_sig(*did).instantiate(self.tcx, args));
497-
let found_sig = &(self.normalize_fn_sig)(Unnormalized::new_wip(sig_tys.with(*hdr)));
496+
self.normalize_fn_sig(self.tcx.fn_sig(*did).instantiate(self.tcx, args));
497+
let found_sig = self.normalize_fn_sig(Unnormalized::new_wip(sig_tys.with(*hdr)));
498498

499-
if !self.same_type_modulo_infer(*found_sig, *expected_sig) {
499+
if !self.same_type_modulo_infer(found_sig, expected_sig) {
500500
return;
501501
}
502502

compiler/rustc_trait_selection/src/error_reporting/mod.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_infer::infer::InferCtxt;
55
use rustc_infer::traits::PredicateObligations;
66
use rustc_macros::extension;
77
use rustc_middle::bug;
8-
use rustc_middle::ty::{self, Ty, Unnormalized};
8+
use rustc_middle::ty::{self, Ty};
99

1010
pub mod infer;
1111
pub mod traits;
@@ -19,13 +19,11 @@ pub mod traits;
1919
/// methods which should not be used during the happy path.
2020
pub struct TypeErrCtxt<'a, 'tcx> {
2121
pub infcx: &'a InferCtxt<'tcx>,
22+
pub param_env: Option<ty::ParamEnv<'tcx>>,
2223

2324
pub typeck_results: Option<std::cell::Ref<'a, ty::TypeckResults<'tcx>>>,
2425
pub diverging_fallback_has_occurred: bool,
2526

26-
pub normalize_fn_sig:
27-
Box<dyn Fn(Unnormalized<'tcx, ty::PolyFnSig<'tcx>>) -> ty::PolyFnSig<'tcx> + 'a>,
28-
2927
pub autoderef_steps: Box<dyn Fn(Ty<'tcx>) -> Vec<(Ty<'tcx>, PredicateObligations<'tcx>)> + 'a>,
3028
}
3129

@@ -36,9 +34,9 @@ impl<'tcx> InferCtxt<'tcx> {
3634
fn err_ctxt(&self) -> TypeErrCtxt<'_, 'tcx> {
3735
TypeErrCtxt {
3836
infcx: self,
37+
param_env: None,
3938
typeck_results: None,
4039
diverging_fallback_has_occurred: false,
41-
normalize_fn_sig: Box::new(|fn_sig| fn_sig.skip_normalization()),
4240
autoderef_steps: Box::new(|ty| {
4341
debug_assert!(false, "shouldn't be using autoderef_steps outside of typeck");
4442
vec![(ty, PredicateObligations::new())]

0 commit comments

Comments
 (0)