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