Skip to content

Commit a1b2290

Browse files
committed
elided_named_lifetimes: LintDiagnostic
1 parent ff1741b commit a1b2290

File tree

5 files changed

+61
-23
lines changed

5 files changed

+61
-23
lines changed

compiler/rustc_lint/messages.ftl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,10 @@ lint_duplicate_macro_attribute =
252252
253253
lint_duplicate_matcher_binding = duplicate matcher binding
254254
255-
lint_elided_lifetime_static =
256-
this lifetime is static, lol
255+
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
257259
258260
lint_enum_intrinsics_mem_discriminant =
259261
the return value of `mem::discriminant` is unspecified when called with a non-enum type

compiler/rustc_lint/src/context/diagnostics.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,11 @@ pub(super) fn decorate_lint(sess: &Session, diagnostic: BuiltinLintDiag, diag: &
441441
BuiltinLintDiag::UnexpectedBuiltinCfg { cfg, cfg_name, controlled_by } => {
442442
lints::UnexpectedBuiltinCfg { cfg, cfg_name, controlled_by }.decorate_lint(diag)
443443
}
444-
BuiltinLintDiag::ElidedIsStatic => lints::ElidedIsStatic {}.decorate_lint(diag),
444+
BuiltinLintDiag::ElidedIsStatic { elided } => {
445+
lints::ElidedNamedLifetime::Static { elided }.decorate_lint(diag)
446+
}
447+
BuiltinLintDiag::ElidedIsParam { elided, param } => {
448+
lints::ElidedNamedLifetime::Param { elided, param }.decorate_lint(diag)
449+
}
445450
}
446451
}

compiler/rustc_lint/src/lints.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2612,8 +2612,20 @@ pub struct ElidedLifetimesInPaths {
26122612
}
26132613

26142614
#[derive(LintDiagnostic)]
2615-
#[diag(lint_elided_lifetime_static)]
2616-
pub struct ElidedIsStatic {}
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+
},
2628+
}
26172629

26182630
#[derive(LintDiagnostic)]
26192631
#[diag(lint_invalid_crate_type_value)]

compiler/rustc_lint_defs/src/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,13 @@ pub enum BuiltinLintDiag {
585585
},
586586
MacroExpandedMacroExportsAccessedByAbsolutePaths(Span),
587587
ElidedLifetimesInPaths(usize, Span, bool, Span),
588-
ElidedIsStatic,
588+
ElidedIsStatic {
589+
elided: Span,
590+
},
591+
ElidedIsParam {
592+
elided: Span,
593+
param: Span,
594+
},
589595
UnknownCrateTypes {
590596
span: Span,
591597
candidate: Option<Symbol>,

compiler/rustc_resolve/src/late.rs

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2039,25 +2039,38 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
20392039
if let Some(prev_res) = self.r.lifetimes_res_map.insert(id, res) {
20402040
panic!("lifetime {id:?} resolved multiple times ({prev_res:?} before, {res:?} now)")
20412041
}
2042+
20422043
match candidate {
2043-
LifetimeElisionCandidate::Missing(missing) => match res {
2044-
LifetimeRes::Static => {
2045-
self.r.lint_buffer.buffer_lint(
2046-
lint::builtin::ELIDED_NAMED_LIFETIMES,
2047-
missing.id,
2048-
missing.span,
2049-
BuiltinLintDiag::ElidedIsStatic {},
2050-
);
2051-
tracing::warn!(?missing, "static")
2052-
}
2053-
LifetimeRes::Param { param, binder } => {
2054-
tracing::warn!(?missing, ?param, ?binder, "named")
2044+
LifetimeElisionCandidate::Missing(missing) => {
2045+
// FIXME: ICEs otherwise
2046+
if missing.kind != MissingLifetimeKind::Ampersand {
2047+
match res {
2048+
LifetimeRes::Static => {
2049+
self.r.lint_buffer.buffer_lint(
2050+
lint::builtin::ELIDED_NAMED_LIFETIMES,
2051+
missing.id,
2052+
missing.span,
2053+
BuiltinLintDiag::ElidedIsStatic { elided: missing.span },
2054+
);
2055+
}
2056+
LifetimeRes::Param { param, binder: _ } => {
2057+
self.r.lint_buffer.buffer_lint(
2058+
lint::builtin::ELIDED_NAMED_LIFETIMES,
2059+
missing.id,
2060+
missing.span,
2061+
BuiltinLintDiag::ElidedIsParam {
2062+
elided: missing.span,
2063+
param: self.r.tcx().source_span(param),
2064+
},
2065+
);
2066+
}
2067+
LifetimeRes::Fresh { .. }
2068+
| LifetimeRes::Infer
2069+
| LifetimeRes::Error
2070+
| LifetimeRes::ElidedAnchor { .. } => {}
2071+
}
20552072
}
2056-
LifetimeRes::Fresh { .. }
2057-
| LifetimeRes::Infer
2058-
| LifetimeRes::Error
2059-
| LifetimeRes::ElidedAnchor { .. } => {}
2060-
},
2073+
}
20612074
LifetimeElisionCandidate::Ignore | LifetimeElisionCandidate::Named => {}
20622075
}
20632076

0 commit comments

Comments
 (0)