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