]> git.lizzy.rs Git - rust.git/blob - src/librustc/infer/error_reporting/nice_region_error/mod.rs
Account for --remap-path-prefix in save-analysis
[rust.git] / src / librustc / infer / error_reporting / nice_region_error / mod.rs
1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use infer::InferCtxt;
12 use infer::lexical_region_resolve::RegionResolutionError;
13 use infer::lexical_region_resolve::RegionResolutionError::*;
14 use syntax::codemap::Span;
15 use ty::{self, TyCtxt};
16 use util::common::ErrorReported;
17
18 mod different_lifetimes;
19 mod find_anon_type;
20 mod named_anon_conflict;
21 mod outlives_closure;
22 mod static_impl_trait;
23 mod util;
24
25 impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> {
26     pub fn try_report_nice_region_error(&self, error: &RegionResolutionError<'tcx>) -> bool {
27         match *error {
28             ConcreteFailure(..) | SubSupConflict(..) => {}
29             _ => return false,  // inapplicable
30         }
31
32         if let Some(tables) = self.in_progress_tables {
33             let tables = tables.borrow();
34             NiceRegionError::new(self.tcx, error.clone(), Some(&tables)).try_report().is_some()
35         } else {
36             NiceRegionError::new(self.tcx, error.clone(), None).try_report().is_some()
37         }
38     }
39 }
40
41 pub struct NiceRegionError<'cx, 'gcx: 'tcx, 'tcx: 'cx> {
42     tcx: TyCtxt<'cx, 'gcx, 'tcx>,
43     error: Option<RegionResolutionError<'tcx>>,
44     regions: Option<(Span, ty::Region<'tcx>, ty::Region<'tcx>)>,
45     tables: Option<&'cx ty::TypeckTables<'tcx>>,
46 }
47
48 impl<'cx, 'gcx, 'tcx> NiceRegionError<'cx, 'gcx, 'tcx> {
49     pub fn new(
50         tcx: TyCtxt<'cx, 'gcx, 'tcx>,
51         error: RegionResolutionError<'tcx>,
52         tables: Option<&'cx ty::TypeckTables<'tcx>>,
53     ) -> Self {
54         Self { tcx, error: Some(error), regions: None, tables }
55     }
56
57     pub fn new_from_span(
58         tcx: TyCtxt<'cx, 'gcx, 'tcx>,
59         span: Span,
60         sub: ty::Region<'tcx>,
61         sup: ty::Region<'tcx>,
62         tables: Option<&'cx ty::TypeckTables<'tcx>>,
63     ) -> Self {
64         Self { tcx, error: None, regions: Some((span, sub, sup)), tables }
65     }
66
67     pub fn try_report(&self) -> Option<ErrorReported> {
68         self.try_report_named_anon_conflict()
69             .or_else(|| self.try_report_anon_anon_conflict())
70             .or_else(|| self.try_report_outlives_closure())
71             .or_else(|| self.try_report_static_impl_trait())
72     }
73
74     pub fn get_regions(&self) -> (Span, ty::Region<'tcx>, ty::Region<'tcx>) {
75         match (&self.error, self.regions) {
76             (&Some(ConcreteFailure(ref origin, sub, sup)), None) => (origin.span(), sub, sup),
77             (&Some(SubSupConflict(_, ref origin, sub, _, sup)), None) => (origin.span(), sub, sup),
78             (None, Some((span, sub, sup))) => (span, sub, sup),
79             (Some(_), Some(_)) => panic!("incorrectly built NiceRegionError"),
80             _ => panic!("trying to report on an incorrect lifetime failure"),
81         }
82     }
83 }