From 547db4a4b7c9bfea52b519d0095510126a0a1cb0 Mon Sep 17 00:00:00 2001 From: Pavel Grigorenko Date: Sun, 1 Sep 2024 04:08:55 +0300 Subject: [PATCH] elided_named_lifetimes: manually implement `LintDiagnostic` --- compiler/rustc_lint/messages.ftl | 3 +- .../rustc_lint/src/context/diagnostics.rs | 32 +++---------- compiler/rustc_lint/src/lints.rs | 47 ++++++++++++++++--- .../async-await/issues/issue-63388-1.stderr | 2 +- .../type-dependent/issue-71348.full.stderr | 2 +- .../type-dependent/issue-71348.min.stderr | 2 +- .../consts/min_const_fn/min_const_fn.stderr | 4 +- .../rpit-assoc-pair-with-lifetime.stderr | 2 +- ...urn-type-requires-explicit-lifetime.stderr | 2 +- ...-existing-name-if-else-using-impl-3.stderr | 2 +- .../example-from-issue48686.stderr | 2 +- .../missing-lifetime-kind.stderr | 6 +-- .../not-tied-to-crate.stderr | 4 +- .../lint/elided-named-lifetimes/static.stderr | 6 +-- .../object-lifetime-default-elision.stderr | 2 +- .../ignore-non-reference-lifetimes.stderr | 4 +- tests/ui/self/self_lifetime-async.stderr | 4 +- tests/ui/self/self_lifetime.stderr | 4 +- .../impl-trait-missing-lifetime-gated.stderr | 2 +- .../missing_lifetime_bound.stderr | 2 +- 20 files changed, 75 insertions(+), 59 deletions(-) diff --git a/compiler/rustc_lint/messages.ftl b/compiler/rustc_lint/messages.ftl index fe211355ac3..5a368f4ed9a 100644 --- a/compiler/rustc_lint/messages.ftl +++ b/compiler/rustc_lint/messages.ftl @@ -255,8 +255,7 @@ lint_duplicate_matcher_binding = duplicate matcher binding lint_elided_named_lifetime = elided lifetime has a name .label_elided = this elided lifetime gets resolved as `{$name}` .label_named = lifetime `{$name}` declared here - -lint_elided_named_lifetime_suggestion = consider specifying it explicitly + .suggestion = consider specifying it explicitly lint_enum_intrinsics_mem_discriminant = the return value of `mem::discriminant` is unspecified when called with a non-enum type diff --git a/compiler/rustc_lint/src/context/diagnostics.rs b/compiler/rustc_lint/src/context/diagnostics.rs index a1a371a032e..de34b9bebe9 100644 --- a/compiler/rustc_lint/src/context/diagnostics.rs +++ b/compiler/rustc_lint/src/context/diagnostics.rs @@ -7,7 +7,6 @@ use rustc_errors::{ elided_lifetime_in_path_suggestion, Applicability, Diag, DiagArgValue, LintDiagnostic, }; -use rustc_hir::MissingLifetimeKind; use rustc_middle::middle::stability; use rustc_session::lint::{BuiltinLintDiag, ElidedLifetimeResolution}; use rustc_session::Session; @@ -15,7 +14,6 @@ use rustc_span::BytePos; use tracing::debug; -use crate::fluent_generated; use crate::lints::{self, ElidedNamedLifetime}; mod check_cfg; @@ -445,31 +443,15 @@ pub(super) fn decorate_lint(sess: &Session, diagnostic: BuiltinLintDiag, diag: & lints::UnexpectedBuiltinCfg { cfg, cfg_name, controlled_by }.decorate_lint(diag) } BuiltinLintDiag::ElidedNamedLifetimes { elided: (span, kind), resolution } => { - let (name, named_declaration) = match resolution { - ElidedLifetimeResolution::Static => (kw::StaticLifetime, None), - ElidedLifetimeResolution::Param(name, declaration) => (name, Some(declaration)), - }; - ElidedNamedLifetime { span, name, named_declaration }.decorate_lint(diag); - - let (applicability, suggestion) = match kind { - MissingLifetimeKind::Underscore => { - (Applicability::MachineApplicable, format!("{name}")) + match resolution { + ElidedLifetimeResolution::Static => { + ElidedNamedLifetime { span, kind, name: kw::StaticLifetime, declaration: None } } - MissingLifetimeKind::Ampersand => { - (Applicability::MachineApplicable, format!("&{name} ")) + ElidedLifetimeResolution::Param(name, declaration) => { + ElidedNamedLifetime { span, kind, name, declaration: Some(declaration) } } - MissingLifetimeKind::Comma => (Applicability::Unspecified, format!("<{name}, ")), - MissingLifetimeKind::Brackets => ( - Applicability::Unspecified, - format!("{}<{name}>", sess.source_map().span_to_snippet(span).unwrap()), - ), - }; - diag.span_suggestion_verbose( - span, - fluent_generated::lint_elided_named_lifetime_suggestion, - suggestion, - applicability, - ); + } + .decorate_lint(diag) } } } diff --git a/compiler/rustc_lint/src/lints.rs b/compiler/rustc_lint/src/lints.rs index 426d671f8e0..693f1b22587 100644 --- a/compiler/rustc_lint/src/lints.rs +++ b/compiler/rustc_lint/src/lints.rs @@ -9,7 +9,7 @@ }; use rustc_hir::def::Namespace; use rustc_hir::def_id::DefId; -use rustc_hir::{self as hir}; +use rustc_hir::{self as hir, MissingLifetimeKind}; use rustc_macros::{LintDiagnostic, Subdiagnostic}; use rustc_middle::ty::inhabitedness::InhabitedPredicate; use rustc_middle::ty::{Clause, PolyExistentialTraitRef, Ty, TyCtxt}; @@ -2623,14 +2623,49 @@ pub(crate) struct ElidedLifetimesInPaths { pub subdiag: ElidedLifetimeInPathSubdiag, } -#[derive(LintDiagnostic)] -#[diag(lint_elided_named_lifetime)] pub(crate) struct ElidedNamedLifetime { - #[label(lint_label_elided)] pub span: Span, + pub kind: MissingLifetimeKind, pub name: Symbol, - #[label(lint_label_named)] - pub named_declaration: Option, + pub declaration: Option, +} + +impl LintDiagnostic<'_, G> for ElidedNamedLifetime { + fn decorate_lint(self, diag: &mut rustc_errors::Diag<'_, G>) { + let Self { span, kind, name, declaration } = self; + diag.primary_message(fluent::lint_elided_named_lifetime); + diag.arg("name", name); + diag.span_label(span, fluent::lint_label_elided); + if let Some(declaration) = declaration { + diag.span_label(declaration, fluent::lint_label_named); + } + match kind { + MissingLifetimeKind::Underscore => diag.span_suggestion_verbose( + span, + fluent::lint_suggestion, + format!("{name}"), + Applicability::MachineApplicable, + ), + MissingLifetimeKind::Ampersand => diag.span_suggestion_verbose( + span.shrink_to_hi(), + fluent::lint_suggestion, + format!("{name} "), + Applicability::MachineApplicable, + ), + MissingLifetimeKind::Comma => diag.span_suggestion_verbose( + span.shrink_to_hi(), + fluent::lint_suggestion, + format!("{name}, "), + Applicability::MachineApplicable, + ), + MissingLifetimeKind::Brackets => diag.span_suggestion_verbose( + span.shrink_to_hi(), + fluent::lint_suggestion, + format!("<{name}>"), + Applicability::MachineApplicable, + ), + }; + } } #[derive(LintDiagnostic)] diff --git a/tests/ui/async-await/issues/issue-63388-1.stderr b/tests/ui/async-await/issues/issue-63388-1.stderr index c8b55977faf..713e4e4dcf5 100644 --- a/tests/ui/async-await/issues/issue-63388-1.stderr +++ b/tests/ui/async-await/issues/issue-63388-1.stderr @@ -11,7 +11,7 @@ LL | ) -> &dyn Foo help: consider specifying it explicitly | LL | ) -> &'a dyn Foo - | ~~~ + | ++ error[E0621]: explicit lifetime required in the type of `foo` --> $DIR/issue-63388-1.rs:13:5 diff --git a/tests/ui/const-generics/type-dependent/issue-71348.full.stderr b/tests/ui/const-generics/type-dependent/issue-71348.full.stderr index 8d1c1ea23a8..394259ce55d 100644 --- a/tests/ui/const-generics/type-dependent/issue-71348.full.stderr +++ b/tests/ui/const-generics/type-dependent/issue-71348.full.stderr @@ -8,7 +8,7 @@ LL | fn ask<'a, const N: &'static str>(&'a self) -> &'a >::Ta help: consider specifying it explicitly | LL | fn ask<'a, const N: &'static str>(&'a self) -> &'a >::Target - | ~~~~ + | +++ warning: 1 warning emitted diff --git a/tests/ui/const-generics/type-dependent/issue-71348.min.stderr b/tests/ui/const-generics/type-dependent/issue-71348.min.stderr index 57da3ce21ce..0f0d75dbac1 100644 --- a/tests/ui/const-generics/type-dependent/issue-71348.min.stderr +++ b/tests/ui/const-generics/type-dependent/issue-71348.min.stderr @@ -8,7 +8,7 @@ LL | fn ask<'a, const N: &'static str>(&'a self) -> &'a >::Ta help: consider specifying it explicitly | LL | fn ask<'a, const N: &'static str>(&'a self) -> &'a >::Target - | ~~~~ + | +++ error: `&'static str` is forbidden as the type of a const generic parameter --> $DIR/issue-71348.rs:10:24 diff --git a/tests/ui/consts/min_const_fn/min_const_fn.stderr b/tests/ui/consts/min_const_fn/min_const_fn.stderr index b7db403ad16..1e88a908af0 100644 --- a/tests/ui/consts/min_const_fn/min_const_fn.stderr +++ b/tests/ui/consts/min_const_fn/min_const_fn.stderr @@ -11,7 +11,7 @@ LL | const fn get_lt(&'a self) -> &T { &self.0 } help: consider specifying it explicitly | LL | const fn get_lt(&'a self) -> &'a T { &self.0 } - | ~~~ + | ++ warning: elided lifetime has a name --> $DIR/min_const_fn.rs:48:42 @@ -25,7 +25,7 @@ LL | const fn get_mut_lt(&'a mut self) -> &mut T { &mut self.0 } help: consider specifying it explicitly | LL | const fn get_mut_lt(&'a mut self) -> &'a mut T { &mut self.0 } - | ~~~ + | ++ error[E0493]: destructor of `Foo` cannot be evaluated at compile-time --> $DIR/min_const_fn.rs:37:25 diff --git a/tests/ui/impl-trait/rpit-assoc-pair-with-lifetime.stderr b/tests/ui/impl-trait/rpit-assoc-pair-with-lifetime.stderr index ec0f0708b15..6eea25b4fc8 100644 --- a/tests/ui/impl-trait/rpit-assoc-pair-with-lifetime.stderr +++ b/tests/ui/impl-trait/rpit-assoc-pair-with-lifetime.stderr @@ -8,7 +8,7 @@ LL | pub fn iter<'a>(v: Vec<(u32, &'a u32)>) -> impl DoubleEndedIterator(v: Vec<(u32, &'a u32)>) -> impl DoubleEndedIterator { - | ~~~ + | ++ warning: 1 warning emitted diff --git a/tests/ui/lifetimes/lifetime-elision-return-type-requires-explicit-lifetime.stderr b/tests/ui/lifetimes/lifetime-elision-return-type-requires-explicit-lifetime.stderr index a7512158661..1339c644f53 100644 --- a/tests/ui/lifetimes/lifetime-elision-return-type-requires-explicit-lifetime.stderr +++ b/tests/ui/lifetimes/lifetime-elision-return-type-requires-explicit-lifetime.stderr @@ -117,7 +117,7 @@ LL | fn m<'a>(_: &'a Foo<'a>) -> &str { "" } help: consider specifying it explicitly | LL | fn m<'a>(_: &'a Foo<'a>) -> &'a str { "" } - | ~~~ + | ++ error: aborting due to 7 previous errors; 1 warning emitted diff --git a/tests/ui/lifetimes/lifetime-errors/ex1-return-one-existing-name-if-else-using-impl-3.stderr b/tests/ui/lifetimes/lifetime-errors/ex1-return-one-existing-name-if-else-using-impl-3.stderr index 0943e0cc0d9..144f9454513 100644 --- a/tests/ui/lifetimes/lifetime-errors/ex1-return-one-existing-name-if-else-using-impl-3.stderr +++ b/tests/ui/lifetimes/lifetime-errors/ex1-return-one-existing-name-if-else-using-impl-3.stderr @@ -10,7 +10,7 @@ LL | fn foo<'a>(&'a self, x: &i32) -> &i32 { help: consider specifying it explicitly | LL | fn foo<'a>(&'a self, x: &i32) -> &'a i32 { - | ~~~ + | ++ error[E0621]: explicit lifetime required in the type of `x` --> $DIR/ex1-return-one-existing-name-if-else-using-impl-3.rs:9:36 diff --git a/tests/ui/lint/elided-named-lifetimes/example-from-issue48686.stderr b/tests/ui/lint/elided-named-lifetimes/example-from-issue48686.stderr index 5af5123c032..2d8c6e99643 100644 --- a/tests/ui/lint/elided-named-lifetimes/example-from-issue48686.stderr +++ b/tests/ui/lint/elided-named-lifetimes/example-from-issue48686.stderr @@ -12,7 +12,7 @@ LL | #![deny(elided_named_lifetimes)] help: consider specifying it explicitly | LL | pub fn get_mut(&'static self, x: &mut u8) -> &'static mut u8 { - | ~~~~~~~~ + | +++++++ error: aborting due to 1 previous error diff --git a/tests/ui/lint/elided-named-lifetimes/missing-lifetime-kind.stderr b/tests/ui/lint/elided-named-lifetimes/missing-lifetime-kind.stderr index 6abaad7f5d2..834292d103d 100644 --- a/tests/ui/lint/elided-named-lifetimes/missing-lifetime-kind.stderr +++ b/tests/ui/lint/elided-named-lifetimes/missing-lifetime-kind.stderr @@ -14,7 +14,7 @@ LL | #![deny(elided_named_lifetimes)] help: consider specifying it explicitly | LL | fn ampersand<'a>(x: &'a u8) -> &'a u8 { - | ~~~ + | ++ error: elided lifetime has a name --> $DIR/missing-lifetime-kind.rs:10:31 @@ -27,7 +27,7 @@ LL | fn brackets<'a>(x: &'a u8) -> Brackets { help: consider specifying it explicitly | LL | fn brackets<'a>(x: &'a u8) -> Brackets<'a> { - | ~~~~~~~~~~~~ + | ++++ error: elided lifetime has a name --> $DIR/missing-lifetime-kind.rs:17:33 @@ -40,7 +40,7 @@ LL | fn comma<'a>(x: &'a u8) -> Comma { help: consider specifying it explicitly | LL | fn comma<'a>(x: &'a u8) -> Comma<'a, u8> { - | ~~~~ + | +++ error: elided lifetime has a name --> $DIR/missing-lifetime-kind.rs:22:34 diff --git a/tests/ui/lint/elided-named-lifetimes/not-tied-to-crate.stderr b/tests/ui/lint/elided-named-lifetimes/not-tied-to-crate.stderr index cbc1b862caa..3c01375d501 100644 --- a/tests/ui/lint/elided-named-lifetimes/not-tied-to-crate.stderr +++ b/tests/ui/lint/elided-named-lifetimes/not-tied-to-crate.stderr @@ -12,7 +12,7 @@ LL | #[warn(elided_named_lifetimes)] help: consider specifying it explicitly | LL | fn bar(x: &'static u8) -> &'static u8 { - | ~~~~~~~~ + | +++++++ error: elided lifetime has a name --> $DIR/not-tied-to-crate.rs:11:31 @@ -28,7 +28,7 @@ LL | #[deny(elided_named_lifetimes)] help: consider specifying it explicitly | LL | fn baz(x: &'static u8) -> &'static u8 { - | ~~~~~~~~ + | +++++++ error: aborting due to 1 previous error; 1 warning emitted diff --git a/tests/ui/lint/elided-named-lifetimes/static.stderr b/tests/ui/lint/elided-named-lifetimes/static.stderr index 6c3944e5ea5..fa2a2d3460f 100644 --- a/tests/ui/lint/elided-named-lifetimes/static.stderr +++ b/tests/ui/lint/elided-named-lifetimes/static.stderr @@ -12,7 +12,7 @@ LL | #![deny(elided_named_lifetimes)] help: consider specifying it explicitly | LL | fn ampersand(x: &'static u8) -> &'static u8 { - | ~~~~~~~~ + | +++++++ error: elided lifetime has a name --> $DIR/static.rs:23:32 @@ -23,7 +23,7 @@ LL | fn brackets(x: &'static u8) -> Brackets { help: consider specifying it explicitly | LL | fn brackets(x: &'static u8) -> Brackets<'static> { - | ~~~~~~~~~~~~~~~~~ + | +++++++++ error: elided lifetime has a name --> $DIR/static.rs:30:34 @@ -34,7 +34,7 @@ LL | fn comma(x: &'static u8) -> Comma { help: consider specifying it explicitly | LL | fn comma(x: &'static u8) -> Comma<'static, u8> { - | ~~~~~~~~~ + | ++++++++ error: elided lifetime has a name --> $DIR/static.rs:35:35 diff --git a/tests/ui/object-lifetime/object-lifetime-default-elision.stderr b/tests/ui/object-lifetime/object-lifetime-default-elision.stderr index 7d239a54eda..f8ed92111a9 100644 --- a/tests/ui/object-lifetime/object-lifetime-default-elision.stderr +++ b/tests/ui/object-lifetime/object-lifetime-default-elision.stderr @@ -10,7 +10,7 @@ LL | fn load2<'a>(ss: &'a dyn SomeTrait) -> &dyn SomeTrait { help: consider specifying it explicitly | LL | fn load2<'a>(ss: &'a dyn SomeTrait) -> &'a dyn SomeTrait { - | ~~~ + | ++ error: lifetime may not live long enough --> $DIR/object-lifetime-default-elision.rs:73:5 diff --git a/tests/ui/self/elision/ignore-non-reference-lifetimes.stderr b/tests/ui/self/elision/ignore-non-reference-lifetimes.stderr index 460d2ce2cc9..13cc3a431a5 100644 --- a/tests/ui/self/elision/ignore-non-reference-lifetimes.stderr +++ b/tests/ui/self/elision/ignore-non-reference-lifetimes.stderr @@ -8,7 +8,7 @@ LL | fn a<'a>(self: Self, a: &'a str) -> &str { help: consider specifying it explicitly | LL | fn a<'a>(self: Self, a: &'a str) -> &'a str { - | ~~~ + | ++ warning: elided lifetime has a name --> $DIR/ignore-non-reference-lifetimes.rs:10:44 @@ -19,7 +19,7 @@ LL | fn b<'a>(self: Foo<'b>, a: &'a str) -> &str { help: consider specifying it explicitly | LL | fn b<'a>(self: Foo<'b>, a: &'a str) -> &'a str { - | ~~~ + | ++ warning: 2 warnings emitted diff --git a/tests/ui/self/self_lifetime-async.stderr b/tests/ui/self/self_lifetime-async.stderr index fd167cf392f..2892d790ac7 100644 --- a/tests/ui/self/self_lifetime-async.stderr +++ b/tests/ui/self/self_lifetime-async.stderr @@ -10,7 +10,7 @@ LL | async fn foo<'b>(self: &'b Foo<'a>) -> &() { self.0 } help: consider specifying it explicitly | LL | async fn foo<'b>(self: &'b Foo<'a>) -> &'b () { self.0 } - | ~~~ + | ++ warning: elided lifetime has a name --> $DIR/self_lifetime-async.rs:12:52 @@ -21,7 +21,7 @@ LL | async fn bar<'a>(self: &Alias, arg: &'a ()) -> &() { arg } help: consider specifying it explicitly | LL | async fn bar<'a>(self: &Alias, arg: &'a ()) -> &'a () { arg } - | ~~~ + | ++ warning: 2 warnings emitted diff --git a/tests/ui/self/self_lifetime.stderr b/tests/ui/self/self_lifetime.stderr index 52189084f04..ceb0ce345a3 100644 --- a/tests/ui/self/self_lifetime.stderr +++ b/tests/ui/self/self_lifetime.stderr @@ -10,7 +10,7 @@ LL | fn foo<'b>(self: &'b Foo<'a>) -> &() { self.0 } help: consider specifying it explicitly | LL | fn foo<'b>(self: &'b Foo<'a>) -> &'b () { self.0 } - | ~~~ + | ++ warning: elided lifetime has a name --> $DIR/self_lifetime.rs:13:46 @@ -21,7 +21,7 @@ LL | fn bar<'a>(self: &Alias, arg: &'a ()) -> &() { arg } help: consider specifying it explicitly | LL | fn bar<'a>(self: &Alias, arg: &'a ()) -> &'a () { arg } - | ~~~ + | ++ warning: 2 warnings emitted diff --git a/tests/ui/suggestions/impl-trait-missing-lifetime-gated.stderr b/tests/ui/suggestions/impl-trait-missing-lifetime-gated.stderr index 92f774767c8..3744dd9f996 100644 --- a/tests/ui/suggestions/impl-trait-missing-lifetime-gated.stderr +++ b/tests/ui/suggestions/impl-trait-missing-lifetime-gated.stderr @@ -134,7 +134,7 @@ LL | fn resolved_anonymous<'a, T: 'a>(f: impl Fn(&'a str) -> &T) { help: consider specifying it explicitly | LL | fn resolved_anonymous<'a, T: 'a>(f: impl Fn(&'a str) -> &'a T) { - | ~~~ + | ++ error[E0658]: anonymous lifetimes in `impl Trait` are unstable --> $DIR/impl-trait-missing-lifetime-gated.rs:6:35 diff --git a/tests/ui/type-alias-impl-trait/missing_lifetime_bound.stderr b/tests/ui/type-alias-impl-trait/missing_lifetime_bound.stderr index 73a362d28e7..376e58ef9e0 100644 --- a/tests/ui/type-alias-impl-trait/missing_lifetime_bound.stderr +++ b/tests/ui/type-alias-impl-trait/missing_lifetime_bound.stderr @@ -10,7 +10,7 @@ LL | fn defining<'a, T>(x: &'a i32) -> Opaque { x } help: consider specifying it explicitly | LL | fn defining<'a, T>(x: &'a i32) -> Opaque<'a, T> { x } - | ~~~~ + | +++ error[E0700]: hidden type for `Opaque2` captures lifetime that does not appear in bounds --> $DIR/missing_lifetime_bound.rs:5:47