Skip to content

Commit 8e0d83a

Browse files
authored
Rollup merge of #105185 - compiler-errors:normalize_fn_sig-in-err-ctxt, r=lcnr
Move `normalize_fn_sig` to `TypeErrCtxt` r? `@lcnr`
2 parents 09e2d0f + ffca711 commit 8e0d83a

File tree

5 files changed

+30
-67
lines changed

5 files changed

+30
-67
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc_middle::ty::{self, Const, Ty, TyCtxt};
2222
use rustc_session::Session;
2323
use rustc_span::symbol::Ident;
2424
use rustc_span::{self, Span};
25-
use rustc_trait_selection::traits::{ObligationCause, ObligationCauseCode};
25+
use rustc_trait_selection::traits::{ObligationCause, ObligationCauseCode, ObligationCtxt};
2626

2727
use std::cell::{Cell, RefCell};
2828
use std::ops::Deref;
@@ -162,6 +162,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
162162
infcx: &self.infcx,
163163
typeck_results: Some(self.typeck_results.borrow()),
164164
fallback_has_occurred: self.fallback_has_occurred.get(),
165+
normalize_fn_sig: Box::new(|fn_sig| {
166+
if fn_sig.has_escaping_bound_vars() {
167+
return fn_sig;
168+
}
169+
self.probe(|_| {
170+
let ocx = ObligationCtxt::new_in_snapshot(self);
171+
let normalized_fn_sig =
172+
ocx.normalize(&ObligationCause::dummy(), self.param_env, fn_sig);
173+
if ocx.select_all_or_error().is_empty() {
174+
let normalized_fn_sig = self.resolve_vars_if_possible(normalized_fn_sig);
175+
if !normalized_fn_sig.needs_infer() {
176+
return normalized_fn_sig;
177+
}
178+
}
179+
fn_sig
180+
})
181+
}),
165182
}
166183
}
167184

compiler/rustc_hir_typeck/src/inherited.rs

+2-27
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use super::callee::DeferredCallResolution;
22

33
use rustc_data_structures::fx::FxHashSet;
4-
use rustc_data_structures::sync::Lrc;
54
use rustc_hir as hir;
65
use rustc_hir::def_id::LocalDefId;
76
use rustc_hir::HirIdMap;
@@ -11,9 +10,7 @@ use rustc_middle::ty::visit::TypeVisitable;
1110
use rustc_middle::ty::{self, Ty, TyCtxt};
1211
use rustc_span::def_id::LocalDefIdMap;
1312
use rustc_span::{self, Span};
14-
use rustc_trait_selection::traits::{
15-
self, ObligationCause, ObligationCtxt, TraitEngine, TraitEngineExt as _,
16-
};
13+
use rustc_trait_selection::traits::{self, TraitEngine, TraitEngineExt as _};
1714

1815
use std::cell::RefCell;
1916
use std::ops::Deref;
@@ -92,29 +89,7 @@ impl<'tcx> Inherited<'tcx> {
9289
infcx: tcx
9390
.infer_ctxt()
9491
.ignoring_regions()
95-
.with_opaque_type_inference(DefiningAnchor::Bind(hir_owner.def_id))
96-
.with_normalize_fn_sig_for_diagnostic(Lrc::new(move |infcx, fn_sig| {
97-
if fn_sig.has_escaping_bound_vars() {
98-
return fn_sig;
99-
}
100-
infcx.probe(|_| {
101-
let ocx = ObligationCtxt::new_in_snapshot(infcx);
102-
let normalized_fn_sig = ocx.normalize(
103-
&ObligationCause::dummy(),
104-
// FIXME(compiler-errors): This is probably not the right param-env...
105-
infcx.tcx.param_env(def_id),
106-
fn_sig,
107-
);
108-
if ocx.select_all_or_error().is_empty() {
109-
let normalized_fn_sig =
110-
infcx.resolve_vars_if_possible(normalized_fn_sig);
111-
if !normalized_fn_sig.needs_infer() {
112-
return normalized_fn_sig;
113-
}
114-
}
115-
fn_sig
116-
})
117-
})),
92+
.with_opaque_type_inference(DefiningAnchor::Bind(hir_owner.def_id)),
11893
def_id,
11994
typeck_results: RefCell::new(ty::TypeckResults::new(hir_owner)),
12095
}

compiler/rustc_infer/src/infer/at.rs

-4
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,6 @@ impl<'tcx> InferCtxt<'tcx> {
7777
err_count_on_creation: self.err_count_on_creation,
7878
in_snapshot: self.in_snapshot.clone(),
7979
universe: self.universe.clone(),
80-
normalize_fn_sig_for_diagnostic: self
81-
.normalize_fn_sig_for_diagnostic
82-
.as_ref()
83-
.map(|f| f.clone()),
8480
intercrate: self.intercrate,
8581
}
8682
}

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

+3-10
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ pub mod nice_region_error;
9595
pub struct TypeErrCtxt<'a, 'tcx> {
9696
pub infcx: &'a InferCtxt<'tcx>,
9797
pub typeck_results: Option<std::cell::Ref<'a, ty::TypeckResults<'tcx>>>,
98+
pub normalize_fn_sig: Box<dyn Fn(ty::PolyFnSig<'tcx>) -> ty::PolyFnSig<'tcx> + 'a>,
9899
pub fallback_has_occurred: bool,
99100
}
100101

@@ -1007,22 +1008,14 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
10071008
}
10081009
}
10091010

