Skip to content

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

Closed
@scottmcm

Description

@scottmcm
Member

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?

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Dec 23, 2022
jdahlstrom

jdahlstrom commented on Dec 23, 2022

@jdahlstrom

impl Fn* for returning closures, too, although in that case it's not necessarily obvious which of the Fn* traits it should be.

estebank

estebank commented on Dec 27, 2022

@estebank
Contributor

For closures we already emit a note, but I'll see if I can do that as a follow up.

In the meantime, I managed to get this working:

screenshot showing compiler output with the proposed suggestion

added 3 commits that reference this issue on Dec 28, 2022

Rollup merge of rust-lang#106172 - estebank:suggest-impl-trait, r=com…

8d2c93e

Rollup merge of rust-lang#106172 - estebank:suggest-impl-trait, r=com…

d40f16e

Rollup merge of rust-lang#106172 - estebank:suggest-impl-trait, r=com…

31f5e75
compiler-errors

compiler-errors commented on Jan 8, 2023

@compiler-errors
Member

I think this is basically completed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

        Participants

        @estebank@compiler-errors@jdahlstrom@scottmcm

        Issue actions

          Consider suggesting RPIT for `-> _` functions returning iterators · Issue #106096 · rust-lang/rust