]> git.lizzy.rs Git - rust.git/blob - src/librustc_infer/infer/error_reporting/nice_region_error/mod.rs
2aed3d9a469fb82704b9e120d72c8885e4e2fc79
[rust.git] / src / librustc_infer / infer / error_reporting / nice_region_error / mod.rs
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;
7
8 mod different_lifetimes;
9 mod find_anon_type;
10 mod named_anon_conflict;
11 mod outlives_closure;
12 mod placeholder_error;
13 mod static_impl_trait;
14 mod trait_impl_difference;
15 mod util;
16
17 impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
18     pub fn try_report_nice_region_error(&self, error: &RegionResolutionError<'tcx>) -> bool {
19         NiceRegionError::new(self, error.clone()).try_report().is_some()
20     }
21 }
22
23 pub struct NiceRegionError<'cx, 'tcx> {
24     infcx: &'cx InferCtxt<'cx, 'tcx>,
25     error: Option<RegionResolutionError<'tcx>>,
26     regions: Option<(Span, ty::Region<'tcx>, ty::Region<'tcx>)>,
27 }
28
29 impl<'cx, 'tcx> NiceRegionError<'cx, 'tcx> {
30     pub fn new(infcx: &'cx InferCtxt<'cx, 'tcx>, error: RegionResolutionError<'tcx>) -> Self {
31         Self { infcx, error: Some(error), regions: None }
32     }
33
34     pub fn new_from_span(
35         infcx: &'cx InferCtxt<'cx, 'tcx>,
36         span: Span,
37         sub: ty::Region<'tcx>,
38         sup: ty::Region<'tcx>,
39     ) -> Self {
40         Self { infcx, error: None, regions: Some((span, sub, sup)) }
41     }
42
43     fn tcx(&self) -> TyCtxt<'tcx> {
44         self.infcx.tcx
45     }
46
47     pub fn try_report_from_nll(&self) -> Option<DiagnosticBuilder<'cx>> {
48         // Due to the improved diagnostics returned by the MIR borrow checker, only a subset of
49         // the nice region errors are required when running under the MIR borrow checker.
50         self.try_report_named_anon_conflict().or_else(|| self.try_report_placeholder_conflict())
51     }
52
53     pub fn try_report(&self) -> Option<ErrorReported> {
54         self.try_report_from_nll()
55             .map(|mut diag| {
56                 diag.emit();
57                 ErrorReported
58             })
59             .or_else(|| self.try_report_anon_anon_conflict())
60             .or_else(|| self.try_report_outlives_closure())
61             .or_else(|| self.try_report_static_impl_trait())
62             .or_else(|| self.try_report_impl_not_conforming_to_trait())
63     }
64
65     pub fn regions(&self) -> Option<(Span, ty::Region<'tcx>, ty::Region<'tcx>)> {
66         match (&self.error, self.regions) {
67             (Some(ConcreteFailure(origin, sub, sup)), None) => Some((origin.span(), sub, sup)),
68             (Some(SubSupConflict(_, _, origin, sub, _, sup)), None) => {
69                 Some((origin.span(), sub, sup))
70             }
71             (None, Some((span, sub, sup))) => Some((span, sub, sup)),
72             _ => None,
73         }
74     }
75 }