diff --git a/src/bootstrap/cache.rs b/src/bootstrap/cache.rs index c27493158826c..f9659fc25688a 100644 --- a/src/bootstrap/cache.rs +++ b/src/bootstrap/cache.rs @@ -17,7 +17,6 @@ use std::ffi::OsStr; use std::fmt; use std::hash::{Hash, Hasher}; use std::marker::PhantomData; -use std::mem; use std::ops::Deref; use std::path::{Path, PathBuf}; use std::sync::Mutex; @@ -114,7 +113,8 @@ impl Deref for Interned { type Target = str; fn deref(&self) -> &'static str { let l = INTERNER.strs.lock().unwrap(); - unsafe { mem::transmute::<&str, &'static str>(l.get(*self)) } + let s: &str = l.get(*self).as_ref(); + unsafe { &*(s as *const _) } } } @@ -122,35 +122,40 @@ impl Deref for Interned { type Target = Path; fn deref(&self) -> &'static Path { let l = INTERNER.paths.lock().unwrap(); - unsafe { mem::transmute::<&Path, &'static Path>(l.get(*self)) } + let p: &Path = l.get(*self).as_ref(); + unsafe { &*(p as *const _) } } } impl AsRef for Interned { fn as_ref(&self) -> &'static Path { let l = INTERNER.paths.lock().unwrap(); - unsafe { mem::transmute::<&Path, &'static Path>(l.get(*self)) } + let p: &Path = l.get(*self).as_ref(); + unsafe { &*(p as *const _) } } } impl AsRef for Interned { fn as_ref(&self) -> &'static Path { let l = INTERNER.strs.lock().unwrap(); - unsafe { mem::transmute::<&Path, &'static Path>(l.get(*self).as_ref()) } + let p: &Path = l.get(*self).as_ref(); + unsafe { &*(p as *const _) } } } impl AsRef for Interned { fn as_ref(&self) -> &'static OsStr { let l = INTERNER.paths.lock().unwrap(); - unsafe { mem::transmute::<&OsStr, &'static OsStr>(l.get(*self).as_ref()) } + let s: &OsStr = l.get(*self).as_ref(); + unsafe { &*(s as *const _) } } } impl AsRef for Interned { fn as_ref(&self) -> &'static OsStr { let l = INTERNER.strs.lock().unwrap(); - unsafe { mem::transmute::<&OsStr, &'static OsStr>(l.get(*self).as_ref()) } + let s: &OsStr = l.get(*self).as_ref(); + unsafe { &*(s as *const _) } } } diff --git a/src/librustc/ty/mod.rs b/src/librustc/ty/mod.rs index e5f6ac8853067..fe4f01a8011b2 100644 --- a/src/librustc/ty/mod.rs +++ b/src/librustc/ty/mod.rs @@ -46,7 +46,6 @@ use std::ops::Deref; use std::rc::Rc; use std::slice; use std::vec::IntoIter; -use std::mem; use syntax::ast::{self, DUMMY_NODE_ID, Name, Ident, NodeId}; use syntax::attr; use syntax::ext::hygiene::{Mark, SyntaxContext}; @@ -592,7 +591,8 @@ impl<'tcx> serialize::UseSpecializedDecodable for &'tcx Slice> {} impl Slice { pub fn empty<'a>() -> &'a Slice { unsafe { - mem::transmute(slice::from_raw_parts(0x1 as *const T, 0)) + let slice = slice::from_raw_parts(0x1 as *const T, 0); + &*(slice as *const [T] as *const Slice) } } } diff --git a/src/librustc_data_structures/blake2b.rs b/src/librustc_data_structures/blake2b.rs index 6b8bf8df0d33f..abecf24a5614d 100644 --- a/src/librustc_data_structures/blake2b.rs +++ b/src/librustc_data_structures/blake2b.rs @@ -128,8 +128,7 @@ fn blake2b_compress(ctx: &mut Blake2bCtx, last: bool) { // before it's overwritten. let m: &mut [u64; 16] = unsafe { - let b: &mut [u8; 128] = &mut ctx.b; - ::std::mem::transmute(b) + &mut *(&mut ctx.b as *mut [u8; 128] as *mut [u64; 16]) }; if cfg!(target_endian = "big") { diff --git a/src/librustc_data_structures/stable_hasher.rs b/src/librustc_data_structures/stable_hasher.rs index d82b712b5b14b..c325a3cdd17de 100644 --- a/src/librustc_data_structures/stable_hasher.rs +++ b/src/librustc_data_structures/stable_hasher.rs @@ -241,10 +241,7 @@ impl HashStable for f32 { fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { - let val: u32 = unsafe { - ::std::mem::transmute(*self) - }; - val.hash_stable(ctx, hasher); + self.to_bits().hash_stable(ctx, hasher); } } @@ -252,10 +249,7 @@ impl HashStable for f64 { fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { - let val: u64 = unsafe { - ::std::mem::transmute(*self) - }; - val.hash_stable(ctx, hasher); + self.to_bits().hash_stable(ctx, hasher); } } diff --git a/src/librustc_trans/type_.rs b/src/librustc_trans/type_.rs index 6cbe175d4d8d5..6597e0b76fe15 100644 --- a/src/librustc_trans/type_.rs +++ b/src/librustc_trans/type_.rs @@ -21,7 +21,6 @@ use rustc::ty::layout::{self, Align, Size}; use std::ffi::CString; use std::fmt; -use std::mem; use std::ptr; use libc::c_uint; @@ -59,7 +58,7 @@ impl Type { } pub fn to_ref_slice(slice: &[Type]) -> &[TypeRef] { - unsafe { mem::transmute(slice) } + unsafe { &*(slice as *const [Type] as *const [TypeRef]) } } pub fn void(ccx: &CrateContext) -> Type { diff --git a/src/libstd/error.rs b/src/libstd/error.rs index eb5022ad5776a..cc48be83874f5 100644 --- a/src/libstd/error.rs +++ b/src/libstd/error.rs @@ -59,7 +59,6 @@ use char; use convert; use core::array; use fmt::{self, Debug, Display}; -use mem::transmute; use num; use str; use string; @@ -491,11 +490,11 @@ impl Error + Send { #[stable(feature = "error_downcast", since = "1.3.0")] /// Attempt to downcast the box to a concrete type. pub fn downcast(self: Box) - -> Result, Box> { + -> Result, Box> { let err: Box = self; ::downcast(err).map_err(|s| unsafe { // reapply the Send marker - transmute::, Box>(s) + Box::from_raw(Box::into_raw(s) as *mut Self) }) } } @@ -509,7 +508,7 @@ impl Error + Send + Sync { let err: Box = self; ::downcast(err).map_err(|s| unsafe { // reapply the Send+Sync marker - transmute::, Box>(s) + Box::from_raw(Box::into_raw(s) as *mut Self) }) } } diff --git a/src/libstd/sys/redox/args.rs b/src/libstd/sys/redox/args.rs index 59ae2a74a6ddd..afda25a367cdd 100644 --- a/src/libstd/sys/redox/args.rs +++ b/src/libstd/sys/redox/args.rs @@ -57,7 +57,6 @@ impl DoubleEndedIterator for Args { mod imp { use os::unix::prelude::*; - use mem; use ffi::{CStr, OsString}; use marker::PhantomData; use libc; @@ -105,7 +104,7 @@ mod imp { } fn get_global_ptr() -> *mut Option>>> { - unsafe { mem::transmute(&GLOBAL_ARGS_PTR) } + unsafe { &mut GLOBAL_ARGS_PTR as *mut _ as *mut _ } } } diff --git a/src/libstd/sys/redox/ext/ffi.rs b/src/libstd/sys/redox/ext/ffi.rs index d59b4fc0b70b8..06968bc4871ab 100644 --- a/src/libstd/sys/redox/ext/ffi.rs +++ b/src/libstd/sys/redox/ext/ffi.rs @@ -13,7 +13,6 @@ #![stable(feature = "rust1", since = "1.0.0")] use ffi::{OsStr, OsString}; -use mem; use sys::os_str::Buf; use sys_common::{FromInner, IntoInner, AsInner}; @@ -53,7 +52,7 @@ pub trait OsStrExt { #[stable(feature = "rust1", since = "1.0.0")] impl OsStrExt for OsStr { fn from_bytes(slice: &[u8]) -> &OsStr { - unsafe { mem::transmute(slice) } + unsafe { &*(slice as *const [u8] as *const OsStr) } } fn as_bytes(&self) -> &[u8] { &self.as_inner().inner diff --git a/src/libstd/sys/redox/os_str.rs b/src/libstd/sys/redox/os_str.rs index 655bfdb916707..b00e2f56aa5e7 100644 --- a/src/libstd/sys/redox/os_str.rs +++ b/src/libstd/sys/redox/os_str.rs @@ -14,7 +14,6 @@ use borrow::Cow; use fmt; use str; -use mem; use rc::Rc; use sync::Arc; use sys_common::{AsInner, IntoInner}; @@ -105,7 +104,7 @@ impl Buf { } pub fn as_slice(&self) -> &Slice { - unsafe { mem::transmute(&*self.inner) } + unsafe { &*(&*self.inner as *const [u8] as *const Slice) } } pub fn into_string(self) -> Result { @@ -118,12 +117,13 @@ impl Buf { #[inline] pub fn into_box(self) -> Box { - unsafe { mem::transmute(self.inner.into_boxed_slice()) } + let boxed = self.inner.into_boxed_slice(); + unsafe { Box::from_raw(Box::into_raw(boxed) as *mut Slice) } } #[inline] pub fn from_box(boxed: Box) -> Buf { - let inner: Box<[u8]> = unsafe { mem::transmute(boxed) }; + let inner = unsafe { Box::from_raw(Box::into_raw(boxed) as *mut [u8]) }; Buf { inner: inner.into_vec() } } @@ -140,7 +140,7 @@ impl Buf { impl Slice { fn from_u8_slice(s: &[u8]) -> &Slice { - unsafe { mem::transmute(s) } + unsafe { &*(s as *const [u8] as *const Slice) } } pub fn from_str(s: &str) -> &Slice { @@ -162,12 +162,12 @@ impl Slice { #[inline] pub fn into_box(&self) -> Box { let boxed: Box<[u8]> = self.inner.into(); - unsafe { mem::transmute(boxed) } + unsafe { Box::from_raw(Box::into_raw(boxed) as *mut Slice) } } pub fn empty_box() -> Box { let boxed: Box<[u8]> = Default::default(); - unsafe { mem::transmute(boxed) } + unsafe { Box::from_raw(Box::into_raw(boxed) as *mut Slice) } } #[inline] diff --git a/src/libstd/sys/unix/os_str.rs b/src/libstd/sys/unix/os_str.rs index e034938799866..19a7d341d479e 100644 --- a/src/libstd/sys/unix/os_str.rs +++ b/src/libstd/sys/unix/os_str.rs @@ -14,7 +14,6 @@ use borrow::Cow; use fmt; use str; -use mem; use rc::Rc; use sync::Arc; use sys_common::{AsInner, IntoInner}; @@ -105,7 +104,7 @@ impl Buf { } pub fn as_slice(&self) -> &Slice { - unsafe { mem::transmute(&*self.inner) } + unsafe { &*(&*self.inner as *const [u8] as *const Slice) } } pub fn into_string(self) -> Result { @@ -118,12 +117,13 @@ impl Buf { #[inline] pub fn into_box(self) -> Box { - unsafe { mem::transmute(self.inner.into_boxed_slice()) } + let boxed = self.inner.into_boxed_slice(); + unsafe { Box::from_raw(Box::into_raw(boxed) as *mut Slice) } } #[inline] pub fn from_box(boxed: Box) -> Buf { - let inner: Box<[u8]> = unsafe { mem::transmute(boxed) }; + let inner = unsafe { Box::from_raw(Box::into_raw(boxed) as *mut [u8]) }; Buf { inner: inner.into_vec() } } @@ -140,7 +140,7 @@ impl Buf { impl Slice { fn from_u8_slice(s: &[u8]) -> &Slice { - unsafe { mem::transmute(s) } + unsafe { &*(s as *const [u8] as *const Slice) } } pub fn from_str(s: &str) -> &Slice { @@ -162,12 +162,12 @@ impl Slice { #[inline] pub fn into_box(&self) -> Box { let boxed: Box<[u8]> = self.inner.into(); - unsafe { mem::transmute(boxed) } + unsafe { Box::from_raw(Box::into_raw(boxed) as *mut Slice) } } pub fn empty_box() -> Box { let boxed: Box<[u8]> = Default::default(); - unsafe { mem::transmute(boxed) } + unsafe { Box::from_raw(Box::into_raw(boxed) as *mut Slice) } } #[inline] diff --git a/src/libstd/sys/wasm/os_str.rs b/src/libstd/sys/wasm/os_str.rs index 543c22ebe18a3..17833e12fd824 100644 --- a/src/libstd/sys/wasm/os_str.rs +++ b/src/libstd/sys/wasm/os_str.rs @@ -14,7 +14,6 @@ use borrow::Cow; use fmt; use str; -use mem; use rc::Rc; use sync::Arc; use sys_common::{AsInner, IntoInner}; @@ -105,7 +104,7 @@ impl Buf { } pub fn as_slice(&self) -> &Slice { - unsafe { mem::transmute(&*self.inner) } + unsafe { &*(&*self.inner as *const [u8] as *const Slice) } } pub fn into_string(self) -> Result { @@ -118,12 +117,13 @@ impl Buf { #[inline] pub fn into_box(self) -> Box { - unsafe { mem::transmute(self.inner.into_boxed_slice()) } + let boxed = self.inner.into_boxed_slice(); + unsafe { Box::from_raw(Box::into_raw(boxed) as *mut Slice) } } #[inline] pub fn from_box(boxed: Box) -> Buf { - let inner: Box<[u8]> = unsafe { mem::transmute(boxed) }; + let inner = unsafe { Box::from_raw(Box::into_raw(boxed) as *mut [u8]) }; Buf { inner: inner.into_vec() } } @@ -140,7 +140,7 @@ impl Buf { impl Slice { fn from_u8_slice(s: &[u8]) -> &Slice { - unsafe { mem::transmute(s) } + unsafe { &*(s as *const [u8] as *const Slice) } } pub fn from_str(s: &str) -> &Slice { @@ -162,12 +162,12 @@ impl Slice { #[inline] pub fn into_box(&self) -> Box { let boxed: Box<[u8]> = self.inner.into(); - unsafe { mem::transmute(boxed) } + unsafe { Box::from_raw(Box::into_raw(boxed) as *mut Slice) } } pub fn empty_box() -> Box { let boxed: Box<[u8]> = Default::default(); - unsafe { mem::transmute(boxed) } + unsafe { Box::from_raw(Box::into_raw(boxed) as *mut Slice) } } #[inline] diff --git a/src/libstd/sys/windows/os_str.rs b/src/libstd/sys/windows/os_str.rs index 414c9c5418e45..baa20ff80490f 100644 --- a/src/libstd/sys/windows/os_str.rs +++ b/src/libstd/sys/windows/os_str.rs @@ -14,7 +14,6 @@ use borrow::Cow; use fmt; use sys_common::wtf8::{Wtf8, Wtf8Buf}; -use mem; use rc::Rc; use sync::Arc; use sys_common::{AsInner, IntoInner, FromInner}; @@ -90,7 +89,7 @@ impl Buf { } pub fn as_slice(&self) -> &Slice { - unsafe { mem::transmute(self.inner.as_slice()) } + unsafe { &*(self.inner.as_slice() as *const Wtf8 as *const Slice) } } pub fn into_string(self) -> Result { @@ -115,12 +114,13 @@ impl Buf { #[inline] pub fn into_box(self) -> Box { - unsafe { mem::transmute(self.inner.into_box()) } + let boxed = self.inner.into_box(); + unsafe { Box::from_raw(Box::into_raw(boxed) as *mut Slice) } } #[inline] pub fn from_box(boxed: Box) -> Buf { - let inner: Box = unsafe { mem::transmute(boxed) }; + let inner = unsafe { Box::from_raw(Box::into_raw(boxed) as *mut Wtf8) }; Buf { inner: Wtf8Buf::from_box(inner) } } @@ -137,7 +137,7 @@ impl Buf { impl Slice { pub fn from_str(s: &str) -> &Slice { - unsafe { mem::transmute(Wtf8::from_str(s)) } + unsafe { &*(Wtf8::from_str(s) as *const Wtf8 as *const Slice) } } pub fn to_str(&self) -> Option<&str> { @@ -156,11 +156,13 @@ impl Slice { #[inline] pub fn into_box(&self) -> Box { - unsafe { mem::transmute(self.inner.into_box()) } + let boxed = self.inner.into_box(); + unsafe { Box::from_raw(Box::into_raw(boxed) as *mut Slice) } } pub fn empty_box() -> Box { - unsafe { mem::transmute(Wtf8::empty_box()) } + let boxed = Wtf8::empty_box(); + unsafe { Box::from_raw(Box::into_raw(boxed) as *mut Slice) } } #[inline] diff --git a/src/libstd_unicode/lossy.rs b/src/libstd_unicode/lossy.rs index cc8e93308a527..592468ff7f40e 100644 --- a/src/libstd_unicode/lossy.rs +++ b/src/libstd_unicode/lossy.rs @@ -12,7 +12,6 @@ use core::str as core_str; use core::fmt; use core::fmt::Write; use char; -use core::mem; /// Lossy UTF-8 string. @@ -27,7 +26,7 @@ impl Utf8Lossy { } pub fn from_bytes(bytes: &[u8]) -> &Utf8Lossy { - unsafe { mem::transmute(bytes) } + unsafe { &*(bytes as *const [u8] as *const Utf8Lossy) } } pub fn chunks(&self) -> Utf8LossyChunksIter {