Closed
Description
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
Labels
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
jdahlstrom commentedon Dec 23, 2022
impl Fn*
for returning closures, too, although in that case it's not necessarily obvious which of theFn*
traits it should be.estebank commentedon Dec 27, 2022
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:
Suggest `impl Iterator` when possible for `_` return type
impl Iterator
when possible for_
return type #106172Rollup merge of rust-lang#106172 - estebank:suggest-impl-trait, r=com…
Rollup merge of rust-lang#106172 - estebank:suggest-impl-trait, r=com…
Rollup merge of rust-lang#106172 - estebank:suggest-impl-trait, r=com…
compiler-errors commentedon Jan 8, 2023
I think this is basically completed.