Skip to content

Commit 96bbb0d

Browse files
committed
Account for impl Trait
Address #49287
1 parent 1beac2b commit 96bbb0d

File tree

3 files changed

+71
-34
lines changed

3 files changed

+71
-34
lines changed

src/librustc_resolve/diagnostics.rs

+55-34
Original file line numberDiff line numberDiff line change
@@ -1519,9 +1519,21 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
15191519
for missing in &self.missing_named_lifetime_spots {
15201520
match missing {
15211521
MissingLifetimeSpot::Generics(generics) => {
1522-
let (span, sugg) = match &generics.params {
1523-
[] => (generics.span, format!("<{}>", lifetime_ref)),
1524-
[param, ..] => (param.span.shrink_to_lo(), format!("{}, ", lifetime_ref)),
1522+
let (span, sugg) = if let Some(param) = generics
1523+
.params
1524+
.iter()
1525+
.filter(|p| match p.kind {
1526+
hir::GenericParamKind::Type {
1527+
synthetic: Some(hir::SyntheticTyParamKind::ImplTrait),
1528+
..
1529+
} => false,
1530+
_ => true,
1531+
})
1532+
.next()
1533+
{
1534+
(param.span.shrink_to_lo(), format!("{}, ", lifetime_ref))
1535+
} else {
1536+
(generics.span, format!("<{}>", lifetime_ref))
15251537
};
15261538
err.span_suggestion(
15271539
span,
@@ -1592,20 +1604,28 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
15921604
Applicability::MaybeIncorrect,
15931605
);
15941606
};
1595-
let suggest_new = |err: &mut DiagnosticBuilder<'_>, sugg: &str| {
1596-
err.span_label(span, "expected named lifetime parameter");
1597-
1598-
for missing in self.missing_named_lifetime_spots.iter().rev() {
1599-
let mut introduce_suggestion = vec![];
1600-
let msg;
1601-
let should_break;
1602-
introduce_suggestion.push(match missing {
1607+
let suggest_new =
1608+
|err: &mut DiagnosticBuilder<'_>, sugg: &str| {
1609+
err.span_label(span, "expected named lifetime parameter");
1610+
1611+
for missing in self.missing_named_lifetime_spots.iter().rev() {
1612+
let mut introduce_suggestion = vec![];
1613+
let msg;
1614+
let should_break;
1615+
introduce_suggestion.push(match missing {
16031616
MissingLifetimeSpot::Generics(generics) => {
16041617
msg = "consider introducing a named lifetime parameter".to_string();
16051618
should_break = true;
1606-
match &generics.params {
1607-
[] => (generics.span, "<'a>".to_string()),
1608-
[param, ..] => (param.span.shrink_to_lo(), "'a, ".to_string()),
1619+
if let Some(param) = generics.params.iter().filter(|p| match p.kind {
1620+
hir::GenericParamKind::Type {
1621+
synthetic: Some(hir::SyntheticTyParamKind::ImplTrait),
1622+
..
1623+
} => false,
1624+
_ => true,
1625+
}).next() {
1626+
(param.span.shrink_to_lo(), "'a, ".to_string())
1627+
} else {
1628+
(generics.span, "<'a>".to_string())
16091629
}
16101630
}
16111631
MissingLifetimeSpot::HigherRanked { span, span_type } => {
@@ -1621,29 +1641,30 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
16211641
(*span, span_type.suggestion("'a"))
16221642
}
16231643
});
1624-
for param in params {
1625-
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(param.span)
1626-
{
1627-
if snippet.starts_with("&") && !snippet.starts_with("&'") {
1628-
introduce_suggestion
1629-
.push((param.span, format!("&'a {}", &snippet[1..])));
1630-
} else if snippet.starts_with("&'_ ") {
1631-
introduce_suggestion
1632-
.push((param.span, format!("&'a {}", &snippet[4..])));
1644+
for param in params {
1645+
if let Ok(snippet) =
1646+
self.tcx.sess.source_map().span_to_snippet(param.span)
1647+
{
1648+
if snippet.starts_with("&") && !snippet.starts_with("&'") {
1649+
introduce_suggestion
1650+
.push((param.span, format!("&'a {}", &snippet[1..])));
1651+
} else if snippet.starts_with("&'_ ") {
1652+
introduce_suggestion
1653+
.push((param.span, format!("&'a {}", &snippet[4..])));
1654+
}
16331655
}
16341656
}
1657+
introduce_suggestion.push((span, sugg.to_string()));
1658+
err.multipart_suggestion(
1659+
&msg,
1660+
introduce_suggestion,
1661+
Applicability::MaybeIncorrect,
1662+
);
1663+
if should_break {
1664+
break;
1665+
}
16351666
}
1636-
introduce_suggestion.push((span, sugg.to_string()));
1637-
err.multipart_suggestion(
1638-
&msg,
1639-
introduce_suggestion,
1640-
Applicability::MaybeIncorrect,
1641-
);
1642-
if should_break {
1643-
break;
1644-
}
1645-
}
1646-
};
1667+
};
16471668

16481669
match (
16491670
lifetime_names.len(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
fn f(_: impl Iterator<Item = &'_ ()>) {} //~ ERROR missing lifetime specifier
2+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0106]: missing lifetime specifier
2+
--> $DIR/impl-trait-missing-lifetime.rs:1:31
3+
|
4+
LL | fn f(_: impl Iterator<Item = &'_ ()>) {}
5+
| ^^ expected named lifetime parameter
6+
|
7+
help: consider introducing a named lifetime parameter
8+
|
9+
LL | fn f<'a>(_: impl Iterator<Item = &'a ()>) {}
10+
| ^^^^ ^^
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0106`.

0 commit comments

Comments
 (0)