From 698f8c2f01ea549d77d7dc3338a12e04c11057b9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:02:58 +0200 Subject: Adding upstream version 1.64.0+dfsg1. Signed-off-by: Daniel Baumann --- .../infer/error_reporting/nice_region_error/mod.rs | 77 ++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 compiler/rustc_infer/src/infer/error_reporting/nice_region_error/mod.rs (limited to 'compiler/rustc_infer/src/infer/error_reporting/nice_region_error/mod.rs') diff --git a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/mod.rs new file mode 100644 index 000000000..53d9acf7d --- /dev/null +++ b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/mod.rs @@ -0,0 +1,77 @@ +use crate::infer::lexical_region_resolve::RegionResolutionError; +use crate::infer::lexical_region_resolve::RegionResolutionError::*; +use crate::infer::InferCtxt; +use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed}; +use rustc_middle::ty::{self, TyCtxt}; +use rustc_span::source_map::Span; + +mod different_lifetimes; +pub mod find_anon_type; +mod mismatched_static_lifetime; +mod named_anon_conflict; +mod placeholder_error; +mod static_impl_trait; +mod trait_impl_difference; +mod util; + +pub use different_lifetimes::suggest_adding_lifetime_params; +pub use find_anon_type::find_anon_type; +pub use static_impl_trait::{suggest_new_region_bound, HirTraitObjectVisitor, TraitObjectVisitor}; +pub use util::find_param_with_region; + +impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> { + pub fn try_report_nice_region_error(&self, error: &RegionResolutionError<'tcx>) -> bool { + NiceRegionError::new(self, error.clone()).try_report().is_some() + } +} + +pub struct NiceRegionError<'cx, 'tcx> { + infcx: &'cx InferCtxt<'cx, 'tcx>, + error: Option>, + regions: Option<(Span, ty::Region<'tcx>, ty::Region<'tcx>)>, +} + +impl<'cx, 'tcx> NiceRegionError<'cx, 'tcx> { + pub fn new(infcx: &'cx InferCtxt<'cx, 'tcx>, error: RegionResolutionError<'tcx>) -> Self { + Self { infcx, error: Some(error), regions: None } + } + + pub fn new_from_span( + infcx: &'cx InferCtxt<'cx, 'tcx>, + span: Span, + sub: ty::Region<'tcx>, + sup: ty::Region<'tcx>, + ) -> Self { + Self { infcx, error: None, regions: Some((span, sub, sup)) } + } + + fn tcx(&self) -> TyCtxt<'tcx> { + self.infcx.tcx + } + + pub fn try_report_from_nll(&self) -> Option> { + // Due to the improved diagnostics returned by the MIR borrow checker, only a subset of + // the nice region errors are required when running under the MIR borrow checker. + self.try_report_named_anon_conflict().or_else(|| self.try_report_placeholder_conflict()) + } + + pub fn try_report(&self) -> Option { + self.try_report_from_nll() + .map(|mut diag| diag.emit()) + .or_else(|| self.try_report_impl_not_conforming_to_trait()) + .or_else(|| self.try_report_anon_anon_conflict()) + .or_else(|| self.try_report_static_impl_trait()) + .or_else(|| self.try_report_mismatched_static_lifetime()) + } + + pub(super) fn regions(&self) -> Option<(Span, ty::Region<'tcx>, ty::Region<'tcx>)> { + match (&self.error, self.regions) { + (Some(ConcreteFailure(origin, sub, sup)), None) => Some((origin.span(), *sub, *sup)), + (Some(SubSupConflict(_, _, origin, sub, _, sup, _)), None) => { + Some((origin.span(), *sub, *sup)) + } + (None, Some((span, sub, sup))) => Some((span, sub, sup)), + _ => None, + } + } +} -- cgit v1.2.3