Would it make sense for never to implement Iterator?

After reading nightly documentation for the never type I started wondering why ! does not implement the Iterator trait.

The documentation states:

In general, if a trait only has methods taking a self parameter (or &self, or an argument of type Self, etc.), consider implementing it for !

Iterator trait meets this criterion. There is also an associated type, but the Not trait has an associated type as well and ! implements Not. In both cases ! seems like the most obvious choice for the item type.

I tried implementing Iterator for an uninhabited enum and it looks like it should be possible for ! as well.

enum Void {}

impl Iterator for Void {
    type Item = Void;

    fn next(&mut self) -> Option<Self::Item> {
       match *self {}
       // could also be None, but it does not matter, right?
    }
}

fn void_panic() -> Void {
    panic!()
}

fn my_foo() -> impl Iterator<Item=u32> {
    void_panic()
    // error[E0271]: expected `Void` to be an iterator that yields `u32`, but it yields `Void`
    // instead of
    // error[E0277]: `Void` is not an iterator
}

fn main() {
    my_foo();
    for x in void_panic() {
    // warning: unreachable expression
    // instead of
    // error[E0277]: `Void` is not an iterator
    }
}

Am I missing some potential problem? Is there a reason why ! does not implement Iterator

2 Likes

The problem is that one could implement it only for one single Item type, so which one to pick?

IMHO, what could possibly be more sensible for this use case is for impl Trait[1] return types in functions that diverge to create some fresh uninhabited !-like anonymous types on the fly which then implement the required trait bound; maybe even just as a fallback.


  1. at least for traits like Iterator where every method has a self argument that makes them uncallable for uninhabited Self types ↩︎

10 Likes

Or, I have no idea how hard this would be to implement, but if we make !: Iterator<!>, then extend fallback such that impl Iterator<!> can coerce to an iterator over any type. That would solve this problem of which item type to use.

2 Likes

Oh, I think I understand the problem now. In other words, it would make sense for ! to implement Iterator for all possible Item types, not just !?

If so, doesn't the same logic applies to std::ops::Not? If would make sense for ! to implement Not for all possible associated Output types. It is just less problematic in practice as it is unlikely to see a function returning impl Not<Output=_some_type_>.

1 Like

that is simply not possible any one trait/type pair can only have one associated type. what would be needed would be something like

struct TypedNever<T>(!, PhantomData<T>)

which could be used to decide custom associated types for such traits. though that wouldn't be perfect

A case that's often disappointing is that stuff like this doesn't work:

fn foo() -> impl Iterator<Item = i32> { todo!() }
11 Likes

It does not make sense, but it would be useful.

In other words, a lot of code requires impl Iterator<Item = SomeSpecificT>, so for the implementation of Iterator for ! to be useful it would have to support all those cases.

However it does not make sense because <! as Iterator>::Item is a single type, so which one is it?

