You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// the compiler thinks that the closure could be returned by by this function// to me, the function signature indicates that the closure and the return value are independentfncalls_closure<'closure,'result>(closure:impl'closure + FnOnce() -> (),) -> impl'result + Iterator<Item = i32>{closure();[2].into_iter()}fncreates_closure<'closure_state,'result>(closure_state:&'closure_state muti32,) -> impl'result + Iterator<Item = i32>{calls_closure(|| {*closure_state += 1;})}
I expected to see this happen: it should compile and run just fine. The lifetimes 'closure and 'result of function calls_closure are not related by any bounds.
Instead, this happened:
error: lifetime may not live long enough
--> src/lib.rs:13:5
|
10 | fn creates_closure<'closure_state, 'result>(
| -------------- ------- lifetime `'result` defined here
| |
| lifetime `'closure_state` defined here
...
13 | / calls_closure(|| {
14 | | *closure_state += 1;
15 | | })
| |______^ function was supposed to return data with lifetime `'result` but it is returning data with lifetime `'closure_state`
|
= help: consider adding the following bound: `'closure_state: 'result`
For some reason, the compiler thinks that calls_closure can return something with a lifetime related to that of the passed closure. But the lifetimes 'closure and 'result are unrelated in its signature.
On nightly, the error message is different, but still seems wrong:
error[E0700]: hidden type for `impl Iterator<Item = i32>` captures lifetime that does not appear in bounds
--> src/lib.rs:13:5
|
10 | fn creates_closure<'closure_state, 'result>(
| -------------- hidden type `impl Iterator<Item = i32>` captures the lifetime `'closure_state` as defined here
...
13 | / calls_closure(|| {
14 | | *closure_state += 1;
15 | | })
| |______^
|
help: to declare that the `impl Trait` captures `'closure_state`, you can add an explicit `'closure_state` lifetime bound
|
12 | ) -> impl 'result + Iterator<Item = i32> + 'closure_state {
| ++++++++++++++++
For more information about this error, try `rustc --explain E0700`.
Meta
rustc --version --verbose:
stable from playground: 1.64.0
nightly from playground: 1.66.0-nightly (2022-10-08 8796e7a)
The text was updated successfully, but these errors were encountered:
(continuing from users.rust-lang.org)
I tried this code:
(playground)
I expected to see this happen: it should compile and run just fine. The lifetimes
'closure
and'result
of functioncalls_closure
are not related by any bounds.Instead, this happened:
For some reason, the compiler thinks that
calls_closure
can return something with a lifetime related to that of the passed closure. But the lifetimes'closure
and'result
are unrelated in its signature.On nightly, the error message is different, but still seems wrong:
Meta
rustc --version --verbose
:stable from playground: 1.64.0
nightly from playground: 1.66.0-nightly (2022-10-08 8796e7a)
The text was updated successfully, but these errors were encountered: