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

Zero-length arrays are non-Copy. #94313

Open
JohnScience opened this issue Feb 24, 2022 · 9 comments
Open

Zero-length arrays are non-Copy. #94313

JohnScience opened this issue Feb 24, 2022 · 9 comments
Labels
A-array Area: `[T; N]` A-specialization Area: Trait impl specialization A-trait-system Area: Trait system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@JohnScience
Copy link
Contributor

JohnScience commented Feb 24, 2022

According to the documentation of core::marker::Copy,

Generally speaking, if your type can implement Copy, it should. Keep in mind, though, that implementing Copy is part of the public API of your type. If the type might become non-Copy in the future, it could be prudent to omit the Copy implementation now, to avoid a breaking API change.

Zero-sized types in general are a bit tricky in that regard because they (just like core::marker::PhantomData) can be used to represent ownership over values of types that non-trivially implement core::ops::Drop.

However, zero-length arrays perhaps should implement Copy because they can be bit-wise copied (assuming 0-bit/0-byte copy is permitted). It would allow implementing Copy on overaligned types like the one below:

Screenshot 2022-02-23 202852

Screenshot 2022-02-23 203825

Is there anything that stops implementing Copy on zero-length arrays aside from marker trait attribute dependency?

P.S. I would love to have a generic type where the first generic parameter would be the type of the stored value and the second parameter would be a generic constant of type usize whose value would be used as a constraint for alignment, yet it is a story for discussion on Rust internals/another issue/RFC.

References

@oli-obk
Copy link
Contributor

oli-obk commented Feb 24, 2022

cc @rust-lang/project-const-generics

@lcnr
Copy link
Contributor

lcnr commented Feb 24, 2022

Is there anything that stops implementing Copy on zero-length arrays aside from marker trait attribute dependency?

pretty much, yeah. Though I am not sure we want it even once marker trait attributes, as it will be inconsistent with other traits which also rely on lattice specialization, e.g. Clone and so on

@JohnScience
Copy link
Contributor Author

@lcnr Can you please explain the inconsistency?

@lcnr
Copy link
Contributor

lcnr commented Feb 24, 2022

We won't be able to implement Clone for zero-length arrays without having lattice specialization or by adding some hacks to the compiler.

e.g. see Default which had the special-case for 0 length arrays and now can't be implemented using const generics https://doc.rust-lang.org/nightly/std/default/trait.Default.html#impl-Default-71.

So if we add impl<T> Copy for [T; 0] once marker traits are stable, we still won't have these impls for Clone and so on, even though they are just as sensible.

Additionally, Clone is a supertrait of Copy, so you wouldn't be able to add a Copy impl without first implementing Clone.

@workingjubilee
Copy link
Member

Using 0-length arrays to align things is a hack and should be replaced by an actually fluent interface to adjust layout computations based on various parameters.

@JohnScience
Copy link
Contributor Author

I suggest the following labels for the issue:

Since I can't account for everything and write a reasonable RFC, I think that this issue can remain open until someone finds it useful enough to implement.

@lcnr lcnr added A-array Area: `[T; N]` A-specialization Area: Trait impl specialization A-trait-system Area: Trait system labels Feb 25, 2022
@JohnScience
Copy link
Contributor Author

JohnScience commented Feb 27, 2022

I want to do something terribly unsafe. I would like to have a struct implement Copy even though the field storing a zero-sized array doesn't. Any ideas which terrible hacks would help me achieve that?

Note: core::mem::ManuallyDrop<T> implements Copy only if T: Copy

@zirconium-n
Copy link
Contributor

zirconium-n commented Feb 28, 2022

Not really solving [T; 0] is non-Copy, but for force alignment, it seems generic_const_exprs would suffice?

#![feature(generic_const_exprs)]
use std::mem::align_of;

fn main() {
    println!("{}", align_of::<K>());  // 8
    println!("{}", align_of::<X<K>>());  // 8
}

struct K(u64);

struct X<A: Sized>
where
    (): AlignT<{ align_of::<A>() }>,
{
    k: [<() as AlignT<{ align_of::<A>() }>>::T; 0],
    x: u8,
}

trait AlignT<const N: usize> {
    type T: Copy;
}

impl AlignT<8> for () {
    type T = u64;
}

impl Copy for X<K> {}
impl Clone for X<K> {
    fn clone(&self) -> Self {
        *self
    }
}

@lukas-code
Copy link
Member

This would be another application of an unsafe impl Copy, as proposed in #62835 and #25053.

@Noratrieb Noratrieb added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Apr 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-array Area: `[T; N]` A-specialization Area: Trait impl specialization A-trait-system Area: Trait system 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

7 participants