Skip to content

Commit 6b4ba02

Browse files
committed
Replace elided_named_lifetimes with mismatched_lifetime_syntaxes
1 parent 5f278ba commit 6b4ba02

File tree

59 files changed

+364
-546
lines changed

Some content is hidden

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

59 files changed

+364
-546
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, SubdiagMessageOp, 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};
@@ -2676,58 +2676,6 @@ pub(crate) struct ElidedLifetimesInPaths {
26762676
pub subdiag: ElidedLifetimeInPathSubdiag,
26772677
}
26782678

2679-
pub(crate) struct ElidedNamedLifetime {
2680-
pub span: Span,
2681-
pub kind: MissingLifetimeKind,
2682-
pub name: Symbol,
2683-
pub declaration: Option<Span>,
2684-
}
2685-
2686-
impl<G: EmissionGuarantee> LintDiagnostic<'_, G> for ElidedNamedLifetime {
2687-
fn decorate_lint(self, diag: &mut rustc_errors::Diag<'_, G>) {
2688-
let Self { span, kind, name, declaration } = self;
2689-
diag.primary_message(fluent::lint_elided_named_lifetime);
2690-
diag.arg("name", name);
2691-
diag.span_label(span, fluent::lint_label_elided);
2692-
if let Some(declaration) = declaration {
2693-
diag.span_label(declaration, fluent::lint_label_named);
2694-
}
2695-
// FIXME(GrigorenkoPV): this `if` and `return` should be removed,
2696-
// but currently this lint's suggestions can conflict with those of `clippy::needless_lifetimes`:
2697-
// https://github.com/rust-lang/rust/pull/129840#issuecomment-2323349119
2698-
// HACK: `'static` suggestions will never sonflict, emit only those for now.
2699-
if name != kw::StaticLifetime {
2700-
return;
2701-
}
2702-
match kind {
2703-
MissingLifetimeKind::Underscore => diag.span_suggestion_verbose(
2704-
span,
2705-
fluent::lint_suggestion,
2706-
format!("{name}"),
2707-
Applicability::MachineApplicable,
2708-
),
2709-
MissingLifetimeKind::Ampersand => diag.span_suggestion_verbose(
2710-
span.shrink_to_hi(),
2711-
fluent::lint_suggestion,
2712-
format!("{name} "),
2713-
Applicability::MachineApplicable,
2714-
),
2715-
MissingLifetimeKind::Comma => diag.span_suggestion_verbose(
2716-
span.shrink_to_hi(),
2717-
fluent::lint_suggestion,
2718-
format!("{name}, "),
2719-
Applicability::MachineApplicable,
2720-
),
2721-
MissingLifetimeKind::Brackets => diag.span_suggestion_verbose(
2722-
span.shrink_to_hi(),
2723-
fluent::lint_suggestion,
2724-
format!("<{name}>"),
2725-
Applicability::MachineApplicable,
2726-
),
2727-
};
2728-
}
2729-
}
2730-
27312679
#[derive(LintDiagnostic)]
27322680
#[diag(lint_invalid_crate_type_value)]
27332681
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
@@ -3259,7 +3259,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
32593259
maybe_static = true;
32603260
in_scope_lifetimes = vec![(
32613261
Ident::with_dummy_span(kw::StaticLifetime),
3262-
(DUMMY_NODE_ID, LifetimeRes::Static { suppress_elision_warning: false }),
3262+
(DUMMY_NODE_ID, LifetimeRes::Static),
32633263
)];
32643264
}
32653265
} else if elided_len == 0 {
@@ -3271,7 +3271,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
32713271
maybe_static = true;
32723272
in_scope_lifetimes = vec![(
32733273
Ident::with_dummy_span(kw::StaticLifetime),
3274-
(DUMMY_NODE_ID, LifetimeRes::Static { suppress_elision_warning: false }),
3274+
(DUMMY_NODE_ID, LifetimeRes::Static),
32753275
)];
32763276
}
32773277
} else if num_params == 1 {

tests/ui/async-await/issues/issue-63388-1.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ trait Foo {}
99
impl Xyz {
1010
async fn do_sth<'a>(
1111
&'a self, foo: &dyn Foo
12-
) -> &dyn Foo //~ WARNING elided lifetime has a name
12+
) -> &dyn Foo
1313
{
1414
foo
1515
//~^ ERROR explicit lifetime required in the type of `foo` [E0621]
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
warning: elided lifetime has a name
2-
--> $DIR/issue-63388-1.rs:12:10
3-
|
4-
LL | async fn do_sth<'a>(
5-
| -- lifetime `'a` declared here
6-
LL | &'a self, foo: &dyn Foo
7-
LL | ) -> &dyn Foo
8-
| ^ this elided lifetime gets resolved as `'a`
9-
|
10-
= note: `#[warn(elided_named_lifetimes)]` on by default
11-
121
error[E0621]: explicit lifetime required in the type of `foo`
132
--> $DIR/issue-63388-1.rs:14:9
143
|
@@ -18,6 +7,6 @@ LL | &'a self, foo: &dyn Foo
187
LL | foo
198
| ^^^ lifetime `'a` required
209

21-
error: aborting due to 1 previous error; 1 warning emitted
10+
error: aborting due to 1 previous error
2211

2312
For more information about this error, try `rustc --explain E0621`.

0 commit comments

Comments
 (0)