The following code does not compile on nightly (and 1.29.1 stable) (playground):
fn foo() -> impl Iterator<Item = i32> {
panic!()
}
I expected this code to compile, but instead I get the following error:
error[E0277]: the trait bound `(): std::iter::Iterator` is not satisfied
--> src/lib.rs:1:13
|
1 | fn foo() -> impl Iterator<Item = i32> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^ `()` is not an iterator; maybe try calling `.iter()` or a similar method
|
= help: the trait `std::iter::Iterator` is not implemented for `()`
= note: the return type of a function must have a statically known size
error: aborting due to previous error
For more information about this error, try `rustc --explain E0277`.
error: Could not compile `playground`.
To learn more, run the command again with --verbose.
This seems like it should work to me.
FYI: This also means that
fn foo() -> impl Iterator<Item = i32> {
unimplemented!()
}
doesn't compile!
Yeah, I always use panic!() instead of unimplemented!() because it's easier to type, but that is exactly how I found it.
Clicking through compiler explorer, the original example has never compiled on any stable release (since stabilizing impl trait on 1.26). The error also occurs on traits without associated types
trait Bar {}
fn foo() -> impl Bar {
unimplemented!()
}
3 | fn foo() -> impl Bar {
| ^^^^^^^^ the trait `Bar` is not implemented for `()`
(It will compile when returning something like impl Eq due to () implementing Eq)
This is a known design question concerning ! in general: https://github.com/rust-lang/rust/issues/35121.
I'm closing this as not-a-bug as we never decided that ! should implement any Trait and so long as it does not, -> impl Trait and unimplemented!() / friends won't unify.
@Centril Yes, they currently won't, but I just assumed that it is uncontroversial that panic!/unreachable! should work in this case. So I thought it would rather be a matter of figuring out _how_ to fix this instead of whether this _should_ be fixed.
However, if the do-we-want-this-to-work comes first, can you refer me to the appropriate way to open an issue/discussion to figure that out? That would be awesome, thanks!
There's some discussion in the tracking issue, e.g. starting with https://github.com/rust-lang/rust/issues/35121#issuecomment-239216426 and probably more in various places (e.g. https://github.com/rust-lang/rfcs/issues/2619). It's far more subtle than "all traits" I think and needs some language design (assuming there's agreement which there might not be). If you want to tackle this I'd read all previous discussions first and then raise an issue on internals.rust-lang.org/. (However, I'm skeptical that this should be a priority for the language and compiler teams).
Ah, I see the issue is already being discussed. If that's the case I'll just let it rest, seems there are enough people aware of it already. Thanks for the links! :)
Most helpful comment
This is a known design question concerning
!in general: https://github.com/rust-lang/rust/issues/35121.