1 use super::callee::DeferredCallResolution;
2 use super::MaybeInProgressTables;
4 use rustc_data_structures::fx::FxHashSet;
6 use rustc_hir::def_id::{DefIdMap, LocalDefId};
7 use rustc_hir::HirIdMap;
8 use rustc_infer::infer;
9 use rustc_infer::infer::{InferCtxt, InferOk, TyCtxtInferExt};
10 use rustc_middle::ty::fold::TypeFoldable;
11 use rustc_middle::ty::{self, Ty, TyCtxt};
12 use rustc_span::{self, Span};
13 use rustc_trait_selection::infer::InferCtxtExt as _;
14 use rustc_trait_selection::traits::{self, ObligationCause, TraitEngine, TraitEngineExt};
16 use std::cell::RefCell;
19 /// Closures defined within the function. For example:
20 /// ```ignore (illustrative)
22 /// bar(move|| { ... })
25 /// Here, the function `foo()` and the closure passed to
26 /// `bar()` will each have their own `FnCtxt`, but they will
27 /// share the inherited fields.
28 pub struct Inherited<'a, 'tcx> {
29 pub(super) infcx: InferCtxt<'a, 'tcx>,
31 pub(super) typeck_results: super::MaybeInProgressTables<'a, 'tcx>,
33 pub(super) locals: RefCell<HirIdMap<super::LocalTy<'tcx>>>,
35 pub(super) fulfillment_cx: RefCell<Box<dyn TraitEngine<'tcx>>>,
37 // Some additional `Sized` obligations badly affect type inference.
38 // These obligations are added in a later stage of typeck.
39 pub(super) deferred_sized_obligations:
40 RefCell<Vec<(Ty<'tcx>, Span, traits::ObligationCauseCode<'tcx>)>>,
42 // When we process a call like `c()` where `c` is a closure type,
43 // we may not have decided yet whether `c` is a `Fn`, `FnMut`, or
44 // `FnOnce` closure. In that case, we defer full resolution of the
45 // call until upvar inference can kick in and make the
46 // decision. We keep these deferred resolutions grouped by the
47 // def-id of the closure, so that once we decide, we can easily go
48 // back and process them.
49 pub(super) deferred_call_resolutions: RefCell<DefIdMap<Vec<DeferredCallResolution<'tcx>>>>,
51 pub(super) deferred_cast_checks: RefCell<Vec<super::cast::CastCheck<'tcx>>>,
53 pub(super) deferred_transmute_checks: RefCell<Vec<(Ty<'tcx>, Ty<'tcx>, Span)>>,
55 pub(super) deferred_asm_checks: RefCell<Vec<(&'tcx hir::InlineAsm<'tcx>, hir::HirId)>>,
57 pub(super) deferred_generator_interiors:
58 RefCell<Vec<(hir::BodyId, Ty<'tcx>, hir::GeneratorKind)>>,
60 pub(super) body_id: Option<hir::BodyId>,
62 /// Whenever we introduce an adjustment from `!` into a type variable,
63 /// we record that type variable here. This is later used to inform
64 /// fallback. See the `fallback` module for details.
65 pub(super) diverging_type_vars: RefCell<FxHashSet<Ty<'tcx>>>,
68 impl<'a, 'tcx> Deref for Inherited<'a, 'tcx> {
69 type Target = InferCtxt<'a, 'tcx>;
70 fn deref(&self) -> &Self::Target {
75 /// A temporary returned by `Inherited::build(...)`. This is necessary
76 /// for multiple `InferCtxt` to share the same `in_progress_typeck_results`
77 /// without using `Rc` or something similar.
78 pub struct InheritedBuilder<'tcx> {
79 infcx: infer::InferCtxtBuilder<'tcx>,
83 impl<'tcx> Inherited<'_, 'tcx> {
84 pub fn build(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> InheritedBuilder<'tcx> {
85 let hir_owner = tcx.hir().local_def_id_to_hir_id(def_id).owner;
88 infcx: tcx.infer_ctxt().with_fresh_in_progress_typeck_results(hir_owner),
94 impl<'tcx> InheritedBuilder<'tcx> {
95 pub fn enter<F, R>(&mut self, f: F) -> R
97 F: for<'a> FnOnce(Inherited<'a, 'tcx>) -> R,
99 let def_id = self.def_id;
100 self.infcx.enter(|infcx| f(Inherited::new(infcx, def_id)))
104 impl<'a, 'tcx> Inherited<'a, 'tcx> {
105 pub(super) fn new(infcx: InferCtxt<'a, 'tcx>, def_id: LocalDefId) -> Self {
107 let item_id = tcx.hir().local_def_id_to_hir_id(def_id);
108 let body_id = tcx.hir().maybe_body_owned_by(item_id);
111 typeck_results: MaybeInProgressTables {
112 maybe_typeck_results: infcx.in_progress_typeck_results,
115 fulfillment_cx: RefCell::new(<dyn TraitEngine<'_>>::new(tcx)),
116 locals: RefCell::new(Default::default()),
117 deferred_sized_obligations: RefCell::new(Vec::new()),
118 deferred_call_resolutions: RefCell::new(Default::default()),
119 deferred_cast_checks: RefCell::new(Vec::new()),
120 deferred_transmute_checks: RefCell::new(Vec::new()),
121 deferred_asm_checks: RefCell::new(Vec::new()),
122 deferred_generator_interiors: RefCell::new(Vec::new()),
123 diverging_type_vars: RefCell::new(Default::default()),
128 #[instrument(level = "debug", skip(self))]
129 pub(super) fn register_predicate(&self, obligation: traits::PredicateObligation<'tcx>) {
130 if obligation.has_escaping_bound_vars() {
131 span_bug!(obligation.cause.span, "escaping bound vars in predicate {:?}", obligation);
133 self.fulfillment_cx.borrow_mut().register_predicate_obligation(self, obligation);
136 pub(super) fn register_predicates<I>(&self, obligations: I)
138 I: IntoIterator<Item = traits::PredicateObligation<'tcx>>,
140 for obligation in obligations {
141 self.register_predicate(obligation);
145 pub(super) fn register_infer_ok_obligations<T>(&self, infer_ok: InferOk<'tcx, T>) -> T {
146 self.register_predicates(infer_ok.obligations);
150 pub(super) fn normalize_associated_types_in<T>(
154 param_env: ty::ParamEnv<'tcx>,
158 T: TypeFoldable<'tcx>,
160 self.normalize_associated_types_in_with_cause(
161 ObligationCause::misc(span, body_id),
167 pub(super) fn normalize_associated_types_in_with_cause<T>(
169 cause: ObligationCause<'tcx>,
170 param_env: ty::ParamEnv<'tcx>,
174 T: TypeFoldable<'tcx>,
176 let ok = self.partially_normalize_associated_types_in(cause, param_env, value);
178 self.register_infer_ok_obligations(ok)