Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 8360a40

Browse files
committed
Migrate named_anon_conflict.rs
1 parent 9b889e5 commit 8360a40

File tree

3 files changed

+47
-26
lines changed

3 files changed

+47
-26
lines changed

compiler/rustc_error_messages/locales/en-US/infer.ftl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,14 @@ infer_msl_unmet_req = because this has an unmet lifetime requirement
172172
infer_msl_trait_note = this has an implicit `'static` lifetime requirement
173173
infer_msl_trait_sugg = consider relaxing the implicit `'static` requirement
174174
infer_suggest_add_let_for_letchains = consider adding `let`
175+
176+
infer_explicit_lifetime_required = explicit lifetime required in {$ident_kind ->
177+
[ident] the type of `{$simple_ident}`
178+
*[param_type] parameter type
179+
}
180+
.label = lifetime `{$named}` required
181+
182+
infer_explicit_lifetime_required_sugg = add explicit lifetime `{$named}` to {$ident_kind ->
183+
[ident] the type of `{$simple_ident}`
184+
*[param_type] type
185+
}

compiler/rustc_infer/src/errors/mod.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,8 @@ impl AddToDiagnostic for LifetimeMismatchLabels {
357357
pub struct AddLifetimeParamsSuggestion<'a> {
358358
pub tcx: TyCtxt<'a>,
359359
pub sub: Region<'a>,
360-
pub ty_sup: &'a Ty<'a>,
361-
pub ty_sub: &'a Ty<'a>,
360+
pub ty_sup: &'a hir::Ty<'a>,
361+
pub ty_sub: &'a hir::Ty<'a>,
362362
pub add_note: bool,
363363
}
364364

@@ -520,3 +520,23 @@ pub struct MismatchedStaticLifetime<'a> {
520520
#[subdiagnostic]
521521
pub implicit_static_lifetimes: Vec<ImplicitStaticLifetimeSubdiag>,
522522
}
523+
524+
#[derive(SessionDiagnostic)]
525+
#[diag(infer::explicit_lifetime_required, code = "E0621")]
526+
pub struct ExplicitLifetimeRequired<'a> {
527+
#[primary_span]
528+
#[label]
529+
pub span: Span,
530+
pub ident_kind: &'static str,
531+
pub simple_ident: String,
532+
pub named: String,
533+
534+
#[suggestion(
535+
infer::explicit_lifetime_required_sugg,
536+
code = "{new_ty}",
537+
applicability = "unspecified"
538+
)]
539+
pub new_ty_span: Span,
540+
#[skip_arg]
541+
pub new_ty: Ty<'a>,
542+
}

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/named_anon_conflict.rs

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
//! Error Reporting for Anonymous Region Lifetime Errors
22
//! where one region is named and the other is anonymous.
3-
use crate::infer::error_reporting::nice_region_error::find_anon_type::find_anon_type;
43
use crate::infer::error_reporting::nice_region_error::NiceRegionError;
5-
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, ErrorGuaranteed};
4+
use crate::{
5+
errors::ExplicitLifetimeRequired,
6+
infer::error_reporting::nice_region_error::find_anon_type::find_anon_type,
7+
};
8+
use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed};
69
use rustc_middle::ty;
710
use rustc_span::symbol::kw;
811

@@ -87,30 +90,17 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
8790
return None;
8891
}
8992

90-
let (error_var, span_label_var) = match param.pat.simple_ident() {
91-
Some(simple_ident) => (
92-
format!("the type of `{}`", simple_ident),
93-
format!("the type of `{}`", simple_ident),
94-
),
95-
None => ("parameter type".to_owned(), "type".to_owned()),
93+
let simple_ident = param.pat.simple_ident();
94+
let (ident_kind, simple_ident) = match simple_ident {
95+
Some(ident) => ("ident", ident.to_string()),
96+
None => ("param_type", String::new()),
9697
};
9798

98-
let mut diag = struct_span_err!(
99-
self.tcx().sess,
100-
span,
101-
E0621,
102-
"explicit lifetime required in {}",
103-
error_var
104-
);
105-
106-
diag.span_label(span, format!("lifetime `{}` required", named));
107-
diag.span_suggestion(
108-
new_ty_span,
109-
&format!("add explicit lifetime `{}` to {}", named, span_label_var),
110-
new_ty,
111-
Applicability::Unspecified,
112-
);
99+
let named = named.to_string();
113100

114-
Some(diag)
101+
let err =
102+
ExplicitLifetimeRequired { span, ident_kind, simple_ident, named, new_ty_span, new_ty };
103+
let err = self.tcx().sess.parse_sess.create_err(err);
104+
Some(err)
115105
}
116106
}

0 commit comments

Comments
 (0)