]> git.lizzy.rs Git - rust.git/blob - src/librustc/infer/error_reporting/nice_region_error/mod.rs
Add riscv64gc-unknown-none-elf target
[rust.git] / src / librustc / infer / error_reporting / nice_region_error / mod.rs
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;
7
8 mod different_lifetimes;
9 mod find_anon_type;
10 mod named_anon_conflict;
11 mod placeholder_error;
12 mod outlives_closure;
13 mod static_impl_trait;
14 mod util;
15
16 impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> {
17     pub fn try_report_nice_region_error(&self, error: &RegionResolutionError<'tcx>) -> bool {
18         match *error {
19             ConcreteFailure(..) | SubSupConflict(..) => {}
20             _ => return false,  // inapplicable
21         }
22
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()
26         } else {
27             NiceRegionError::new(self, error.clone(), None).try_report().is_some()
28         }
29     }
30 }
31
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>>,
37 }
38
39 impl<'cx, 'gcx, 'tcx> NiceRegionError<'cx, 'gcx, 'tcx> {
40     pub fn new(
41         infcx: &'cx InferCtxt<'cx, 'gcx, 'tcx>,
42         error: RegionResolutionError<'tcx>,
43         tables: Option<&'cx ty::TypeckTables<'tcx>>,
44     ) -> Self {
45         Self { infcx, error: Some(error), regions: None, tables }
46     }
47
48     pub fn new_from_span(
49         infcx: &'cx InferCtxt<'cx, 'gcx, 'tcx>,
50         span: Span,
51         sub: ty::Region<'tcx>,
52         sup: ty::Region<'tcx>,
53         tables: Option<&'cx ty::TypeckTables<'tcx>>,
54     ) -> Self {
55         Self { infcx, error: None, regions: Some((span, sub, sup)), tables }
56     }
57
58     fn tcx(&self) -> TyCtxt<'cx, 'gcx, 'tcx> {
59         self.infcx.tcx
60     }
61
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())
67     }
68
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())
74     }
75
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"),
83         }
84     }
85 }