Migrate named_anon_conflict.rs

This commit is contained in:
Nikita Tomashevich 2022-09-08 22:28:00 +03:00
parent 9b889e53e7
commit 8360a40a8a
No known key found for this signature in database
GPG Key ID: B29791D4D878E345
3 changed files with 47 additions and 26 deletions

View File

@ -172,3 +172,14 @@ 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_sugg = consider relaxing the implicit `'static` requirement
infer_suggest_add_let_for_letchains = consider adding `let`
infer_explicit_lifetime_required = explicit lifetime required in {$ident_kind ->
[ident] the type of `{$simple_ident}`
*[param_type] parameter type
}
.label = lifetime `{$named}` required
infer_explicit_lifetime_required_sugg = add explicit lifetime `{$named}` to {$ident_kind ->
[ident] the type of `{$simple_ident}`
*[param_type] type
}

View File

@ -357,8 +357,8 @@ impl AddToDiagnostic for LifetimeMismatchLabels {
pub struct AddLifetimeParamsSuggestion<'a> {
pub tcx: TyCtxt<'a>,
pub sub: Region<'a>,
pub ty_sup: &'a Ty<'a>,
pub ty_sub: &'a Ty<'a>,
pub ty_sup: &'a hir::Ty<'a>,
pub ty_sub: &'a hir::Ty<'a>,
pub add_note: bool,
}
@ -520,3 +520,23 @@ pub struct MismatchedStaticLifetime<'a> {
#[subdiagnostic]
pub implicit_static_lifetimes: Vec<ImplicitStaticLifetimeSubdiag>,
}
#[derive(SessionDiagnostic)]
#[diag(infer::explicit_lifetime_required, code = "E0621")]
pub struct ExplicitLifetimeRequired<'a> {
#[primary_span]
#[label]
pub span: Span,
pub ident_kind: &'static str,
pub simple_ident: String,
pub named: String,
#[suggestion(
infer::explicit_lifetime_required_sugg,
code = "{new_ty}",
applicability = "unspecified"
)]
pub new_ty_span: Span,
#[skip_arg]
pub new_ty: Ty<'a>,
}

View File

@ -1,8 +1,11 @@
//! Error Reporting for Anonymous Region Lifetime Errors
//! where one region is named and the other is anonymous.
use crate::infer::error_reporting::nice_region_error::find_anon_type::find_anon_type;
use crate::infer::error_reporting::nice_region_error::NiceRegionError;
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, ErrorGuaranteed};
use crate::{
errors::ExplicitLifetimeRequired,
infer::error_reporting::nice_region_error::find_anon_type::find_anon_type,
};
use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed};
use rustc_middle::ty;
use rustc_span::symbol::kw;
@ -87,30 +90,17 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
return None;
}
let (error_var, span_label_var) = match param.pat.simple_ident() {
Some(simple_ident) => (
format!("the type of `{}`", simple_ident),
format!("the type of `{}`", simple_ident),
),
None => ("parameter type".to_owned(), "type".to_owned()),
let simple_ident = param.pat.simple_ident();
let (ident_kind, simple_ident) = match simple_ident {
Some(ident) => ("ident", ident.to_string()),
None => ("param_type", String::new()),
};
let mut diag = struct_span_err!(
self.tcx().sess,
span,
E0621,
"explicit lifetime required in {}",
error_var
);
let named = named.to_string();
diag.span_label(span, format!("lifetime `{}` required", named));
diag.span_suggestion(
new_ty_span,
&format!("add explicit lifetime `{}` to {}", named, span_label_var),
new_ty,
Applicability::Unspecified,
);
Some(diag)
let err =
ExplicitLifetimeRequired { span, ident_kind, simple_ident, named, new_ty_span, new_ty };
let err = self.tcx().sess.parse_sess.create_err(err);
Some(err)
}
}