Skip to content

Async fns that take structs with elided lifetime parameters are broken. #60203

Closed
@Lymia

Description

@Lymia

This code fails to compile with a very strange error:

#![feature(test, futures_api, async_await, await_macro)]
struct HasLifetime<'a>(&'a bool);
async fn bug(lifetime: HasLifetime) { 
    if *lifetime.0 { }
}

Error given:

error: cannot infer an appropriate lifetime
 --> src/lib.rs:3:37
  |
3 |   async fn bug(lifetime: HasLifetime) { 
  |                                       ^
  |                                       |
  |  _____________________________________this return type evaluates to the `'static` lifetime...
  | |
4 | |     if *lifetime.0 { }
5 | | }
  | |_^ ...but this borrow...
  |
note: ...can't outlive the anonymous lifetime #1 defined on the function body at 3:1
 --> src/lib.rs:3:1
  |
3 | / async fn bug(lifetime: HasLifetime) { 
4 | |     if *lifetime.0 { }
5 | | }
  | |_^
help: you can add a constraint to the return type to make it last less than `'static` and match the anonymous lifetime #1 defined on the function body at 3:1
  |
3 | async fn bug(lifetime: HasLifetime)  + '_{ 
  |                                      ^^^^

It seems the compiler fails to generate a lifetime constraint on the impl Future<...> the function compiles down to because there is no apparent lifetime on HasLifetime. Replacing it with HasLifetime<'_> fixes the problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-async-awaitArea: Async & AwaitA-lifetimesArea: Lifetimes / regionsAsyncAwait-PolishAsync-await issues that are part of the "polish" areaC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language 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