Skip to content

RPIT with 'static bound lifetime capture issue with generic parameter #125075

Closed as not planned
@drdo

Description

@drdo

I tried this code:

pub fn bar(x: &str) -> impl Iterator<Item=String> + 'static {
    foo(x)
}
            
pub fn foo<A>(_arg: A) -> impl Iterator<Item=String> + 'static {
    std::iter::empty()
}

I expected to see this happen:
Successful compilation.

Instead, this happened:

error[E0700]: hidden type for `impl Iterator<Item = String> + 'static` captures lifetime that does not appear in bounds
 --> main.rs:4:5
  |
3 | pub fn bar(x: &str) -> impl Iterator<Item=String> + 'static {
  |               ----     ------------------------------------ opaque type defined here
  |               |
  |               hidden type `impl Iterator<Item = String> + 'static` captures the anonymous lifetime defined here
4 |     foo(x)
  |     ^^^^^^

error: aborting due to 1 previous error

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

The RPIT returned from foo has a 'static bound so it is unclear why there would be any complaints about
capturing a smaller lifetime (which still doesn't happen in this case as the RPIT returned from foo does not depend on _arg at all).

If the definition of foo above is changed to (by replacing the generic parameter A with &str):

pub fn foo(_arg: &str) -> impl Iterator<Item=String> + 'static {
    std::iter::empty()
}

then the code compiles without a problem.

Meta

rustc --version --verbose:

rustc 1.80.0-nightly (ef0027897 2024-05-12)
binary: rustc
commit-hash: ef0027897d2e9014766fb47dce9ddbb925d2f540
commit-date: 2024-05-12
host: aarch64-apple-darwin
release: 1.80.0-nightly
LLVM version: 18.1.4
Backtrace

<backtrace>

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.C-bugCategory: This is a bug.T-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