Skip to content

Commit ffca711

Browse files
Move normalize_fn_sig to TypeErrCtxt
1 parent e960b5e commit ffca711

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

Lines changed: 18 additions & 1 deletion
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

Lines changed: 2 additions & 27 deletions
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

Lines changed: 0 additions & 4 deletions
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

Lines changed: 3 additions & 10 deletions
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

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,6 @@ pub struct InferCtxt<'tcx> {
334334
/// bound.
335335
universe: Cell<ty::UniverseIndex>,
336336

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

580575
pub trait TyCtxtInferExt<'tcx> {
@@ -587,7 +582,6 @@ impl<'tcx> TyCtxtInferExt<'tcx> for TyCtxt<'tcx> {
587582
tcx: self,
588583
defining_use_anchor: DefiningAnchor::Error,
589584
considering_regions: true,
590-
normalize_fn_sig_for_diagnostic: None,
591585
intercrate: false,
592586
}
593587
}
@@ -615,14 +609,6 @@ impl<'tcx> InferCtxtBuilder<'tcx> {
615609
self
616610
}
617611

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

646632
pub fn build(&mut self) -> InferCtxt<'tcx> {
647-
let InferCtxtBuilder {
648-
tcx,
649-
defining_use_anchor,
650-
considering_regions,
651-
ref normalize_fn_sig_for_diagnostic,
652-
intercrate,
653-
} = *self;
633+
let InferCtxtBuilder { tcx, defining_use_anchor, considering_regions, intercrate } = *self;
654634
InferCtxt {
655635
tcx,
656636
defining_use_anchor,
@@ -666,9 +646,6 @@ impl<'tcx> InferCtxtBuilder<'tcx> {
666646
in_snapshot: Cell::new(false),
667647
skip_leak_check: Cell::new(false),
668648
universe: Cell::new(ty::UniverseIndex::ROOT),
669-
normalize_fn_sig_for_diagnostic: normalize_fn_sig_for_diagnostic
670-
.as_ref()
671-
.map(|f| f.clone()),
672649
intercrate,
673650
}
674651
}
@@ -709,7 +686,12 @@ impl<'tcx> InferCtxt<'tcx> {
709686
/// Creates a `TypeErrCtxt` for emitting various inference errors.
710687
/// During typeck, use `FnCtxt::err_ctxt` instead.
711688
pub fn err_ctxt(&self) -> TypeErrCtxt<'_, 'tcx> {
712-
TypeErrCtxt { infcx: self, typeck_results: None, fallback_has_occurred: false }
689+
TypeErrCtxt {
690+
infcx: self,
691+
typeck_results: None,
692+
fallback_has_occurred: false,
693+
normalize_fn_sig: Box::new(|fn_sig| fn_sig),
694+
}
713695
}
714696

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

0 commit comments

Comments
 (0)