Suggest 'r instead of 'lifetime

This commit is contained in:
Esteban Küber 2020-01-27 12:49:12 -08:00
parent 7e1464336a
commit fa4594196d
19 changed files with 120 additions and 59 deletions

View File

@ -1498,8 +1498,8 @@ crate fn add_missing_lifetime_specifiers_label(
msg = "consider introducing a named lifetime parameter";
should_break = true;
match &generics.params {
[] => (generics.span, "<'lifetime>".to_string()),
[param, ..] => (param.span.shrink_to_lo(), "'lifetime, ".to_string()),
[] => (generics.span, "<'r>".to_string()),
[param, ..] => (param.span.shrink_to_lo(), "'r, ".to_string()),
}
}
MissingLifetimeSpot::HRLT { span, span_type } => {
@ -1510,8 +1510,8 @@ crate fn add_missing_lifetime_specifiers_label(
https://doc.rust-lang.org/nomicon/hrtb.html",
);
let suggestion = match span_type {
HRLTSpanType::Empty => "for<'lifetime> ",
HRLTSpanType::Tail => ", 'lifetime",
HRLTSpanType::Empty => "for<'r> ",
HRLTSpanType::Tail => ", 'r",
};
(*span, suggestion.to_string())
}
@ -1520,7 +1520,7 @@ crate fn add_missing_lifetime_specifiers_label(
if let Ok(snippet) = source_map.span_to_snippet(param.span) {
if snippet.starts_with("&") && !snippet.starts_with("&'") {
introduce_suggestion
.push((param.span, format!("&'lifetime {}", &snippet[1..])));
.push((param.span, format!("&'r {}", &snippet[1..])));
}
}
}
@ -1543,13 +1543,13 @@ crate fn add_missing_lifetime_specifiers_label(
suggest_existing(err, format!("{}<{}>", snippet, name));
}
(0, _, Some("&")) => {
suggest_new(err, "&'lifetime ");
suggest_new(err, "&'r ");
}
(0, _, Some("'_")) => {
suggest_new(err, "'lifetime");
suggest_new(err, "'r");
}
(0, _, Some(snippet)) if !snippet.ends_with(">") => {
suggest_new(err, &format!("{}<'lifetime>", snippet));
suggest_new(err, &format!("{}<'r>", snippet));
}
_ => {
err.span_label(span, "expected lifetime parameter");

View File

@ -6,8 +6,8 @@ LL | x: &bool,
|
help: consider introducing a named lifetime parameter
|
LL | struct Foo<'lifetime> {
LL | x: &'lifetime bool,
LL | struct Foo<'r> {
LL | x: &'r bool,
|
error[E0106]: missing lifetime specifier
@ -18,9 +18,9 @@ LL | B(&bool),
|
help: consider introducing a named lifetime parameter
|
LL | enum Bar<'lifetime> {
LL | enum Bar<'r> {
LL | A(u8),
LL | B(&'lifetime bool),
LL | B(&'r bool),
|
error[E0106]: missing lifetime specifier
@ -31,8 +31,8 @@ LL | type MyStr = &str;
|
help: consider introducing a named lifetime parameter
|
LL | type MyStr<'lifetime> = &'lifetime str;
| ^^^^^^^^^^^ ^^^^^^^^^^
LL | type MyStr<'r> = &'r str;
| ^^^^ ^^^
error[E0106]: missing lifetime specifier
--> $DIR/E0106.rs:17:10
@ -42,8 +42,8 @@ LL | baz: Baz,
|
help: consider introducing a named lifetime parameter
|
LL | struct Quux<'lifetime> {
LL | baz: Baz<'lifetime>,
LL | struct Quux<'r> {
LL | baz: Baz<'r>,
|
error[E0106]: missing lifetime specifiers

View File

@ -6,8 +6,8 @@ LL | type Output = &i32;
|
help: consider introducing a named lifetime parameter
|
LL | type Output<'lifetime> = &'lifetime i32;
| ^^^^^^^^^^^ ^^^^^^^^^^
LL | type Output<'r> = &'r i32;
| ^^^^ ^^^
error[E0106]: missing lifetime specifier
--> $DIR/assoc-type.rs:16:20
@ -17,8 +17,8 @@ LL | type Output = &'_ i32;
|
help: consider introducing a named lifetime parameter
|
LL | type Output<'lifetime> = &'lifetime i32;
| ^^^^^^^^^^^ ^^^^^^^^^
LL | type Output<'r> = &'r i32;
| ^^^^ ^^
error: aborting due to 2 previous errors

View File

@ -6,8 +6,8 @@ LL | struct Heartbreak(Betrayal);
|
help: consider introducing a named lifetime parameter
|
LL | struct Heartbreak<'lifetime>(Betrayal<'lifetime>);
| ^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^
LL | struct Heartbreak<'r>(Betrayal<'r>);
| ^^^^ ^^^^^^^^^^^^
error: aborting due to previous error

View File

@ -11,8 +11,8 @@ LL | type Foo = fn(&u8, &u8) -> &u8;
| ^^^ ^^^
help: consider introducing a named lifetime parameter
|
LL | type Foo<'lifetime> = fn(&'lifetime u8, &'lifetime u8) -> &'lifetime u8;
| ^^^^^^^^^^^ ^^^^^^^^^^^^^ ^^^^^^^^^^^^^ ^^^^^^^^^^
LL | type Foo<'r> = fn(&'r u8, &'r u8) -> &'r u8;
| ^^^^ ^^^^^^ ^^^^^^ ^^^
error[E0106]: missing lifetime specifier
--> $DIR/issue-19707.rs:5:27
@ -28,12 +28,12 @@ LL | fn bar<F: Fn(&u8, &u8) -> &u8>(f: &F) {}
= note: for more information on Higher-Ranked lifetimes, visit https://doc.rust-lang.org/nomicon/hrtb.html
help: consider introducing a Higher-Ranked lifetime
|
LL | fn bar<F: for<'lifetime> Fn(&'lifetime u8, &'lifetime u8) -> &'lifetime u8>(f: &F) {}
| ^^^^^^^^^^^^^^ ^^^^^^^^^^^^^ ^^^^^^^^^^^^^ ^^^^^^^^^^
LL | fn bar<F: for<'r> Fn(&'r u8, &'r u8) -> &'r u8>(f: &F) {}
| ^^^^^^^ ^^^^^^ ^^^^^^ ^^^
help: consider introducing a named lifetime parameter
|
LL | fn bar<'lifetime, F: Fn(&'lifetime u8, &'lifetime u8) -> &'lifetime u8>(f: &F) {}
| ^^^^^^^^^^ ^^^^^^^^^^^^^ ^^^^^^^^^^^^^ ^^^^^^^^^^
LL | fn bar<'r, F: Fn(&'r u8, &'r u8) -> &'r u8>(f: &F) {}
| ^^^ ^^^^^^ ^^^^^^ ^^^
error: aborting due to 2 previous errors

View File

@ -11,8 +11,8 @@ LL | fn parse_type(iter: Box<dyn Iterator<Item=&str>+'static>) -> &str { iter.ne
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: consider introducing a named lifetime parameter
|
LL | fn parse_type<'lifetime>(iter: Box<dyn Iterator<Item=&str>+'static>) -> &'lifetime str { iter.next() }
| ^^^^^^^^^^^ ^^^^^^^^^^
LL | fn parse_type<'r>(iter: Box<dyn Iterator<Item=&str>+'static>) -> &'r str { iter.next() }
| ^^^^ ^^^
error[E0106]: missing lifetime specifier
--> $DIR/issue-26638.rs:4:40

