Skip to content

Commit 4310b74

Browse files
committed
Account for impl Trait
Address #49287
1 parent bedda9d commit 4310b74

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
@@ -1514,9 +1514,21 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
15141514
for missing in &self.missing_named_lifetime_spots {
15151515
match missing {
15161516
MissingLifetimeSpot::Generics(generics) => {
1517-
let (span, sugg) = match &generics.params {
1518-
[] => (generics.span, format!("<{}>", lifetime_ref)),
1519-
[param, ..] => (param.span.shrink_to_lo(), format!("{}, ", lifetime_ref)),
1517+
let (span, sugg) = if let Some(param) = generics
1518+
.params
1519+
.iter()
1520+
.filter(|p| match p.kind {
1521+
hir::GenericParamKind::Type {
1522+
synthetic: Some(hir::SyntheticTyParamKind::ImplTrait),
1523+
..
1524+
} => false,
1525+
_ => true,
1526+
})
1527+
.next()
1528+
{
1529+
(param.span.shrink_to_lo(), format!("{}, ", lifetime_ref))
1530+
} else {
1531+
(generics.span, format!("<{}>", lifetime_ref))
15201532
};
15211533
err.span_suggestion(
15221534
span,
@@ -1587,20 +1599,28 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
15871599
Applicability::MaybeIncorrect,
15881600
);
15891601
};
1590-
let suggest_new = |err: &mut DiagnosticBuilder<'_>, sugg: &str| {
1591-
err.span_label(span, "expected named lifetime parameter");
1592-
1593-
for missing in self.missing_named_lifetime_spots.iter().rev() {
1594-
let mut introduce_suggestion = vec![];
1595-
let msg;
1596-
let should_break;
1597-
introduce_suggestion.push(match missing {
1602+
let suggest_new =
1603+
|err: &mut DiagnosticBuilder<'_>, sugg: &str| {
1604+
err.span_label(span, "expected named lifetime parameter");
1605+
1606+
for missing in self.missing_named_lifetime_spots.iter().rev() {
1607+
let mut introduce_suggestion = vec![];
1608+
let msg;
1609+
let should_break;
1610+
introduce_suggestion.push(match missing {
15981611
MissingLifetimeSpot::Generics(generics) => {
15991612
msg = "consider introducing a named lifetime parameter".to_string();
16001613
should_break = true;
1601-
match &generics.params {
1602-
[] => (generics.span, "<'a>".to_string()),
1603-
[param, ..] => (param.span.shrink_to_lo(), "'a, ".to_string()),
1614+
if let Some(param) = generics.params.iter().filter(|p| match p.kind {
1615+
hir::GenericParamKind::Type {
1616+
synthetic: Some(hir::SyntheticTyParamKind::ImplTrait),
1617+
..
1618+
} => false,
1619+
_ => true,
1620+
}).next() {
1621+
(param.span.shrink_to_lo(), "'a, ".to_string())
1622+
} else {
1623+
(generics.span, "<'a>".to_string())
16041624
}
16051625
}
16061626
MissingLifetimeSpot::HigherRanked { span, span_type } => {
@@ -1616,29 +1636,30 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
16161636
(*span, span_type.suggestion("'a"))
16171637
}
16181638
});
1619-
for param in params {
1620-
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(param.span)
1621-
{
1622-
if snippet.starts_with("&") && !snippet.starts_with("&'") {
1623-
introduce_suggestion
1624-
.push((param.span, format!("&'a {}", &snippet[1..])));
1625-
} else if snippet.starts_with("&'_ ") {
1626-
introduce_suggestion
1627-
.push((param.span, format!("&'a {}", &snippet[4..])));
1639+
for param in params {
1640+
if let Ok(snippet) =
1641+
self.tcx.sess.source_map().span_to_snippet(param.span)
1642+
{
1643+
if snippet.starts_with("&") && !snippet.starts_with("&'") {
1644+
introduce_suggestion
1645+
.push((param.span, format!("&'a {}", &snippet[1..])));
1646+
} else if snippet.starts_with("&'_ ") {
1647+
introduce_suggestion
1648+
.push((param.span, format!("&'a {}", &snippet[4..])));
1649+
}
16281650
}
16291651
}
1652+
introduce_suggestion.push((span, sugg.to_string()));
1653+
err.multipart_suggestion(
1654+
&msg,
1655+
introduce_suggestion,
1656+
Applicability::MaybeIncorrect,
1657+
);
1658+
if should_break {
1659+
break;
1660+
}
16301661
}
1631-
introduce_suggestion.push((span, sugg.to_string()));
1632-
err.multipart_suggestion(
1633-
&msg,
1634-
introduce_suggestion,
1635-
Applicability::MaybeIncorrect,
1636-
);
1637-
if should_break {
1638-
break;
1639-
}
1640-
}
1641-
};
1662+
};
16421663

16431664
match (
16441665
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)