Skip to content

Commit

Permalink
Rollup merge of rust-lang#71446 - Amanieu:transmute_copy, r=sfackler
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JohnTitor authored Apr 23, 2020
2 parents 6f18ad5 + 99de372 commit 9ba70a9
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/libcore/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,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.
Expand Down

0 comments on commit 9ba70a9

Please sign in to comment.