View File

@ -11,8 +11,8 @@ LL | fn f(a: &S, b: i32) -> &i32 {
| ^^
help: consider introducing a named lifetime parameter
|
LL | fn f<'lifetime>(a: &'lifetime S, b: i32) -> &'lifetime i32 {
| ^^^^^^^^^^^ ^^^^^^^^^^^^ ^^^^^^^^^^
LL | fn f<'r>(a: &'r S, b: i32) -> &'r i32 {
| ^^^^ ^^^^^ ^^^
error[E0106]: missing lifetime specifier
--> $DIR/issue-30255.rs:14:34
@ -27,8 +27,8 @@ LL | fn g(a: &S, b: bool, c: &i32) -> &i32 {
| ^^ ^^^^
help: consider introducing a named lifetime parameter
|
LL | fn g<'lifetime>(a: &'lifetime S, b: bool, c: &'lifetime i32) -> &'lifetime i32 {
| ^^^^^^^^^^^ ^^^^^^^^^^^^ ^^^^^^^^^^^^^^ ^^^^^^^^^^
LL | fn g<'r>(a: &'r S, b: bool, c: &'r i32) -> &'r i32 {
| ^^^^ ^^^^^ ^^^^^^^ ^^^
error[E0106]: missing lifetime specifier
--> $DIR/issue-30255.rs:19:44
@ -43,8 +43,8 @@ LL | fn h(a: &bool, b: bool, c: &S, d: &i32) -> &i32 {
| ^^^^^ ^^ ^^^^
help: consider introducing a named lifetime parameter
|
LL | fn h<'lifetime>(a: &'lifetime bool, b: bool, c: &'lifetime S, d: &'lifetime i32) -> &'lifetime i32 {
| ^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^ ^^^^^^^^^^^^^^ ^^^^^^^^^^
LL | fn h<'r>(a: &'r bool, b: bool, c: &'r S, d: &'r i32) -> &'r i32 {
| ^^^^ ^^^^^^^^ ^^^^^ ^^^^^^^ ^^^
error: aborting due to 3 previous errors

View File

@ -19,8 +19,8 @@ LL | fn g(_x: &isize, _y: &isize) -> &isize {
| ^^^^^^ ^^^^^^
help: consider introducing a named lifetime parameter
|
LL | fn g<'lifetime>(_x: &'lifetime isize, _y: &'lifetime isize) -> &'lifetime isize {
| ^^^^^^^^^^^ ^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^ ^^^^^^^^^^
LL | fn g<'r>(_x: &'r isize, _y: &'r isize) -> &'r isize {
| ^^^^ ^^^^^^^^^ ^^^^^^^^^ ^^^
error[E0106]: missing lifetime specifier
--> $DIR/lifetime-elision-return-type-requires-explicit-lifetime.rs:17:19
@ -35,8 +35,8 @@ LL | fn h(_x: &Foo) -> &isize {
| ^^^^
help: consider introducing a named lifetime parameter
|
LL | fn h<'lifetime>(_x: &'lifetime Foo) -> &'lifetime isize {
| ^^^^^^^^^^^ ^^^^^^^^^^^^^^ ^^^^^^^^^^
LL | fn h<'r>(_x: &'r Foo) -> &'r isize {
| ^^^^ ^^^^^^^ ^^^
error[E0106]: missing lifetime specifier
--> $DIR/lifetime-elision-return-type-requires-explicit-lifetime.rs:21:20

View File

@ -11,8 +11,8 @@ LL | fn foo(x: &i32, y: &i32) -> &i32 {
| ^^^^ ^^^^
help: consider introducing a named lifetime parameter
|
LL | fn foo<'lifetime>(x: &'lifetime i32, y: &'lifetime i32) -> &'lifetime i32 {
| ^^^^^^^^^^^ ^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^ ^^^^^^^^^^
LL | fn foo<'r>(x: &'r i32, y: &'r i32) -> &'r i32 {
| ^^^^ ^^^^^^^ ^^^^^^^ ^^^
error: aborting due to previous error

View File

@ -6,8 +6,8 @@ LL | a: &u64
|
help: consider introducing a named lifetime parameter
|
LL | struct A<'lifetime> {
LL | a: &'lifetime u64
LL | struct A<'r> {
LL | a: &'r u64
|
error: aborting due to previous error

View File

@ -6,8 +6,8 @@ LL | Bar(&isize)
|
help: consider introducing a named lifetime parameter
|
LL | enum Foo<'lifetime> {
LL | Bar(&'lifetime isize)
LL | enum Foo<'r> {
LL | Bar(&'r isize)
|
error: aborting due to previous error

View File

@ -6,8 +6,8 @@ LL | x: &isize
|
help: consider introducing a named lifetime parameter
|
LL | struct Foo<'lifetime> {
LL | x: &'lifetime isize
LL | struct Foo<'r> {
LL | x: &'r isize
|
error: aborting due to previous error

View File

@ -0,0 +1,8 @@
struct S1<F: Fn(&i32, &i32) -> &'a i32>(F); //~ ERROR use of undeclared lifetime name `'a`
struct S2<F: Fn(&i32, &i32) -> &i32>(F); //~ ERROR missing lifetime specifier
struct S3<F: for<'a> Fn(&i32, &i32) -> &'a i32>(F);
//~^ ERROR binding for associated type `Output` references lifetime `'a`, which does not appear
struct S4<F: for<'x> Fn(&'x i32, &'x i32) -> &'x i32>(F);
const C: Option<Box<dyn for<'a> Fn(&usize, &usize) -> &'a usize>> = None;
//~^ ERROR binding for associated type `Output` references lifetime `'a`, which does not appear
fn main() {}

View File

@ -0,0 +1,53 @@
error[E0261]: use of undeclared lifetime name `'a`
--> $DIR/fn-missing-lifetime-in-item.rs:1:33
|
LL | struct S1<F: Fn(&i32, &i32) -> &'a i32>(F);
| ^^ undeclared lifetime
|
= note: for more information on Higher-Ranked lifetimes, visit https://doc.rust-lang.org/nomicon/hrtb.html
help: consider introducing lifetime `'a` here
|
LL | struct S1<'a, F: Fn(&i32, &i32) -> &'a i32>(F);
| ^^^
help: consider introducing a Higher-Ranked lifetime `'a` here
|
LL | struct S1<F: for<'a> Fn(&i32, &i32) -> &'a i32>(F);
| ^^^^^^^
error[E0106]: missing lifetime specifier
--> $DIR/fn-missing-lifetime-in-item.rs:2:32
|
LL | struct S2<F: Fn(&i32, &i32) -> &i32>(F);
| ^ 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 argument 1 or argument 2
--> $DIR/fn-missing-lifetime-in-item.rs:2:17
|
LL | struct S2<F: Fn(&i32, &i32) -> &i32>(F);
| ^^^^ ^^^^
= note: for more information on Higher-Ranked lifetimes, visit https://doc.rust-lang.org/nomicon/hrtb.html
help: consider introducing a Higher-Ranked lifetime
|
LL | struct S2<F: for<'r> Fn(&'r i32, &'r i32) -> &'r i32>(F);
| ^^^^^^^ ^^^^^^^ ^^^^^^^ ^^^
help: consider introducing a named lifetime parameter
|
LL | struct S2<'r, F: Fn(&'r i32, &'r i32) -> &'r i32>(F);
| ^^^ ^^^^^^^ ^^^^^^^ ^^^
error[E0582]: binding for associated type `Output` references lifetime `'a`, which does not appear in the trait input types
--> $DIR/fn-missing-lifetime-in-item.rs:3:40
|
LL | struct S3<F: for<'a> Fn(&i32, &i32) -> &'a i32>(F);
| ^^^^^^^
error[E0582]: binding for associated type `Output` references lifetime `'a`, which does not appear in the trait input types
--> $DIR/fn-missing-lifetime-in-item.rs:6:55
|
LL | const C: Option<Box<dyn for<'a> Fn(&usize, &usize) -> &'a usize>> = None;
| ^^^^^^^^^
error: aborting due to 4 previous errors
Some errors have detailed explanations: E0106, E0261, E0582.
For more information about an error, try `rustc --explain E0106`.

View File

@ -11,7 +11,7 @@ LL | let _: dyn Foo(&isize, &usize) -> &usize;
| ^^^^^^ ^^^^^^
help: consider introducing a named lifetime parameter
|
LL | fn main<'lifetime>() {
LL | fn main<'r>() {
LL | eq::< dyn for<'a> Foo<(&'a isize,), Output=&'a isize>,
LL | dyn Foo(&isize) -> &isize >();
LL | eq::< dyn for<'a> Foo<(&'a isize,), Output=(&'a isize, &'a isize)>,

View File

@ -6,8 +6,8 @@ LL | x: Box<dyn Debug + '_>,
|
help: consider introducing a named lifetime parameter
|
LL | struct Foo<'lifetime> {
LL | x: Box<dyn Debug + 'lifetime>,
LL | struct Foo<'r> {
LL | x: Box<dyn Debug + 'r>,
|
error[E0228]: the lifetime bound for this object type cannot be deduced from context; please supply an explicit bound

View File

@ -11,8 +11,8 @@ LL | fn foo(x: &u32, y: &u32) -> &'_ u32 { loop { } }
| ^^^^ ^^^^
help: consider introducing a named lifetime parameter
|
LL | fn foo<'lifetime>(x: &'lifetime u32, y: &'lifetime u32) -> &'lifetime u32 { loop { } }
| ^^^^^^^^^^^ ^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^ ^^^^^^^^^
LL | fn foo<'r>(x: &'r u32, y: &'r u32) -> &'r u32 { loop { } }
| ^^^^ ^^^^^^^ ^^^^^^^ ^^
error: aborting due to previous error

View File

@ -6,8 +6,8 @@ LL | x: &'_ u32,
|
help: consider introducing a named lifetime parameter
|
LL | struct Foo<'lifetime> {
LL | x: &'lifetime u32,
LL | struct Foo<'r> {
LL | x: &'r u32,
|
error[E0106]: missing lifetime specifier
@ -18,8 +18,8 @@ LL | Variant(&'_ u32),
|
help: consider introducing a named lifetime parameter
|
LL | enum Bar<'lifetime> {
LL | Variant(&'lifetime u32),
LL | enum Bar<'r> {
LL | Variant(&'r u32),
|
error: aborting due to 2 previous errors

View File

@ -37,8 +37,8 @@ LL | fn foo2(_: &'_ u8, y: &'_ u8) -> &'_ u8 { y }
| ^^^^^^ ^^^^^^
help: consider introducing a named lifetime parameter
|
LL | fn foo2<'lifetime>(_: &'_ u8, y: &'_ u8) -> &'lifetime u8 { y }
| ^^^^^^^^^^^ ^^^^^^^^^
LL | fn foo2<'r>(_: &'_ u8, y: &'_ u8) -> &'r u8 { y }
| ^^^^ ^^
error: aborting due to 5 previous errors