1 use super::callee::DeferredCallResolution;
3 use rustc_data_structures::fx::FxHashSet;
5 use rustc_hir::def_id::LocalDefId;
6 use rustc_hir::HirIdMap;
7 use rustc_infer::infer;
8 use rustc_infer::infer::{InferCtxt, InferOk, TyCtxtInferExt};
9 use rustc_middle::ty::fold::TypeFoldable;
10 use rustc_middle::ty::visit::TypeVisitable;
11 use rustc_middle::ty::{self, Ty, TyCtxt};
12 use rustc_span::def_id::LocalDefIdMap;
13 use rustc_span::{self, Span};
14 use rustc_trait_selection::infer::InferCtxtExt as _;
15 use rustc_trait_selection::traits::{self, ObligationCause, TraitEngine, TraitEngineExt};
17 use std::cell::RefCell;
20 /// Closures defined within the function. For example:
21 /// ```ignore (illustrative)
23 /// bar(move|| { ... })
26 /// Here, the function `foo()` and the closure passed to
27 /// `bar()` will each have their own `FnCtxt`, but they will
28 /// share the inherited fields.
29 pub struct Inherited<'a, 'tcx> {
30 pub(super) infcx: InferCtxt<'a, 'tcx>,
32 pub(super) typeck_results: &'a RefCell<ty::TypeckResults<'tcx>>,
34 pub(super) locals: RefCell<HirIdMap<super::LocalTy<'tcx>>>,
36 pub(super) fulfillment_cx: RefCell<Box<dyn TraitEngine<'tcx>>>,
38 // When we process a call like `c()` where `c` is a closure type,
39 // we may not have decided yet whether `c` is a `Fn`, `FnMut`, or
40 // `FnOnce` closure. In that case, we defer full resolution of the
41 // call until upvar inference can kick in and make the
42 // decision. We keep these deferred resolutions grouped by the
43 // def-id of the closure, so that once we decide, we can easily go
44 // back and process them.
45 pub(super) deferred_call_resolutions: RefCell<LocalDefIdMap<Vec<DeferredCallResolution<'tcx>>>>,
47 pub(super) deferred_cast_checks: RefCell<Vec<super::cast::CastCheck<'tcx>>>,
49 pub(super) deferred_transmute_checks: RefCell<Vec<(Ty<'tcx>, Ty<'tcx>, Span)>>,
51 pub(super) deferred_asm_checks: RefCell<Vec<(&'tcx hir::InlineAsm<'tcx>, hir::HirId)>>,
53 pub(super) deferred_generator_interiors:
54 RefCell<Vec<(hir::BodyId, Ty<'tcx>, hir::GeneratorKind)>>,
56 pub(super) body_id: Option<hir::BodyId>,
58 /// Whenever we introduce an adjustment from `!` into a type variable,
59 /// we record that type variable here. This is later used to inform
60 /// fallback. See the `fallback` module for details.
61 pub(super) diverging_type_vars: RefCell<FxHashSet<Ty<'tcx>>>,
64 impl<'a, 'tcx> Deref for Inherited<'a, 'tcx> {
65 type Target = InferCtxt<'a, 'tcx>;
66 fn deref(&self) -> &Self::Target {
71 /// A temporary returned by `Inherited::build(...)`. This is necessary
72 /// for multiple `InferCtxt` to share the same `in_progress_typeck_results`
73 /// without using `Rc` or something similar.
74 pub struct InheritedBuilder<'tcx> {
75 infcx: infer::InferCtxtBuilder<'tcx>,
79 impl<'tcx> Inherited<'_, 'tcx> {
80 pub fn build(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> InheritedBuilder<'tcx> {
81 let hir_owner = tcx.hir().local_def_id_to_hir_id(def_id).owner;
87 .with_fresh_in_progress_typeck_results(hir_owner),
93 impl<'tcx> InheritedBuilder<'tcx> {
94 pub fn enter<F, R>(&mut self, f: F) -> R
96 F: for<'a> FnOnce(Inherited<'a, 'tcx>) -> R,
98 let def_id = self.def_id;
99 self.infcx.enter(|infcx| f(Inherited::new(infcx, def_id)))
103 impl<'a, 'tcx> Inherited<'a, 'tcx> {
104 fn new(infcx: InferCtxt<'a, 'tcx>, def_id: LocalDefId) -> Self {
106 let body_id = tcx.hir().maybe_body_owned_by(def_id);
108 infcx.in_progress_typeck_results.expect("building `FnCtxt` without typeck results");
113 fulfillment_cx: RefCell::new(<dyn TraitEngine<'_>>::new(tcx)),
114 locals: RefCell::new(Default::default()),
115 deferred_call_resolutions: RefCell::new(Default::default()),
116 deferred_cast_checks: RefCell::new(Vec::new()),
117 deferred_transmute_checks: RefCell::new(Vec::new()),
118 deferred_asm_checks: RefCell::new(Vec::new()),
119 deferred_generator_interiors: RefCell::new(Vec::new()),
120 diverging_type_vars: RefCell::new(Default::default()),
125 #[instrument(level = "debug", skip(self))]
126 pub(super) fn register_predicate(&self, obligation: traits::PredicateObligation<'tcx>) {
127 if obligation.has_escaping_bound_vars() {
128 span_bug!(obligation.cause.span, "escaping bound vars in predicate {:?}", obligation);
130 self.fulfillment_cx.borrow_mut().register_predicate_obligation(self, obligation);
133 pub(super) fn register_predicates<I>(&self, obligations: I)
135 I: IntoIterator<Item = traits::PredicateObligation<'tcx>>,
137 for obligation in obligations {
138 self.register_predicate(obligation);
142 pub(super) fn register_infer_ok_obligations<T>(&self, infer_ok: InferOk<'tcx, T>) -> T {
143 self.register_predicates(infer_ok.obligations);
147 pub(super) fn normalize_associated_types_in<T>(
151 param_env: ty::ParamEnv<'tcx>,
155 T: TypeFoldable<'tcx>,
157 self.normalize_associated_types_in_with_cause(
158 ObligationCause::misc(span, body_id),
164 pub(super) fn normalize_associated_types_in_with_cause<T>(
166 cause: ObligationCause<'tcx>,
167 param_env: ty::ParamEnv<'tcx>,
171 T: TypeFoldable<'tcx>,
173 let ok = self.partially_normalize_associated_types_in(cause, param_env, value);
175 self.register_infer_ok_obligations(ok)