]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_infer/src/infer/error_reporting/nice_region_error/mod.rs
Move `{core,std}::stream::Stream` to `{core,std}::async_iter::AsyncIterator`.
[rust.git] / compiler / rustc_infer / src / 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 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;
15 mod util;
16
17 pub use static_impl_trait::suggest_new_region_bound;
18
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()
22     }
23 }
24
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>)>,
29 }
30
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 }
34     }
35
36     pub fn new_from_span(
37         infcx: &'cx InferCtxt<'cx, 'tcx>,
38         span: Span,
39         sub: ty::Region<'tcx>,
40         sup: ty::Region<'tcx>,
41     ) -> Self {
42         Self { infcx, error: None, regions: Some((span, sub, sup)) }
43     }
44
45     fn tcx(&self) -> TyCtxt<'tcx> {
46         self.infcx.tcx
47     }
48
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())
53     }
54
55     pub fn try_report(&self) -> Option<ErrorReported> {
56         self.try_report_from_nll()
57             .map(|mut diag| {
58                 diag.emit();
59                 ErrorReported
60             })
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())
65     }
66
67     pub 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))
72             }
73             (None, Some((span, sub, sup))) => Some((span, sub, sup)),
74             _ => None,
75         }
76     }
77 }