rustc_hir_typeck/
typeck_root_ctxt.rs1use std::cell::{Cell, RefCell};
2use std::ops::Deref;
3
4use rustc_data_structures::unord::{UnordMap, UnordSet};
5use rustc_hir::def_id::LocalDefId;
6use rustc_hir::{self as hir, HirId, HirIdMap, LangItem};
7use rustc_infer::infer::{InferCtxt, InferOk, OpaqueTypeStorageEntries, TyCtxtInferExt};
8use rustc_middle::span_bug;
9use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt, TypingMode};
10use rustc_span::Span;
11use rustc_span::def_id::LocalDefIdMap;
12use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
13use rustc_trait_selection::traits::{
14 self, FulfillmentError, PredicateObligation, TraitEngine, TraitEngineExt as _,
15};
16use tracing::{debug, instrument};
17
18use super::callee::DeferredCallResolution;
19
20#[derive(Debug, Default, Copy, Clone)]
21pub(crate) struct InferVarInfo {
22 pub self_in_trait: bool,
28 pub output: bool,
31}
32
33pub(crate) struct TypeckRootCtxt<'tcx> {
45 pub(super) infcx: InferCtxt<'tcx>,
46
47 pub(super) typeck_results: RefCell<ty::TypeckResults<'tcx>>,
48
49 pub(super) locals: RefCell<HirIdMap<Ty<'tcx>>>,
50
51 pub(super) fulfillment_cx: RefCell<Box<dyn TraitEngine<'tcx, FulfillmentError<'tcx>>>>,
52
53 pub(super) checked_opaque_types_storage_entries: Cell<Option<OpaqueTypeStorageEntries>>,
57
58 pub(super) deferred_sized_obligations:
62 RefCell<Vec<(Ty<'tcx>, Span, traits::ObligationCauseCode<'tcx>)>>,
63
64 pub(super) deferred_call_resolutions: RefCell<LocalDefIdMap<Vec<DeferredCallResolution<'tcx>>>>,
72
73 pub(super) deferred_cast_checks: RefCell<Vec<super::cast::CastCheck<'tcx>>>,
74
75 pub(super) deferred_transmute_checks: RefCell<Vec<(Ty<'tcx>, Ty<'tcx>, HirId)>>,
76
77 pub(super) deferred_asm_checks: RefCell<Vec<(&'tcx hir::InlineAsm<'tcx>, HirId)>>,
78
79 pub(super) deferred_repeat_expr_checks:
80 RefCell<Vec<(&'tcx hir::Expr<'tcx>, Ty<'tcx>, ty::Const<'tcx>)>>,
81
82 pub(super) diverging_type_vars: RefCell<UnordSet<Ty<'tcx>>>,
86
87 pub(super) infer_var_info: RefCell<UnordMap<ty::TyVid, InferVarInfo>>,
88}
89
90impl<'tcx> Deref for TypeckRootCtxt<'tcx> {
91 type Target = InferCtxt<'tcx>;
92 fn deref(&self) -> &Self::Target {
93 &self.infcx
94 }
95}
96
97impl<'tcx> TypeckRootCtxt<'tcx> {
98 pub(crate) fn new(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Self {
99 let hir_owner = tcx.local_def_id_to_hir_id(def_id).owner;
100
101 let infcx = tcx
102 .infer_ctxt()
103 .ignoring_regions()
104 .in_hir_typeck()
105 .build(TypingMode::typeck_for_body(tcx, def_id));
106 let typeck_results = RefCell::new(ty::TypeckResults::new(hir_owner));
107 let fulfillment_cx = RefCell::new(<dyn TraitEngine<'_, _>>::new(&infcx));
108
109 TypeckRootCtxt {
110 infcx,
111 typeck_results,
112 locals: RefCell::new(Default::default()),
113 fulfillment_cx,
114 checked_opaque_types_storage_entries: Cell::new(None),
115 deferred_sized_obligations: RefCell::new(Vec::new()),
116 deferred_call_resolutions: RefCell::new(Default::default()),
117 deferred_cast_checks: RefCell::new(Vec::new()),
118 deferred_transmute_checks: RefCell::new(Vec::new()),
119 deferred_asm_checks: RefCell::new(Vec::new()),
120 deferred_repeat_expr_checks: RefCell::new(Vec::new()),
121 diverging_type_vars: RefCell::new(Default::default()),
122 infer_var_info: RefCell::new(Default::default()),
123 }
124 }
125
126 #[instrument(level = "debug", skip(self))]
127 pub(super) fn register_predicate(&self, obligation: traits::PredicateObligation<'tcx>) {
128 if obligation.has_escaping_bound_vars() {
129 span_bug!(obligation.cause.span, "escaping bound vars in predicate {:?}", obligation);
130 }
131
132 self.update_infer_var_info(&obligation);
133
134 self.fulfillment_cx.borrow_mut().register_predicate_obligation(self, obligation);
135 }
136
137 pub(super) fn register_predicates<I>(&self, obligations: I)
138 where
139 I: IntoIterator<Item = traits::PredicateObligation<'tcx>>,
140 {
141 for obligation in obligations {
142 self.register_predicate(obligation);
143 }
144 }
145
146 pub(super) fn register_infer_ok_obligations<T>(&self, infer_ok: InferOk<'tcx, T>) -> T {
147 self.register_predicates(infer_ok.obligations);
148 infer_ok.value
149 }
150
151 fn update_infer_var_info(&self, obligation: &PredicateObligation<'tcx>) {
152 let infer_var_info = &mut self.infer_var_info.borrow_mut();
153
154 if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(tpred)) =
156 obligation.predicate.kind().skip_binder()
157 && let Some(ty) =
158 self.shallow_resolve(tpred.self_ty()).ty_vid().map(|t| self.root_var(t))
159 && !self.tcx.is_lang_item(tpred.trait_ref.def_id, LangItem::Sized)
160 {
161 let new_self_ty = self.tcx.types.unit;
162
163 let o = obligation.with(
166 self.tcx,
167 obligation.predicate.kind().rebind(
168 ty::PredicateKind::Clause(ty::ClauseKind::Trait(
170 tpred.with_replaced_self_ty(self.tcx, new_self_ty),
171 )),
172 ),
173 );
174 if let Ok(result) = self.probe(|_| self.evaluate_obligation(&o))
176 && result.may_apply()
177 {
178 infer_var_info.entry(ty).or_default().self_in_trait = true;
179 }
180 }
181
182 if let ty::PredicateKind::Clause(ty::ClauseKind::Projection(predicate)) =
183 obligation.predicate.kind().skip_binder()
184 && let Some(vid) = predicate.term.as_type().and_then(|ty| ty.ty_vid())
187 {
188 debug!("infer_var_info: {:?}.output = true", vid);
189 infer_var_info.entry(vid).or_default().output = true;
190 }
191 }
192}