Skip to content

Commit 3c0c387

Browse files
committed
Implement @jackh726's suggestions
1 parent 439ef6d commit 3c0c387

File tree

5 files changed

+65
-77
lines changed

5 files changed

+65
-77
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+59-72
Original file line numberDiff line numberDiff line change
@@ -1821,21 +1821,19 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
18211821
crate fn add_missing_lifetime_specifiers_label(
18221822
&self,
18231823
err: &mut DiagnosticBuilder<'_>,
1824-
spans: Vec<Span>,
1825-
counts: Vec<usize>,
1824+
spans_with_counts: Vec<(Span, usize)>,
18261825
lifetime_names: &FxHashSet<Symbol>,
18271826
lifetime_spans: Vec<Span>,
18281827
params: &[ElisionFailureInfo],
18291828
) {
1830-
let snippets: Vec<Option<String>> = spans
1829+
let snippets: Vec<Option<String>> = spans_with_counts
18311830
.iter()
1832-
.copied()
1833-
.map(|span| self.tcx.sess.source_map().span_to_snippet(span).ok())
1831+
.map(|(span, _)| self.tcx.sess.source_map().span_to_snippet(*span).ok())
18341832
.collect();
18351833

1836-
for (span, count) in spans.iter().zip(counts.iter()) {
1834+
for (span, count) in &spans_with_counts {
18371835
err.span_label(
1838-
span.clone(),
1836+
*span,
18391837
format!(
18401838
"expected {} lifetime parameter{}",
18411839
if *count == 1 { "named".to_string() } else { count.to_string() },
@@ -1847,7 +1845,7 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
18471845
let suggest_existing =
18481846
|err: &mut DiagnosticBuilder<'_>,
18491847
name: &str,
1850-
formatters: &Vec<Option<Box<dyn Fn(&str) -> String>>>| {
1848+
formatters: Vec<Option<Box<dyn Fn(&str) -> String>>>| {
18511849
if let Some(MissingLifetimeSpot::HigherRanked { span: for_span, span_type }) =
18521850
self.missing_named_lifetime_spots.iter().rev().next()
18531851
{
@@ -1892,9 +1890,9 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
18921890
}
18931891
}
18941892
introduce_suggestion.push((*for_span, for_sugg));
1895-
for (span, formatter) in spans.iter().copied().zip(formatters.iter()) {
1893+
for ((span, _), formatter) in spans_with_counts.iter().zip(formatters.iter()) {
18961894
if let Some(formatter) = formatter {
1897-
introduce_suggestion.push((span, formatter(&lt_name)));
1895+
introduce_suggestion.push((*span, formatter(&lt_name)));
18981896
}
18991897
}
19001898
err.multipart_suggestion_with_style(
@@ -1905,12 +1903,12 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
19051903
);
19061904
}
19071905

1908-
let mut spans_suggs: Vec<_> = Vec::new();
1909-
for (span, fmt) in spans.iter().copied().zip(formatters.iter()) {
1910-
if let Some(formatter) = fmt {
1911-
spans_suggs.push((span, formatter(name)));
1912-
}
1913-
}
1906+
let spans_suggs: Vec<_> = formatters
1907+
.into_iter()
1908+
.filter_map(|fmt| fmt)
1909+
.zip(spans_with_counts.iter())
1910+
.map(|(formatter, (span, _))| (*span, formatter(name)))
1911+
.collect();
19141912
err.multipart_suggestion_with_style(
19151913
&format!(
19161914
"consider using the `{}` lifetime",
@@ -1921,7 +1919,7 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
19211919
SuggestionStyle::ShowAlways,
19221920
);
19231921
};
1924-
let suggest_new = |err: &mut DiagnosticBuilder<'_>, suggs: &Vec<Option<String>>| {
1922+
let suggest_new = |err: &mut DiagnosticBuilder<'_>, suggs: Vec<Option<String>>| {
19251923
for missing in self.missing_named_lifetime_spots.iter().rev() {
19261924
let mut introduce_suggestion = vec![];
19271925
let msg;
@@ -1967,8 +1965,8 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
19671965
}
19681966
MissingLifetimeSpot::Static => {
19691967
let mut spans_suggs = Vec::new();
1970-
for ((span, snippet), count) in
1971-
spans.iter().copied().zip(snippets.iter()).zip(counts.iter().copied())
1968+
for ((span, count), snippet) in
1969+
spans_with_counts.iter().copied().zip(snippets.iter())
19721970
{
19731971
let (span, sugg) = match snippet.as_deref() {
19741972
Some("&") => (span.shrink_to_hi(), "'static ".to_owned()),
@@ -2018,7 +2016,7 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
20182016
}
20192017
}
20202018
}
2021-
for (span, sugg) in spans.iter().copied().zip(suggs.iter()) {
2019+
for ((span, _), sugg) in spans_with_counts.iter().copied().zip(suggs.iter()) {
20222020
if let Some(sugg) = sugg {
20232021
introduce_suggestion.push((span, sugg.to_string()));
20242022
}
@@ -2039,68 +2037,57 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
20392037
match &lifetime_names[..] {
20402038
[name] => {
20412039
let mut suggs: Vec<Option<Box<dyn Fn(&str) -> String>>> = Vec::new();
2042-
for (snippet, count) in snippets.iter().cloned().zip(counts.iter().copied()) {
2043-
if snippet == Some("&".to_string()) {
2044-
suggs.push(Some(Box::new(|name| format!("&{} ", name))));
2045-
} else if snippet == Some("'_".to_string()) {
2046-
suggs.push(Some(Box::new(|n| n.to_string())));
2047-
} else if snippet == Some("".to_string()) {
2048-
suggs.push(Some(Box::new(move |n| format!("{}, ", n).repeat(count))));
2049-
} else if let Some(snippet) = snippet {
2050-
if !snippet.ends_with('>') {
2051-
suggs.push(Some(Box::new(move |name| {
2052-
format!(
2053-
"{}<{}>",
2054-
snippet,
2055-
std::iter::repeat(name.to_string())
2056-
.take(count)
2057-
.collect::<Vec<_>>()
2058-
.join(", ")
2059-
)
2060-
})));
2061-
} else {
2062-
suggs.push(None);
2063-
}
2064-
} else {
2065-
suggs.push(None);
2066-
}
2040+
for (snippet, (_, count)) in snippets.iter().zip(spans_with_counts.iter().copied())
2041+
{
2042+
suggs.push(match snippet.as_deref() {
2043+
Some("&") => Some(Box::new(|name| format!("&{} ", name))),
2044+
Some("'_") => Some(Box::new(|n| n.to_string())),
2045+
Some("") => Some(Box::new(move |n| format!("{}, ", n).repeat(count))),
2046+
Some(snippet) if !snippet.ends_with('>') => Some(Box::new(move |name| {
2047+
format!(
2048+
"{}<{}>",
2049+
snippet,
2050+
std::iter::repeat(name.to_string())
2051+
.take(count)
2052+
.collect::<Vec<_>>()
2053+
.join(", ")
2054+
)
2055+
})),
2056+
_ => None,
2057+
});
20672058
}
2068-
suggest_existing(err, &name.as_str()[..], &suggs);
2059+
suggest_existing(err, &name.as_str()[..], suggs);
20692060
}
20702061
[] => {
2071-
let mut suggs: Vec<Option<String>> = Vec::new();
2072-
for (snippet, count) in snippets.iter().cloned().zip(counts.iter().copied()) {
2073-
if snippet == Some("&".to_string()) {
2074-
suggs.push(Some("&'a ".to_string()));
2075-
} else if snippet == Some("'_".to_string()) {
2076-
suggs.push(Some("'a".to_string()));
2077-
} else if let Some(snippet) = snippet {
2078-
if snippet == "" {
2079-
suggs.push(Some(
2080-
std::iter::repeat("'a, ").take(count).collect::<Vec<_>>().join(""),
2081-
));
2082-
} else {
2083-
suggs.push(Some(format!(
2084-
"{}<{}>",
2085-
snippet,
2086-
std::iter::repeat("'a").take(count).collect::<Vec<_>>().join(", ")
2087-
)));
2062+
let mut suggs = Vec::new();
2063+
for (snippet, (_, count)) in
2064+
snippets.iter().cloned().zip(spans_with_counts.iter().copied())
2065+
{
2066+
suggs.push(match snippet.as_deref() {
2067+
Some("&") => Some("&'a ".to_string()),
2068+
Some("'_") => Some("'a".to_string()),
2069+
Some("") => {
2070+
Some(std::iter::repeat("'a, ").take(count).collect::<Vec<_>>().join(""))
20882071
}
2089-
} else {
2090-
suggs.push(None);
2091-
}
2072+
Some(snippet) => Some(format!(
2073+
"{}<{}>",
2074+
snippet,
2075+
std::iter::repeat("'a").take(count).collect::<Vec<_>>().join(", "),
2076+
)),
2077+
None => None,
2078+
});
20922079
}
2093-
suggest_new(err, &suggs);
2080+
suggest_new(err, suggs);
20942081
}
20952082
lts if lts.len() > 1 => {
20962083
err.span_note(lifetime_spans, "these named lifetimes are available to use");
20972084

20982085
let mut spans_suggs: Vec<_> = Vec::new();
2099-
for (span, snippet) in spans.iter().copied().zip(snippets.iter()) {
2100-
if Some("") == snippet.as_deref() {
2101-
spans_suggs.push((span, "'lifetime, ".to_string()));
2102-
} else if Some("&") == snippet.as_deref() {
2103-
spans_suggs.push((span, "&'lifetime ".to_string()));
2086+
for ((span, _), snippet) in spans_with_counts.iter().copied().zip(snippets.iter()) {
2087+
match snippet.as_deref() {
2088+
Some("") => spans_suggs.push((span, "'lifetime, ".to_string())),
2089+
Some("&") => spans_suggs.push((span, "&'lifetime ".to_string())),
2090+
_ => {}
21042091
}
21052092
}
21062093

compiler/rustc_resolve/src/late/lifetimes.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -3038,8 +3038,10 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
30383038
spans.sort();
30393039
let mut spans_dedup = spans.clone();
30403040
spans_dedup.dedup();
3041-
let counts: Vec<_> =
3042-
spans_dedup.iter().map(|sp| spans.iter().filter(|nsp| *nsp == sp).count()).collect();
3041+
let spans_with_counts: Vec<_> = spans_dedup
3042+
.into_iter()
3043+
.map(|sp| (sp, spans.iter().filter(|nsp| *nsp == &sp).count()))
3044+
.collect();
30433045

30443046
let mut err = self.report_missing_lifetime_specifiers(spans.clone(), lifetime_refs.len());
30453047

@@ -3052,8 +3054,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
30523054

30533055
self.add_missing_lifetime_specifiers_label(
30543056
&mut err,
3055-
spans,
3056-
counts,
3057+
spans_with_counts,
30573058
&lifetime_names,
30583059
lifetime_spans,
30593060
error.unwrap_or(&[]),

src/tools/tidy/src/ui_tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::path::Path;
88
const ENTRY_LIMIT: usize = 1000;
99
// FIXME: The following limits should be reduced eventually.
1010
const ROOT_ENTRY_LIMIT: usize = 1388;
11-
const ISSUES_ENTRY_LIMIT: usize = 2557;
11+
const ISSUES_ENTRY_LIMIT: usize = 2551;
1212

1313
fn check_entries(path: &Path, bad: &mut bool) {
1414
let dirs = walkdir::WalkDir::new(&path.join("test/ui"))

0 commit comments

Comments
 (0)