Skip to content

Incorrect "use of undeclared lifetime name" error with nested async functions #93911

Closed
@alexcrichton

Description

@alexcrichton

Given this Rust code:

struct Foo<'a>(&'a u32);

impl<'a> Foo<'a> {
    async fn foo() {
        struct Bar<'b>(&'b u32);

        impl<'b> Bar<'b> {
            async fn bar() {}
        }
    }
}

the compiler errors out with:

$ rustc +nightly foo.rs --crate-type lib --edition 2021
error[E0261]: use of undeclared lifetime name `'a`
 --> foo.rs:3:6
  |
3 | impl<'a> Foo<'a> {
  |      ^^ undeclared lifetime
  |
  = help: if you want to experiment with in-band lifetime bindings, add `#![feature(in_band_lifetimes)]` to the crate attributes
help: consider introducing lifetime `'a` here
  |
7 |         impl<'a, 'b> Bar<'b> {
  |              +++
help: consider introducing lifetime `'a` here
  |
8 |             async fn bar<'a>() {}
  |                         ++++

error: aborting due to previous error

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

however this code:

struct Foo<'a>(&'a u32);

impl<'a> Foo<'a> {
    async fn foo() {}
}

struct Bar<'b>(&'b u32);

impl<'b> Bar<'b> {
    async fn bar() {}
}

compiles successfully.

This seems like it might be related to the expansion of async code internally in rustc but I'm not entirely sure myself.

The nightly I tested is:

$ rustc +nightly -vV
rustc 1.60.0-nightly (e646f3d2a 2022-02-10)
binary: rustc
commit-hash: e646f3d2a9541952310778288854943678738ea9
commit-date: 2022-02-10
host: aarch64-unknown-linux-gnu
release: 1.60.0-nightly
LLVM version: 13.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regionsE-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions