1 use crate::infer::lexical_region_resolve::RegionResolutionError;
2 use crate::infer::lexical_region_resolve::RegionResolutionError::*;
3 use crate::infer::InferCtxt;
4 use rustc_errors::{DiagnosticBuilder, ErrorReported};
5 use rustc_middle::ty::{self, TyCtxt};
6 use rustc_span::source_map::Span;
8 mod different_lifetimes;
9 pub mod find_anon_type;
10 mod mismatched_static_lifetime;
11 mod named_anon_conflict;
12 mod placeholder_error;
13 mod static_impl_trait;
14 mod trait_impl_difference;
17 pub use static_impl_trait::suggest_new_region_bound;
19 impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
20 pub fn try_report_nice_region_error(&self, error: &RegionResolutionError<'tcx>) -> bool {
21 NiceRegionError::new(self, error.clone()).try_report().is_some()
25 pub struct NiceRegionError<'cx, 'tcx> {
26 infcx: &'cx InferCtxt<'cx, 'tcx>,
27 error: Option<RegionResolutionError<'tcx>>,
28 regions: Option<(Span, ty::Region<'tcx>, ty::Region<'tcx>)>,
31 impl<'cx, 'tcx> NiceRegionError<'cx, 'tcx> {
32 pub fn new(infcx: &'cx InferCtxt<'cx, 'tcx>, error: RegionResolutionError<'tcx>) -> Self {
33 Self { infcx, error: Some(error), regions: None }
37 infcx: &'cx InferCtxt<'cx, 'tcx>,
39 sub: ty::Region<'tcx>,
40 sup: ty::Region<'tcx>,
42 Self { infcx, error: None, regions: Some((span, sub, sup)) }
45 fn tcx(&self) -> TyCtxt<'tcx> {
49 pub fn try_report_from_nll(&self) -> Option<DiagnosticBuilder<'tcx>> {
50 // Due to the improved diagnostics returned by the MIR borrow checker, only a subset of
51 // the nice region errors are required when running under the MIR borrow checker.
52 self.try_report_named_anon_conflict().or_else(|| self.try_report_placeholder_conflict())
55 pub fn try_report(&self) -> Option<ErrorReported> {
56 self.try_report_from_nll()
61 .or_else(|| self.try_report_impl_not_conforming_to_trait())
62 .or_else(|| self.try_report_anon_anon_conflict())
63 .or_else(|| self.try_report_static_impl_trait())
64 .or_else(|| self.try_report_mismatched_static_lifetime())
67 pub(super) fn regions(&self) -> Option<(Span, ty::Region<'tcx>, ty::Region<'tcx>)> {
68 match (&self.error, self.regions) {
69 (Some(ConcreteFailure(origin, sub, sup)), None) => Some((origin.span(), *sub, *sup)),
70 (Some(SubSupConflict(_, _, origin, sub, _, sup, _)), None) => {
71 Some((origin.span(), *sub, *sup))
73 (None, Some((span, sub, sup))) => Some((span, sub, sup)),