diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs index 089f1f3639062..09bb4519170da 100644 --- a/library/core/src/array/mod.rs +++ b/library/core/src/array/mod.rs @@ -339,9 +339,7 @@ impl Copy for [T; N] {} impl Clone for [T; N] { #[inline] fn clone(&self) -> Self { - // SAFETY: we know for certain that this iterator will yield exactly `N` - // items. - unsafe { collect_into_array_unchecked(&mut self.iter().cloned()) } + SpecArrayClone::clone(self) } #[inline] @@ -350,6 +348,29 @@ impl Clone for [T; N] { } } +#[cfg(not(bootstrap))] +trait SpecArrayClone: Clone { + fn clone(array: &[Self; N]) -> [Self; N]; +} + +#[cfg(not(bootstrap))] +impl SpecArrayClone for T { + #[inline] + default fn clone(array: &[T; N]) -> [T; N] { + // SAFETY: we know for certain that this iterator will yield exactly `N` + // items. + unsafe { collect_into_array_unchecked(&mut array.iter().cloned()) } + } +} + +#[cfg(not(bootstrap))] +impl SpecArrayClone for T { + #[inline] + fn clone(array: &[T; N]) -> [T; N] { + *array + } +} + // The Default impls cannot be done with const generics because `[T; 0]` doesn't // require Default to be implemented, and having different impl blocks for // different numbers isn't supported yet. diff --git a/src/test/codegen/array-clone.rs b/src/test/codegen/array-clone.rs new file mode 100644 index 0000000000000..0d42963bcd2ce --- /dev/null +++ b/src/test/codegen/array-clone.rs @@ -0,0 +1,15 @@ +// compile-flags: -O + +#![crate_type = "lib"] + +// CHECK-LABEL: @array_clone +#[no_mangle] +pub fn array_clone(a: &[u8; 2]) -> [u8; 2] { + // CHECK-NOT: getelementptr + // CHECK-NOT: load i8 + // CHECK-NOT: zext + // CHECK-NOT: shl + // CHECK: load i16 + // CHECK-NEXT: ret i16 + a.clone() +}