]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_typeck/src/check/inherited.rs
Rollup merge of #101175 - tmandry:curse-push-hook, r=jyn514
[rust.git] / compiler / rustc_typeck / src / check / 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::{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};
16
17 use std::cell::RefCell;
18 use std::ops::Deref;
19
20 /// Closures defined within the function. For example:
21 /// ```ignore (illustrative)
22 /// fn foo() {
23 ///     bar(move|| { ... })
24 /// }
25 /// ```
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>,
31
32     pub(super) typeck_results: &'a RefCell<ty::TypeckResults<'tcx>>,
33
34     pub(super) locals: RefCell<HirIdMap<super::LocalTy<'tcx>>>,
35
36     pub(super) fulfillment_cx: RefCell<Box<dyn TraitEngine<'tcx>>>,
37
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>>>>,
46
47     pub(super) deferred_cast_checks: RefCell<Vec<super::cast::CastCheck<'tcx>>>,
48
49     pub(super) deferred_transmute_checks: RefCell<Vec<(Ty<'tcx>, Ty<'tcx>, Span)>>,
50
51     pub(super) deferred_asm_checks: RefCell<Vec<(&'tcx hir::InlineAsm<'tcx>, hir::HirId)>>,
52
53     pub(super) deferred_generator_interiors:
54         RefCell<Vec<(hir::BodyId, Ty<'tcx>, hir::GeneratorKind)>>,
55
56     pub(super) body_id: Option<hir::BodyId>,
57
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>>>,
62 }
63
64 impl<'a, 'tcx> Deref for Inherited<'a, 'tcx> {
65     type Target = InferCtxt<'a, 'tcx>;
66     fn deref(&self) -> &Self::Target {
67         &self.infcx
68     }
69 }
70
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>,
76     def_id: LocalDefId,
77 }
78
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;
82
83         InheritedBuilder {
84             infcx: tcx
85                 .infer_ctxt()
86                 .ignoring_regions()
87                 .with_fresh_in_progress_typeck_results(hir_owner),
88             def_id,
89         }
90     }
91 }
92
93 impl<'tcx> InheritedBuilder<'tcx> {
94     pub fn enter<F, R>(&mut self, f: F) -> R
95     where
96         F: for<'a> FnOnce(Inherited<'a, 'tcx>) -> R,
97     {
98         let def_id = self.def_id;
99         self.infcx.enter(|infcx| f(Inherited::new(infcx, def_id)))
100     }
101 }
102
103 impl<'a, 'tcx> Inherited<'a, 'tcx> {
104     fn new(infcx: InferCtxt<'a, 'tcx>, def_id: LocalDefId) -> Self {
105         let tcx = infcx.tcx;
106         let body_id = tcx.hir().maybe_body_owned_by(def_id);
107         let typeck_results =
108             infcx.in_progress_typeck_results.expect("building `FnCtxt` without typeck results");
109
110         Inherited {
111             typeck_results,
112             infcx,
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()),
121             body_id,
122         }
123     }
124
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);
129         }
130         self.fulfillment_cx.borrow_mut().register_predicate_obligation(self, obligation);
131     }
132
133     pub(super) fn register_predicates<I>(&self, obligations: I)
134     where
135         I: IntoIterator<Item = traits::PredicateObligation<'tcx>>,
136     {
137         for obligation in obligations {
138             self.register_predicate(obligation);
139         }
140     }
141
142     pub(super) fn register_infer_ok_obligations<T>(&self, infer_ok: InferOk<'tcx, T>) -> T {
143         self.register_predicates(infer_ok.obligations);
144         infer_ok.value
145     }
146
147     pub(super) fn normalize_associated_types_in<T>(
148         &self,
149         span: Span,
150         body_id: hir::HirId,
151         param_env: ty::ParamEnv<'tcx>,
152         value: T,
153     ) -> T
154     where
155         T: TypeFoldable<'tcx>,
156     {
157         self.normalize_associated_types_in_with_cause(
158             ObligationCause::misc(span, body_id),
159             param_env,
160             value,
161         )
162     }
163
164     pub(super) fn normalize_associated_types_in_with_cause<T>(
165         &self,
166         cause: ObligationCause<'tcx>,
167         param_env: ty::ParamEnv<'tcx>,
168         value: T,
169     ) -> T
170     where
171         T: TypeFoldable<'tcx>,
172     {
173         let ok = self.partially_normalize_associated_types_in(cause, param_env, value);
174         debug!(?ok);
175         self.register_infer_ok_obligations(ok)
176     }
177 }