Skip to content

[WIP] Remove various transmutes #47005

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/bootstrap/cache.rs
Original file line number Diff line number Diff line change
@@ -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,43 +113,49 @@ impl Deref for Interned<String> {
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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's as_ref called on? If it's &String then you should remove .as_ref() and let it coerce.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for all the other 5 cases in this file.

unsafe { &*(s as *const _) }
}
}

impl Deref for Interned<PathBuf> {
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<Path> for Interned<PathBuf> {
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<Path> for Interned<String> {
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<OsStr> for Interned<PathBuf> {
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<OsStr> for Interned<String> {
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 _) }
}
}

4 changes: 2 additions & 2 deletions src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
@@ -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<Ty<'tcx>> {}
impl<T> Slice<T> {
pub fn empty<'a>() -> &'a Slice<T> {
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<T>)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be &*(mem::align_of::<T>() as *const [T; 0] as *const [T] as *const Slice<T>).

}
}
}
3 changes: 1 addition & 2 deletions src/librustc_data_structures/blake2b.rs
Original file line number Diff line number Diff line change
@@ -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") {
10 changes: 2 additions & 8 deletions src/librustc_data_structures/stable_hasher.rs
Original file line number Diff line number Diff line change
@@ -241,21 +241,15 @@ impl<CTX> HashStable<CTX> for f32 {
fn hash_stable<W: StableHasherResult>(&self,
ctx: &mut CTX,
hasher: &mut StableHasher<W>) {
let val: u32 = unsafe {
::std::mem::transmute(*self)
};
val.hash_stable(ctx, hasher);
self.to_bits().hash_stable(ctx, hasher);
}
}

impl<CTX> HashStable<CTX> for f64 {
fn hash_stable<W: StableHasherResult>(&self,
ctx: &mut CTX,
hasher: &mut StableHasher<W>) {
let val: u64 = unsafe {
::std::mem::transmute(*self)
};
val.hash_stable(ctx, hasher);
self.to_bits().hash_stable(ctx, hasher);
}
}

3 changes: 1 addition & 2 deletions src/librustc_trans/type_.rs
Original file line number Diff line number Diff line change
@@ -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 {
7 changes: 3 additions & 4 deletions src/libstd/error.rs
Original file line number Diff line number Diff line change
@@ -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<T: Error + 'static>(self: Box<Self>)
-> Result<Box<T>, Box<Error + Send>> {
-> Result<Box<T>, Box<Self>> {
let err: Box<Error> = self;
<Error>::downcast(err).map_err(|s| unsafe {
// reapply the Send marker
transmute::<Box<Error>, Box<Error + Send>>(s)
Box::from_raw(Box::into_raw(s) as *mut Self)
})
}
}
@@ -509,7 +508,7 @@ impl Error + Send + Sync {
let err: Box<Error> = self;
<Error>::downcast(err).map_err(|s| unsafe {
// reapply the Send+Sync marker
transmute::<Box<Error>, Box<Error + Send + Sync>>(s)
Box::from_raw(Box::into_raw(s) as *mut Self)
})
}
}
3 changes: 1 addition & 2 deletions src/libstd/sys/redox/args.rs
Original file line number Diff line number Diff line change
@@ -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<Box<Vec<Vec<u8>>>> {
unsafe { mem::transmute(&GLOBAL_ARGS_PTR) }
unsafe { &mut GLOBAL_ARGS_PTR as *mut _ as *mut _ }
}

}
3 changes: 1 addition & 2 deletions src/libstd/sys/redox/ext/ffi.rs
Original file line number Diff line number Diff line change
@@ -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
14 changes: 7 additions & 7 deletions src/libstd/sys/redox/os_str.rs
Original file line number Diff line number Diff line change
@@ -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<String, Buf> {
@@ -118,12 +117,13 @@ impl Buf {

#[inline]
pub fn into_box(self) -> Box<Slice> {
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<Slice>) -> 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<Slice> {
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<Slice> {
let boxed: Box<[u8]> = Default::default();
unsafe { mem::transmute(boxed) }
unsafe { Box::from_raw(Box::into_raw(boxed) as *mut Slice) }
}

#[inline]
14 changes: 7 additions & 7 deletions src/libstd/sys/unix/os_str.rs
Original file line number Diff line number Diff line change
@@ -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<String, Buf> {
@@ -118,12 +117,13 @@ impl Buf {

#[inline]
pub fn into_box(self) -> Box<Slice> {
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<Slice>) -> 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<Slice> {
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<Slice> {
let boxed: Box<[u8]> = Default::default();
unsafe { mem::transmute(boxed) }
unsafe { Box::from_raw(Box::into_raw(boxed) as *mut Slice) }
}

#[inline]
14 changes: 7 additions & 7 deletions src/libstd/sys/wasm/os_str.rs
Original file line number Diff line number Diff line change
@@ -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<String, Buf> {
@@ -118,12 +117,13 @@ impl Buf {

#[inline]
pub fn into_box(self) -> Box<Slice> {
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<Slice>) -> 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<Slice> {
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<Slice> {
let boxed: Box<[u8]> = Default::default();
unsafe { mem::transmute(boxed) }
unsafe { Box::from_raw(Box::into_raw(boxed) as *mut Slice) }
}

#[inline]
16 changes: 9 additions & 7 deletions src/libstd/sys/windows/os_str.rs
Original file line number Diff line number Diff line change
@@ -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<String, Buf> {
@@ -115,12 +114,13 @@ impl Buf {

#[inline]
pub fn into_box(self) -> Box<Slice> {
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<Slice>) -> Buf {
let inner: Box<Wtf8> = 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<Slice> {
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<Slice> {
unsafe { mem::transmute(Wtf8::empty_box()) }
let boxed = Wtf8::empty_box();
unsafe { Box::from_raw(Box::into_raw(boxed) as *mut Slice) }
}

#[inline]
3 changes: 1 addition & 2 deletions src/libstd_unicode/lossy.rs
Original file line number Diff line number Diff line change
@@ -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 {