Skip to content
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

Use more accurate spans when proposing adding lifetime to item #87983

Merged
merged 3 commits into from
Aug 19, 2021
Merged
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
86 changes: 76 additions & 10 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2073,20 +2073,85 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
continue;
}
});

struct Lifetime(Span, String);
impl Lifetime {
fn is_unnamed(&self) -> bool {
self.1.starts_with('&') && !self.1.starts_with("&'")
}
fn is_underscore(&self) -> bool {
self.1.starts_with("&'_ ")
}
fn is_named(&self) -> bool {
self.1.starts_with("&'")
}
fn suggestion(&self, sugg: String) -> Option<(Span, String)> {
Some(
match (
self.is_unnamed(),
self.is_underscore(),
self.is_named(),
sugg.starts_with("&"),
) {
(true, _, _, false) => (self.span_unnamed_borrow(), sugg),
(true, _, _, true) => {
(self.span_unnamed_borrow(), sugg[1..].to_string())
}
(_, true, _, false) => {
(self.span_underscore_borrow(), sugg.trim().to_string())
}
(_, true, _, true) => {
(self.span_underscore_borrow(), sugg[1..].trim().to_string())
}
(_, _, true, false) => {
(self.span_named_borrow(), sugg.trim().to_string())
}
(_, _, true, true) => {
(self.span_named_borrow(), sugg[1..].trim().to_string())
}
_ => return None,
},
)
}
fn span_unnamed_borrow(&self) -> Span {
let lo = self.0.lo() + BytePos(1);
self.0.with_lo(lo).with_hi(lo)
}
fn span_named_borrow(&self) -> Span {
let lo = self.0.lo() + BytePos(1);
self.0.with_lo(lo)
}
fn span_underscore_borrow(&self) -> Span {
let lo = self.0.lo() + BytePos(1);
let hi = lo + BytePos(2);
self.0.with_lo(lo).with_hi(hi)
}
}

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("&'_ ") {
estebank marked this conversation as resolved.
Show resolved Hide resolved
introduce_suggestion.push((param.span, format!("&'a {}", &stripped)));
if let Some((span, sugg)) =
Lifetime(param.span, snippet).suggestion("'a ".to_string())
{
introduce_suggestion.push((span, sugg));
}
}
}
for ((span, _), sugg) in spans_with_counts.iter().copied().zip(suggs.iter()) {
if let Some(sugg) = sugg {
introduce_suggestion.push((span, sugg.to_string()));
}
for (span, sugg) in spans_with_counts.iter().copied().zip(suggs.iter()).filter_map(
|((span, _), sugg)| match &sugg {
Some(sugg) => Some((span, sugg.to_string())),
_ => None,
},
) {
let (span, sugg) = self
.tcx
.sess
.source_map()
.span_to_snippet(span)
.ok()
.and_then(|snippet| Lifetime(span, snippet).suggestion(sugg.clone()))
.unwrap_or((span, sugg));
introduce_suggestion.push((span, sugg.to_string()));
}
err.multipart_suggestion_with_style(
&msg,
Expand Down Expand Up @@ -2159,7 +2224,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())),
_ => {}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/error-codes/E0106.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/impl-header-lifetime-elision/assoc-type.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/issues/issue-19707.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-26638.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/issues/issue-30255.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/rfc1623-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/suggestions/fn-missing-lifetime-in-item.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/suggestions/issue-84592.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/suggestions/issue-86667.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/suggestions/missing-lt-for-hrtb.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions src/test/ui/suggestions/return-elided-lifetime.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down