Rollup merge of #71446 - Amanieu:transmute_copy, r=sfackler

Only use read_unaligned in transmute_copy if necessary

I've noticed that this causes LLVM to generate poor code on targets that don't support unaligned memory accesses.
This commit is contained in:
Dylan DPC 2020-04-23 20:35:04 +02:00 committed by GitHub
commit 199f4deef0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -924,7 +924,12 @@ pub fn drop<T>(_x: T) {}
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub unsafe fn transmute_copy<T, U>(src: &T) -> U {
ptr::read_unaligned(src as *const T as *const U)
// If U has a higher alignment requirement, src may not be suitably aligned.
if align_of::<U>() > align_of::<T>() {
ptr::read_unaligned(src as *const T as *const U)
} else {
ptr::read(src as *const T as *const U)
}
}
/// Opaque type representing the discriminant of an enum.