Skip to content

Improve diagnostics for impl Trait capturing lifetimes #78402

@djc

Description

@djc

I tried this code:

use futures_util::stream::{once, Stream};
use std::io;

fn assert_static<T: 'static>(value: T) {}

async fn foo(a: &str) {
    assert_static(bar(a.split(',')));
}

fn bar<T>(_: T) -> impl Stream<Item = Result<Vec<u8>, io::Error>> {
    once(async { Ok(vec![]) })
}

I expected to see this happen: compiles correctly.

Instead, this happened:

error[E0759]: `a` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement
 --> src/lib.rs:6:14
  |
6 | async fn foo(a: &str) {
  |              ^  ---- this data with an anonymous lifetime `'_`...
  |              |
  |              ...is captured here...
7 |     assert_static(bar(a.split(',')));
  |     ------------- ...and is required to live as long as `'static` here

Meta

rustc 1.47.0. This appears to be #42940, fixing which might depend on type-alias-impl-trait (although so far that's only a workaround, not really a fix?). It would be nice to have more specific diagnostics here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-async-awaitArea: Async & AwaitA-borrow-checkerArea: The borrow checkerA-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.A-lifetimesArea: Lifetimes / regionsAsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.C-enhancementCategory: An issue proposing an enhancement or a PR with one.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