Account for `impl Trait`

Address #49287
This commit is contained in:
Esteban Küber 2020-01-30 11:53:47 -08:00
parent 1beac2b774
commit 96bbb0d67e
3 changed files with 70 additions and 33 deletions

View File

@ -1519,9 +1519,21 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
for missing in &self.missing_named_lifetime_spots {
match missing {
MissingLifetimeSpot::Generics(generics) => {
let (span, sugg) = match &generics.params {
[] => (generics.span, format!("<{}>", lifetime_ref)),
[param, ..] => (param.span.shrink_to_lo(), format!("{}, ", lifetime_ref)),
let (span, sugg) = if let Some(param) = generics
.params
.iter()
.filter(|p| match p.kind {
hir::GenericParamKind::Type {
synthetic: Some(hir::SyntheticTyParamKind::ImplTrait),
..
} => false,
_ => true,
})
.next()
{
(param.span.shrink_to_lo(), format!("{}, ", lifetime_ref))
} else {
(generics.span, format!("<{}>", lifetime_ref))
};
err.span_suggestion(
span,
@ -1592,7 +1604,8 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
Applicability::MaybeIncorrect,
);
};
let suggest_new = |err: &mut DiagnosticBuilder<'_>, sugg: &str| {
let suggest_new =
|err: &mut DiagnosticBuilder<'_>, sugg: &str| {
err.span_label(span, "expected named lifetime parameter");
for missing in self.missing_named_lifetime_spots.iter().rev() {
@ -1603,9 +1616,16 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
MissingLifetimeSpot::Generics(generics) => {
msg = "consider introducing a named lifetime parameter".to_string();
should_break = true;
match &generics.params {
[] => (generics.span, "<'a>".to_string()),
[param, ..] => (param.span.shrink_to_lo(), "'a, ".to_string()),
if let Some(param) = generics.params.iter().filter(|p| match p.kind {
hir::GenericParamKind::Type {
synthetic: Some(hir::SyntheticTyParamKind::ImplTrait),
..
} => false,
_ => true,
}).next() {
(param.span.shrink_to_lo(), "'a, ".to_string())
} else {
(generics.span, "<'a>".to_string())
}
}
MissingLifetimeSpot::HigherRanked { span, span_type } => {
@ -1622,7 +1642,8 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
}
});
for param in params {
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(param.span)
if let Ok(snippet) =
self.tcx.sess.source_map().span_to_snippet(param.span)
{
if snippet.starts_with("&") && !snippet.starts_with("&'") {
introduce_suggestion

View File

@ -0,0 +1,2 @@
fn f(_: impl Iterator<Item = &'_ ()>) {} //~ ERROR missing lifetime specifier
fn main() {}

View File

@ -0,0 +1,14 @@
error[E0106]: missing lifetime specifier
--> $DIR/impl-trait-missing-lifetime.rs:1:31
|
LL | fn f(_: impl Iterator<Item = &'_ ()>) {}
| ^^ expected named lifetime parameter
|
help: consider introducing a named lifetime parameter
|
LL | fn f<'a>(_: impl Iterator<Item = &'a ()>) {}
| ^^^^ ^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0106`.