Skip to content

Consider suggesting RPIT for -> _ functions returning iterators #106096

Closed
@scottmcm

Description

@scottmcm

Given the following code: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=c36b48aedbf2651c7b2cfc4fc42e1070

fn evens_squared(n: usize) -> _ {
    (1..n).filter(|x| x % 2 == 0).map(|x| x * x)
}

The current output is:

error[E0121]: the placeholder `_` is not allowed within types on item signatures for return types
 --> src/lib.rs:1:31
  |
1 | fn evens_squared(n: usize) -> _ {
  |                               ^ not allowed in type signatures

Ideally the output should look like:

error[E0121]: the placeholder `_` is not allowed within types on item signatures for return types
 --> src/lib.rs:1:31
  |
1 | fn evens_squared(n: usize) -> _ {
  |                               ^
  |                               |
  |                               not allowed in type signatures
  |                               help: you could return an iterator as `impl Iterator<Item = usize>`

Inspired by https://users.rust-lang.org/t/idiomatic-alternative-to-writing-a-function-that-returns-an-iterator/86391?u=scottmcm, where I went to do my usual "just say -> _ and the compiler will tell you!" only to find that it doesn't work here -- presumably because of the Voldemort type.

I don't know if there's a more general version of this, but maybe special casing -> impl Iterator<Item = …> would be worth doing as a particularly important case of RPIT. Maybe -> impl Future<Output = …> too?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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