Skip to content

Clone is not defined for [_; 256] #30244

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

Closed
untitaker opened this issue Dec 6, 2015 · 8 comments · Fixed by #30247
Closed

Clone is not defined for [_; 256] #30244

untitaker opened this issue Dec 6, 2015 · 8 comments · Fixed by #30247
Labels
regression-from-stable-to-nightly Performance or correctness regression from stable to nightly.

Comments

@untitaker
Copy link
Contributor

#30130 broke this code:

#[derive(Copy, Clone)]
struct Array {
    arr: [[u8; 256]; 4]
}
@bluss bluss added the regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. label Dec 6, 2015
@bluss
Copy link
Member

bluss commented Dec 6, 2015

Clone is not implemented for [[u8; 256]; 4] anymore, because it now requires [T; 4] where T: Clone (previously T: Copy), which doesn't hold for arrays bigger than 32.

@sfackler
Copy link
Member

sfackler commented Dec 6, 2015

I'm assuming that trying to call clone on an Array would previously ICE the compiler?

@bluss
Copy link
Member

bluss commented Dec 6, 2015

In certain contexts, such as fn foo<T: Copy>(t: &T) { t.clone(); } it would / will ICE yes, issue #25733

@sfackler
Copy link
Member

sfackler commented Dec 6, 2015

Ah, the bound was T: Copy before.

@retep998
Copy link
Member

retep998 commented Dec 7, 2015

If only we had type level integers so we could just properly implement Clone for all fixed size array sizes without massive bloat.

bors added a commit that referenced this issue Dec 7, 2015
Revert "PR #30130 Implement `Clone` for more arrays"

This reverts commit e22a64e.

This caused a regression such that types like `[[u8; 256]; 4]`
no longer implemented Clone. This previously worked due to Clone
for `[T; N]` (N in 0 to 32) being implemented for T: Copy.

Due to fixed size arrays not implementing Clone for sizes above 32,
the new implementation requiring T: Clone would not allow
`[[u8; 256]; 4]` to be Clone.

Fixes #30244

Due to changing back, this is technically a [breaking-change],
albeit for a behavior that existed for a very short time.
@petrochenkov
Copy link
Contributor

If only we had type level integers so we could just properly implement Clone for all fixed size array sizes without massive bloat.

It doesn't even requires actual numbers, most of impls act on "size-erased" arrays.
Something like impl<T> Clone for [T; _] {...} would be enough.

@durka
Copy link
Contributor

durka commented Dec 7, 2015

@petrochenkov how would that work? Don't you need to loop N times to do the actual clone?

@petrochenkov
Copy link
Contributor

Something like:

fn clone(&self) -> Self {
    unsafe {
        let result: Self = mem::uninitialized();
        clone_slice_into_uninitialized_memory(self.begin(), self.len(), result.begin());
        result
    }
}

So N is not used explicitly in the implementation, although it is still needed by Self internally. Whether it is easier to implement than type level integers or not is a separate question.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
regression-from-stable-to-nightly Performance or correctness regression from stable to nightly.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants