1use std::cell::{Cell, RefCell};
2use std::ops::Deref;
34use rustc_data_structures::unord::UnordSet;
5use rustc_hir::def_id::LocalDefId;
6use rustc_hir::{selfas 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, TraitEngineExtas _};
13use tracing::instrument;
1415use super::callee::DeferredCallResolution;
1617/// Data shared between a "typeck root" and its nested bodies,
18/// e.g. closures defined within the function. For example:
19/// ```ignore (illustrative)
20/// fn foo() {
21/// bar(move || { ... })
22/// }
23/// ```
24/// Here, the function `foo()` and the closure passed to
25/// `bar()` will each have their own `FnCtxt`, but they will
26/// share the inference context, will process obligations together,
27/// can access each other's local types (scoping permitted), etc.
28pub(crate) struct TypeckRootCtxt<'tcx> {
29pub(super) infcx: InferCtxt<'tcx>,
3031pub(super) typeck_results: RefCell<ty::TypeckResults<'tcx>>,
3233pub(super) locals: RefCell<HirIdMap<Ty<'tcx>>>,
3435pub(super) fulfillment_cx: RefCell<Box<dyn TraitEngine<'tcx, FulfillmentError<'tcx>>>>,
3637// Used to detect opaque types uses added after we've already checked them.
38 //
39 // See [FnCtxt::detect_opaque_types_added_during_writeback] for more details.
40pub(super) checked_opaque_types_storage_entries: Cell<Option<OpaqueTypeStorageEntries>>,
4142/// Some additional `Sized` obligations badly affect type inference.
43 /// These obligations are added in a later stage of typeck.
44 /// Removing these may also cause additional complications, see #101066.
45pub(super) deferred_sized_obligations:
46RefCell<Vec<(Ty<'tcx>, Span, traits::ObligationCauseCode<'tcx>)>>,
4748/// When we process a call like `c()` where `c` is a closure type,
49 /// we may not have decided yet whether `c` is a `Fn`, `FnMut`, or
50 /// `FnOnce` closure. In that case, we defer full resolution of the
51 /// call until upvar inference can kick in and make the
52 /// decision. We keep these deferred resolutions grouped by the
53 /// def-id of the closure, so that once we decide, we can easily go
54 /// back and process them.
55pub(super) deferred_call_resolutions: RefCell<LocalDefIdMap<Vec<DeferredCallResolution<'tcx>>>>,
5657pub(super) deferred_cast_checks: RefCell<Vec<super::cast::CastCheck<'tcx>>>,
5859pub(super) deferred_transmute_checks: RefCell<Vec<(Ty<'tcx>, Ty<'tcx>, HirId)>>,
6061pub(super) deferred_asm_checks: RefCell<Vec<(&'tcx hir::InlineAsm<'tcx>, HirId)>>,
6263pub(super) deferred_repeat_expr_checks:
64RefCell<Vec<(&'tcx hir::Expr<'tcx>, Ty<'tcx>, ty::Const<'tcx>)>>,
6566/// Whenever we introduce an adjustment from `!` into a type variable,
67 /// we record that type variable here. This is later used to inform
68 /// fallback. See the `fallback` module for details.
69pub(super) diverging_type_vars: RefCell<UnordSet<Ty<'tcx>>>,
70}
7172impl<'tcx> Dereffor TypeckRootCtxt<'tcx> {
73type Target = InferCtxt<'tcx>;
74fn deref(&self) -> &Self::Target {
75&self.infcx
76 }
77}
7879impl<'tcx> TypeckRootCtxt<'tcx> {
80pub(crate) fn new(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Self {
81let hir_owner = tcx.local_def_id_to_hir_id(def_id).owner;
8283let infcx = tcx84 .infer_ctxt()
85 .ignoring_regions()
86 .in_hir_typeck()
87 .build(TypingMode::typeck_for_body(tcx, def_id));
88let typeck_results = RefCell::new(ty::TypeckResults::new(hir_owner));
89let fulfillment_cx = RefCell::new(<dyn TraitEngine<'_, _>>::new(&infcx));
9091TypeckRootCtxt {
92infcx,
93typeck_results,
94 locals: RefCell::new(Default::default()),
95fulfillment_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 }
106107#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("register_predicate",
"rustc_hir_typeck::typeck_root_ctxt",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/typeck_root_ctxt.rs"),
::tracing_core::__macro_support::Option::Some(107u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::typeck_root_ctxt"),
::tracing_core::field::FieldSet::new(&["obligation"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&obligation)
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: () = loop {};
return __tracing_attr_fake_return;
}
{
if obligation.has_escaping_bound_vars() {
::rustc_middle::util::bug::span_bug_fmt(obligation.cause.span,
format_args!("escaping bound vars in predicate {0:?}",
obligation));
}
self.fulfillment_cx.borrow_mut().register_predicate_obligation(self,
obligation);
}
}
}#[instrument(level = "debug", skip(self))]108pub(super) fn register_predicate(&self, obligation: traits::PredicateObligation<'tcx>) {
109if obligation.has_escaping_bound_vars() {
110span_bug!(obligation.cause.span, "escaping bound vars in predicate {:?}", obligation);
111 }
112113self.fulfillment_cx.borrow_mut().register_predicate_obligation(self, obligation);
114 }
115116pub(super) fn register_predicates<I>(&self, obligations: I)
117where
118I: IntoIterator<Item = traits::PredicateObligation<'tcx>>,
119 {
120for obligation in obligations {
121self.register_predicate(obligation);
122 }
123 }
124125pub(super) fn register_infer_ok_obligations<T>(&self, infer_ok: InferOk<'tcx, T>) -> T {
126self.register_predicates(infer_ok.obligations);
127infer_ok.value
128 }
129}