1 use crate::infer::InferCtxt;
2 use crate::infer::lexical_region_resolve::RegionResolutionError;
3 use crate::infer::lexical_region_resolve::RegionResolutionError::*;
4 use syntax::source_map::Span;
5 use crate::ty::{self, TyCtxt};
6 use crate::util::common::ErrorReported;
8 mod different_lifetimes;
10 mod named_anon_conflict;
11 mod placeholder_error;
13 mod static_impl_trait;
16 impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> {
17 pub fn try_report_nice_region_error(&self, error: &RegionResolutionError<'tcx>) -> bool {
19 ConcreteFailure(..) | SubSupConflict(..) => {}
20 _ => return false, // inapplicable
23 if let Some(tables) = self.in_progress_tables {
24 let tables = tables.borrow();
25 NiceRegionError::new(self, error.clone(), Some(&tables)).try_report().is_some()
27 NiceRegionError::new(self, error.clone(), None).try_report().is_some()
32 pub struct NiceRegionError<'cx, 'gcx: 'tcx, 'tcx: 'cx> {
33 infcx: &'cx InferCtxt<'cx, 'gcx, 'tcx>,
34 error: Option<RegionResolutionError<'tcx>>,
35 regions: Option<(Span, ty::Region<'tcx>, ty::Region<'tcx>)>,
36 tables: Option<&'cx ty::TypeckTables<'tcx>>,
39 impl<'cx, 'gcx, 'tcx> NiceRegionError<'cx, 'gcx, 'tcx> {
41 infcx: &'cx InferCtxt<'cx, 'gcx, 'tcx>,
42 error: RegionResolutionError<'tcx>,
43 tables: Option<&'cx ty::TypeckTables<'tcx>>,
45 Self { infcx, error: Some(error), regions: None, tables }
49 infcx: &'cx InferCtxt<'cx, 'gcx, 'tcx>,
51 sub: ty::Region<'tcx>,
52 sup: ty::Region<'tcx>,
53 tables: Option<&'cx ty::TypeckTables<'tcx>>,
55 Self { infcx, error: None, regions: Some((span, sub, sup)), tables }
58 fn tcx(&self) -> TyCtxt<'cx, 'gcx, 'tcx> {
62 pub fn try_report_from_nll(&self) -> Option<ErrorReported> {
63 // Due to the improved diagnostics returned by the MIR borrow checker, only a subset of
64 // the nice region errors are required when running under the MIR borrow checker.
65 self.try_report_named_anon_conflict()
66 .or_else(|| self.try_report_placeholder_conflict())
69 pub fn try_report(&self) -> Option<ErrorReported> {
70 self.try_report_from_nll()
71 .or_else(|| self.try_report_anon_anon_conflict())
72 .or_else(|| self.try_report_outlives_closure())
73 .or_else(|| self.try_report_static_impl_trait())
76 pub fn get_regions(&self) -> (Span, ty::Region<'tcx>, ty::Region<'tcx>) {
77 match (&self.error, self.regions) {
78 (Some(ConcreteFailure(origin, sub, sup)), None) => (origin.span(), sub, sup),
79 (Some(SubSupConflict(_, _, origin, sub, _, sup)), None) => (origin.span(), sub, sup),
80 (None, Some((span, sub, sup))) => (span, sub, sup),
81 (Some(_), Some(_)) => panic!("incorrectly built NiceRegionError"),
82 _ => panic!("trying to report on an incorrect lifetime failure"),