Skip to content

Commit 1625496

Browse files
committed
Replace elided_named_lifetimes with mismatched_lifetime_syntaxes
1 parent 412c8ab commit 1625496

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};
@@ -2648,58 +2648,6 @@ pub(crate) struct ElidedLifetimesInPaths {
26482648
pub subdiag: ElidedLifetimeInPathSubdiag,
26492649
}
26502650

2651-
pub(crate) struct ElidedNamedLifetime {
2652-
pub span: Span,
2653-
pub kind: MissingLifetimeKind,
2654-
pub name: Symbol,
2655-
pub declaration: Option<Span>,
2656-
}
2657-
2658-
impl<G: EmissionGuarantee> LintDiagnostic<'_, G> for ElidedNamedLifetime {
2659-
fn decorate_lint(self, diag: &mut rustc_errors::Diag<'_, G>) {
2660-
let Self { span, kind, name, declaration } = self;
2661-
diag.primary_message(fluent::lint_elided_named_lifetime);
2662-
diag.arg("name", name);
2663-
diag.span_label(span, fluent::lint_label_elided);
2664-
if let Some(declaration) = declaration {
2665-
diag.span_label(declaration, fluent::lint_label_named);
2666-
}
2667-
// FIXME(GrigorenkoPV): this `if` and `return` should be removed,
2668-
// but currently this lint's suggestions can conflict with those of `clippy::needless_lifetimes`:
2669-
// https://github.com/rust-lang/rust/pull/129840#issuecomment-2323349119
2670-
// HACK: `'static` suggestions will never sonflict, emit only those for now.
2671-
if name != kw::StaticLifetime {
2672-
return;
2673-
}
2674-
match kind {
2675-
MissingLifetimeKind::Underscore => diag.span_suggestion_verbose(
2676-
span,
2677-
fluent::lint_suggestion,
2678-
format!("{name}"),
2679-
Applicability::MachineApplicable,
2680-
),
2681-
MissingLifetimeKind::Ampersand => diag.span_suggestion_verbose(
2682-
span.shrink_to_hi(),
2683-
fluent::lint_suggestion,
2684-
format!("{name} "),
2685-
Applicability::MachineApplicable,
2686-
),
2687-
MissingLifetimeKind::Comma => diag.span_suggestion_verbose(
2688-
span.shrink_to_hi(),
2689-
fluent::lint_suggestion,
2690-
format!("{name}, "),
2691-
Applicability::MachineApplicable,
2692-
),
2693-
MissingLifetimeKind::Brackets => diag.span_suggestion_verbose(
2694-
span.shrink_to_hi(),
2695-
fluent::lint_suggestion,
2696-
format!("<{name}>"),
2697-
Applicability::MachineApplicable,
2698-
),
2699-
};
2700-
}
2701-
}
2702-
27032651
#[derive(LintDiagnostic)]
27042652
#[diag(lint_invalid_crate_type_value)]
27052653
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,
@@ -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
@@ -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
@@ -1707,7 +1707,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
17071707
if ident.name == kw::StaticLifetime {
17081708
self.record_lifetime_res(
17091709
lifetime.id,
1710-
LifetimeRes::Static { suppress_elision_warning: false },
1710+
LifetimeRes::Static,
17111711
LifetimeElisionCandidate::Named,
17121712
);
17131713
return;
@@ -1852,8 +1852,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
18521852
if lifetimes_in_scope.is_empty() {
18531853
self.record_lifetime_res(
18541854
lifetime.id,
1855-
// We are inside a const item, so do not warn.
1856-
LifetimeRes::Static { suppress_elision_warning: true },
1855+
LifetimeRes::Static,
18571856
elision_candidate,
18581857
);
18591858
return;
@@ -2200,47 +2199,6 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
22002199
panic!("lifetime {id:?} resolved multiple times ({prev_res:?} before, {res:?} now)")
22012200
}
22022201

2203-
match candidate {
2204-
LifetimeElisionCandidate::Missing(missing @ MissingLifetime { .. }) => {
2205-
debug_assert_eq!(id, missing.id);
2206-
match res {
2207-
LifetimeRes::Static { suppress_elision_warning } => {
2208-
if !suppress_elision_warning {
2209-
self.r.lint_buffer.buffer_lint(
2210-
lint::builtin::ELIDED_NAMED_LIFETIMES,
2211-
missing.id_for_lint,
2212-
missing.span,
2213-
BuiltinLintDiag::ElidedNamedLifetimes {
2214-
elided: (missing.span, missing.kind),
2215-
resolution: lint::ElidedLifetimeResolution::Static,
2216-
},
2217-
);
2218-
}
2219-
}
2220-
LifetimeRes::Param { param, binder: _ } => {
2221-
let tcx = self.r.tcx();
2222-
self.r.lint_buffer.buffer_lint(
2223-
lint::builtin::ELIDED_NAMED_LIFETIMES,
2224-
missing.id_for_lint,
2225-
missing.span,
2226-
BuiltinLintDiag::ElidedNamedLifetimes {
2227-
elided: (missing.span, missing.kind),
2228-
resolution: lint::ElidedLifetimeResolution::Param(
2229-
tcx.item_name(param.into()),
2230-
tcx.source_span(param),
2231-
),
2232-
},
2233-
);
2234-
}
2235-
LifetimeRes::Fresh { .. }
2236-
| LifetimeRes::Infer
2237-
| LifetimeRes::Error
2238-
| LifetimeRes::ElidedAnchor { .. } => {}
2239-
}
2240-
}
2241-
LifetimeElisionCandidate::Ignore | LifetimeElisionCandidate::Named => {}
2242-
}
2243-
22442202
match res {
22452203
LifetimeRes::Param { .. } | LifetimeRes::Fresh { .. } | LifetimeRes::Static { .. } => {
22462204
if let Some(ref mut candidates) = self.lifetime_elision_candidates {
@@ -2756,14 +2714,9 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
27562714

27572715
ItemKind::Static(box ast::StaticItem { ref ty, ref expr, .. }) => {
27582716
self.with_static_rib(def_kind, |this| {
2759-
this.with_lifetime_rib(
2760-
LifetimeRibKind::Elided(LifetimeRes::Static {
2761-
suppress_elision_warning: true,
2762-
}),
2763-
|this| {
2764-
this.visit_ty(ty);
2765-
},
2766-
);
2717+
this.with_lifetime_rib(LifetimeRibKind::Elided(LifetimeRes::Static), |this| {
2718+
this.visit_ty(ty);
2719+
});
27672720
if let Some(expr) = expr {
27682721
// We already forbid generic params because of the above item rib,
27692722
// so it doesn't matter whether this is a trivial constant.
@@ -2792,9 +2745,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
27922745
this.visit_generics(generics);
27932746

27942747
this.with_lifetime_rib(
2795-
LifetimeRibKind::Elided(LifetimeRes::Static {
2796-
suppress_elision_warning: true,
2797-
}),
2748+
LifetimeRibKind::Elided(LifetimeRes::Static),
27982749
|this| this.visit_ty(ty),
27992750
);
28002751

compiler/rustc_resolve/src/late/diagnostics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3258,7 +3258,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
32583258
maybe_static = true;
32593259
in_scope_lifetimes = vec![(
32603260
Ident::with_dummy_span(kw::StaticLifetime),
3261-
(DUMMY_NODE_ID, LifetimeRes::Static { suppress_elision_warning: false }),
3261+
(DUMMY_NODE_ID, LifetimeRes::Static),
32623262
)];
32633263
}
32643264
} else if elided_len == 0 {
@@ -3270,7 +3270,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
32703270
maybe_static = true;
32713271
in_scope_lifetimes = vec![(
32723272
Ident::with_dummy_span(kw::StaticLifetime),
3273-
(DUMMY_NODE_ID, LifetimeRes::Static { suppress_elision_warning: false }),
3273+
(DUMMY_NODE_ID, LifetimeRes::Static),
32743274
)];
32753275
}
32763276
} 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)