Skip to content

Commit 199f4de

Browse files
authored
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.
2 parents 1363a4b + 99de372 commit 199f4de

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/libcore/mem/mod.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,12 @@ pub fn drop<T>(_x: T) {}
924924
#[inline]
925925
#[stable(feature = "rust1", since = "1.0.0")]
926926
pub unsafe fn transmute_copy<T, U>(src: &T) -> U {
927-
ptr::read_unaligned(src as *const T as *const U)
927+
// If U has a higher alignment requirement, src may not be suitably aligned.
928+
if align_of::<U>() > align_of::<T>() {
929+
ptr::read_unaligned(src as *const T as *const U)
930+
} else {
931+
ptr::read(src as *const T as *const U)
932+
}
928933
}
929934

930935
/// Opaque type representing the discriminant of an enum.

0 commit comments

Comments
 (0)