rustc_trait_selection/error_reporting/infer/nice_region_error/
mod.rs1use rustc_errors::{Diag, ErrorGuaranteed};
2use rustc_hir::def_id::LocalDefId;
3use rustc_middle::ty::{self, TyCtxt};
4use rustc_span::Span;
5
6use crate::error_reporting::TypeErrCtxt;
7use crate::infer::RegionResolutionError;
8use crate::infer::RegionResolutionError::*;
9
10mod different_lifetimes;
11pub mod find_anon_type;
12mod mismatched_static_lifetime;
13mod named_anon_conflict;
14pub(crate) mod placeholder_error;
15mod placeholder_relation;
16mod static_impl_trait;
17mod trait_impl_difference;
18mod util;
19
20pub use different_lifetimes::suggest_adding_lifetime_params;
21pub use find_anon_type::find_anon_type;
22pub use static_impl_trait::{HirTraitObjectVisitor, TraitObjectVisitor, suggest_new_region_bound};
23pub use util::find_param_with_region;
24
25impl<'cx, 'tcx> TypeErrCtxt<'cx, 'tcx> {
26 pub fn try_report_nice_region_error(
27 &'cx self,
28 generic_param_scope: LocalDefId,
29 error: &RegionResolutionError<'tcx>,
30 ) -> Option<ErrorGuaranteed> {
31 NiceRegionError::new(self, generic_param_scope, error.clone()).try_report()
32 }
33}
34
35pub struct NiceRegionError<'cx, 'tcx> {
36 cx: &'cx TypeErrCtxt<'cx, 'tcx>,
37 generic_param_scope: LocalDefId,
40 error: Option<RegionResolutionError<'tcx>>,
41 regions: Option<(Span, ty::Region<'tcx>, ty::Region<'tcx>)>,
42}
43
44impl<'cx, 'tcx> NiceRegionError<'cx, 'tcx> {
45 pub fn new(
46 cx: &'cx TypeErrCtxt<'cx, 'tcx>,
47 generic_param_scope: LocalDefId,
48 error: RegionResolutionError<'tcx>,
49 ) -> Self {
50 Self { cx, error: Some(error), regions: None, generic_param_scope }
51 }
52
53 pub fn new_from_span(
54 cx: &'cx TypeErrCtxt<'cx, 'tcx>,
55 generic_param_scope: LocalDefId,
56 span: Span,
57 sub: ty::Region<'tcx>,
58 sup: ty::Region<'tcx>,
59 ) -> Self {
60 Self { cx, error: None, regions: Some((span, sub, sup)), generic_param_scope }
61 }
62
63 fn tcx(&self) -> TyCtxt<'tcx> {
64 self.cx.tcx
65 }
66
67 pub fn try_report_from_nll(&self) -> Option<Diag<'tcx>> {
68 self.try_report_named_anon_conflict()
71 .or_else(|| self.try_report_placeholder_conflict())
72 .or_else(|| self.try_report_placeholder_relation())
73 }
74
75 pub fn try_report(&self) -> Option<ErrorGuaranteed> {
76 self.try_report_from_nll()
77 .map(|diag| diag.emit())
78 .or_else(|| self.try_report_impl_not_conforming_to_trait())
79 .or_else(|| self.try_report_anon_anon_conflict())
80 .or_else(|| self.try_report_static_impl_trait())
81 .or_else(|| self.try_report_mismatched_static_lifetime())
82 }
83
84 pub(super) fn regions(&self) -> Option<(Span, ty::Region<'tcx>, ty::Region<'tcx>)> {
85 match (&self.error, self.regions) {
86 (Some(ConcreteFailure(origin, sub, sup)), None) => Some((origin.span(), *sub, *sup)),
87 (Some(SubSupConflict(_, _, origin, sub, _, sup, _)), None) => {
88 Some((origin.span(), *sub, *sup))
89 }
90 (None, Some((span, sub, sup))) => Some((span, sub, sup)),
91 _ => None,
92 }
93 }
94}