|
| 1 | +//! Error Reporting for when the lifetime for a type doesn't match the `impl` selected for a predicate |
| 2 | +//! to hold. |
| 3 | +
|
| 4 | +use crate::infer::error_reporting::nice_region_error::NiceRegionError; |
| 5 | +use crate::infer::error_reporting::note_and_explain_region; |
| 6 | +use crate::infer::lexical_region_resolve::RegionResolutionError; |
| 7 | +use crate::infer::{SubregionOrigin, TypeTrace}; |
| 8 | +use crate::traits::ObligationCauseCode; |
| 9 | +use rustc_errors::{Applicability, ErrorReported}; |
| 10 | +use rustc_hir as hir; |
| 11 | +use rustc_hir::intravisit::Visitor; |
| 12 | +use rustc_middle::ty::{self, TypeVisitor}; |
| 13 | +use rustc_span::MultiSpan; |
| 14 | + |
| 15 | +impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { |
| 16 | + pub(super) fn try_report_mismatched_static_lifetime(&self) -> Option<ErrorReported> { |
| 17 | + let error = self.error.as_ref()?; |
| 18 | + debug!("try_report_mismatched_static_lifetime {:?}", error); |
| 19 | + |
| 20 | + let (origin, sub, sup) = match error.clone() { |
| 21 | + RegionResolutionError::ConcreteFailure(origin, sub, sup) => (origin, sub, sup), |
| 22 | + _ => return None, |
| 23 | + }; |
| 24 | + if *sub != ty::RegionKind::ReStatic { |
| 25 | + return None; |
| 26 | + } |
| 27 | + let cause = match origin { |
| 28 | + SubregionOrigin::Subtype(box TypeTrace { ref cause, .. }) => cause, |
| 29 | + _ => return None, |
| 30 | + }; |
| 31 | + let (parent, impl_def_id) = match &cause.code { |
| 32 | + ObligationCauseCode::MatchImpl(parent, impl_def_id) => (parent, impl_def_id), |
| 33 | + _ => return None, |
| 34 | + }; |
| 35 | + let binding_span = match **parent { |
| 36 | + ObligationCauseCode::BindingObligation(_def_id, binding_span) => binding_span, |
| 37 | + _ => return None, |
| 38 | + }; |
| 39 | + let mut err = self.tcx().sess.struct_span_err(cause.span, "incompatible lifetime on type"); |
| 40 | + // FIXME: we should point at the lifetime |
| 41 | + let mut multi_span: MultiSpan = vec![binding_span].into(); |
| 42 | + multi_span |
| 43 | + .push_span_label(binding_span, "introduces a `'static` lifetime requirement".into()); |
| 44 | + err.span_note(multi_span, "because this has an unmet lifetime requirement"); |
| 45 | + note_and_explain_region(self.tcx(), &mut err, "...", sup, "..."); |
| 46 | + if let Some(impl_node) = self.tcx().hir().get_if_local(*impl_def_id) { |
| 47 | + let ty = self.tcx().type_of(*impl_def_id); |
| 48 | + let mut v = super::static_impl_trait::TraitObjectVisitor(vec![]); |
| 49 | + v.visit_ty(ty); |
| 50 | + let matching_def_ids = v.0; |
| 51 | + |
| 52 | + let impl_self_ty = match impl_node { |
| 53 | + hir::Node::Item(hir::Item { |
| 54 | + kind: hir::ItemKind::Impl(hir::Impl { self_ty, .. }), |
| 55 | + .. |
| 56 | + }) => self_ty, |
| 57 | + _ => bug!("Node not an impl."), |
| 58 | + }; |
| 59 | + |
| 60 | + for matching_def_id in matching_def_ids { |
| 61 | + let mut hir_v = |
| 62 | + super::static_impl_trait::HirTraitObjectVisitor(vec![], matching_def_id); |
| 63 | + hir_v.visit_ty(&impl_self_ty); |
| 64 | + |
| 65 | + let mut multi_span: MultiSpan = hir_v.0.clone().into(); |
| 66 | + for span in &hir_v.0 { |
| 67 | + multi_span.push_span_label( |
| 68 | + *span, |
| 69 | + "this has an implicit `'static` lifetime requirement".to_string(), |
| 70 | + ); |
| 71 | + err.span_suggestion_verbose( |
| 72 | + span.shrink_to_hi(), |
| 73 | + "consider relaxing the implicit `'static` requirement", |
| 74 | + " + '_".to_string(), |
| 75 | + Applicability::MaybeIncorrect, |
| 76 | + ); |
| 77 | + } |
| 78 | + err.span_note(multi_span, "...does not necessarily outlive the static lifetime introduced by the compatible `impl`"); |
| 79 | + } |
| 80 | + } |
| 81 | + err.emit(); |
| 82 | + Some(ErrorReported) |
| 83 | + } |
| 84 | +} |
0 commit comments