]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_hir_typeck/src/inherited.rs
Store relationships on Inherent
[rust.git] / compiler / rustc_hir_typeck / src / inherited.rs
1 use super::callee::DeferredCallResolution;
2
3 use rustc_data_structures::fx::{FxHashMap, 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     pub(super) relationships: RefCell<FxHashMap<ty::TyVid, ty::FoundRelationships>>,
68 }
69
70 impl<'tcx> Deref for Inherited<'tcx> {
71     type Target = InferCtxt<'tcx>;
72     fn deref(&self) -> &Self::Target {
73         &self.infcx
74     }
75 }
76
77 /// A temporary returned by `Inherited::build(...)`. This is necessary
78 /// for multiple `InferCtxt` to share the same `typeck_results`
79 /// without using `Rc` or something similar.
80 pub struct InheritedBuilder<'tcx> {
81     infcx: infer::InferCtxtBuilder<'tcx>,
82     def_id: LocalDefId,
83     typeck_results: RefCell<ty::TypeckResults<'tcx>>,
84 }
85
86 impl<'tcx> Inherited<'tcx> {
87     pub fn build(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> InheritedBuilder<'tcx> {
88         let hir_owner = tcx.hir().local_def_id_to_hir_id(def_id).owner;
89
90         InheritedBuilder {
91             infcx: tcx
92                 .infer_ctxt()
93                 .ignoring_regions()
94                 .with_opaque_type_inference(DefiningAnchor::Bind(hir_owner.def_id)),
95             def_id,
96             typeck_results: RefCell::new(ty::TypeckResults::new(hir_owner)),
97         }
98     }
99 }
100
101 impl<'tcx> InheritedBuilder<'tcx> {
102     pub fn enter<F, R>(mut self, f: F) -> R
103     where
104         F: FnOnce(&Inherited<'tcx>) -> R,
105     {
106         let def_id = self.def_id;
107         f(&Inherited::new(self.infcx.build(), def_id, self.typeck_results))
108     }
109 }
110
111 impl<'tcx> Inherited<'tcx> {
112     fn new(
113         infcx: InferCtxt<'tcx>,
114         def_id: LocalDefId,
115         typeck_results: RefCell<ty::TypeckResults<'tcx>>,
116     ) -> Self {
117         let tcx = infcx.tcx;
118         let body_id = tcx.hir().maybe_body_owned_by(def_id);
119
120         Inherited {
121             typeck_results,
122             infcx,
123             fulfillment_cx: RefCell::new(<dyn TraitEngine<'_>>::new(tcx)),
124             locals: RefCell::new(Default::default()),
125             deferred_sized_obligations: RefCell::new(Vec::new()),
126             deferred_call_resolutions: RefCell::new(Default::default()),
127             deferred_cast_checks: RefCell::new(Vec::new()),
128             deferred_transmute_checks: RefCell::new(Vec::new()),
129             deferred_asm_checks: RefCell::new(Vec::new()),
130             deferred_generator_interiors: RefCell::new(Vec::new()),
131             diverging_type_vars: RefCell::new(Default::default()),
132             body_id,
133             relationships: RefCell::new(Default::default()),
134         }
135     }
136
137     #[instrument(level = "debug", skip(self))]
138     pub(super) fn register_predicate(&self, obligation: traits::PredicateObligation<'tcx>) {
139         if obligation.has_escaping_bound_vars() {
140             span_bug!(obligation.cause.span, "escaping bound vars in predicate {:?}", obligation);
141         }
142
143         super::relationships::update(
144             &self.infcx,
145             &mut self.relationships.borrow_mut(),
146             &obligation,
147         );
148
149         self.fulfillment_cx.borrow_mut().register_predicate_obligation(self, obligation);
150     }
151
152     pub(super) fn register_predicates<I>(&self, obligations: I)
153     where
154         I: IntoIterator<Item = traits::PredicateObligation<'tcx>>,
155     {
156         for obligation in obligations {
157             self.register_predicate(obligation);
158         }
159     }
160
161     pub(super) fn register_infer_ok_obligations<T>(&self, infer_ok: InferOk<'tcx, T>) -> T {
162         self.register_predicates(infer_ok.obligations);
163         infer_ok.value
164     }
165 }