Skip to content

The output help is incorrect, lifetime appears in an unexpected position #121267

Closed
@A4-Tacks

Description

@A4-Tacks

Code

fn main() {}

fn foo(src: &crate::Foo) -> Option<i32> {
    todo!()
}
fn bar(src: &crate::Foo) -> impl Iterator<Item = i32> {
    [0].into_iter()
        .filter_map(|_| foo(src))
}

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

Current output

Checking test1 v0.1.0 (/home/lrne/Project/Rust/test1)
error[E0700]: hidden type for `impl Iterator<Item = i32>` captures lifetime that does not appear in bounds
 --> src/main.rs:7:5
  |
6 |   fn bar(src: &crate::Foo) -> impl Iterator<Item = i32> {
  |                ----------     ------------------------- opaque type defined here
  |                |
  |                hidden type `FilterMap<std::array::IntoIter<i32, 1>, {closure@src/main.rs:8:21: 8:24}>` captures the anonymous lifetime defined here
7 | /     [0].into_iter()
8 | |         .filter_map(|_| foo(src))
  | |_________________________________^
  |
help: to declare that `impl Iterator<Item = i32>` captures `'_`, you can introduce a named lifetime parameter `'a`
  |
6 | fn bar<'a>(src: &'a crate::'a) -> impl Iterator<Item = i32> + 'a  {
  |       ++++       ++        ~~                               ++++

For more information about this error, try `rustc --explain E0700`.
error: could not compile `test1` (bin "test1") due to 1 previous error

Desired output

Checking test1 v0.1.0 (/home/lrne/Project/Rust/test1)
error[E0700]: hidden type for `impl Iterator<Item = i32>` captures lifetime that does not appear in bounds
 --> src/main.rs:7:5
  |
6 |   fn bar(src: &crate::Foo) -> impl Iterator<Item = i32> {
  |                ----------     ------------------------- opaque type defined here
  |                |
  |                hidden type `FilterMap<std::array::IntoIter<i32, 1>, {closure@src/main.rs:8:21: 8:24}>` captures the anonymous lifetime defined here
7 | /     [0].into_iter()
8 | |         .filter_map(|_| foo(src))
  | |_________________________________^
  |
help: to declare that `impl Iterator<Item = i32>` captures `'_`, you can introduce a named lifetime parameter `'a`
  |
6 | fn bar<'a>(src: &'a crate::Foo<'a>) -> impl Iterator<Item = i32> + 'a  {
  |       ++++       ++        ~~                               ++++

For more information about this error, try `rustc --explain E0700`.
error: could not compile `test1` (bin "test1") due to 1 previous error

Rationale and extra context

No response

Other cases

fn main() {}

fn foo(src: &crate::Foo) -> Option<i32> {
    todo!()
}
fn bar(src: &crate::Foo<'_>) -> impl Iterator<Item = i32> {
    [0].into_iter()
        .filter_map(|_| foo(src))
}

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

Rust Version

rustc 1.77.0-nightly (bf8716f1c 2023-12-24)
binary: rustc
commit-hash: bf8716f1cd6416266807706bcae0ecb2e51c9d4a
commit-date: 2023-12-24
host: aarch64-unknown-linux-gnu
release: 1.77.0-nightly
LLVM version: 17.0.6

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.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