Skip to main content

rustc_trait_selection/error_reporting/
mod.rs

1use std::ops::Deref;
2
3use 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};
9
10pub mod infer;
11pub mod traits;
12
13/// 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> {
21    pub infcx: &'a InferCtxt<'tcx>,
22    pub param_env: Option<ty::ParamEnv<'tcx>>,
23
24    pub typeck_results: Option<std::cell::Ref<'a, ty::TypeckResults<'tcx>>>,
25    pub diverging_fallback_has_occurred: bool,
26
27    pub autoderef_steps: Box<dyn Fn(Ty<'tcx>) -> Vec<(Ty<'tcx>, PredicateObligations<'tcx>)> + 'a>,
28}
29
30impl<'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.
34    fn err_ctxt(&self) -> TypeErrCtxt<'_, 'tcx> {
35        TypeErrCtxt {
36            infcx: self,
37            param_env: None,
38            typeck_results: None,
39            diverging_fallback_has_occurred: false,
40            autoderef_steps: Box::new(|ty| {
41                debug_assert!(false, "shouldn't be using autoderef_steps outside of typeck");
42                vec![(ty, PredicateObligations::new())]
43            }),
44        }
45    }
46}
47
48impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
49    pub fn dcx(&self) -> DiagCtxtHandle<'a> {
50        self.infcx.dcx()
51    }
52
53    /// 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)]
57    pub 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}
61
62impl<'tcx> Deref for TypeErrCtxt<'_, 'tcx> {
63    type Target = InferCtxt<'tcx>;
64    fn deref(&self) -> &InferCtxt<'tcx> {
65        self.infcx
66    }
67}