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.
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.
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;
18 mod different_lifetimes;
20 mod named_anon_conflict;
22 mod static_impl_trait;
25 impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> {
26 pub fn try_report_nice_region_error(&self, error: &RegionResolutionError<'tcx>) -> bool {
28 ConcreteFailure(..) | SubSupConflict(..) => {}
29 _ => return false, // inapplicable
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()
36 NiceRegionError::new(self.tcx, error.clone(), None).try_report().is_some()
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>>,
48 impl<'cx, 'gcx, 'tcx> NiceRegionError<'cx, 'gcx, 'tcx> {
50 tcx: TyCtxt<'cx, 'gcx, 'tcx>,
51 error: RegionResolutionError<'tcx>,
52 tables: Option<&'cx ty::TypeckTables<'tcx>>,
54 Self { tcx, error: Some(error), regions: None, tables }
58 tcx: TyCtxt<'cx, 'gcx, 'tcx>,
60 sub: ty::Region<'tcx>,
61 sup: ty::Region<'tcx>,
62 tables: Option<&'cx ty::TypeckTables<'tcx>>,
64 Self { tcx, error: None, regions: Some((span, sub, sup)), tables }
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())
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"),