Use more accurate spans when proposing adding lifetime to item

This commit is contained in:
Esteban Kuber 2021-08-12 18:54:29 +00:00
parent 679dea4cc3
commit 14add46e94
17 changed files with 62 additions and 37 deletions

View File

@ -2076,16 +2076,40 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
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("&'") {
introduce_suggestion
.push((param.span, format!("&'a {}", &snippet[1..])));
} else if let Some(stripped) = snippet.strip_prefix("&'_ ") {
introduce_suggestion.push((param.span, format!("&'a {}", &stripped)));
let lo = param.span.lo() + BytePos(1);
let span = param.span.with_lo(lo).with_hi(lo);
introduce_suggestion.push((span, "'a ".to_string()));
} else if let Some(_) = snippet.strip_prefix("&'_ ") {
let lo = param.span.lo() + BytePos(1);
let hi = lo + BytePos(2);
let span = param.span.with_lo(lo).with_hi(hi);
introduce_suggestion.push((span, "'a".to_string()));
}
}
}
for ((span, _), sugg) in spans_with_counts.iter().copied().zip(suggs.iter()) {
if let Some(sugg) = sugg {
introduce_suggestion.push((span, sugg.to_string()));
match (sugg, self.tcx.sess.source_map().span_to_snippet(span)) {
(Some(sugg), Ok(snippet))
if snippet.starts_with('&')
&& !snippet.starts_with("&'")
&& sugg.starts_with("&") =>
{
let lo = span.lo() + BytePos(1);
let span = span.with_lo(lo).with_hi(lo);
introduce_suggestion.push((span, sugg[1..].to_string()));
}
(Some(sugg), Ok(snippet))
if snippet.starts_with("&'_ ") && sugg.starts_with("&") =>
{
let lo = span.lo() + BytePos(1);
let hi = lo + BytePos(2);
let span = span.with_lo(lo).with_hi(hi);
introduce_suggestion.push((span, sugg[1..].to_string()));
}
(Some(sugg), _) => {
introduce_suggestion.push((span, sugg.to_string()));
}
_ => {}
}
}
err.multipart_suggestion_with_style(
@ -2159,7 +2183,8 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
for ((span, _), snippet) in spans_with_counts.iter().copied().zip(snippets.iter()) {
match snippet.as_deref() {
Some("") => spans_suggs.push((span, "'lifetime, ".to_string())),
Some("&") => spans_suggs.push((span, "&'lifetime ".to_string())),
Some("&") => spans_suggs
.push((span.with_lo(span.lo() + BytePos(1)), "'lifetime ".to_string())),
_ => {}
}
}

View File

@ -59,7 +59,7 @@ LL | type MyStr = &str;
help: consider introducing a named lifetime parameter
|
LL | type MyStr<'a> = &'a str;
| ++++ ~~~
| ++++ ++
error: aborting due to 5 previous errors

View File

@ -15,7 +15,7 @@ LL | type F<T1> = &[u8];
help: consider introducing a named lifetime parameter
|
LL | type F<'a, T1> = &'a [u8];
| +++ ~~~
| +++ ++
error: aborting due to 2 previous errors

View File

@ -7,7 +7,7 @@ LL | type Output = &i32;
help: consider introducing a named lifetime parameter
|
LL | type Output<'a> = &'a i32;
| ++++ ~~~
| ++++ ++
error[E0106]: missing lifetime specifier
--> $DIR/assoc-type.rs:16:20

View File

@ -9,11 +9,11 @@ LL | type Foo = fn(&u8, &u8) -> &u8;
help: consider making the type lifetime-generic with a new `'a` lifetime
|
LL | type Foo = for<'a> fn(&'a u8, &'a u8) -> &'a u8;
| +++++++ ~~~~~~ ~~~~~~ ~~~
| +++++++ ++ ++ ++
help: consider introducing a named lifetime parameter
|
LL | type Foo<'a> = fn(&'a u8, &'a u8) -> &'a u8;
| ++++ ~~~~~~ ~~~~~~ ~~~
| ++++ ++ ++ ++
error[E0106]: missing lifetime specifier
--> $DIR/issue-19707.rs:5:27
@ -26,11 +26,11 @@ LL | fn bar<F: Fn(&u8, &u8) -> &u8>(f: &F) {}
help: consider making the bound lifetime-generic with a new `'a` lifetime
|
LL | fn bar<F: for<'a> Fn(&'a u8, &'a u8) -> &'a u8>(f: &F) {}
| +++++++ ~~~~~~ ~~~~~~ ~~~
| +++++++ ++ ++ ++
help: consider introducing a named lifetime parameter
|
LL | fn bar<'a, F: Fn(&'a u8, &'a u8) -> &'a u8>(f: &F) {}
| +++ ~~~~~~ ~~~~~~ ~~~
| +++ ++ ++ ++
error: aborting due to 2 previous errors

View File

@ -8,7 +8,7 @@ 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<'a>(iter: Box<dyn Iterator<Item=&str>+'static>) -> &'a str { iter.next() }
| ++++ ~~~
| ++++ ++
error[E0106]: missing lifetime specifier
--> $DIR/issue-26638.rs:4:40

View File

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

View File

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

View File

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

View File

@ -9,7 +9,7 @@ LL | static NON_ELIDABLE_FN: &fn(&u8, &u8) -> &u8 =
help: consider making the type lifetime-generic with a new `'a` lifetime
|
LL | static NON_ELIDABLE_FN: &for<'a> fn(&'a u8, &'a u8) -> &'a u8 =
| +++++++ ~~~~~~ ~~~~~~ ~~~
| +++++++ ++ ++ ++
error[E0106]: missing lifetime specifier
--> $DIR/rfc1623-2.rs:10:39
@ -22,7 +22,7 @@ LL | &(non_elidable as fn(&u8, &u8) -> &u8);
help: consider making the type lifetime-generic with a new `'a` lifetime
|
LL | &(non_elidable as for<'a> fn(&'a u8, &'a u8) -> &'a u8);
| +++++++ ~~~~~~ ~~~~~~ ~~~
| +++++++ ++ ++ ++
error: aborting due to 2 previous errors

View File

@ -25,11 +25,11 @@ LL | struct S2<F: Fn(&i32, &i32) -> &i32>(F);
help: consider making the bound lifetime-generic with a new `'a` lifetime
|
LL | struct S2<F: for<'a> Fn(&'a i32, &'a i32) -> &'a i32>(F);
| +++++++ ~~~~~~~ ~~~~~~~ ~~~
| +++++++ ++ ++ ++
help: consider introducing a named lifetime parameter
|
LL | struct S2<'a, F: Fn(&'a i32, &'a 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:3:40

View File

@ -10,7 +10,7 @@ LL | fn two_lifetimes_needed(a: &(), b: &()) -> TwoLifetimes<'_, '_> {
help: consider introducing a named lifetime parameter
|
LL | fn two_lifetimes_needed<'a>(a: &'a (), b: &'a ()) -> TwoLifetimes<'a, 'a> {
| ++++ ~~~~~~ ~~~~~~ ~~ ~~
| ++++ ++ ++ ~~ ~~
error: aborting due to previous error

View File

@ -8,7 +8,7 @@ LL | async fn a(s1: &str, s2: &str) -> &str {
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
@ -20,7 +20,7 @@ LL | fn b(s1: &str, s2: &str) -> &str {
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

View File

@ -47,7 +47,7 @@ LL | struct V<'a>(&'a dyn for<'b> Fn(&X) -> &X);
help: consider using one of the available lifetimes here
|
LL | struct V<'a>(&'a dyn for<'b> Fn(&X) -> &'lifetime X);
| ~~~~~~~~~~
| +++++++++
error[E0106]: missing lifetime specifier
--> $DIR/missing-lt-for-hrtb.rs:5:41

View File

@ -80,7 +80,7 @@ LL | fn f3(s: &S) -> &i32 { loop {} }
help: consider introducing a named lifetime parameter
|
LL | fn f3<'a>(s: &'a S) -> &'a i32 { loop {} }
| ++++ ~~~~~ ~~~
| ++++ ++ ++
error[E0106]: missing lifetime specifier
--> $DIR/return-elided-lifetime.rs:21:26
@ -92,7 +92,7 @@ LL | fn f3_(s: &S, t: &S) -> (&i32, &i32) { loop {} }
help: consider introducing a named lifetime parameter
|
LL | fn f3_<'a>(s: &'a S, t: &'a S) -> (&'a i32, &i32) { loop {} }
| ++++ ~~~~~ ~~~~~ ~~~
| ++++ ++ ++ ++
error[E0106]: missing lifetime specifier
--> $DIR/return-elided-lifetime.rs:21:32
@ -104,7 +104,7 @@ LL | fn f3_(s: &S, t: &S) -> (&i32, &i32) { loop {} }
help: consider introducing a named lifetime parameter
|
LL | fn f3_<'a>(s: &'a S, t: &'a S) -> (&i32, &'a i32) { loop {} }
| ++++ ~~~~~ ~~~~~ ~~~
| ++++ ++ ++ ++
error[E0106]: missing lifetime specifier
--> $DIR/return-elided-lifetime.rs:25:42
@ -121,7 +121,7 @@ LL | fn f4<'a, 'b>(a: &'a i32, b: &'b i32) -> &i32 { loop {} }
help: consider using one of the available lifetimes here
|
LL | fn f4<'a, 'b>(a: &'a i32, b: &'b i32) -> &'lifetime i32 { loop {} }
| ~~~~~~~~~~
| +++++++++
error[E0106]: missing lifetime specifier
--> $DIR/return-elided-lifetime.rs:27:44
@ -138,7 +138,7 @@ LL | fn f4_<'a, 'b>(a: &'a i32, b: &'b i32) -> (&i32, &i32) { loop {} }
help: consider using one of the available lifetimes here
|
LL | fn f4_<'a, 'b>(a: &'a i32, b: &'b i32) -> (&'lifetime i32, &i32) { loop {} }
| ~~~~~~~~~~
| +++++++++
error[E0106]: missing lifetime specifier
--> $DIR/return-elided-lifetime.rs:27:50
@ -155,7 +155,7 @@ LL | fn f4_<'a, 'b>(a: &'a i32, b: &'b i32) -> (&i32, &i32) { loop {} }
help: consider using one of the available lifetimes here
|
LL | fn f4_<'a, 'b>(a: &'a i32, b: &'b i32) -> (&i32, &'lifetime i32) { loop {} }
| ~~~~~~~~~~
| +++++++++
error[E0106]: missing lifetime specifier
--> $DIR/return-elided-lifetime.rs:31:35

View File

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

View File

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