diff options
| author | bors <bors@rust-lang.org> | 2023-01-06 12:08:49 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2023-01-06 12:08:49 +0000 |
| commit | 7bbbaabbb6ec93800409478e2af7bc063701604b (patch) | |
| tree | 62c0e13b4d4bb4c3a3350e9813324ce7ad96ed56 /src | |
| parent | 1146560e1a53d26d04b33548d4eeb8e083d78509 (diff) | |
| parent | 523fe7a121e80b5959bfa6203268a46e9d9e7dd4 (diff) | |
| download | rust-7bbbaabbb6ec93800409478e2af7bc063701604b.tar.gz rust-7bbbaabbb6ec93800409478e2af7bc063701604b.zip | |
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.
Diffstat (limited to 'src')
| -rw-r--r-- | src/test/ui/impl-trait/static-return-lifetime-infered.rs | 2 | ||||
| -rw-r--r-- | src/test/ui/impl-trait/static-return-lifetime-infered.stderr | 32 | ||||
| -rw-r--r-- | src/test/ui/lifetimes/issue-105227.fixed | 26 | ||||
| -rw-r--r-- | src/test/ui/lifetimes/issue-105227.rs | 26 | ||||
| -rw-r--r-- | src/test/ui/lifetimes/issue-105227.stderr | 47 |
5 files changed, 102 insertions, 31 deletions
diff --git a/src/test/ui/impl-trait/static-return-lifetime-infered.rs b/src/test/ui/impl-trait/static-return-lifetime-infered.rs index f940c1949d0..36ef9ea4443 100644 --- a/src/test/ui/impl-trait/static-return-lifetime-infered.rs +++ b/src/test/ui/impl-trait/static-return-lifetime-infered.rs @@ -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 } } diff --git a/src/test/ui/impl-trait/static-return-lifetime-infered.stderr b/src/test/ui/impl-trait/static-return-lifetime-infered.stderr index b365bd88454..c451f8e37c4 100644 --- a/src/test/ui/impl-trait/static-return-lifetime-infered.stderr +++ b/src/test/ui/impl-trait/static-return-lifetime-infered.stderr @@ -12,36 +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 - | -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[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) | ^^^^^^^^^^^^^^^^^^^^^^^^^^ | @@ -50,6 +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: aborting due to 4 previous errors +error: aborting due to 2 previous errors For more information about this error, try `rustc --explain E0700`. diff --git a/src/test/ui/lifetimes/issue-105227.fixed b/src/test/ui/lifetimes/issue-105227.fixed new file mode 100644 index 00000000000..f6ed9c82e91 --- /dev/null +++ b/src/test/ui/lifetimes/issue-105227.fixed @@ -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() { +} diff --git a/src/test/ui/lifetimes/issue-105227.rs b/src/test/ui/lifetimes/issue-105227.rs new file mode 100644 index 00000000000..6427a50bb87 --- /dev/null +++ b/src/test/ui/lifetimes/issue-105227.rs @@ -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() { +} diff --git a/src/test/ui/lifetimes/issue-105227.stderr b/src/test/ui/lifetimes/issue-105227.stderr new file mode 100644 index 00000000000..d2114593735 --- /dev/null +++ b/src/test/ui/lifetimes/issue-105227.stderr @@ -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`. |
