Skip to content

async/await: Usage of trait objects reports multiple different lifetimes when only one is used #58885

Closed
@Matthias247

Description

@Matthias247

The following code (playground) fails to compile:

#![feature(futures_api, async_await, await_macro)]

struct Xyz {
    a: u64,
}

trait Foo {}

impl Xyz {
    async fn do_sth<'a>(
        &'a self, foo: &'a dyn Foo
    ) -> bool
    {
        true
    }
}

Error:

error[E0709]: multiple different lifetimes used in arguments of `async fn`
  --> src/lib.rs:11:10
   |
11 |         &'a self, foo: &'a dyn Foo
   |          ^^                ^^^^^^^ different lifetime here
   |          |
   |          first lifetime here
   |
   = help: `async fn` can only accept borrowed values with identical lifetimes

This is very surprising, since only one lifetime is visible.

Workaround (thanks to @Nemo157 ):

Defining the function as

async fn do_sth<'a>(&'a self, foo: &'a (dyn Foo + 'a)) -> bool

will allow compilation.

Metadata

Metadata

Assignees

Labels

A-async-awaitArea: Async & AwaitAsyncAwait-PolishAsync-await issues that are part of the "polish" areaT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions