Skip to content

Commit 2cedccb

Browse files
committed
Fix diagnostics spans for missing lifetimes in edge cases
1 parent 338dc1f commit 2cedccb

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+28-6
Original file line numberDiff line numberDiff line change
@@ -1821,7 +1821,7 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
18211821
crate fn add_missing_lifetime_specifiers_label(
18221822
&self,
18231823
err: &mut DiagnosticBuilder<'_>,
1824-
spans_with_counts: Vec<(Span, usize)>,
1824+
mut spans_with_counts: Vec<(Span, usize)>,
18251825
lifetime_names: &FxHashSet<Symbol>,
18261826
lifetime_spans: Vec<Span>,
18271827
params: &[ElisionFailureInfo],
@@ -1831,13 +1831,21 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
18311831
.map(|(span, _)| self.tcx.sess.source_map().span_to_snippet(*span).ok())
18321832
.collect();
18331833

1834-
for (span, count) in &spans_with_counts {
1834+
// Empty generics are marked with a span of "<", but since from now on
1835+
// that information is in the snippets it can be removed from the spans.
1836+
for ((span, _), snippet) in spans_with_counts.iter_mut().zip(&snippets) {
1837+
if snippet.as_deref() == Some("<") {
1838+
*span = span.shrink_to_hi();
1839+
}
1840+
}
1841+
1842+
for &(span, count) in &spans_with_counts {
18351843
err.span_label(
1836-
*span,
1844+
span,
18371845
format!(
18381846
"expected {} lifetime parameter{}",
1839-
if *count == 1 { "named".to_string() } else { count.to_string() },
1840-
pluralize!(*count),
1847+
if count == 1 { "named".to_string() } else { count.to_string() },
1848+
pluralize!(count),
18411849
),
18421850
);
18431851
}
@@ -1982,6 +1990,14 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
19821990
.collect::<Vec<_>>()
19831991
.join(", "),
19841992
)
1993+
} else if snippet == "<" || snippet == "(" {
1994+
(
1995+
span.shrink_to_hi(),
1996+
std::iter::repeat("'static")
1997+
.take(count)
1998+
.collect::<Vec<_>>()
1999+
.join(", "),
2000+
)
19852001
} else {
19862002
(
19872003
span.shrink_to_hi(),
@@ -1990,7 +2006,7 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
19902006
std::iter::repeat("'static")
19912007
.take(count)
19922008
.collect::<Vec<_>>()
1993-
.join(", ")
2009+
.join(", "),
19942010
),
19952011
)
19962012
}
@@ -2045,6 +2061,9 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
20452061
Some("&") => Some(Box::new(|name| format!("&{} ", name))),
20462062
Some("'_") => Some(Box::new(|n| n.to_string())),
20472063
Some("") => Some(Box::new(move |n| format!("{}, ", n).repeat(count))),
2064+
Some("<") => Some(Box::new(move |n| {
2065+
std::iter::repeat(n).take(count).collect::<Vec<_>>().join(", ")
2066+
})),
20482067
Some(snippet) if !snippet.ends_with('>') => Some(Box::new(move |name| {
20492068
format!(
20502069
"{}<{}>",
@@ -2071,6 +2090,9 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
20712090
Some("") => {
20722091
Some(std::iter::repeat("'a, ").take(count).collect::<Vec<_>>().join(""))
20732092
}
2093+
Some("<") => {
2094+
Some(std::iter::repeat("'a").take(count).collect::<Vec<_>>().join(", "))
2095+
}
20742096
Some(snippet) => Some(format!(
20752097
"{}<{}>",
20762098
snippet,

0 commit comments

Comments
 (0)