Skip to content

Commit 9da1ebb

Browse files
committed
Replace elided_named_lifetimes with mismatched_lifetime_syntaxes
1 parent ce3327c commit 9da1ebb

File tree

63 files changed

+375
-556
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+375
-556
lines changed

compiler/rustc_hir/src/def.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -850,12 +850,7 @@ pub enum LifetimeRes {
850850
/// late resolution. Those lifetimes will be inferred by typechecking.
851851
Infer,
852852
/// `'static` lifetime.
853-
Static {
854-
/// We do not want to emit `elided_named_lifetimes`
855-
/// when we are inside of a const item or a static,
856-
/// because it would get too annoying.
857-
suppress_elision_warning: bool,
858-
},
853+
Static,
859854
/// Resolution failure.
860855
Error,
861856
/// HACK: This is used to recover the NodeId of an elided lifetime.

compiler/rustc_lint/messages.ftl

-5
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,6 @@ lint_duplicate_macro_attribute =
251251
252252
lint_duplicate_matcher_binding = duplicate matcher binding
253253
254-
lint_elided_named_lifetime = elided lifetime has a name
255-
.label_elided = this elided lifetime gets resolved as `{$name}`
256-
.label_named = lifetime `{$name}` declared here
257-
.suggestion = consider specifying it explicitly
258-
259254
lint_enum_intrinsics_mem_discriminant =
260255
the return value of `mem::discriminant` is unspecified when called with a non-enum type
261256
.note = the argument to `discriminant` should be a reference to an enum, but it was passed a reference to a `{$ty_param}`, which is not an enum

compiler/rustc_lint/src/early/diagnostics.rs

+3-14
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ use rustc_errors::{
1010
use rustc_middle::middle::stability;
1111
use rustc_middle::ty::TyCtxt;
1212
use rustc_session::Session;
13-
use rustc_session::lint::{BuiltinLintDiag, ElidedLifetimeResolution};
14-
use rustc_span::{BytePos, kw};
13+
use rustc_session::lint::BuiltinLintDiag;
14+
use rustc_span::BytePos;
1515
use tracing::debug;
1616

17-
use crate::lints::{self, ElidedNamedLifetime};
17+
use crate::lints;
1818

1919
mod check_cfg;
2020

@@ -450,16 +450,5 @@ pub(super) fn decorate_lint(
450450
BuiltinLintDiag::UnexpectedBuiltinCfg { cfg, cfg_name, controlled_by } => {
451451
lints::UnexpectedBuiltinCfg { cfg, cfg_name, controlled_by }.decorate_lint(diag)
452452
}
453-
BuiltinLintDiag::ElidedNamedLifetimes { elided: (span, kind), resolution } => {
454-
match resolution {
455-
ElidedLifetimeResolution::Static => {
456-
ElidedNamedLifetime { span, kind, name: kw::StaticLifetime, declaration: None }
457-
}
458-
ElidedLifetimeResolution::Param(name, declaration) => {
459-
ElidedNamedLifetime { span, kind, name, declaration: Some(declaration) }
460-
}
461-
}
462-
.decorate_lint(diag)
463-
}
464453
}
465454
}

compiler/rustc_lint/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ fn register_builtins(store: &mut LintStore) {
356356
store.register_renamed("unused_tuple_struct_fields", "dead_code");
357357
store.register_renamed("static_mut_ref", "static_mut_refs");
358358
store.register_renamed("temporary_cstring_as_ptr", "dangling_pointers_from_temporaries");
359+
store.register_renamed("elided_named_lifetimes", "mismatched_lifetime_syntaxes");
359360

360361
// These were moved to tool lints, but rustc still sees them when compiling normally, before
361362
// tool lints are registered, so `check_tool_name_for_backwards_compat` doesn't work. Use

compiler/rustc_lint/src/lifetime_style.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ declare_lint! {
6262
/// In certain `unsafe` code, lifetime elision combined with
6363
/// inconsistent lifetime syntax may result in unsound code.
6464
pub MISMATCHED_LIFETIME_SYNTAXES,
65-
Allow,
65+
Warn,
6666
"detects when an elided lifetime uses different syntax between arguments and return values"
6767
}
6868

compiler/rustc_lint/src/lints.rs

+2-54
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ use rustc_errors::{
88
Applicability, Diag, DiagArgValue, DiagMessage, DiagStyledString, ElidedLifetimeInPathSubdiag,
99
EmissionGuarantee, LintDiagnostic, MultiSpan, Subdiagnostic, SuggestionStyle,
1010
};
11+
use rustc_hir as hir;
1112
use rustc_hir::def::Namespace;
1213
use rustc_hir::def_id::DefId;
1314
use rustc_hir::intravisit::VisitorExt;
14-
use rustc_hir::{self as hir, MissingLifetimeKind};
1515
use rustc_macros::{LintDiagnostic, Subdiagnostic};
1616
use rustc_middle::ty::inhabitedness::InhabitedPredicate;
1717
use rustc_middle::ty::{Clause, PolyExistentialTraitRef, Ty, TyCtxt};
1818
use rustc_session::Session;
1919
use rustc_session::lint::AmbiguityErrorDiag;
2020
use rustc_span::edition::Edition;
21-
use rustc_span::{Ident, MacroRulesNormalizedIdent, Span, Symbol, kw, sym};
21+
use rustc_span::{Ident, MacroRulesNormalizedIdent, Span, Symbol, sym};
2222

2323
use crate::builtin::{InitError, ShorthandAssocTyCollector, TypeAliasBounds};
2424
use crate::errors::{OverruledAttributeSub, RequestedLevel};
@@ -2689,58 +2689,6 @@ pub(crate) struct ElidedLifetimesInPaths {
26892689
pub subdiag: ElidedLifetimeInPathSubdiag,
26902690
}
26912691

2692-
pub(crate) struct ElidedNamedLifetime {
2693-
pub span: Span,
2694-
pub kind: MissingLifetimeKind,
2695-
pub name: Symbol,
2696-
pub declaration: Option<Span>,
2697-
}
2698-
2699-
impl<G: EmissionGuarantee> LintDiagnostic<'_, G> for ElidedNamedLifetime {
2700-
fn decorate_lint(self, diag: &mut rustc_errors::Diag<'_, G>) {
2701-
let Self { span, kind, name, declaration } = self;
2702-
diag.primary_message(fluent::lint_elided_named_lifetime);
2703-
diag.arg("name", name);
2704-
diag.span_label(span, fluent::lint_label_elided);
2705-
if let Some(declaration) = declaration {
2706-
diag.span_label(declaration, fluent::lint_label_named);
2707-
}
2708-
// FIXME(GrigorenkoPV): this `if` and `return` should be removed,
2709-
// but currently this lint's suggestions can conflict with those of `clippy::needless_lifetimes`:
2710-
// https://github.com/rust-lang/rust/pull/129840#issuecomment-2323349119
2711-
// HACK: `'static` suggestions will never sonflict, emit only those for now.
2712-
if name != kw::StaticLifetime {
2713-
return;
2714-
}
2715-
match kind {
2716-
MissingLifetimeKind::Underscore => diag.span_suggestion_verbose(
2717-
span,
2718-
fluent::lint_suggestion,
2719-
format!("{name}"),
2720-
Applicability::MachineApplicable,
2721-
),
2722-
MissingLifetimeKind::Ampersand => diag.span_suggestion_verbose(
2723-
span.shrink_to_hi(),
2724-
fluent::lint_suggestion,
2725-
format!("{name} "),
2726-
Applicability::MachineApplicable,
2727-
),
2728-
MissingLifetimeKind::Comma => diag.span_suggestion_verbose(
2729-
span.shrink_to_hi(),
2730-
fluent::lint_suggestion,
2731-
format!("{name}, "),
2732-
Applicability::MachineApplicable,
2733-
),
2734-
MissingLifetimeKind::Brackets => diag.span_suggestion_verbose(
2735-
span.shrink_to_hi(),
2736-
fluent::lint_suggestion,
2737-
format!("<{name}>"),
2738-
Applicability::MachineApplicable,
2739-
),
2740-
};
2741-
}
2742-
}
2743-
27442692
#[derive(LintDiagnostic)]
27452693
#[diag(lint_invalid_crate_type_value)]
27462694
pub(crate) struct UnknownCrateTypes {

compiler/rustc_lint_defs/src/builtin.rs

-33
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ declare_lint_pass! {
3939
DUPLICATE_MACRO_ATTRIBUTES,
4040
ELIDED_LIFETIMES_IN_ASSOCIATED_CONSTANT,
4141
ELIDED_LIFETIMES_IN_PATHS,
42-
ELIDED_NAMED_LIFETIMES,
4342
EXPLICIT_BUILTIN_CFGS_IN_FLAGS,
4443
EXPORTED_PRIVATE_DEPENDENCIES,
4544
FFI_UNWIND_CALLS,
@@ -1827,38 +1826,6 @@ declare_lint! {
18271826
"hidden lifetime parameters in types are deprecated"
18281827
}
18291828

1830-
declare_lint! {
1831-
/// The `elided_named_lifetimes` lint detects when an elided
1832-
/// lifetime ends up being a named lifetime, such as `'static`
1833-
/// or some lifetime parameter `'a`.
1834-
///
1835-
/// ### Example
1836-
///
1837-
/// ```rust,compile_fail
1838-
/// #![deny(elided_named_lifetimes)]
1839-
/// struct Foo;
1840-
/// impl Foo {
1841-
/// pub fn get_mut(&'static self, x: &mut u8) -> &mut u8 {
1842-
/// unsafe { &mut *(x as *mut _) }
1843-
/// }
1844-
/// }
1845-
/// ```
1846-
///
1847-
/// {{produces}}
1848-
///
1849-
/// ### Explanation
1850-
///
1851-
/// Lifetime elision is quite useful, because it frees you from having
1852-
/// to give each lifetime its own name, but sometimes it can produce
1853-
/// somewhat surprising resolutions. In safe code, it is mostly okay,
1854-
/// because the borrow checker prevents any unsoundness, so the worst
1855-
/// case scenario is you get a confusing error message in some other place.
1856-
/// But with `unsafe` code, such unexpected resolutions may lead to unsound code.
1857-
pub ELIDED_NAMED_LIFETIMES,
1858-
Warn,
1859-
"detects when an elided lifetime gets resolved to be `'static` or some named parameter"
1860-
}
1861-
18621829
declare_lint! {
18631830
/// The `bare_trait_objects` lint suggests using `dyn Trait` for trait
18641831
/// objects.

compiler/rustc_lint_defs/src/lib.rs

+1-11
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_data_structures::stable_hasher::{
99
use rustc_error_messages::{DiagMessage, MultiSpan};
1010
use rustc_hir::def::Namespace;
1111
use rustc_hir::def_id::DefPathHash;
12-
use rustc_hir::{HashStableContext, HirId, ItemLocalId, MissingLifetimeKind};
12+
use rustc_hir::{HashStableContext, HirId, ItemLocalId};
1313
use rustc_macros::{Decodable, Encodable, HashStable_Generic};
1414
pub use rustc_span::edition::Edition;
1515
use rustc_span::{Ident, MacroRulesNormalizedIdent, Span, Symbol, sym};
@@ -635,12 +635,6 @@ pub enum DeprecatedSinceKind {
635635
InVersion(String),
636636
}
637637

638-
#[derive(Debug)]
639-
pub enum ElidedLifetimeResolution {
640-
Static,
641-
Param(Symbol, Span),
642-
}
643-
644638
// This could be a closure, but then implementing derive trait
645639
// becomes hacky (and it gets allocated).
646640
#[derive(Debug)]
@@ -653,10 +647,6 @@ pub enum BuiltinLintDiag {
653647
},
654648
MacroExpandedMacroExportsAccessedByAbsolutePaths(Span),
655649
ElidedLifetimesInPaths(usize, Span, bool, Span),
656-
ElidedNamedLifetimes {
657-
elided: (Span, MissingLifetimeKind),
658-
resolution: ElidedLifetimeResolution,
659-
},
660650
UnknownCrateTypes {
661651
span: Span,
662652
candidate: Option<Symbol>,

compiler/rustc_resolve/src/late.rs

+6-55
Original file line numberDiff line numberDiff line change
@@ -1706,7 +1706,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
17061706
if ident.name == kw::StaticLifetime {
17071707
self.record_lifetime_res(
17081708
lifetime.id,
1709-
LifetimeRes::Static { suppress_elision_warning: false },
1709+
LifetimeRes::Static,
17101710
LifetimeElisionCandidate::Named,
17111711
);
17121712
return;
@@ -1854,8 +1854,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
18541854
if lifetimes_in_scope.is_empty() {
18551855
self.record_lifetime_res(
18561856
lifetime.id,
1857-
// We are inside a const item, so do not warn.
1858-
LifetimeRes::Static { suppress_elision_warning: true },
1857+
LifetimeRes::Static,
18591858
elision_candidate,
18601859
);
18611860
return;
@@ -2202,47 +2201,6 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
22022201
panic!("lifetime {id:?} resolved multiple times ({prev_res:?} before, {res:?} now)")
22032202
}
22042203

2205-
match candidate {
2206-
LifetimeElisionCandidate::Missing(missing @ MissingLifetime { .. }) => {
2207-
debug_assert_eq!(id, missing.id);
2208-
match res {
2209-
LifetimeRes::Static { suppress_elision_warning } => {
2210-
if !suppress_elision_warning {
2211-
self.r.lint_buffer.buffer_lint(
2212-
lint::builtin::ELIDED_NAMED_LIFETIMES,
2213-
missing.id_for_lint,
2214-
missing.span,
2215-
BuiltinLintDiag::ElidedNamedLifetimes {
2216-
elided: (missing.span, missing.kind),
2217-
resolution: lint::ElidedLifetimeResolution::Static,
2218-
},
2219-
);
2220-
}
2221-
}
2222-
LifetimeRes::Param { param, binder: _ } => {
2223-
let tcx = self.r.tcx();
2224-
self.r.lint_buffer.buffer_lint(
2225-
lint::builtin::ELIDED_NAMED_LIFETIMES,
2226-
missing.id_for_lint,
2227-
missing.span,
2228-
BuiltinLintDiag::ElidedNamedLifetimes {
2229-
elided: (missing.span, missing.kind),
2230-
resolution: lint::ElidedLifetimeResolution::Param(
2231-
tcx.item_name(param.into()),
2232-
tcx.source_span(param),
2233-
),
2234-
},
2235-
);
2236-
}
2237-
LifetimeRes::Fresh { .. }
2238-
| LifetimeRes::Infer
2239-
| LifetimeRes::Error
2240-
| LifetimeRes::ElidedAnchor { .. } => {}
2241-
}
2242-
}
2243-
LifetimeElisionCandidate::Ignore | LifetimeElisionCandidate::Named => {}
2244-
}
2245-
22462204
match res {
22472205
LifetimeRes::Param { .. } | LifetimeRes::Fresh { .. } | LifetimeRes::Static { .. } => {
22482206
if let Some(ref mut candidates) = self.lifetime_elision_candidates {
@@ -2760,14 +2718,9 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
27602718
..
27612719
}) => {
27622720
self.with_static_rib(def_kind, |this| {
2763-
this.with_lifetime_rib(
2764-
LifetimeRibKind::Elided(LifetimeRes::Static {
2765-
suppress_elision_warning: true,
2766-
}),
2767-
|this| {
2768-
this.visit_ty(ty);
2769-
},
2770-
);
2721+
this.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Static), |this| {
2722+
this.visit_ty(ty);
2723+
});
27712724
if let Some(expr) = expr {
27722725
// We already forbid generic params because of the above item rib,
27732726
// so it doesn't matter whether this is a trivial constant.
@@ -2804,9 +2757,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
28042757
this.visit_generics(generics);
28052758

28062759
this.with_lifetime_rib(
2807-
LifetimeRibKind::Elided(LifetimeRes::Static {
2808-
suppress_elision_warning: true,
2809-
}),
2760+
LifetimeRibKind::Elided(LifetimeRes::Static),
28102761
|this| this.visit_ty(ty),
28112762
);
28122763

compiler/rustc_resolve/src/late/diagnostics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3276,7 +3276,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
32763276
maybe_static = true;
32773277
in_scope_lifetimes = vec![(
32783278
Ident::with_dummy_span(kw::StaticLifetime),
3279-
(DUMMY_NODE_ID, LifetimeRes::Static { suppress_elision_warning: false }),
3279+
(DUMMY_NODE_ID, LifetimeRes::Static),
32803280
)];
32813281
}
32823282
} else if elided_len == 0 {
@@ -3288,7 +3288,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
32883288
maybe_static = true;
32893289
in_scope_lifetimes = vec![(
32903290
Ident::with_dummy_span(kw::StaticLifetime),
3291-
(DUMMY_NODE_ID, LifetimeRes::Static { suppress_elision_warning: false }),
3291+
(DUMMY_NODE_ID, LifetimeRes::Static),
32923292
)];
32933293
}
32943294
} else if num_params == 1 {

src/tools/clippy/tests/ui/needless_lifetimes.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
clippy::unnecessary_wraps,
1111
dyn_drop,
1212
clippy::get_first,
13-
elided_named_lifetimes
13+
mismatched_lifetime_syntaxes,
1414
)]
1515

1616
extern crate proc_macros;

src/tools/clippy/tests/ui/needless_lifetimes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
clippy::unnecessary_wraps,
1111
dyn_drop,
1212
clippy::get_first,
13-
elided_named_lifetimes
13+
mismatched_lifetime_syntaxes,
1414
)]
1515

1616
extern crate proc_macros;

src/tools/clippy/tests/ui/ptr_arg.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ mod issue_9218 {
312312

313313
// Inferred to be `&'a str`, afaik.
314314
fn cow_good_ret_ty<'a>(input: &'a Cow<'a, str>) -> &str {
315-
//~^ ERROR: elided lifetime has a name
315+
//~^ ERROR: lifetime flowing from input to output with different syntax
316316
todo!()
317317
}
318318
}

0 commit comments

Comments
 (0)