From 523fe7a121e80b5959bfa6203268a46e9d9e7dd4 Mon Sep 17 00:00:00 2001 From: yanchen4791 Date: Fri, 16 Dec 2022 14:47:24 -0800 Subject: Suggests adding named lifetime when the return contains value borrowed from more than one lifetimes of the function's inputs --- .../impl-trait/static-return-lifetime-infered.rs | 2 - .../static-return-lifetime-infered.stderr | 32 ++------------- src/test/ui/lifetimes/issue-105227.fixed | 26 ++++++++++++ src/test/ui/lifetimes/issue-105227.rs | 26 ++++++++++++ src/test/ui/lifetimes/issue-105227.stderr | 47 ++++++++++++++++++++++ 5 files changed, 102 insertions(+), 31 deletions(-) create mode 100644 src/test/ui/lifetimes/issue-105227.fixed create mode 100644 src/test/ui/lifetimes/issue-105227.rs create mode 100644 src/test/ui/lifetimes/issue-105227.stderr (limited to 'src/test') 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 { 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 { 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 + '_ { | ++++ error[E0700]: hidden type for `impl Iterator` captures lifetime that does not appear in bounds - --> $DIR/static-return-lifetime-infered.rs:7:9 - | -LL | fn iter_values_anon(&self) -> impl Iterator { - | ----- hidden type `Map, [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` captures `'_`, you can add an explicit `'_` lifetime bound - | -LL | fn iter_values_anon(&self) -> impl Iterator + '_ { - | ++++ - -error[E0700]: hidden type for `impl Iterator` 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 { - | -- hidden type `Map, [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` captures `'a`, you can add an explicit `'a` lifetime bound - | -LL | fn iter_values<'a>(&'a self) -> impl Iterator + 'a { - | ++++ - -error[E0700]: hidden type for `impl Iterator` 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 { - | -- hidden type `Map, [closure@$DIR/static-return-lifetime-infered.rs:12:27: 12:30]>` captures the lifetime `'a` as defined here + | -- hidden type `Map, [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` captures `'a`, you can add an LL | fn iter_values<'a>(&'a self) -> impl Iterator + '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 + 'a { +//~^ HELP to declare that `impl Iterator` captures `'_`, you can introduce a named lifetime parameter `'a` + v.0.chars().chain(v.1.chars()) + //~^ ERROR hidden type for `impl Iterator` captures lifetime that does not appear in bounds +} + +fn chars1<'a>(v0 : &'a str, v1 : &'a str) -> impl Iterator + 'a { +//~^ HELP to declare that `impl Iterator` captures `'_`, you can introduce a named lifetime parameter `'a` + v0.chars().chain(v1.chars()) + //~^ ERROR hidden type for `impl Iterator` 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` captures `'_`, you can use the named lifetime parameter `'b` + (impl Iterator + 'b , &'b str) +{ + (v0.chars().chain(v1.chars()), v2) + //~^ ERROR hidden type for `impl Iterator` 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 { +//~^ HELP to declare that `impl Iterator` captures `'_`, you can introduce a named lifetime parameter `'a` + v.0.chars().chain(v.1.chars()) + //~^ ERROR hidden type for `impl Iterator` captures lifetime that does not appear in bounds +} + +fn chars1(v0 : & str, v1 : &str) -> impl Iterator { +//~^ HELP to declare that `impl Iterator` captures `'_`, you can introduce a named lifetime parameter `'a` + v0.chars().chain(v1.chars()) + //~^ ERROR hidden type for `impl Iterator` captures lifetime that does not appear in bound +} + +fn chars2<'b>(v0 : &str, v1 : &'_ str, v2 : &'b str) -> +//~^ HELP to declare that `impl Iterator` captures `'_`, you can use the named lifetime parameter `'b` + (impl Iterator, &'b str) +{ + (v0.chars().chain(v1.chars()), v2) + //~^ ERROR hidden type for `impl Iterator` 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` captures lifetime that does not appear in bounds + --> $DIR/issue-105227.rs:7:5 + | +LL | fn chars0(v :(& str, &str)) -> impl Iterator { + | ----- hidden type `std::iter::Chain, Chars<'_>>` captures the anonymous lifetime defined here +LL | +LL | v.0.chars().chain(v.1.chars()) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: to declare that `impl Iterator` captures `'_`, you can introduce a named lifetime parameter `'a` + | +LL | fn chars0<'a>(v :(&'a str, &'a str)) -> impl Iterator + 'a { + | ++++ ++ ++ ++++ + +error[E0700]: hidden type for `impl Iterator` captures lifetime that does not appear in bounds + --> $DIR/issue-105227.rs:13:5 + | +LL | fn chars1(v0 : & str, v1 : &str) -> impl Iterator { + | ----- hidden type `std::iter::Chain, Chars<'_>>` captures the anonymous lifetime defined here +LL | +LL | v0.chars().chain(v1.chars()) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: to declare that `impl Iterator` captures `'_`, you can introduce a named lifetime parameter `'a` + | +LL | fn chars1<'a>(v0 : &'a str, v1 : &'a str) -> impl Iterator + 'a { + | ++++ ++ ++ ++++ + +error[E0700]: hidden type for `impl Iterator` 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<'_>>` captures the anonymous lifetime defined here +... +LL | (v0.chars().chain(v1.chars()), v2) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: to declare that `impl Iterator` 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 + 'b , &'b str) + | + +error: aborting due to 3 previous errors + +For more information about this error, try `rustc --explain E0700`. -- cgit 1.4.1-3-g733a5