Even if you make ! coercible to something else that implements Iterator<Item = SomeSpecificT>` it still won't work because coercions are not attempted when a generic type is expected.

Yes, the implementation of Not for ! is not ideal and has the same issues as an hypothetic implementation of Iterator for !

2 Likes

For Iterator specifically, I use [].into_iter().

Yeah, but if you still want the panic then you have to "hide" it to avoid unused lints, e.g.

if true { todo!() }
[].into_iter()
1 Like

Reduce `unreachable-code` churn after `todo!()` by llogiq · Pull Request #149543 · rust-lang/rust · GitHub aims to make this better, by not warning on unreachable code that is unreachable because of a todo!().

7 Likes

I think the problem is that Rust does not have type hierarchy[1], and thus trait bounds cannot define covariance and contravariance. Because of that ! cannot be a "true" Bottom Type like full OOP languages can have - it just have lots of special-casing to make it behave like one.


  1. Traits and lifetimes have hierarchies - but not types. ↩︎

I wonder why did we make Item an associated type instead of a type parameter of trait Iterator? One possible reason I can think of is making .collect() works better, but are there any other reasons?

Intuitively it makes sense that if something can be iterated over it will yield one type of items, while if it were a generic it would be kinda confusing that you can iterate in a way and get one type of items or iterate in another way and get a different type of items.

2 Likes

I have a somewhat harder time imagining a language design principle based on which to coerce any impl Iterator<Item = !> to a (new, anonymous, wrapping) type implementing impl Iterator<Item = T> for any (particular) other type; compared to my previously mentioned idea of coercing ! to an (new, anonymous, uninhabited) type implementing impl Iterator<Item = T>.

Both features would require some amount of introspection into the method signature of the trait[1] but the latter seems conceptually simpler IMO (and more narrow) as it only deals with uninhabited types.

I suppose that @idanarye now mentioned above sounds relevant though

and I think indeed such a coercion of an impl Iterator<Item = !> to an impl Iterator<Item = T> sounds - arguably - like something that might come out of any “trait variance” design ideas, after all.

Specifically, similar coercions are sometimes desirable for coercing between impl Fn… kind of types (e.g. in a manner mirroring the variances of fn pointer types), in particular the Output type of FnOnce is comparable to the Item type of Iterator.[2]


  1. and based on language design choices presumably also an opt-in ↩︎

  2. On the other hand, as long as Fn… traits are not user-implementable yet, I can also imagine other designs to achieve their variance that don’t involve coercing to anonymous wrapper types, but instead some way of promising through the trait system the existence of other generic instantiations of the same closure type.

    (This could allow achieving true subtyping between the original and coerced closure types, in cases where the argument types also were subtypes. Note that ! is not actually in a true subtyping relation to other types either, further complicating the story in any case, if it’s meant to apply to ! -> T coercions for the Output type.) ↩︎

1 Like

Well, we do have that, but adapters are used instead of direct iteration (cf. String::chars() and String::lines()). Even when direct iteration was used, it proved problematic (e.g., Range and friends).

Because otherwise this doesn't compile:

for x in my_iter {
    println!("{}", x);
}

as there's nothing in that code that would pick the item type.

(Well, perhaps modulo the one-impl rule but that's super fragile and never a good thing to depend on if you can avoid it.)

And similarly for all kinds of other similar situations. it.filter(|x| x.foo > 10) wouldn't work because that closure wouldn't define the element type and field access needs to know the type, for example, but does with the associated type today because there's only the one possibility once the it type is known.

8 Likes

There are cases in practice where a type wants to implement Iterator (or at least IntoIter) two different ways. &str is a good example, as an iteration over it can plausibly return either u8 or char (Rust currently deals with this situation by using .bytes() and .chars() methods to return an appropriate iterable rather than implementing Iterator on &str directly). So it doesn't seem too ridiculous for a type to want to implement Iterator for two different element types.

For what it's worth, I consider associated types to be one of the most dangerous features of Rust, in the sense that it is hard to gain confidence that the compiler implements them correctly (and yet if the compiler implements them incorrectly, it can lead to unsoundness in safe code). The alternative would be to make them type parameters instead, using Iterator<T> rather than Iterator<Item=T>, and in that case it would be plausible to impl<T> Iterator<T> for !.

1 Like

I can see this being an argument for IntoIterator being generic (even though I feel that being explicit is better), but not Iterator.

If you model this using a generic Iterator implementation then it means you can mix the u8 and char iteration: you could call next to get the next u8 (possibly moving in the middle of a char) and then next to get the next char (what would that mean then?).

7 Likes

It works poorly when some other branch has already defined a concrete type :sweat_smile:

TeChNiCaLlY, in the for ... in ... case, won't changing Iterator and leaving IntoIterator as is with associated type work? Something like this:

trait IntoIterator {
   type IntoIter : Iterator<Self::Item>,
   type Item;
   
   fn into_iter(self) -> Self::IntoIter;
}

trait Iterator<Item> {
   fn next(&mut self) -> Option<Item>;
}

Edit: Apparently you can't desugar for loop with this, it seems there's no way to express let next: IntoIterator::<typeof(iter_expr)>::Item ;