1010-
fn normalize_fn_sig_for_diagnostic(&self, sig: ty::PolyFnSig<'tcx>) -> ty::PolyFnSig<'tcx> {
1011-
if let Some(normalize) = &self.normalize_fn_sig_for_diagnostic {
1012-
normalize(self, sig)
1013-
} else {
1014-
sig
1015-
}
1016-
}
1017-
10181011
/// Given two `fn` signatures highlight only sub-parts that are different.
10191012
fn cmp_fn_sig(
10201013
&self,
10211014
sig1: &ty::PolyFnSig<'tcx>,
10221015
sig2: &ty::PolyFnSig<'tcx>,
10231016
) -> (DiagnosticStyledString, DiagnosticStyledString) {
1024-
let sig1 = &self.normalize_fn_sig_for_diagnostic(*sig1);
1025-
let sig2 = &self.normalize_fn_sig_for_diagnostic(*sig2);
1017+
let sig1 = &(self.normalize_fn_sig)(*sig1);
1018+
let sig2 = &(self.normalize_fn_sig)(*sig2);
10261019

10271020
let get_lifetimes = |sig| {
10281021
use rustc_hir::def::Namespace;

compiler/rustc_infer/src/infer/mod.rs

+7-25
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,6 @@ pub struct InferCtxt<'tcx> {
333333
/// bound.
334334
universe: Cell<ty::UniverseIndex>,
335335

336-
normalize_fn_sig_for_diagnostic:
337-
Option<Lrc<dyn Fn(&InferCtxt<'tcx>, ty::PolyFnSig<'tcx>) -> ty::PolyFnSig<'tcx>>>,
338-
339336
/// During coherence we have to assume that other crates may add
340337
/// additional impls which we currently don't know about.
341338
///
@@ -572,8 +569,6 @@ pub struct InferCtxtBuilder<'tcx> {
572569
considering_regions: bool,
573570
/// Whether we are in coherence mode.
574571
intercrate: bool,
575-
normalize_fn_sig_for_diagnostic:
576-
Option<Lrc<dyn Fn(&InferCtxt<'tcx>, ty::PolyFnSig<'tcx>) -> ty::PolyFnSig<'tcx>>>,
577572
}
578573

579574
pub trait TyCtxtInferExt<'tcx> {
@@ -586,7 +581,6 @@ impl<'tcx> TyCtxtInferExt<'tcx> for TyCtxt<'tcx> {
586581
tcx: self,
587582
defining_use_anchor: DefiningAnchor::Error,
588583
considering_regions: true,
589-
normalize_fn_sig_for_diagnostic: None,
590584
intercrate: false,
591585
}
592586
}
@@ -614,14 +608,6 @@ impl<'tcx> InferCtxtBuilder<'tcx> {
614608
self
615609
}
616610

617-
pub fn with_normalize_fn_sig_for_diagnostic(
618-
mut self,
619-
fun: Lrc<dyn Fn(&InferCtxt<'tcx>, ty::PolyFnSig<'tcx>) -> ty::PolyFnSig<'tcx>>,
620-
) -> Self {
621-
self.normalize_fn_sig_for_diagnostic = Some(fun);
622-
self
623-
}
624-
625611
/// Given a canonical value `C` as a starting point, create an
626612
/// inference context that contains each of the bound values
627613
/// within instantiated as a fresh variable. The `f` closure is
@@ -643,13 +629,7 @@ impl<'tcx> InferCtxtBuilder<'tcx> {
643629
}
644630

645631
pub fn build(&mut self) -> InferCtxt<'tcx> {
646-
let InferCtxtBuilder {
647-
tcx,
648-
defining_use_anchor,
649-
considering_regions,
650-
ref normalize_fn_sig_for_diagnostic,
651-
intercrate,
652-
} = *self;
632+
let InferCtxtBuilder { tcx, defining_use_anchor, considering_regions, intercrate } = *self;
653633
InferCtxt {
654634
tcx,
655635
defining_use_anchor,
@@ -665,9 +645,6 @@ impl<'tcx> InferCtxtBuilder<'tcx> {
665645
in_snapshot: Cell::new(false),
666646
skip_leak_check: Cell::new(false),
667647
universe: Cell::new(ty::UniverseIndex::ROOT),
668-
normalize_fn_sig_for_diagnostic: normalize_fn_sig_for_diagnostic
669-
.as_ref()
670-
.map(|f| f.clone()),
671648
intercrate,
672649
}
673650
}
@@ -708,7 +685,12 @@ impl<'tcx> InferCtxt<'tcx> {
708685
/// Creates a `TypeErrCtxt` for emitting various inference errors.
709686
/// During typeck, use `FnCtxt::err_ctxt` instead.
710687
pub fn err_ctxt(&self) -> TypeErrCtxt<'_, 'tcx> {
711-
TypeErrCtxt { infcx: self, typeck_results: None, fallback_has_occurred: false }
688+
TypeErrCtxt {
689+
infcx: self,
690+
typeck_results: None,
691+
fallback_has_occurred: false,
692+
normalize_fn_sig: Box::new(|fn_sig| fn_sig),
693+
}
712694
}
713695

714696
pub fn is_in_snapshot(&self) -> bool {

0 commit comments

Comments
 (0)