]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_hir_typeck/src/inherited.rs
New upstream version 1.67.1+dfsg1
[rustc.git] / compiler / rustc_hir_typeck / src / inherited.rs
CommitLineData
1b1a35ee 1use super::callee::DeferredCallResolution;
1b1a35ee 2
c295e0f8 3use rustc_data_structures::fx::FxHashSet;
1b1a35ee 4use rustc_hir as hir;
064997fb 5use rustc_hir::def_id::LocalDefId;
1b1a35ee
XL
6use rustc_hir::HirIdMap;
7use rustc_infer::infer;
2b03887a 8use rustc_infer::infer::{DefiningAnchor, InferCtxt, InferOk, TyCtxtInferExt};
064997fb 9use rustc_middle::ty::visit::TypeVisitable;
94222f64 10use rustc_middle::ty::{self, Ty, TyCtxt};
064997fb 11use rustc_span::def_id::LocalDefIdMap;
1b1a35ee 12use rustc_span::{self, Span};
487cf647 13use rustc_trait_selection::traits::{self, TraitEngine, TraitEngineExt as _};
1b1a35ee
XL
14
15use std::cell::RefCell;
16use std::ops::Deref;
17
18/// Closures defined within the function. For example:
04454e1e
FG
19/// ```ignore (illustrative)
20/// fn foo() {
21/// bar(move|| { ... })
22/// }
23/// ```
1b1a35ee
XL
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.
2b03887a
FG
27pub struct Inherited<'tcx> {
28 pub(super) infcx: InferCtxt<'tcx>,
1b1a35ee 29
2b03887a 30 pub(super) typeck_results: RefCell<ty::TypeckResults<'tcx>>,
1b1a35ee
XL
31
32 pub(super) locals: RefCell<HirIdMap<super::LocalTy<'tcx>>>,
33
34 pub(super) fulfillment_cx: RefCell<Box<dyn TraitEngine<'tcx>>>,
35
487cf647
FG
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.
1b1a35ee
XL
39 pub(super) deferred_sized_obligations:
40 RefCell<Vec<(Ty<'tcx>, Span, traits::ObligationCauseCode<'tcx>)>>,
41
487cf647
FG
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.
064997fb 49 pub(super) deferred_call_resolutions: RefCell<LocalDefIdMap<Vec<DeferredCallResolution<'tcx>>>>,
1b1a35ee
XL
50
51 pub(super) deferred_cast_checks: RefCell<Vec<super::cast::CastCheck<'tcx>>>,
52
2b03887a 53 pub(super) deferred_transmute_checks: RefCell<Vec<(Ty<'tcx>, Ty<'tcx>, hir::HirId)>>,
923072b8
FG
54
55 pub(super) deferred_asm_checks: RefCell<Vec<(&'tcx hir::InlineAsm<'tcx>, hir::HirId)>>,
56
1b1a35ee
XL
57 pub(super) deferred_generator_interiors:
58 RefCell<Vec<(hir::BodyId, Ty<'tcx>, hir::GeneratorKind)>>,
59
1b1a35ee 60 pub(super) body_id: Option<hir::BodyId>,
c295e0f8
XL
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>>>,
1b1a35ee
XL
66}
67
2b03887a
FG
68impl<'tcx> Deref for Inherited<'tcx> {
69 type Target = InferCtxt<'tcx>;
1b1a35ee
XL
70 fn deref(&self) -> &Self::Target {
71 &self.infcx
72 }
73}
74
5e7ed085 75/// A temporary returned by `Inherited::build(...)`. This is necessary
2b03887a 76/// for multiple `InferCtxt` to share the same `typeck_results`
5e7ed085 77/// without using `Rc` or something similar.
1b1a35ee
XL
78pub struct InheritedBuilder<'tcx> {
79 infcx: infer::InferCtxtBuilder<'tcx>,
80 def_id: LocalDefId,
2b03887a 81 typeck_results: RefCell<ty::TypeckResults<'tcx>>,
1b1a35ee
XL
82}
83
2b03887a 84impl<'tcx> Inherited<'tcx> {
1b1a35ee
XL
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 {
064997fb
FG
89 infcx: tcx
90 .infer_ctxt()
91 .ignoring_regions()
487cf647 92 .with_opaque_type_inference(DefiningAnchor::Bind(hir_owner.def_id)),
1b1a35ee 93 def_id,
2b03887a 94 typeck_results: RefCell::new(ty::TypeckResults::new(hir_owner)),
1b1a35ee
XL
95 }
96 }
97}
98
99impl<'tcx> InheritedBuilder<'tcx> {
2b03887a 100 pub fn enter<F, R>(mut self, f: F) -> R
1b1a35ee 101 where
2b03887a 102 F: FnOnce(&Inherited<'tcx>) -> R,
1b1a35ee
XL
103 {
104 let def_id = self.def_id;
2b03887a 105 f(&Inherited::new(self.infcx.build(), def_id, self.typeck_results))
1b1a35ee
XL
106 }
107}
108
2b03887a
FG
109impl<'tcx> Inherited<'tcx> {
110 fn new(
111 infcx: InferCtxt<'tcx>,
112 def_id: LocalDefId,
113 typeck_results: RefCell<ty::TypeckResults<'tcx>>,
114 ) -> Self {
1b1a35ee 115 let tcx = infcx.tcx;
064997fb 116 let body_id = tcx.hir().maybe_body_owned_by(def_id);
1b1a35ee
XL
117
118 Inherited {
064997fb 119 typeck_results,
1b1a35ee 120 infcx,
6a06907d 121 fulfillment_cx: RefCell::new(<dyn TraitEngine<'_>>::new(tcx)),
1b1a35ee
XL
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()),
923072b8
FG
126 deferred_transmute_checks: RefCell::new(Vec::new()),
127 deferred_asm_checks: RefCell::new(Vec::new()),
1b1a35ee 128 deferred_generator_interiors: RefCell::new(Vec::new()),
c295e0f8 129 diverging_type_vars: RefCell::new(Default::default()),
1b1a35ee
XL
130 body_id,
131 }
132 }
133
5e7ed085 134 #[instrument(level = "debug", skip(self))]
1b1a35ee 135 pub(super) fn register_predicate(&self, obligation: traits::PredicateObligation<'tcx>) {
1b1a35ee
XL
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 }
1b1a35ee 155}