Rollup merge of #86678 - FabianWolff:issue-86667, r=jackh726

Fix garbled suggestion for missing lifetime specifier

This PR fixes #86667. The suggestion code currently checks whether there is a generic parameter that is not a synthetic `impl Trait` parameter and, if so, suggests to insert a new lifetime `'a` before that generic parameter. However, it does not make sense to insert `'a` in front of an elided lifetime parameter, since these are synthetic as well, which leads to the garbled suggestion in #86667.
This commit is contained in:
Yuki Okushi 2021-06-29 08:46:16 +09:00 committed by GitHub
commit a89c6be16e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 0 deletions

View file

@ -1962,6 +1962,8 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
hir::GenericParamKind::Type {
synthetic: Some(hir::SyntheticTyParamKind::ImplTrait),
..
} | hir::GenericParamKind::Lifetime {
kind: hir::LifetimeParamKind::Elided
}
)
}) {

View file

@ -0,0 +1,16 @@
// Regression test for #86667, where a garbled suggestion was issued for
// a missing named lifetime parameter.
// compile-flags: --edition 2018
async fn a(s1: &str, s2: &str) -> &str {
//~^ ERROR: missing lifetime specifier [E0106]
s1
}
fn b(s1: &str, s2: &str) -> &str {
//~^ ERROR: missing lifetime specifier [E0106]
s1
}
fn main() {}

View file

@ -0,0 +1,27 @@
error[E0106]: missing lifetime specifier
--> $DIR/issue-86667.rs:6:35
|
LL | async fn a(s1: &str, s2: &str) -> &str {
| ---- ---- ^ expected named lifetime parameter
|
= help: this function's return type contains a borrowed value, but the signature does not say whether it is borrowed from `s1` or `s2`
help: consider introducing a named lifetime parameter
|
LL | async fn a<'a>(s1: &'a str, s2: &'a str) -> &'a str {
| ^^^^ ^^^^^^^ ^^^^^^^ ^^^
error[E0106]: missing lifetime specifier
--> $DIR/issue-86667.rs:11:29
|
LL | fn b(s1: &str, s2: &str) -> &str {
| ---- ---- ^ expected named lifetime parameter
|
= help: this function's return type contains a borrowed value, but the signature does not say whether it is borrowed from `s1` or `s2`
help: consider introducing a named lifetime parameter
|
LL | fn b<'a>(s1: &'a str, s2: &'a str) -> &'a str {
| ^^^^ ^^^^^^^ ^^^^^^^ ^^^
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0106`.