::fix_hidden_lifetime_bug
rust,ignore
error[E0700]: hidden type for `impl Trait` captures lifetime that does not appear in bounds
--> examples/main.rs:13:40
|
13 | fn foo<'a, 'b> (it: &'a mut &'b ()) -> impl 'a + Sized {
| ^^^^^^^^^^^^^^^
|
note: hidden type `&'a mut &'b ()` captures the lifetime `'b` as defined on the function body at 13:12
--> examples/main.rs:13:12
|
13 | fn foo<'a, 'b> (it: &'a mut &'b ()) -> impl 'a + Sized {
| ^^
Problematic code
rust,compile_fail
fn foo<'a, 'b> (it: &'a mut &'b ()) -> impl 'a + Sized {
it
}
rust,ignore
error[E0700]: hidden type for `impl Trait` captures lifetime that does not appear in bounds
--> examples/main.rs:8:45
|
8 | async fn bar<'a> (_: &(), _: Box<dyn Send>) {
| ^
|
note: hidden type `impl Future` captures lifetime smaller than the function body
--> examples/main.rs:8:45
|
8 | async fn bar<'a> (_: &(), _: Box<dyn Send>) {
| ^
Problematic code
rust,compile_fail
async fn bar<'a> (_: &(), _: Box<dyn Send>) {
/* … */
}
rust,ignore
error[E0700]: hidden type for `impl Trait` captures lifetime that does not appear in bounds
--> examples/main.rs:4:57
|
4 | async fn baz<'a> (a: &'static (), b: &'_ (), c: &'_ ()) {
| ^
|
note: hidden type `impl Future` captures lifetime smaller than the function body
--> examples/main.rs:4:57
|
4 | async fn baz<'a> (a: &'static (), b: &'_ (), c: &'_ ()) {
| ^
Problematic code
rust,compile_fail
async fn baz<'a> (a: &'static (), b: &'_ (), c: &'_ ()) {
/* … */
}
Then you can can the attribute provided by this crate to automagically generate an equivalent signature that soothes this grumpy compiler
See [the lifetime bug async
issue], as well as this other comment for
more info.
The fix is thus to perform the unsugaring from an async fn
to an fn
yielding a Future
, and then just adding the necessary + Captures<'_>
bounds.
cargo add fix_hidden_lifetime_bug
, or add the following to your Cargo.toml
file:
toml
[dependencies]
fix_hidden_lifetime_bug = "x.y.z"
cargo search fix_hidden_lifetime_bug
Add the following to your lib.rs
file:
```rust,ignore
extern crate fixhiddenlifetime_bug; ```
Slap a #[fix_hidden_lifetime_bug]
on the problematic function:
```rust
fn foo<'a, 'b>(it: &'a mut &'b ()) -> impl 'a + Sized { it } ```
```rust
async fn baz<'a>(fst: &'static str, snd: &str, thrd: &str) { /* … */ } ```