rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
96.58k stars 12.48k forks source link

Most core trait impls for empty arrays are over-constrained #52246

Open scottmcm opened 6 years ago

scottmcm commented 6 years ago

All of the following have where T: Trait on their impls for [T; 0], but don't need it:

A demo of those not in rustdoc:

struct Nothing;
fn must_be_copy<T: Copy>() {}
fn must_be_clone<T: Clone>() {}

pub fn empty_array_is_copy_and_clone() {
    must_be_copy::<[Nothing; 0]>();
    //^ error: the trait bound `Nothing: std::marker::Copy` is not satisfied in `[Nothing; 0]`
    must_be_clone::<[Nothing; 0]>();
    //^ error: the trait bound `Nothing: std::clone::Clone` is not satisfied in `[Nothing; 0]`
}

Kudos to whomever did the Default impls for getting this right: https://doc.rust-lang.org/std/primitive.array.html#impl-Default-29

hanna-kruppe commented 6 years ago

Special casing array length 0 here might become an issue when we want to generalize these impls with const generics. This wouldn't be an issue if we had specialization for const generics or some kind of where N > 0 that is actually understood by overlap checks, that's pretty tricky in general and I don't expect it to happen in the first implementation.

abonander commented 6 years ago

@rkruppe if specialization is extended to consider const generics then the impls for [T; const N] could be defaulted and then specialized for [T; 0]. If specialization moves forward via Aaron Turon's proposal then this would be a straightforward extension of that logic, e.g. specialize(const N = 0).

hanna-kruppe commented 6 years ago

I could be wrong about how easy it is to extend specialization to const values, but I'm a bit pessimistic about anything regarding specialization being straightforward. And in any case, it would suck to have basic const generics available (enough to write impls for all arrays) but not be able to apply them to core APIs because we fixed this minor issue before.

So I think it might be prudent to hold off lifting the restrictions on impls for empty arrays until we have both const generic and some way to special case N = 0. Relatedly, I think libs team policy so far has been to not add any (stable?) APIs that can't be implemented without specialization.

steveklabnik commented 4 years ago

Triage: not aware of any changes