Skip to content

Do not suggest duplicate 'a lifetime #75000

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/librustc_resolve/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1212,7 +1212,7 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
let mut introduce_suggestion = vec![];
let msg;
let should_break;
introduce_suggestion.push(match missing {
let missing_lifetime = match missing {
MissingLifetimeSpot::Generics(generics) => {
msg = "consider introducing a named lifetime parameter".to_string();
should_break = true;
Expand All @@ -1223,9 +1223,14 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
} => false,
_ => true,
}) {
(param.span.shrink_to_lo(), "'a, ".to_string())
if param.name.ident().as_str().contains("'a") {
// Do not add duplicate lifetime.
None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know there's a function somewhere to get the next one letter name. Could we use that instead? On mobile so I can't find it back can look for it tomorrow.

} else {
Some((param.span.shrink_to_lo(), "'a, ".to_string()))
}
} else {
(generics.span, "<'a>".to_string())
Some((generics.span, "<'a>".to_string()))
}
}
MissingLifetimeSpot::HigherRanked { span, span_type } => {
Expand All @@ -1238,9 +1243,12 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
"for more information on higher-ranked polymorphism, visit \
https://doc.rust-lang.org/nomicon/hrtb.html",
);
(*span, span_type.suggestion("'a"))
Some((*span, span_type.suggestion("'a")))
}
});
};
if let Some((span, snip)) = missing_lifetime {
introduce_suggestion.push((span, snip));
}
for param in params {
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(param.span) {
if snippet.starts_with('&') && !snippet.starts_with("&'") {
Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/suggestions/missing-lifetime-with-named-lifetime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
struct X<'a>(&'a ());
struct S<'a>(&'a dyn Fn(&X) -> &X);
//~^ ERROR: missing lifetime specifier
//~| ERROR: missing lifetime specifier

fn main() {
let x = S(&|x| {
x
});
x.0(&X(&()));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
error[E0106]: missing lifetime specifier
--> $DIR/missing-lifetime-with-named-lifetime.rs:2:32
|
LL | struct S<'a>(&'a dyn Fn(&X) -> &X);
| -- ^ expected named lifetime parameter
|
= help: this function's return type contains a borrowed value, but the signature does not say which one of argument 1's 2 lifetimes it is borrowed from
= note: for more information on higher-ranked polymorphism, visit https://doc.rust-lang.org/nomicon/hrtb.html
help: consider making the bound lifetime-generic with a new `'a` lifetime
|
LL | struct S<'a>(&'a dyn for<'a> Fn(&'a X) -> &'a X);
| ^^^^^^^ ^^^^^ ^^^
help: consider introducing a named lifetime parameter
|
LL | struct S<'a>(&'a dyn Fn(&'a X) -> &'a X);
| ^^^^^ ^^^

error[E0106]: missing lifetime specifier
--> $DIR/missing-lifetime-with-named-lifetime.rs:2:33
|
LL | struct S<'a>(&'a dyn Fn(&X) -> &X);
| -- ^ expected named lifetime parameter
|
= help: this function's return type contains a borrowed value, but the signature does not say which one of argument 1's 2 lifetimes it is borrowed from
= note: for more information on higher-ranked polymorphism, visit https://doc.rust-lang.org/nomicon/hrtb.html
help: consider making the bound lifetime-generic with a new `'a` lifetime
|
LL | struct S<'a>(&'a dyn for<'a> Fn(&'a X) -> &X<'a>);
| ^^^^^^^ ^^^^^ ^^^^^
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we also deal with this case and suggest for<'b> instead?

help: consider introducing a named lifetime parameter
|
LL | struct S<'a>(&'a dyn Fn(&'a X) -> &X<'a>);
| ^^^^^ ^^^^^

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0106`.