]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_hir_typeck/src/inherited.rs
fix various subst_identity vs skip_binder
[rust.git] / compiler / rustc_hir_typeck / src / inherited.rs
1 use super::callee::DeferredCallResolution;
2
3 use rustc_data_structures::fx::FxHashSet;
4 use rustc_hir as hir;
5 use rustc_hir::def_id::LocalDefId;
6 use rustc_hir::HirIdMap;
7 use rustc_infer::infer;
8 use rustc_infer::infer::{DefiningAnchor, InferCtxt, InferOk, TyCtxtInferExt};
9 use rustc_middle::ty::visit::TypeVisitable;
10 use rustc_middle::ty::{self, Ty, TyCtxt};
11 use rustc_span::def_id::LocalDefIdMap;
12 use rustc_span::{self, Span};
13 use rustc_trait_selection::traits::{self, TraitEngine, TraitEngineExt as _};
14
15 use std::cell::RefCell;
16 use std::ops::Deref;
17
18 /// Closures defined within the function. For example:
19 /// ```ignore (illustrative)
20 /// fn foo() {
21 ///     bar(move|| { ... })
22 /// }
23 /// ```
24 /// Here, the function `foo()` and the closure passed to
25 /// `bar()` will each have their own `FnCtxt`, but they will
26 /// share the inherited fields.
27 pub struct Inherited<'tcx> {
28     pub(super) infcx: InferCtxt<'tcx>,
29
30     pub(super) typeck_results: RefCell<ty::TypeckResults<'tcx>>,
31
32     pub(super) locals: RefCell<HirIdMap<super::LocalTy<'tcx>>>,
33
34     pub(super) fulfillment_cx: RefCell<Box<dyn TraitEngine<'tcx>>>,
35
36     /// Some additional `Sized` obligations badly affect type inference.
37     /// These obligations are added in a later stage of typeck.
38     /// Removing these may also cause additional complications, see #101066.
39     pub(super) deferred_sized_obligations:
40         RefCell<Vec<(Ty<'tcx>, Span, traits::ObligationCauseCode<'tcx>)>>,
41
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<LocalDefIdMap<Vec<DeferredCallResolution<'tcx>>>>,
50
51     pub(super) deferred_cast_checks: RefCell<Vec<super::cast::CastCheck<'tcx>>>,
52
53     pub(super) deferred_transmute_checks: RefCell<Vec<(Ty<'tcx>, Ty<'tcx>, hir::HirId)>>,
54
55     pub(super) deferred_asm_checks: RefCell<Vec<(&'tcx hir::InlineAsm<'tcx>, hir::HirId)>>,
56
57     pub(super) deferred_generator_interiors:
58         RefCell<Vec<(hir::BodyId, Ty<'tcx>, hir::GeneratorKind)>>,
59
60     pub(super) body_id: Option<hir::BodyId>,
61
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>>>,
66 }
67
68 impl<'tcx> Deref for Inherited<'tcx> {
69     type Target = InferCtxt<'tcx>;
70     fn deref(&self) -> &Self::Target {
71         &self.infcx
72     }
73 }
74
75 /// A temporary returned by `Inherited::build(...)`. This is necessary
76 /// for multiple `InferCtxt` to share the same `typeck_results`
77 /// without using `Rc` or something similar.
78 pub struct InheritedBuilder<'tcx> {
79     infcx: infer::InferCtxtBuilder<'tcx>,
80     def_id: LocalDefId,
81     typeck_results: RefCell<ty::TypeckResults<'tcx>>,
82 }
83
84 impl<'tcx> Inherited<'tcx> {
85     pub fn build(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> InheritedBuilder<'tcx> {
86         let hir_owner = tcx.hir().local_def_id_to_hir_id(def_id).owner;
87
88         InheritedBuilder {
89             infcx: tcx
90                 .infer_ctxt()
91                 .ignoring_regions()
92                 .with_opaque_type_inference(DefiningAnchor::Bind(hir_owner.def_id)),
93             def_id,
94             typeck_results: RefCell::new(ty::TypeckResults::new(hir_owner)),
95         }
96     }
97 }
98
99 impl<'tcx> InheritedBuilder<'tcx> {
100     pub fn enter<F, R>(mut self, f: F) -> R
101     where
102         F: FnOnce(&Inherited<'tcx>) -> R,
103     {
104         let def_id = self.def_id;
105         f(&Inherited::new(self.infcx.build(), def_id, self.typeck_results))
106     }
107 }
108
109 impl<'tcx> Inherited<'tcx> {
110     fn new(
111         infcx: InferCtxt<'tcx>,
112         def_id: LocalDefId,
113         typeck_results: RefCell<ty::TypeckResults<'tcx>>,
114     ) -> Self {
115         let tcx = infcx.tcx;
116         let body_id = tcx.hir().maybe_body_owned_by(def_id);
117
118         Inherited {
119             typeck_results,
120             infcx,
121             fulfillment_cx: RefCell::new(<dyn TraitEngine<'_>>::new(tcx)),
122             locals: RefCell::new(Default::default()),
123             deferred_sized_obligations: RefCell::new(Vec::new()),
124             deferred_call_resolutions: RefCell::new(Default::default()),
125             deferred_cast_checks: RefCell::new(Vec::new()),
126             deferred_transmute_checks: RefCell::new(Vec::new()),
127             deferred_asm_checks: RefCell::new(Vec::new()),
128             deferred_generator_interiors: RefCell::new(Vec::new()),
129             diverging_type_vars: RefCell::new(Default::default()),
130             body_id,
131         }
132     }
133
134     #[instrument(level = "debug", skip(self))]
135     pub(super) fn register_predicate(&self, obligation: traits::PredicateObligation<'tcx>) {
136         if obligation.has_escaping_bound_vars() {
137             span_bug!(obligation.cause.span, "escaping bound vars in predicate {:?}", obligation);
138         }
139         self.fulfillment_cx.borrow_mut().register_predicate_obligation(self, obligation);
140     }
141
142     pub(super) fn register_predicates<I>(&self, obligations: I)
143     where
144         I: IntoIterator<Item = traits::PredicateObligation<'tcx>>,
145     {
146         for obligation in obligations {
147             self.register_predicate(obligation);
148         }
149     }
150
151     pub(super) fn register_infer_ok_obligations<T>(&self, infer_ok: InferOk<'tcx, T>) -> T {
152         self.register_predicates(infer_ok.obligations);
153         infer_ok.value
154     }
155 }