1use std::ops::Deref;
23use rustc_errors::DiagCtxtHandle;
4use rustc_infer::infer::InferCtxt;
5use rustc_infer::traits::PredicateObligations;
6use rustc_macros::extension;
7use rustc_middle::bug;
8use rustc_middle::ty::{self, Ty};
910pub mod infer;
11pub mod traits;
1213/// A helper for building type related errors. The `typeck_results`
14/// field is only populated during an in-progress typeck.
15/// Get an instance by calling `InferCtxt::err_ctxt` or `FnCtxt::err_ctxt`.
16///
17/// You must only create this if you intend to actually emit an error (or
18/// perhaps a warning, though preferably not.) It provides a lot of utility
19/// methods which should not be used during the happy path.
20pub struct TypeErrCtxt<'a, 'tcx> {
21pub infcx: &'a InferCtxt<'tcx>,
22pub param_env: Option<ty::ParamEnv<'tcx>>,
2324pub typeck_results: Option<std::cell::Ref<'a, ty::TypeckResults<'tcx>>>,
25pub diverging_fallback_has_occurred: bool,
2627pub autoderef_steps: Box<dyn Fn(Ty<'tcx>) -> Vec<(Ty<'tcx>, PredicateObligations<'tcx>)> + 'a>,
28}
2930impl<'tcx> InferCtxtErrorExt<'tcx> for InferCtxt<'tcx> {
#[doc = " Creates a `TypeErrCtxt` for emitting various inference errors."]
#[doc = " During typeck, use `FnCtxt::err_ctxt` instead."]
fn err_ctxt(&self) -> TypeErrCtxt<'_, 'tcx> {
TypeErrCtxt {
infcx: self,
param_env: None,
typeck_results: None,
diverging_fallback_has_occurred: false,
autoderef_steps: Box::new(|ty|
{
if true {
if !false {
{
::core::panicking::panic_fmt(format_args!("shouldn\'t be using autoderef_steps outside of typeck"));
}
};
};
::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(ty, PredicateObligations::new())]))
}),
}
}
}#[extension(pub trait InferCtxtErrorExt<'tcx>)]31impl<'tcx> InferCtxt<'tcx> {
32/// Creates a `TypeErrCtxt` for emitting various inference errors.
33 /// During typeck, use `FnCtxt::err_ctxt` instead.
34fn err_ctxt(&self) -> TypeErrCtxt<'_, 'tcx> {
35TypeErrCtxt {
36 infcx: self,
37 param_env: None,
38 typeck_results: None,
39 diverging_fallback_has_occurred: false,
40 autoderef_steps: Box::new(|ty| {
41debug_assert!(false, "shouldn't be using autoderef_steps outside of typeck");
42vec![(ty, PredicateObligations::new())]
43 }),
44 }
45 }
46}
4748impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
49pub fn dcx(&self) -> DiagCtxtHandle<'a> {
50self.infcx.dcx()
51 }
5253/// This is just to avoid a potential footgun of accidentally
54 /// dropping `typeck_results` by calling `InferCtxt::err_ctxt`
55#[deprecated(note = "you already have a `TypeErrCtxt`")]
56 #[allow(unused)]
57pub fn err_ctxt(&self) -> ! {
58::rustc_middle::util::bug::bug_fmt(format_args!("called `err_ctxt` on `TypeErrCtxt`. Try removing the call"));bug!("called `err_ctxt` on `TypeErrCtxt`. Try removing the call");
59 }
60}
6162impl<'tcx> Dereffor TypeErrCtxt<'_, 'tcx> {
63type Target = InferCtxt<'tcx>;
64fn deref(&self) -> &InferCtxt<'tcx> {
65self.infcx
66 }
67}