Skip to content

multiple spurious errors when T: 'static is required in async fn #115376

Description

@aliemjay

Given this code:

async fn test<T>(_: &u8) {
    let _: &'static T;
    //~^ ERROR the parameter type `T` may not live long enough
    //~| ERROR the parameter type `T` may not live long enough
}

Two errors are emitted: one requires T: 'static, as expected, and the other requires T: 'a where 'a is the anonymous lifetime.

This affects lifetime constraints that originate in async fn, async blocks, and closures.

An error is emitted for each lifetime that appears in the function because 'static is an upper bound for all lifetimes.

error[E0310]: the parameter type `T` may not live long enough
 --> src/lib.rs:2:12
  |
2 |     let _: &'static T;
  |            ^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
  |
help: consider adding an explicit lifetime bound...
  |
1 | async fn test<T: 'static>(_: &u8) {
  |                +++++++++

error[E0311]: the parameter type `T` may not live long enough
 --> src/lib.rs:2:12
  |
2 |     let _: &'static T;
  |            ^^^^^^^^^^
  |
note: the parameter type `T` must be valid for the anonymous lifetime defined here...
 --> src/lib.rs:1:21
  |
1 | async fn test<T>(_: &u8) {
  |                     ^^^
note: ...so that the type `T` will meet its required lifetime bounds
 --> src/lib.rs:2:12
  |
2 |     let _: &'static T;
  |            ^^^^^^^^^^
help: consider adding an explicit lifetime bound...
  |
1 | async fn test<'a, T: 'a>(_: &'a u8) {
  |               +++  ++++      ++

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-borrow-checkerArea: The borrow checkerA-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.D-verboseDiagnostics: Too much output caused by a single piece of incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions