Skip to content

Commit 90a17e9

Browse files
committed
Replace many transmutes with casts or safe code
1 parent 8724337 commit 90a17e9

File tree

13 files changed

+53
-60
lines changed

13 files changed

+53
-60
lines changed

src/bootstrap/cache.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use std::ffi::OsStr;
1717
use std::fmt;
1818
use std::hash::{Hash, Hasher};
1919
use std::marker::PhantomData;
20-
use std::mem;
2120
use std::ops::Deref;
2221
use std::path::{Path, PathBuf};
2322
use std::sync::Mutex;
@@ -114,43 +113,47 @@ impl Deref for Interned<String> {
114113
type Target = str;
115114
fn deref(&self) -> &'static str {
116115
let l = INTERNER.strs.lock().unwrap();
117-
unsafe { mem::transmute::<&str, &'static str>(l.get(*self)) }
116+
let s: &str = l.get(*self).as_ref();
117+
unsafe { &*(s *const _) }
118118
}
119119
}
120120

121121
impl Deref for Interned<PathBuf> {
122122
type Target = Path;
123123
fn deref(&self) -> &'static Path {
124124
let l = INTERNER.paths.lock().unwrap();
125-
unsafe { mem::transmute::<&Path, &'static Path>(l.get(*self)) }
125+
let p: &Path = l.get(*self).as_ref();
126+
unsafe { &*(p *const _) }
126127
}
127128
}
128129

129130
impl AsRef<Path> for Interned<PathBuf> {
130131
fn as_ref(&self) -> &'static Path {
131-
let l = INTERNER.paths.lock().unwrap();
132-
unsafe { mem::transmute::<&Path, &'static Path>(l.get(*self)) }
132+
&*self
133133
}
134134
}
135135

136136
impl AsRef<Path> for Interned<String> {
137137
fn as_ref(&self) -> &'static Path {
138138
let l = INTERNER.strs.lock().unwrap();
139-
unsafe { mem::transmute::<&Path, &'static Path>(l.get(*self).as_ref()) }
139+
let p: &Path = l.get(*self).as_ref();
140+
unsafe { &*(p *const _) }
140141
}
141142
}
142143

143144
impl AsRef<OsStr> for Interned<PathBuf> {
144145
fn as_ref(&self) -> &'static OsStr {
145146
let l = INTERNER.paths.lock().unwrap();
146-
unsafe { mem::transmute::<&OsStr, &'static OsStr>(l.get(*self).as_ref()) }
147+
let s: &OsStr = l.get(*self).as_ref();
148+
unsafe { &*(s *const _) }
147149
}
148150
}
149151

150152
impl AsRef<OsStr> for Interned<String> {
151153
fn as_ref(&self) -> &'static OsStr {
152154
let l = INTERNER.strs.lock().unwrap();
153-
unsafe { mem::transmute::<&OsStr, &'static OsStr>(l.get(*self).as_ref()) }
155+
let s: &OsStr = l.get(*self).as_ref();
156+
unsafe { &*(s *const _) }
154157
}
155158
}
156159

src/librustc/ty/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ use std::ops::Deref;
4646
use std::rc::Rc;
4747
use std::slice;
4848
use std::vec::IntoIter;
49-
use std::mem;
5049
use syntax::ast::{self, DUMMY_NODE_ID, Name, Ident, NodeId};
5150
use syntax::attr;
5251
use syntax::ext::hygiene::{Mark, SyntaxContext};
@@ -592,7 +591,8 @@ impl<'tcx> serialize::UseSpecializedDecodable for &'tcx Slice<Ty<'tcx>> {}
592591
impl<T> Slice<T> {
593592
pub fn empty<'a>() -> &'a Slice<T> {
594593
unsafe {
595-
mem::transmute(slice::from_raw_parts(0x1 as *const T, 0))
594+
let slice = slice::from_raw_parts(0x1 as *const T, 0);
595+
&*(slice as *const [T] as *const Slice<T>)
596596
}
597597
}
598598
}

src/librustc_data_structures/blake2b.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,7 @@ fn blake2b_compress(ctx: &mut Blake2bCtx, last: bool) {
128128
// before it's overwritten.
129129

130130
let m: &mut [u64; 16] = unsafe {
131-
let b: &mut [u8; 128] = &mut ctx.b;
132-
::std::mem::transmute(b)
131+
&*(&mut ctx.b as *mut [u8; 128] as *mut [u64; 16])
133132
};
134133

135134
if cfg!(target_endian = "big") {

src/librustc_data_structures/stable_hasher.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -241,21 +241,15 @@ impl<CTX> HashStable<CTX> for f32 {
241241
fn hash_stable<W: StableHasherResult>(&self,
242242
ctx: &mut CTX,
243243
hasher: &mut StableHasher<W>) {
244-
let val: u32 = unsafe {
245-
::std::mem::transmute(*self)
246-
};
247-
val.hash_stable(ctx, hasher);
244+
self.to_bits().hash_stable(ctx, hasher);
248245
}
249246
}
250247

251248
impl<CTX> HashStable<CTX> for f64 {
252249
fn hash_stable<W: StableHasherResult>(&self,
253250
ctx: &mut CTX,
254251
hasher: &mut StableHasher<W>) {
255-
let val: u64 = unsafe {
256-
::std::mem::transmute(*self)
257-
};
258-
val.hash_stable(ctx, hasher);
252+
self.to_bits().hash_stable(ctx, hasher);
259253
}
260254
}
261255

src/librustc_trans/type_.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use rustc::ty::layout::{self, Align, Size};
2121

2222
use std::ffi::CString;
2323
use std::fmt;
24-
use std::mem;
2524
use std::ptr;
2625

2726
use libc::c_uint;
@@ -59,7 +58,7 @@ impl Type {
5958
}
6059

6160
pub fn to_ref_slice(slice: &[Type]) -> &[TypeRef] {
62-
unsafe { mem::transmute(slice) }
61+
unsafe { &*(slice as *const [Type] as *const [TypeRef]) }
6362
}
6463

6564
pub fn void(ccx: &CrateContext) -> Type {

src/libstd/error.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ use char;
5959
use convert;
6060
use core::array;
6161
use fmt::{self, Debug, Display};
62-
use mem::transmute;
6362
use num;
6463
use str;
6564
use string;
@@ -491,11 +490,11 @@ impl Error + Send {
491490
#[stable(feature = "error_downcast", since = "1.3.0")]
492491
/// Attempt to downcast the box to a concrete type.
493492
pub fn downcast<T: Error + 'static>(self: Box<Self>)
494-
-> Result<Box<T>, Box<Error + Send>> {
493+
-> Result<Box<T>, Box<Self>> {
495494
let err: Box<Error> = self;
496495
<Error>::downcast(err).map_err(|s| unsafe {
497496
// reapply the Send marker
498-
transmute::<Box<Error>, Box<Error + Send>>(s)
497+
Box::from_raw(Box::into_raw(s) as *mut Self)
499498
})
500499
}
501500
}
@@ -509,7 +508,7 @@ impl Error + Send + Sync {
509508
let err: Box<Error> = self;
510509
<Error>::downcast(err).map_err(|s| unsafe {
511510
// reapply the Send+Sync marker
512-
transmute::<Box<Error>, Box<Error + Send + Sync>>(s)
511+
Box::from_raw(Box::into_raw(s) as *mut Self)
513512
})
514513
}
515514
}

src/libstd/sys/redox/args.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ impl DoubleEndedIterator for Args {
5757

5858
mod imp {
5959
use os::unix::prelude::*;
60-
use mem;
6160
use ffi::{CStr, OsString};
6261
use marker::PhantomData;
6362
use libc;
@@ -105,7 +104,7 @@ mod imp {
105104
}
106105

107106
fn get_global_ptr() -> *mut Option<Box<Vec<Vec<u8>>>> {
108-
unsafe { mem::transmute(&GLOBAL_ARGS_PTR) }
107+
unsafe { &mut GLOBAL_ARGS_PTR as *mut _ as *mut _ }
109108
}
110109

111110
}

src/libstd/sys/redox/ext/ffi.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#![stable(feature = "rust1", since = "1.0.0")]
1414

1515
use ffi::{OsStr, OsString};
16-
use mem;
1716
use sys::os_str::Buf;
1817
use sys_common::{FromInner, IntoInner, AsInner};
1918

@@ -53,7 +52,7 @@ pub trait OsStrExt {
5352
#[stable(feature = "rust1", since = "1.0.0")]
5453
impl OsStrExt for OsStr {
5554
fn from_bytes(slice: &[u8]) -> &OsStr {
56-
unsafe { mem::transmute(slice) }
55+
unsafe { &*(slice as *const [u8] as *const OsStr) }
5756
}
5857
fn as_bytes(&self) -> &[u8] {
5958
&self.as_inner().inner

src/libstd/sys/redox/os_str.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use borrow::Cow;
1515
use fmt;
1616
use str;
17-
use mem;
1817
use rc::Rc;
1918
use sync::Arc;
2019
use sys_common::{AsInner, IntoInner};
@@ -105,7 +104,7 @@ impl Buf {
105104
}
106105

107106
pub fn as_slice(&self) -> &Slice {
108-
unsafe { mem::transmute(&*self.inner) }
107+
&*(&*self.inner as *const [u8] as *const Slice)
109108
}
110109

111110
pub fn into_string(self) -> Result<String, Buf> {
@@ -118,12 +117,13 @@ impl Buf {
118117

119118
#[inline]
120119
pub fn into_box(self) -> Box<Slice> {
121-
unsafe { mem::transmute(self.inner.into_boxed_slice()) }
120+
let boxed = self.inner.into_boxed_slice();
121+
unsafe { Box::from_raw(Box::into_raw(boxed) as *mut Slice) }
122122
}
123123

124124
#[inline]
125125
pub fn from_box(boxed: Box<Slice>) -> Buf {
126-
let inner: Box<[u8]> = unsafe { mem::transmute(boxed) };
126+
let inner = unsafe { Box::from_raw(Box::into_raw(boxed) as *mut [u8]) };
127127
Buf { inner: inner.into_vec() }
128128
}
129129

@@ -140,7 +140,7 @@ impl Buf {
140140

141141
impl Slice {
142142
fn from_u8_slice(s: &[u8]) -> &Slice {
143-
unsafe { mem::transmute(s) }
143+
unsafe { &*(s as *const [u8] as *const Slice) }
144144
}
145145

146146
pub fn from_str(s: &str) -> &Slice {
@@ -162,12 +162,12 @@ impl Slice {
162162
#[inline]
163163
pub fn into_box(&self) -> Box<Slice> {
164164
let boxed: Box<[u8]> = self.inner.into();
165-
unsafe { mem::transmute(boxed) }
165+
unsafe { Box::from_raw(Box::into_raw(boxed) as *mut Slice) }
166166
}
167167

168168
pub fn empty_box() -> Box<Slice> {
169169
let boxed: Box<[u8]> = Default::default();
170-
unsafe { mem::transmute(boxed) }
170+
unsafe { Box::from_raw(Box::into_raw(boxed) as *mut Slice) }
171171
}
172172

173173
#[inline]

src/libstd/sys/unix/os_str.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use borrow::Cow;
1515
use fmt;
1616
use str;
17-
use mem;
1817
use rc::Rc;
1918
use sync::Arc;
2019
use sys_common::{AsInner, IntoInner};
@@ -105,7 +104,7 @@ impl Buf {
105104
}
106105

107106
pub fn as_slice(&self) -> &Slice {
108-
unsafe { mem::transmute(&*self.inner) }
107+
&*(&*self.inner as *const [u8] as *const Slice)
109108
}
110109

111110
pub fn into_string(self) -> Result<String, Buf> {
@@ -118,12 +117,13 @@ impl Buf {
118117

119118
#[inline]
120119
pub fn into_box(self) -> Box<Slice> {
121-
unsafe { mem::transmute(self.inner.into_boxed_slice()) }
120+
let boxed = self.inner.into_boxed_slice();
121+
unsafe { Box::from_raw(Box::into_raw(boxed) as *mut Slice) }
122122
}
123123

124124
#[inline]
125125
pub fn from_box(boxed: Box<Slice>) -> Buf {
126-
let inner: Box<[u8]> = unsafe { mem::transmute(boxed) };
126+
let inner = unsafe { Box::from_raw(Box::into_raw(boxed) as *mut [u8]) };
127127
Buf { inner: inner.into_vec() }
128128
}
129129

@@ -140,7 +140,7 @@ impl Buf {
140140

141141
impl Slice {
142142
fn from_u8_slice(s: &[u8]) -> &Slice {
143-
unsafe { mem::transmute(s) }
143+
unsafe { &*(s as *const [u8] as *const Slice) }
144144
}
145145

146146
pub fn from_str(s: &str) -> &Slice {
@@ -162,12 +162,12 @@ impl Slice {
162162
#[inline]
163163
pub fn into_box(&self) -> Box<Slice> {
164164
let boxed: Box<[u8]> = self.inner.into();
165-
unsafe { mem::transmute(boxed) }
165+
unsafe { Box::from_raw(Box::into_raw(boxed) as *mut Slice) }
166166
}
167167

168168
pub fn empty_box() -> Box<Slice> {
169169
let boxed: Box<[u8]> = Default::default();
170-
unsafe { mem::transmute(boxed) }
170+
unsafe { Box::from_raw(Box::into_raw(boxed) as *mut Slice) }
171171
}
172172

173173
#[inline]

src/libstd/sys/wasm/os_str.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use borrow::Cow;
1515
use fmt;
1616
use str;
17-
use mem;
1817
use rc::Rc;
1918
use sync::Arc;
2019
use sys_common::{AsInner, IntoInner};
@@ -105,7 +104,7 @@ impl Buf {
105104
}
106105

107106
pub fn as_slice(&self) -> &Slice {
108-
unsafe { mem::transmute(&*self.inner) }
107+
&*(&*self.inner as *const [u8] as *const Slice)
109108
}
110109

111110
pub fn into_string(self) -> Result<String, Buf> {
@@ -118,12 +117,13 @@ impl Buf {
118117

119118
#[inline]
120119
pub fn into_box(self) -> Box<Slice> {
121-
unsafe { mem::transmute(self.inner.into_boxed_slice()) }
120+
let boxed = self.inner.into_boxed_slice();
121+
unsafe { Box::from_raw(Box::into_raw(boxed) as *mut Slice) }
122122
}
123123

124124
#[inline]
125125
pub fn from_box(boxed: Box<Slice>) -> Buf {
126-
let inner: Box<[u8]> = unsafe { mem::transmute(boxed) };
126+
let inner = unsafe { Box::from_raw(Box::into_raw(boxed) as *mut [u8]) };
127127
Buf { inner: inner.into_vec() }
128128
}
129129

@@ -140,7 +140,7 @@ impl Buf {
140140

141141
impl Slice {
142142
fn from_u8_slice(s: &[u8]) -> &Slice {
143-
unsafe { mem::transmute(s) }
143+
unsafe { &*(s as *const [u8] as *const Slice) }
144144
}
145145

146146
pub fn from_str(s: &str) -> &Slice {
@@ -162,12 +162,12 @@ impl Slice {
162162
#[inline]
163163
pub fn into_box(&self) -> Box<Slice> {
164164
let boxed: Box<[u8]> = self.inner.into();
165-
unsafe { mem::transmute(boxed) }
165+
unsafe { Box::from_raw(Box::into_raw(boxed) as *mut Slice) }
166166
}
167167

168168
pub fn empty_box() -> Box<Slice> {
169169
let boxed: Box<[u8]> = Default::default();
170-
unsafe { mem::transmute(boxed) }
170+
unsafe { Box::from_raw(Box::into_raw(boxed) as *mut Slice) }
171171
}
172172

173173
#[inline]

0 commit comments

Comments
 (0)