]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_typeck/src/check/inherited.rs
New upstream version 1.59.0+dfsg1
[rustc.git] / compiler / rustc_typeck / src / check / inherited.rs
CommitLineData
1b1a35ee
XL
1use super::callee::DeferredCallResolution;
2use super::MaybeInProgressTables;
3
c295e0f8 4use rustc_data_structures::fx::FxHashSet;
1b1a35ee
XL
5use rustc_hir as hir;
6use rustc_hir::def_id::{DefIdMap, LocalDefId};
7use rustc_hir::HirIdMap;
8use rustc_infer::infer;
9use rustc_infer::infer::{InferCtxt, InferOk, TyCtxtInferExt};
10use rustc_middle::ty::fold::TypeFoldable;
94222f64 11use rustc_middle::ty::{self, Ty, TyCtxt};
1b1a35ee
XL
12use rustc_span::{self, Span};
13use rustc_trait_selection::infer::InferCtxtExt as _;
136023e0 14use rustc_trait_selection::traits::{self, ObligationCause, TraitEngine, TraitEngineExt};
1b1a35ee
XL
15
16use std::cell::RefCell;
17use std::ops::Deref;
18
19/// Closures defined within the function. For example:
20///
21/// fn foo() {
22/// bar(move|| { ... })
23/// }
24///
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.
28pub struct Inherited<'a, 'tcx> {
29 pub(super) infcx: InferCtxt<'a, 'tcx>,
30
31 pub(super) typeck_results: super::MaybeInProgressTables<'a, 'tcx>,
32
33 pub(super) locals: RefCell<HirIdMap<super::LocalTy<'tcx>>>,
34
35 pub(super) fulfillment_cx: RefCell<Box<dyn TraitEngine<'tcx>>>,
36
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>)>>,
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<DefIdMap<Vec<DeferredCallResolution<'tcx>>>>,
50
51 pub(super) deferred_cast_checks: RefCell<Vec<super::cast::CastCheck<'tcx>>>,
52
53 pub(super) deferred_generator_interiors:
54 RefCell<Vec<(hir::BodyId, Ty<'tcx>, hir::GeneratorKind)>>,
55
1b1a35ee 56 pub(super) body_id: Option<hir::BodyId>,
c295e0f8
XL
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>>>,
1b1a35ee
XL
62}
63
64impl<'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/// Helper type of a temporary returned by `Inherited::build(...)`.
72/// Necessary because we can't write the following bound:
73/// `F: for<'b, 'tcx> where 'tcx FnOnce(Inherited<'b, 'tcx>)`.
74pub struct InheritedBuilder<'tcx> {
75 infcx: infer::InferCtxtBuilder<'tcx>,
76 def_id: LocalDefId,
77}
78
a2a8927a 79impl<'tcx> Inherited<'_, 'tcx> {
1b1a35ee
XL
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.infer_ctxt().with_fresh_in_progress_typeck_results(hir_owner),
85 def_id,
86 }
87 }
88}
89
90impl<'tcx> InheritedBuilder<'tcx> {
91 pub fn enter<F, R>(&mut self, f: F) -> R
92 where
93 F: for<'a> FnOnce(Inherited<'a, 'tcx>) -> R,
94 {
95 let def_id = self.def_id;
96 self.infcx.enter(|infcx| f(Inherited::new(infcx, def_id)))
97 }
98}
99
a2a8927a 100impl<'a, 'tcx> Inherited<'a, 'tcx> {
1b1a35ee
XL
101 pub(super) fn new(infcx: InferCtxt<'a, 'tcx>, def_id: LocalDefId) -> Self {
102 let tcx = infcx.tcx;
103 let item_id = tcx.hir().local_def_id_to_hir_id(def_id);
104 let body_id = tcx.hir().maybe_body_owned_by(item_id);
105
106 Inherited {
107 typeck_results: MaybeInProgressTables {
108 maybe_typeck_results: infcx.in_progress_typeck_results,
109 },
110 infcx,
6a06907d 111 fulfillment_cx: RefCell::new(<dyn TraitEngine<'_>>::new(tcx)),
1b1a35ee
XL
112 locals: RefCell::new(Default::default()),
113 deferred_sized_obligations: RefCell::new(Vec::new()),
114 deferred_call_resolutions: RefCell::new(Default::default()),
115 deferred_cast_checks: RefCell::new(Vec::new()),
116 deferred_generator_interiors: RefCell::new(Vec::new()),
c295e0f8 117 diverging_type_vars: RefCell::new(Default::default()),
1b1a35ee
XL
118 body_id,
119 }
120 }
121
122 pub(super) fn register_predicate(&self, obligation: traits::PredicateObligation<'tcx>) {
123 debug!("register_predicate({:?})", obligation);
124 if obligation.has_escaping_bound_vars() {
125 span_bug!(obligation.cause.span, "escaping bound vars in predicate {:?}", obligation);
126 }
127 self.fulfillment_cx.borrow_mut().register_predicate_obligation(self, obligation);
128 }
129
130 pub(super) fn register_predicates<I>(&self, obligations: I)
131 where
132 I: IntoIterator<Item = traits::PredicateObligation<'tcx>>,
133 {
134 for obligation in obligations {
135 self.register_predicate(obligation);
136 }
137 }
138
139 pub(super) fn register_infer_ok_obligations<T>(&self, infer_ok: InferOk<'tcx, T>) -> T {
140 self.register_predicates(infer_ok.obligations);
141 infer_ok.value
142 }
143
144 pub(super) fn normalize_associated_types_in<T>(
145 &self,
146 span: Span,
147 body_id: hir::HirId,
148 param_env: ty::ParamEnv<'tcx>,
fc512014 149 value: T,
1b1a35ee
XL
150 ) -> T
151 where
152 T: TypeFoldable<'tcx>,
153 {
136023e0
XL
154 self.normalize_associated_types_in_with_cause(
155 ObligationCause::misc(span, body_id),
156 param_env,
157 value,
158 )
159 }
160
161 pub(super) fn normalize_associated_types_in_with_cause<T>(
162 &self,
163 cause: ObligationCause<'tcx>,
164 param_env: ty::ParamEnv<'tcx>,
165 value: T,
166 ) -> T
167 where
168 T: TypeFoldable<'tcx>,
169 {
170 let ok = self.partially_normalize_associated_types_in(cause, param_env, value);
171 debug!(?ok);
1b1a35ee
XL
172 self.register_infer_ok_obligations(ok)
173 }
174}