1 use crate::infer::error_reporting::TypeErrCtxt; 2 use crate::infer::lexical_region_resolve::RegionResolutionError; 3 use crate::infer::lexical_region_resolve::RegionResolutionError::*; 4 use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed}; 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 pub(crate) mod placeholder_error; 13 mod placeholder_relation; 14 mod static_impl_trait; 15 mod trait_impl_difference; 16 mod util; 17 18 pub use different_lifetimes::suggest_adding_lifetime_params; 19 pub use find_anon_type::find_anon_type; 20 pub use static_impl_trait::{suggest_new_region_bound, HirTraitObjectVisitor, TraitObjectVisitor}; 21 pub use util::find_param_with_region; 22 23 impl<'cx, 'tcx> TypeErrCtxt<'cx, 'tcx> { try_report_nice_region_error(&'cx self, error: &RegionResolutionError<'tcx>) -> bool24 pub fn try_report_nice_region_error(&'cx self, error: &RegionResolutionError<'tcx>) -> bool { 25 NiceRegionError::new(self, error.clone()).try_report().is_some() 26 } 27 } 28 29 pub struct NiceRegionError<'cx, 'tcx> { 30 cx: &'cx TypeErrCtxt<'cx, 'tcx>, 31 error: Option<RegionResolutionError<'tcx>>, 32 regions: Option<(Span, ty::Region<'tcx>, ty::Region<'tcx>)>, 33 } 34 35 impl<'cx, 'tcx> NiceRegionError<'cx, 'tcx> { new(cx: &'cx TypeErrCtxt<'cx, 'tcx>, error: RegionResolutionError<'tcx>) -> Self36 pub fn new(cx: &'cx TypeErrCtxt<'cx, 'tcx>, error: RegionResolutionError<'tcx>) -> Self { 37 Self { cx, error: Some(error), regions: None } 38 } 39 new_from_span( cx: &'cx TypeErrCtxt<'cx, 'tcx>, span: Span, sub: ty::Region<'tcx>, sup: ty::Region<'tcx>, ) -> Self40 pub fn new_from_span( 41 cx: &'cx TypeErrCtxt<'cx, 'tcx>, 42 span: Span, 43 sub: ty::Region<'tcx>, 44 sup: ty::Region<'tcx>, 45 ) -> Self { 46 Self { cx, error: None, regions: Some((span, sub, sup)) } 47 } 48 tcx(&self) -> TyCtxt<'tcx>49 fn tcx(&self) -> TyCtxt<'tcx> { 50 self.cx.tcx 51 } 52 try_report_from_nll(&self) -> Option<DiagnosticBuilder<'tcx, ErrorGuaranteed>>53 pub fn try_report_from_nll(&self) -> Option<DiagnosticBuilder<'tcx, ErrorGuaranteed>> { 54 // Due to the improved diagnostics returned by the MIR borrow checker, only a subset of 55 // the nice region errors are required when running under the MIR borrow checker. 56 self.try_report_named_anon_conflict() 57 .or_else(|| self.try_report_placeholder_conflict()) 58 .or_else(|| self.try_report_placeholder_relation()) 59 } 60 try_report(&self) -> Option<ErrorGuaranteed>61 pub fn try_report(&self) -> Option<ErrorGuaranteed> { 62 self.try_report_from_nll() 63 .map(|mut diag| diag.emit()) 64 .or_else(|| self.try_report_impl_not_conforming_to_trait()) 65 .or_else(|| self.try_report_anon_anon_conflict()) 66 .or_else(|| self.try_report_static_impl_trait()) 67 .or_else(|| self.try_report_mismatched_static_lifetime()) 68 } 69 regions(&self) -> Option<(Span, ty::Region<'tcx>, ty::Region<'tcx>)>70 pub(super) fn regions(&self) -> Option<(Span, ty::Region<'tcx>, ty::Region<'tcx>)> { 71 match (&self.error, self.regions) { 72 (Some(ConcreteFailure(origin, sub, sup)), None) => Some((origin.span(), *sub, *sup)), 73 (Some(SubSupConflict(_, _, origin, sub, _, sup, _)), None) => { 74 Some((origin.span(), *sub, *sup)) 75 } 76 (None, Some((span, sub, sup))) => Some((span, sub, sup)), 77 _ => None, 78 } 79 } 80 } 81