Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
scottmcm opened this issue Dec 23, 2022 · 3 comments
Closed

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

scottmcm opened this issue Dec 23, 2022 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@scottmcm
Copy link
Member

scottmcm commented Dec 23, 2022

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?

@scottmcm scottmcm added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 23, 2022
@jdahlstrom
Copy link

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

@estebank
Copy link
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

estebank added a commit to estebank/rust that referenced this issue Dec 27, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 28, 2022
…piler-errors

Suggest `impl Iterator` when possible for `_` return type

Address rust-lang#106096.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 28, 2022
…piler-errors

Suggest `impl Iterator` when possible for `_` return type

Address rust-lang#106096.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 28, 2022
…piler-errors

Suggest `impl Iterator` when possible for `_` return type

Address rust-lang#106096.
@compiler-errors
Copy link
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
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants