Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit adb7901

Browse files
committedAug 18, 2024·
elided_named_lifetimes: improve the message a bit
1 parent 066671f commit adb7901

File tree

7 files changed

+29
-34
lines changed

7 files changed

+29
-34
lines changed
 

‎compiler/rustc_ast_lowering/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1643,7 +1643,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
16431643
for lifetime in captured_lifetimes_to_duplicate {
16441644
let res = self.resolver.get_lifetime_res(lifetime.id).unwrap_or(LifetimeRes::Error);
16451645
let (old_def_id, missing_kind) = match res {
1646-
LifetimeRes::Param { param: old_def_id, binder: _ } => (old_def_id, None),
1646+
LifetimeRes::Param { param: (old_def_id, _), binder: _ } => (old_def_id, None),
16471647

16481648
LifetimeRes::Fresh { param, kind, .. } => {
16491649
debug_assert_eq!(lifetime.ident.name, kw::UnderscoreLifetime);
@@ -2061,7 +2061,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20612061
res: LifetimeRes,
20622062
) -> &'hir hir::Lifetime {
20632063
let res = match res {
2064-
LifetimeRes::Param { param, .. } => {
2064+
LifetimeRes::Param { param: (param, _), .. } => {
20652065
let param = self.get_remapped_def_id(param);
20662066
hir::LifetimeName::Param(param)
20672067
}

‎compiler/rustc_hir/src/def.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_data_structures::unord::UnordMap;
88
use rustc_macros::{Decodable, Encodable, HashStable_Generic};
99
use rustc_span::def_id::{DefId, LocalDefId};
1010
use rustc_span::hygiene::MacroKind;
11-
use rustc_span::symbol::kw;
11+
use rustc_span::symbol::{kw, Ident};
1212
use rustc_span::Symbol;
1313

1414
use crate::definitions::DefPathData;
@@ -796,8 +796,8 @@ impl<Id> Res<Id> {
796796
pub enum LifetimeRes {
797797
/// Successfully linked the lifetime to a generic parameter.
798798
Param {
799-
/// Id of the generic parameter that introduced it.
800-
param: LocalDefId,
799+
/// `LocalDefId` & `Ident` of the generic parameter that introduced it.
800+
param: (LocalDefId, Ident),
801801
/// Id of the introducing place. That can be:
802802
/// - an item's id, for the item's generic parameters;
803803
/// - a TraitRef's ref_id, identifying the `for<...>` binder;

‎compiler/rustc_lint/messages.ftl

+2-3
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,8 @@ lint_duplicate_macro_attribute =
253253
lint_duplicate_matcher_binding = duplicate matcher binding
254254
255255
lint_elided_named_lifetime = elided lifetime has a name
256-
.label_static = this elided lifetime gets resolved as `'static`
257-
.label_this = this elided lifetime
258-
.label_that = gets resolved as this named lifetime
256+
.label_elided = this elided lifetime gets resolved as `{$name}`
257+
.label_named = lifetime `{$name}` declared here
259258
260259
lint_enum_intrinsics_mem_discriminant =
261260
the return value of `mem::discriminant` is unspecified when called with a non-enum type

‎compiler/rustc_lint/src/context/diagnostics.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use rustc_errors::{
1010
use rustc_middle::middle::stability;
1111
use rustc_session::lint::BuiltinLintDiag;
1212
use rustc_session::Session;
13+
use rustc_span::symbol::kw;
1314
use rustc_span::BytePos;
1415
use tracing::debug;
1516

@@ -442,10 +443,14 @@ pub(super) fn decorate_lint(sess: &Session, diagnostic: BuiltinLintDiag, diag: &
442443
lints::UnexpectedBuiltinCfg { cfg, cfg_name, controlled_by }.decorate_lint(diag)
443444
}
444445
BuiltinLintDiag::ElidedIsStatic { elided } => {
445-
lints::ElidedNamedLifetime::Static { elided }.decorate_lint(diag)
446+
lints::ElidedNamedLifetime { elided, name: kw::StaticLifetime, named_declaration: None }
447+
.decorate_lint(diag)
446448
}
447-
BuiltinLintDiag::ElidedIsParam { elided, param } => {
448-
lints::ElidedNamedLifetime::Param { elided, param }.decorate_lint(diag)
449+
BuiltinLintDiag::ElidedIsParam { elided, param } => lints::ElidedNamedLifetime {
450+
elided,
451+
name: param.name,
452+
named_declaration: Some(param.span),
449453
}
454+
.decorate_lint(diag),
450455
}
451456
}

‎compiler/rustc_lint/src/lints.rs

+7-13
Original file line numberDiff line numberDiff line change
@@ -2612,19 +2612,13 @@ pub struct ElidedLifetimesInPaths {
26122612
}
26132613

26142614
#[derive(LintDiagnostic)]
2615-
pub enum ElidedNamedLifetime {
2616-
#[diag(lint_elided_named_lifetime)]
2617-
Static {
2618-
#[label(lint_label_static)]
2619-
elided: Span,
2620-
},
2621-
#[diag(lint_elided_named_lifetime)]
2622-
Param {
2623-
#[label(lint_label_this)]
2624-
elided: Span,
2625-
#[label(lint_label_that)]
2626-
param: Span,
2627-
},
2615+
#[diag(lint_elided_named_lifetime)]
2616+
pub struct ElidedNamedLifetime {
2617+
#[label(lint_label_elided)]
2618+
pub elided: Span,
2619+
pub name: Symbol,
2620+
#[label(lint_label_named)]
2621+
pub named_declaration: Option<Span>,
26282622
}
26292623

26302624
#[derive(LintDiagnostic)]

‎compiler/rustc_lint_defs/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ pub enum BuiltinLintDiag {
590590
},
591591
ElidedIsParam {
592592
elided: Span,
593-
param: Span,
593+
param: Ident,
594594
},
595595
UnknownCrateTypes {
596596
span: Span,

‎compiler/rustc_resolve/src/late.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -1597,7 +1597,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
15971597
if let Some(&(_, res)) = rib.bindings.get(&normalized_ident) {
15981598
self.record_lifetime_res(lifetime.id, res, LifetimeElisionCandidate::Named);
15991599

1600-
if let LifetimeRes::Param { param, binder } = res {
1600+
if let LifetimeRes::Param { param: (param, _), binder } = res {
16011601
match self.lifetime_uses.entry(param) {
16021602
Entry::Vacant(v) => {
16031603
debug!("First use of {:?} at {:?}", res, ident.span);
@@ -2065,7 +2065,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
20652065
}
20662066

20672067
match candidate {
2068-
LifetimeElisionCandidate::Missing(missing) => {
2068+
LifetimeElisionCandidate::Missing(missing @ MissingLifetime { span: elided, .. }) => {
20692069
debug_assert_eq!(id, missing.id);
20702070
match res {
20712071
LifetimeRes::Static => {
@@ -2074,11 +2074,11 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
20742074
lint::builtin::ELIDED_NAMED_LIFETIMES,
20752075
missing.id_if_not_fake_or(self.crate_node_id),
20762076
missing.span,
2077-
BuiltinLintDiag::ElidedIsStatic { elided: missing.span },
2077+
BuiltinLintDiag::ElidedIsStatic { elided },
20782078
);
20792079
}
20802080
}
2081-
LifetimeRes::Param { param, binder } => {
2081+
LifetimeRes::Param { param: (_, param), binder } => {
20822082
self.r.lint_buffer.buffer_lint(
20832083
lint::builtin::ELIDED_NAMED_LIFETIMES,
20842084
// It should be possible to use `self.crate_node_id`
@@ -2088,10 +2088,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
20882088
// we would have to do some additional work.
20892089
missing.id_if_not_fake_or(binder),
20902090
missing.span,
2091-
BuiltinLintDiag::ElidedIsParam {
2092-
elided: missing.span,
2093-
param: self.r.tcx().source_span(param),
2094-
},
2091+
BuiltinLintDiag::ElidedIsParam { elided, param },
20952092
);
20962093
}
20972094
LifetimeRes::Fresh { .. }
@@ -2826,7 +2823,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
28262823
(&mut function_value_rib, DefKind::ConstParam)
28272824
}
28282825
GenericParamKind::Lifetime => {
2829-
let res = LifetimeRes::Param { param: def_id, binder };
2826+
let res = LifetimeRes::Param { param: (def_id, param.ident), binder };
28302827
self.record_lifetime_param(param.id, res);
28312828
function_lifetime_rib.bindings.insert(ident, (param.id, res));
28322829
continue;

0 commit comments

Comments
 (0)
Please sign in to comment.