dtolnay / dtolnay/async-trait

Elided lifetimes in function arguments trigger error: lifetime parameters do not match the trait definition

Open
#294 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
2.2k
Forks
101
PR merge metrics
No merged PRs in 30d

Description

When we elide a lifetime in the impl block but not in the trait definition block, async-trait produces different lifetime requirements, which make the code not compile.

Example ([Playground]):

```rust
use async_trait::async_trait; // 0.1.89

struct MyStruct<'a>(&'a ());

#[async_trait]
trait MyTrait {
async fn foo(&self, s: &MyStruct);
}

struct TypeA;

// this works
#[async_trait]
impl MyTrait for TypeA {
async fn foo(&self, _s: &MyStruct) {}
}

struct TypeB;

// this doesn't
#[async_trait]
impl MyTrait for TypeB {
async fn foo(&self, _s: &MyStruct<'_>) {}
}
```

This code produces the following compiler error:

```
error[E0195]: lifetime parameters do not match the trait definition
--> src/lib.rs:23:39
|
23 | async fn foo(&self, _s: &MyStruct<'_>) {}
| ^^
|
= note: lifetime parameters differ in whether they are early- or late-bound
note: `'life2` differs between the trait and impl
--> src/lib.rs:7:29
|
6 | trait MyTrait {
| ------------- in this trait...
7 | async fn foo(&self, s: &MyStruct);
| ^^^^^^^^ `'_` is late-bound
...
22 | impl MyTrait for TypeB {
| ---------------------- in this impl...
23 | async fn foo(&self, _s: &MyStruct<'_>) {}
| ^^
| |
| `'life2` is early-bound
| this lifetime bound makes `'life2` early-bound

For more information about this error, try `rustc --explain E0195`.
```

This code would be accepted by the compiler if async-trait was not used. Since async-trait obviously cannot guess whether lifetimes were elided or not, I think non-elided lifetimes should be treated like elided lifetimes.

[Playground]: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=282137dd3ec929faa99246c8a909cf26

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.