infer: use derive more

Signed-off-by: David Wood <david.wood@huawei.com>
This commit is contained in:
David Wood 2022-10-14 13:25:59 +01:00
parent f8b628bce4
commit 913f597402
4 changed files with 46 additions and 49 deletions

View file

@ -164,7 +164,9 @@ infer_region_explanation = {$pref_kind ->
} }
infer_mismatched_static_lifetime = incompatible lifetime on type infer_mismatched_static_lifetime = incompatible lifetime on type
infer_msl_impl_note = ...does not necessarily outlive the static lifetime introduced by the compatible `impl` infer_does_not_outlive_static_from_impl = ...does not necessarily outlive the static lifetime introduced by the compatible `impl`
infer_implicit_static_lifetime_note = this has an implicit `'static` lifetime requirement
infer_implicit_static_lifetime_suggestion = consider relaxing the implicit `'static` requirement
infer_msl_introduces_static = introduces a `'static` lifetime requirement infer_msl_introduces_static = introduces a `'static` lifetime requirement
infer_msl_unmet_req = because this has an unmet lifetime requirement infer_msl_unmet_req = because this has an unmet lifetime requirement
infer_msl_trait_note = this has an implicit `'static` lifetime requirement infer_msl_trait_note = this has an implicit `'static` lifetime requirement

View file

@ -459,47 +459,34 @@ impl AddToDiagnostic for IntroducesStaticBecauseUnmetLifetimeReq {
} }
} }
pub struct ImplNote { // FIXME(#100717): replace with a `Option<Span>` when subdiagnostic supports that
pub impl_span: Option<Span>, #[derive(Subdiagnostic)]
pub enum DoesNotOutliveStaticFromImpl {
#[note(infer::does_not_outlive_static_from_impl)]
Spanned {
#[primary_span]
span: Span,
},
#[note(infer::does_not_outlive_static_from_impl)]
Unspanned,
} }
impl AddToDiagnostic for ImplNote { #[derive(Subdiagnostic)]
fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F) pub enum ImplicitStaticLifetimeSubdiag {
where #[note(infer::implicit_static_lifetime_note)]
F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage, Note {
{ #[primary_span]
match self.impl_span { span: Span,
Some(span) => diag.span_note(span, fluent::infer::msl_impl_note), },
None => diag.note(fluent::infer::msl_impl_note), #[suggestion_verbose(
}; infer::implicit_static_lifetime_suggestion,
} code = " + '_",
} applicability = "maybe-incorrect"
)]
pub enum TraitSubdiag { Sugg {
Note { span: Span }, #[primary_span]
Sugg { span: Span }, span: Span,
} },
// FIXME(#100717) used in `Vec<TraitSubdiag>` so requires eager translation/list support
impl AddToDiagnostic for TraitSubdiag {
fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F)
where
F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
{
match self {
TraitSubdiag::Note { span } => {
diag.span_note(span, "this has an implicit `'static` lifetime requirement");
}
TraitSubdiag::Sugg { span } => {
diag.span_suggestion_verbose(
span,
"consider relaxing the implicit `'static` requirement",
" + '_".to_owned(),
rustc_errors::Applicability::MaybeIncorrect,
);
}
}
}
} }
#[derive(Diagnostic)] #[derive(Diagnostic)]
@ -512,7 +499,7 @@ pub struct MismatchedStaticLifetime<'a> {
#[subdiagnostic] #[subdiagnostic]
pub expl: Option<note_and_explain::RegionExplanation<'a>>, pub expl: Option<note_and_explain::RegionExplanation<'a>>,
#[subdiagnostic] #[subdiagnostic]
pub impl_note: ImplNote, pub does_not_outlive_static_from_impl: DoesNotOutliveStaticFromImpl,
#[subdiagnostic] #[subdiagnostic(eager)]
pub trait_subdiags: Vec<TraitSubdiag>, pub implicit_static_lifetimes: Vec<ImplicitStaticLifetimeSubdiag>,
} }

View file

@ -2,7 +2,9 @@
//! to hold. //! to hold.
use crate::errors::{note_and_explain, IntroducesStaticBecauseUnmetLifetimeReq}; use crate::errors::{note_and_explain, IntroducesStaticBecauseUnmetLifetimeReq};
use crate::errors::{ImplNote, MismatchedStaticLifetime, TraitSubdiag}; use crate::errors::{
DoesNotOutliveStaticFromImpl, ImplicitStaticLifetimeSubdiag, MismatchedStaticLifetime,
};
use crate::infer::error_reporting::nice_region_error::NiceRegionError; use crate::infer::error_reporting::nice_region_error::NiceRegionError;
use crate::infer::lexical_region_resolve::RegionResolutionError; use crate::infer::lexical_region_resolve::RegionResolutionError;
use crate::infer::{SubregionOrigin, TypeTrace}; use crate::infer::{SubregionOrigin, TypeTrace};
@ -56,7 +58,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
note_and_explain::SuffixKind::Continues, note_and_explain::SuffixKind::Continues,
); );
let mut impl_span = None; let mut impl_span = None;
let mut trait_subdiags = Vec::new(); let mut implicit_static_lifetimes = Vec::new();
if let Some(impl_node) = self.tcx().hir().get_if_local(*impl_def_id) { if let Some(impl_node) = self.tcx().hir().get_if_local(*impl_def_id) {
// If an impl is local, then maybe this isn't what they want. Try to // If an impl is local, then maybe this isn't what they want. Try to
// be as helpful as possible with implicit lifetimes. // be as helpful as possible with implicit lifetimes.
@ -90,10 +92,12 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
// Otherwise, point at all implicit static lifetimes // Otherwise, point at all implicit static lifetimes
for span in &traits { for span in &traits {
trait_subdiags.push(TraitSubdiag::Note { span: *span }); implicit_static_lifetimes
.push(ImplicitStaticLifetimeSubdiag::Note { span: *span });
// It would be nice to put this immediately under the above note, but they get // It would be nice to put this immediately under the above note, but they get
// pushed to the end. // pushed to the end.
trait_subdiags.push(TraitSubdiag::Sugg { span: span.shrink_to_hi() }); implicit_static_lifetimes
.push(ImplicitStaticLifetimeSubdiag::Sugg { span: span.shrink_to_hi() });
} }
} }
} else { } else {
@ -105,8 +109,10 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
cause_span: cause.span, cause_span: cause.span,
unmet_lifetime_reqs: multispan_subdiag, unmet_lifetime_reqs: multispan_subdiag,
expl, expl,
impl_note: ImplNote { impl_span }, does_not_outlive_static_from_impl: impl_span
trait_subdiags, .map(|span| DoesNotOutliveStaticFromImpl::Spanned { span })
.unwrap_or(DoesNotOutliveStaticFromImpl::Unspanned),
implicit_static_lifetimes,
}; };
let reported = self.tcx().sess.emit_err(err); let reported = self.tcx().sess.emit_err(err);
Some(reported) Some(reported)

View file

@ -309,6 +309,8 @@ impl<'parent, 'a> SubdiagnosticDeriveVariantBuilder<'parent, 'a> {
report_error_if_not_applied_to_span(attr, &info)?; report_error_if_not_applied_to_span(attr, &info)?;
let binding = info.binding.binding.clone(); let binding = info.binding.binding.clone();
// FIXME(#100717): support `Option<Span>` on `primary_span` like in the
// diagnostic derive
self.span_field.set_once(binding, span); self.span_field.set_once(binding, span);
} }