rustc_hir_typeck/
typeck_root_ctxt.rs

1use 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    /// This is true if we identified that this Ty (`?T`) is found in a `?T: Foo`
23    /// obligation, where:
24    ///
25    ///  * `Foo` is not `Sized`
26    ///  * `(): Foo` may be satisfied
27    pub self_in_trait: bool,
28    /// This is true if we identified that this Ty (`?T`) is found in a `<_ as
29    /// _>::AssocType = ?T`
30    pub output: bool,
31}
32
33/// Data shared between a "typeck root" and its nested bodies,
34/// e.g. closures defined within the function. For example:
35/// ```ignore (illustrative)
36/// fn foo() {
37///     bar(move || { ... })
38/// }
39/// ```
40/// Here, the function `foo()` and the closure passed to
41/// `bar()` will each have their own `FnCtxt`, but they will
42/// share the inference context, will process obligations together,
43/// can access each other's local types (scoping permitted), etc.
44pub(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    // Used to detect opaque types uses added after we've already checked them.
54    //
55    // See [FnCtxt::detect_opaque_types_added_during_writeback] for more details.
56    pub(super) checked_opaque_types_storage_entries: Cell<Option<OpaqueTypeStorageEntries>>,
57
58    /// Some additional `Sized` obligations badly affect type inference.
59    /// These obligations are added in a later stage of typeck.
60    /// Removing these may also cause additional complications, see #101066.
61    pub(super) deferred_sized_obligations:
62        RefCell<Vec<(Ty<'tcx>, Span, traits::ObligationCauseCode<'tcx>)>>,
63
64    /// When we process a call like `c()` where `c` is a closure type,
65    /// we may not have decided yet whether `c` is a `Fn`, `FnMut`, or
66    /// `FnOnce` closure. In that case, we defer full resolution of the
67    /// call until upvar inference can kick in and make the
68    /// decision. We keep these deferred resolutions grouped by the
69    /// def-id of the closure, so that once we decide, we can easily go
70    /// back and process them.
71    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    /// Whenever we introduce an adjustment from `!` into a type variable,
83    /// we record that type variable here. This is later used to inform
84    /// fallback. See the `fallback` module for details.
85    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        // (*) binder skipped
155        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            // Then construct a new obligation with Self = () added
164            // to the ParamEnv, and see if it holds.
165            let o = obligation.with(
166                self.tcx,
167                obligation.predicate.kind().rebind(
168                    // (*) binder moved here
169                    ty::PredicateKind::Clause(ty::ClauseKind::Trait(
170                        tpred.with_replaced_self_ty(self.tcx, new_self_ty),
171                    )),
172                ),
173            );
174            // Don't report overflow errors. Otherwise equivalent to may_hold.
175            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            // If the projection predicate (Foo::Bar == X) has X as a non-TyVid,
185            // we need to make it into one.
186            && 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}