rustc_hir_typeck/
typeck_root_ctxt.rs1use std::cell::{Cell, RefCell};
2use std::ops::Deref;
3
4use rustc_data_structures::unord::UnordSet;
5use rustc_hir::def_id::LocalDefId;
6use rustc_hir::{self as hir, HirId, HirIdMap};
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::{self, FulfillmentError, TraitEngine, TraitEngineExt as _};
13use tracing::instrument;
14
15use super::callee::DeferredCallResolution;
16
17pub(crate) struct TypeckRootCtxt<'tcx> {
29 pub(super) infcx: InferCtxt<'tcx>,
30
31 pub(super) typeck_results: RefCell<ty::TypeckResults<'tcx>>,
32
33 pub(super) locals: RefCell<HirIdMap<Ty<'tcx>>>,
34
35 pub(super) fulfillment_cx: RefCell<Box<dyn TraitEngine<'tcx, FulfillmentError<'tcx>>>>,
36
37 pub(super) checked_opaque_types_storage_entries: Cell<Option<OpaqueTypeStorageEntries>>,
41
42 pub(super) deferred_sized_obligations:
46 RefCell<Vec<(Ty<'tcx>, Span, traits::ObligationCauseCode<'tcx>)>>,
47
48 pub(super) deferred_call_resolutions: RefCell<LocalDefIdMap<Vec<DeferredCallResolution<'tcx>>>>,
56
57 pub(super) deferred_cast_checks: RefCell<Vec<super::cast::CastCheck<'tcx>>>,
58
59 pub(super) deferred_transmute_checks: RefCell<Vec<(Ty<'tcx>, Ty<'tcx>, HirId)>>,
60
61 pub(super) deferred_asm_checks: RefCell<Vec<(&'tcx hir::InlineAsm<'tcx>, HirId)>>,
62
63 pub(super) deferred_repeat_expr_checks:
64 RefCell<Vec<(&'tcx hir::Expr<'tcx>, Ty<'tcx>, ty::Const<'tcx>)>>,
65
66 pub(super) diverging_type_vars: RefCell<UnordSet<Ty<'tcx>>>,
70}
71
72impl<'tcx> Deref for TypeckRootCtxt<'tcx> {
73 type Target = InferCtxt<'tcx>;
74 fn deref(&self) -> &Self::Target {
75 &self.infcx
76 }
77}
78
79impl<'tcx> TypeckRootCtxt<'tcx> {
80 pub(crate) fn new(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Self {
81 let hir_owner = tcx.local_def_id_to_hir_id(def_id).owner;
82
83 let infcx = tcx
84 .infer_ctxt()
85 .ignoring_regions()
86 .in_hir_typeck()
87 .build(TypingMode::typeck_for_body(tcx, def_id));
88 let typeck_results = RefCell::new(ty::TypeckResults::new(hir_owner));
89 let fulfillment_cx = RefCell::new(<dyn TraitEngine<'_, _>>::new(&infcx));
90
91 TypeckRootCtxt {
92 infcx,
93 typeck_results,
94 locals: RefCell::new(Default::default()),
95 fulfillment_cx,
96 checked_opaque_types_storage_entries: Cell::new(None),
97 deferred_sized_obligations: RefCell::new(Vec::new()),
98 deferred_call_resolutions: RefCell::new(Default::default()),
99 deferred_cast_checks: RefCell::new(Vec::new()),
100 deferred_transmute_checks: RefCell::new(Vec::new()),
101 deferred_asm_checks: RefCell::new(Vec::new()),
102 deferred_repeat_expr_checks: RefCell::new(Vec::new()),
103 diverging_type_vars: RefCell::new(Default::default()),
104 }
105 }
106
107 #[instrument(level = "debug", skip(self))]
108 pub(super) fn register_predicate(&self, obligation: traits::PredicateObligation<'tcx>) {
109 if obligation.has_escaping_bound_vars() {
110 span_bug!(obligation.cause.span, "escaping bound vars in predicate {:?}", obligation);
111 }
112
113 self.fulfillment_cx.borrow_mut().register_predicate_obligation(self, obligation);
114 }
115
116 pub(super) fn register_predicates<I>(&self, obligations: I)
117 where
118 I: IntoIterator<Item = traits::PredicateObligation<'tcx>>,
119 {
120 for obligation in obligations {
121 self.register_predicate(obligation);
122 }
123 }
124
125 pub(super) fn register_infer_ok_obligations<T>(&self, infer_ok: InferOk<'tcx, T>) -> T {
126 self.register_predicates(infer_ok.obligations);
127 infer_ok.value
128 }
129}