Skip to content

Commit 702e59d

Browse files
committed
Replace elided_named_lifetimes with mismatched_lifetime_syntaxes
1 parent 18a161d commit 702e59d

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
@@ -845,12 +845,7 @@ pub enum LifetimeRes {
845845
/// late resolution. Those lifetimes will be inferred by typechecking.
846846
Infer,
847847
/// `'static` lifetime.
848-
Static {
849-
/// We do not want to emit `elided_named_lifetimes`
850-
/// when we are inside of a const item or a static,
851-
/// because it would get too annoying.
852-
suppress_elision_warning: bool,
853-
},
848+
Static,
854849
/// Resolution failure.
855850
Error,
856851
/// 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
@@ -354,6 +354,7 @@ fn register_builtins(store: &mut LintStore) {
354354
store.register_renamed("unused_tuple_struct_fields", "dead_code");
355355
store.register_renamed("static_mut_ref", "static_mut_refs");
356356
store.register_renamed("temporary_cstring_as_ptr", "dangling_pointers_from_temporaries");
357+
store.register_renamed("elided_named_lifetimes", "mismatched_lifetime_syntaxes");
357358

358359
// These were moved to tool lints, but rustc still sees them when compiling normally, before
359360
// 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};
@@ -2653,58 +2653,6 @@ pub(crate) struct ElidedLifetimesInPaths {
26532653
pub subdiag: ElidedLifetimeInPathSubdiag,
26542654
}
26552655

2656-
pub(crate) struct ElidedNamedLifetime {
2657-
pub span: Span,
2658-
pub kind: MissingLifetimeKind,
2659-
pub name: Symbol,
2660-
pub declaration: Option<Span>,
2661-
}
2662-
2663-
impl<G: EmissionGuarantee> LintDiagnostic<'_, G> for ElidedNamedLifetime {
2664-
fn decorate_lint(self, diag: &mut rustc_errors::Diag<'_, G>) {
2665-
let Self { span, kind, name, declaration } = self;
2666-
diag.primary_message(fluent::lint_elided_named_lifetime);
2667-
diag.arg("name", name);
2668-
diag.span_label(span, fluent::lint_label_elided);
2669-
if let Some(declaration) = declaration {
2670-
diag.span_label(declaration, fluent::lint_label_named);
2671-
}
2672-
// FIXME(GrigorenkoPV): this `if` and `return` should be removed,
2673-
// but currently this lint's suggestions can conflict with those of `clippy::needless_lifetimes`:
2674-
// https://github.com/rust-lang/rust/pull/129840#issuecomment-2323349119
2675-
// HACK: `'static` suggestions will never sonflict, emit only those for now.
2676-
if name != kw::StaticLifetime {
2677-
return;
2678-
}
2679-
match kind {
2680-
MissingLifetimeKind::Underscore => diag.span_suggestion_verbose(
2681-
span,
2682-
fluent::lint_suggestion,
2683-
format!("{name}"),
2684-
Applicability::MachineApplicable,
2685-
),
2686-
MissingLifetimeKind::Ampersand => diag.span_suggestion_verbose(
2687-
span.shrink_to_hi(),
2688-
fluent::lint_suggestion,
2689-
format!("{name} "),
2690-
Applicability::MachineApplicable,
2691-
),
2692-
MissingLifetimeKind::Comma => diag.span_suggestion_verbose(
2693-
span.shrink_to_hi(),
2694-
fluent::lint_suggestion,
2695-
format!("{name}, "),
2696-
Applicability::MachineApplicable,
2697-
),
2698-
MissingLifetimeKind::Brackets => diag.span_suggestion_verbose(
2699-
span.shrink_to_hi(),
2700-
fluent::lint_suggestion,
2701-
format!("<{name}>"),
2702-
Applicability::MachineApplicable,
2703-
),
2704-
};
2705-
}
2706-
}
2707-
27082656
#[derive(LintDiagnostic)]
27092657
#[diag(lint_invalid_crate_type_value)]
27102658
pub(crate) struct UnknownCrateTypes {

compiler/rustc_lint_defs/src/builtin.rs

-33
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ declare_lint_pass! {
4040
DUPLICATE_MACRO_ATTRIBUTES,
4141
ELIDED_LIFETIMES_IN_ASSOCIATED_CONSTANT,
4242
ELIDED_LIFETIMES_IN_PATHS,
43-
ELIDED_NAMED_LIFETIMES,
4443
EXPLICIT_BUILTIN_CFGS_IN_FLAGS,
4544
EXPORTED_PRIVATE_DEPENDENCIES,
4645
FFI_UNWIND_CALLS,
@@ -1828,38 +1827,6 @@ declare_lint! {
18281827
"hidden lifetime parameters in types are deprecated"
18291828
}
18301829

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

compiler/rustc_lint_defs/src/lib.rs

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

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

compiler/rustc_resolve/src/late.rs

+6-55
Original file line numberDiff line numberDiff line change
@@ -1700,7 +1700,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
17001700
if ident.name == kw::StaticLifetime {
17011701
self.record_lifetime_res(
17021702
lifetime.id,
1703-
LifetimeRes::Static { suppress_elision_warning: false },
1703+
LifetimeRes::Static,
17041704
LifetimeElisionCandidate::Named,
17051705
);
17061706
return;
@@ -1845,8 +1845,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
18451845
if lifetimes_in_scope.is_empty() {
18461846
self.record_lifetime_res(
18471847
lifetime.id,
1848-
// We are inside a const item, so do not warn.
1849-
LifetimeRes::Static { suppress_elision_warning: true },
1848+
LifetimeRes::Static,
18501849
elision_candidate,
18511850
);
18521851
return;
@@ -2193,47 +2192,6 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
21932192
panic!("lifetime {id:?} resolved multiple times ({prev_res:?} before, {res:?} now)")
21942193
}
21952194

2196-
match candidate {
2197-
LifetimeElisionCandidate::Missing(missing @ MissingLifetime { .. }) => {
2198-
debug_assert_eq!(id, missing.id);
2199-
match res {
2200-
LifetimeRes::Static { suppress_elision_warning } => {
2201-
if !suppress_elision_warning {
2202-
self.r.lint_buffer.buffer_lint(
2203-
lint::builtin::ELIDED_NAMED_LIFETIMES,
2204-
missing.id_for_lint,
2205-
missing.span,
2206-
BuiltinLintDiag::ElidedNamedLifetimes {
2207-
elided: (missing.span, missing.kind),
2208-
resolution: lint::ElidedLifetimeResolution::Static,
2209-
},
2210-
);
2211-
}
2212-
}
2213-
LifetimeRes::Param { param, binder: _ } => {
2214-
let tcx = self.r.tcx();
2215-
self.r.lint_buffer.buffer_lint(
2216-
lint::builtin::ELIDED_NAMED_LIFETIMES,
2217-
missing.id_for_lint,
2218-
missing.span,
2219-
BuiltinLintDiag::ElidedNamedLifetimes {
2220-
elided: (missing.span, missing.kind),
2221-
resolution: lint::ElidedLifetimeResolution::Param(
2222-
tcx.item_name(param.into()),
2223-
tcx.source_span(param),
2224-
),
2225-
},
2226-
);
2227-
}
2228-
LifetimeRes::Fresh { .. }
2229-
| LifetimeRes::Infer
2230-
| LifetimeRes::Error
2231-
| LifetimeRes::ElidedAnchor { .. } => {}
2232-
}
2233-
}
2234-
LifetimeElisionCandidate::Ignore | LifetimeElisionCandidate::Named => {}
2235-
}
2236-
22372195
match res {
22382196
LifetimeRes::Param { .. } | LifetimeRes::Fresh { .. } | LifetimeRes::Static { .. } => {
22392197
if let Some(ref mut candidates) = self.lifetime_elision_candidates {
@@ -2748,14 +2706,9 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
27482706
ref ty, ref expr, ref define_opaque, ..
27492707
}) => {
27502708
self.with_static_rib(def_kind, |this| {
2751-
this.with_lifetime_rib(
2752-
LifetimeRibKind::Elided(LifetimeRes::Static {
2753-
suppress_elision_warning: true,
2754-
}),
2755-
|this| {
2756-
this.visit_ty(ty);
2757-
},
2758-
);
2709+
this.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Static), |this| {
2710+
this.visit_ty(ty);
2711+
});
27592712
if let Some(expr) = expr {
27602713
// We already forbid generic params because of the above item rib,
27612714
// so it doesn't matter whether this is a trivial constant.
@@ -2791,9 +2744,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
27912744
this.visit_generics(generics);
27922745

27932746
this.with_lifetime_rib(
2794-
LifetimeRibKind::Elided(LifetimeRes::Static {
2795-
suppress_elision_warning: true,
2796-
}),
2747+
LifetimeRibKind::Elided(LifetimeRes::Static),
27972748
|this| this.visit_ty(ty),
27982749
);
27992750

compiler/rustc_resolve/src/late/diagnostics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3279,7 +3279,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
32793279
maybe_static = true;
32803280
in_scope_lifetimes = vec![(
32813281
Ident::with_dummy_span(kw::StaticLifetime),
3282-
(DUMMY_NODE_ID, LifetimeRes::Static { suppress_elision_warning: false }),
3282+
(DUMMY_NODE_ID, LifetimeRes::Static),
32833283
)];
32843284
}
32853285
} else if elided_len == 0 {
@@ -3291,7 +3291,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
32913291
maybe_static = true;
32923292
in_scope_lifetimes = vec![(
32933293
Ident::with_dummy_span(kw::StaticLifetime),
3294-
(DUMMY_NODE_ID, LifetimeRes::Static { suppress_elision_warning: false }),
3294+
(DUMMY_NODE_ID, LifetimeRes::Static),
32953295
)];
32963296
}
32973297
} 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)