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, Unnormalized};
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
23    pub typeck_results: Option<std::cell::Ref<'a, ty::TypeckResults<'tcx>>>,
24    pub diverging_fallback_has_occurred: bool,
25
26    pub normalize_fn_sig:
27        Box<dyn Fn(Unnormalized<'tcx, ty::PolyFnSig<'tcx>>) -> ty::PolyFnSig<'tcx> + 'a>,
28
29    pub autoderef_steps: Box<dyn Fn(Ty<'tcx>) -> Vec<(Ty<'tcx>, PredicateObligations<'tcx>)> + 'a>,
30}
31
32impl<'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,
            typeck_results: None,
            diverging_fallback_has_occurred: false,
            normalize_fn_sig: Box::new(|fn_sig| fn_sig.skip_normalization()),
            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>)]
33impl<'tcx> InferCtxt<'tcx> {
34    /// Creates a `TypeErrCtxt` for emitting various inference errors.
35    /// During typeck, use `FnCtxt::err_ctxt` instead.
36    fn err_ctxt(&self) -> TypeErrCtxt<'_, 'tcx> {
37        TypeErrCtxt {
38            infcx: self,
39            typeck_results: None,
40            diverging_fallback_has_occurred: false,
41            normalize_fn_sig: Box::new(|fn_sig| fn_sig.skip_normalization()),
42            autoderef_steps: Box::new(|ty| {
43                debug_assert!(false, "shouldn't be using autoderef_steps outside of typeck");
44                vec![(ty, PredicateObligations::new())]
45            }),
46        }
47    }
48}
49
50impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
51    pub fn dcx(&self) -> DiagCtxtHandle<'a> {
52        self.infcx.dcx()
53    }
54
55    /// This is just to avoid a potential footgun of accidentally
56    /// dropping `typeck_results` by calling `InferCtxt::err_ctxt`
57    #[deprecated(note = "you already have a `TypeErrCtxt`")]
58    #[allow(unused)]
59    pub fn err_ctxt(&self) -> ! {
60        ::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");
61    }
62}
63
64impl<'tcx> Deref for TypeErrCtxt<'_, 'tcx> {
65    type Target = InferCtxt<'tcx>;
66    fn deref(&self) -> &InferCtxt<'tcx> {
67        self.infcx
68    }
69}