Auto merge of #105805 - yanchen4791:issue-105227-fix, r=estebank
Suggest adding named lifetime when the return contains value borrowed from more than one lifetimes of function inputs fix for #105227. The problem: The suggestion of adding an explicit `'_` lifetime bound is **incorrect** when the function's return type contains a value which could be borrowed from more than one lifetimes of the function's inputs. Instead, a named lifetime parameter can be introduced in such a case. The solution: Checking the number of elided lifetimes in the function signature. If more than one lifetimes found in the function inputs when the suggestion of adding explicit `'_` lifetime, change it to using named lifetime parameter `'a` instead.
This commit is contained in:
commit
7bbbaabbb6
8 changed files with 184 additions and 40 deletions
|
|
@ -6,12 +6,10 @@ impl A {
|
|||
fn iter_values_anon(&self) -> impl Iterator<Item=u32> {
|
||||
self.x.iter().map(|a| a.0)
|
||||
//~^ ERROR: captures lifetime that does not appear in bounds
|
||||
//~| ERROR: captures lifetime that does not appear in bounds
|
||||
}
|
||||
fn iter_values<'a>(&'a self) -> impl Iterator<Item=u32> {
|
||||
self.x.iter().map(|a| a.0)
|
||||
//~^ ERROR: captures lifetime that does not appear in bounds
|
||||
//~| ERROR: captures lifetime that does not appear in bounds
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -12,23 +12,10 @@ LL | fn iter_values_anon(&self) -> impl Iterator<Item=u32> + '_ {
|
|||
| ++++
|
||||
|
||||
error[E0700]: hidden type for `impl Iterator<Item = u32>` captures lifetime that does not appear in bounds
|
||||
--> $DIR/static-return-lifetime-infered.rs:7:9
|
||||
|
|
||||
LL | fn iter_values_anon(&self) -> impl Iterator<Item=u32> {
|
||||
| ----- hidden type `Map<std::slice::Iter<'_, (u32, u32)>, [closure@$DIR/static-return-lifetime-infered.rs:7:27: 7:30]>` captures the anonymous lifetime defined here
|
||||
LL | self.x.iter().map(|a| a.0)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
help: to declare that `impl Iterator<Item = u32>` captures `'_`, you can add an explicit `'_` lifetime bound
|
||||
|
|
||||
LL | fn iter_values_anon(&self) -> impl Iterator<Item=u32> + '_ {
|
||||
| ++++
|
||||
|
||||
error[E0700]: hidden type for `impl Iterator<Item = u32>` captures lifetime that does not appear in bounds
|
||||
--> $DIR/static-return-lifetime-infered.rs:12:9
|
||||
--> $DIR/static-return-lifetime-infered.rs:11:9
|
||||
|
|
||||
LL | fn iter_values<'a>(&'a self) -> impl Iterator<Item=u32> {
|
||||
| -- hidden type `Map<std::slice::Iter<'a, (u32, u32)>, [closure@$DIR/static-return-lifetime-infered.rs:12:27: 12:30]>` captures the lifetime `'a` as defined here
|
||||
| -- hidden type `Map<std::slice::Iter<'a, (u32, u32)>, [closure@$DIR/static-return-lifetime-infered.rs:11:27: 11:30]>` captures the lifetime `'a` as defined here
|
||||
LL | self.x.iter().map(|a| a.0)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
|
|
@ -37,19 +24,6 @@ help: to declare that `impl Iterator<Item = u32>` captures `'a`, you can add an
|
|||
LL | fn iter_values<'a>(&'a self) -> impl Iterator<Item=u32> + 'a {
|
||||
| ++++
|
||||
|
||||
error[E0700]: hidden type for `impl Iterator<Item = u32>` captures lifetime that does not appear in bounds
|
||||
--> $DIR/static-return-lifetime-infered.rs:12:9
|
||||
|
|
||||
LL | fn iter_values<'a>(&'a self) -> impl Iterator<Item=u32> {
|
||||
| -- hidden type `Map<std::slice::Iter<'a, (u32, u32)>, [closure@$DIR/static-return-lifetime-infered.rs:12:27: 12:30]>` captures the lifetime `'a` as defined here
|
||||
LL | self.x.iter().map(|a| a.0)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
help: to declare that `impl Iterator<Item = u32>` captures `'a`, you can add an explicit `'a` lifetime bound
|
||||
|
|
||||
LL | fn iter_values<'a>(&'a self) -> impl Iterator<Item=u32> + 'a {
|
||||
| ++++
|
||||
|
||||
error: aborting due to 4 previous errors
|
||||
error: aborting due to 2 previous errors
|
||||
|
||||
For more information about this error, try `rustc --explain E0700`.
|
||||
|
|
|
|||
26
src/test/ui/lifetimes/issue-105227.fixed
Normal file
26
src/test/ui/lifetimes/issue-105227.fixed
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
// Regression test for issue #105227.
|
||||
|
||||
// run-rustfix
|
||||
#![allow(warnings)]
|
||||
fn chars0<'a>(v :(&'a str, &'a str)) -> impl Iterator<Item = char> + 'a {
|
||||
//~^ HELP to declare that `impl Iterator<Item = char>` captures `'_`, you can introduce a named lifetime parameter `'a`
|
||||
v.0.chars().chain(v.1.chars())
|
||||
//~^ ERROR hidden type for `impl Iterator<Item = char>` captures lifetime that does not appear in bounds
|
||||
}
|
||||
|
||||
fn chars1<'a>(v0 : &'a str, v1 : &'a str) -> impl Iterator<Item = char> + 'a {
|
||||
//~^ HELP to declare that `impl Iterator<Item = char>` captures `'_`, you can introduce a named lifetime parameter `'a`
|
||||
v0.chars().chain(v1.chars())
|
||||
//~^ ERROR hidden type for `impl Iterator<Item = char>` captures lifetime that does not appear in bound
|
||||
}
|
||||
|
||||
fn chars2<'b>(v0 : &'b str, v1 : &'b str, v2 : &'b str) ->
|
||||
//~^ HELP to declare that `impl Iterator<Item = char>` captures `'_`, you can use the named lifetime parameter `'b`
|
||||
(impl Iterator<Item = char> + 'b , &'b str)
|
||||
{
|
||||
(v0.chars().chain(v1.chars()), v2)
|
||||
//~^ ERROR hidden type for `impl Iterator<Item = char>` captures lifetime that does not appear in bound
|
||||
}
|
||||
|
||||
fn main() {
|
||||
}
|
||||
26
src/test/ui/lifetimes/issue-105227.rs
Normal file
26
src/test/ui/lifetimes/issue-105227.rs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
// Regression test for issue #105227.
|
||||
|
||||
// run-rustfix
|
||||
#![allow(warnings)]
|
||||
fn chars0(v :(& str, &str)) -> impl Iterator<Item = char> {
|
||||
//~^ HELP to declare that `impl Iterator<Item = char>` captures `'_`, you can introduce a named lifetime parameter `'a`
|
||||
v.0.chars().chain(v.1.chars())
|
||||
//~^ ERROR hidden type for `impl Iterator<Item = char>` captures lifetime that does not appear in bounds
|
||||
}
|
||||
|
||||
fn chars1(v0 : & str, v1 : &str) -> impl Iterator<Item = char> {
|
||||
//~^ HELP to declare that `impl Iterator<Item = char>` captures `'_`, you can introduce a named lifetime parameter `'a`
|
||||
v0.chars().chain(v1.chars())
|
||||
//~^ ERROR hidden type for `impl Iterator<Item = char>` captures lifetime that does not appear in bound
|
||||
}
|
||||
|
||||
fn chars2<'b>(v0 : &str, v1 : &'_ str, v2 : &'b str) ->
|
||||
//~^ HELP to declare that `impl Iterator<Item = char>` captures `'_`, you can use the named lifetime parameter `'b`
|
||||
(impl Iterator<Item = char>, &'b str)
|
||||
{
|
||||
(v0.chars().chain(v1.chars()), v2)
|
||||
//~^ ERROR hidden type for `impl Iterator<Item = char>` captures lifetime that does not appear in bound
|
||||
}
|
||||
|
||||
fn main() {
|
||||
}
|
||||
47
src/test/ui/lifetimes/issue-105227.stderr
Normal file
47
src/test/ui/lifetimes/issue-105227.stderr
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
error[E0700]: hidden type for `impl Iterator<Item = char>` captures lifetime that does not appear in bounds
|
||||
--> $DIR/issue-105227.rs:7:5
|
||||
|
|
||||
LL | fn chars0(v :(& str, &str)) -> impl Iterator<Item = char> {
|
||||
| ----- hidden type `std::iter::Chain<Chars<'_>, Chars<'_>>` captures the anonymous lifetime defined here
|
||||
LL |
|
||||
LL | v.0.chars().chain(v.1.chars())
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
help: to declare that `impl Iterator<Item = char>` captures `'_`, you can introduce a named lifetime parameter `'a`
|
||||
|
|
||||
LL | fn chars0<'a>(v :(&'a str, &'a str)) -> impl Iterator<Item = char> + 'a {
|
||||
| ++++ ++ ++ ++++
|
||||
|
||||
error[E0700]: hidden type for `impl Iterator<Item = char>` captures lifetime that does not appear in bounds
|
||||
--> $DIR/issue-105227.rs:13:5
|
||||
|
|
||||
LL | fn chars1(v0 : & str, v1 : &str) -> impl Iterator<Item = char> {
|
||||
| ----- hidden type `std::iter::Chain<Chars<'_>, Chars<'_>>` captures the anonymous lifetime defined here
|
||||
LL |
|
||||
LL | v0.chars().chain(v1.chars())
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
help: to declare that `impl Iterator<Item = char>` captures `'_`, you can introduce a named lifetime parameter `'a`
|
||||
|
|
||||
LL | fn chars1<'a>(v0 : &'a str, v1 : &'a str) -> impl Iterator<Item = char> + 'a {
|
||||
| ++++ ++ ++ ++++
|
||||
|
||||
error[E0700]: hidden type for `impl Iterator<Item = char>` captures lifetime that does not appear in bounds
|
||||
--> $DIR/issue-105227.rs:21:5
|
||||
|
|
||||
LL | fn chars2<'b>(v0 : &str, v1 : &'_ str, v2 : &'b str) ->
|
||||
| ---- hidden type `std::iter::Chain<Chars<'_>, Chars<'_>>` captures the anonymous lifetime defined here
|
||||
...
|
||||
LL | (v0.chars().chain(v1.chars()), v2)
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
help: to declare that `impl Iterator<Item = char>` captures `'_`, you can use the named lifetime parameter `'b`
|
||||
|
|
||||
LL ~ fn chars2<'b>(v0 : &'b str, v1 : &'b str, v2 : &'b str) ->
|
||||
LL |
|
||||
LL ~ (impl Iterator<Item = char> + 'b , &'b str)
|
||||
|
|
||||
|
||||
error: aborting due to 3 previous errors
|
||||
|
||||
For more information about this error, try `rustc --explain E0700`.
|
||||
Loading…
Add table
Add a link
Reference in a new issue