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