Skip to content

Commit 657b09e

Browse files
committed
librustc: Rename reinterpret_cast to transmute_copy and remove the intrinsic
1 parent 4c148a5 commit 657b09e

23 files changed

+79
-97
lines changed

src/libcore/at_vec.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ pub mod traits {}
185185

186186
pub mod raw {
187187
use at_vec::{capacity, rustrt};
188-
use cast::transmute;
188+
use cast::{transmute, transmute_copy};
189189
use libc;
190190
use ptr;
191191
use sys;
@@ -211,12 +211,11 @@ pub mod raw {
211211

212212
#[inline(always)]
213213
pub unsafe fn push<T>(v: &mut @[T], initval: T) {
214-
let repr: **VecRepr = ::cast::reinterpret_cast(&v);
214+
let repr: **VecRepr = transmute_copy(&v);
215215
let fill = (**repr).unboxed.fill;
216216
if (**repr).unboxed.alloc > fill {
217217
push_fast(v, initval);
218-
}
219-
else {
218+
} else {
220219
push_slow(v, initval);
221220
}
222221
}

src/libcore/cast.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,19 @@ pub unsafe fn reinterpret_cast<T, U>(src: &T) -> U {
3636
rusti::reinterpret_cast(*src)
3737
}
3838

39+
/// Unsafely copies and casts the value at `src` to U, even if the value is
40+
/// noncopyable. The two types must have the same length.
41+
#[inline(always)]
42+
#[cfg(stage0)]
43+
pub unsafe fn transmute_copy<T, U>(src: &T) -> U {
44+
rusti::reinterpret_cast(*src)
45+
}
46+
3947
#[inline(always)]
4048
#[cfg(stage1)]
4149
#[cfg(stage2)]
4250
#[cfg(stage3)]
43-
pub unsafe fn reinterpret_cast<T, U>(src: &T) -> U {
51+
pub unsafe fn transmute_copy<T, U>(src: &T) -> U {
4452
let mut dest: U = unstable::intrinsics::init();
4553
{
4654
let dest_ptr: *mut u8 = rusti::transmute(&mut dest);
@@ -148,11 +156,20 @@ pub unsafe fn copy_lifetime_vec<'a,S,T>(_ptr: &'a [S], ptr: &T) -> &'a T {
148156

149157
#[cfg(test)]
150158
mod tests {
151-
use cast::{bump_box_refcount, reinterpret_cast, transmute};
159+
use cast::{bump_box_refcount, transmute};
152160

153161
#[test]
162+
#[cfg(stage0)]
154163
fn test_reinterpret_cast() {
155-
assert!(1u == unsafe { reinterpret_cast(&1) });
164+
assert!(1u == unsafe { ::cast::reinterpret_cast(&1) });
165+
}
166+
167+
#[test]
168+
#[cfg(stage1)]
169+
#[cfg(stage2)]
170+
#[cfg(stage3)]
171+
fn test_transmute_copy() {
172+
assert!(1u == unsafe { ::cast::transmute_copy(&1) });
156173
}
157174

158175
#[test]

src/libcore/pipes.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ bounded and unbounded protocols allows for less code duplication.
8282
8383
*/
8484

85-
use cast::{forget, reinterpret_cast, transmute};
85+
use cast::{forget, transmute, transmute_copy};
8686
use either::{Either, Left, Right};
8787
use kinds::Owned;
8888
use libc;
@@ -131,7 +131,7 @@ pub struct PacketHeader {
131131
mut state: State,
132132
mut blocked_task: *rust_task,
133133

134-
// This is a reinterpret_cast of a ~buffer, that can also be cast
134+
// This is a transmute_copy of a ~buffer, that can also be cast
135135
// to a buffer_header if need be.
136136
mut buffer: *libc::c_void,
137137
}
@@ -170,12 +170,12 @@ pub impl PacketHeader {
170170
// thing. You'll proobably want to forget them when you're done.
171171
unsafe fn buf_header(&self) -> ~BufferHeader {
172172
assert!(self.buffer.is_not_null());
173-
reinterpret_cast(&self.buffer)
173+
transmute_copy(&self.buffer)
174174
}
175175

176176
fn set_buffer<T:Owned>(&self, b: ~Buffer<T>) {
177177
unsafe {
178-
self.buffer = reinterpret_cast(&b);
178+
self.buffer = transmute_copy(&b);
179179
}
180180
}
181181
}
@@ -211,7 +211,7 @@ fn unibuffer<T>() -> ~Buffer<Packet<T>> {
211211
};
212212

213213
unsafe {
214-
b.data.header.buffer = reinterpret_cast(&b);
214+
b.data.header.buffer = transmute_copy(&b);
215215
}
216216
b
217217
}
@@ -229,7 +229,7 @@ pub fn entangle_buffer<T:Owned,Tstart:Owned>(
229229
init: &fn(*libc::c_void, x: &T) -> *Packet<Tstart>)
230230
-> (SendPacketBuffered<Tstart, T>, RecvPacketBuffered<Tstart, T>)
231231
{
232-
let p = init(unsafe { reinterpret_cast(&buffer) }, &buffer.data);
232+
let p = init(unsafe { transmute_copy(&buffer) }, &buffer.data);
233233
unsafe { forget(buffer) }
234234
(SendPacketBuffered(p), RecvPacketBuffered(p))
235235
}

src/libcore/ptr.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ pub unsafe fn set_memory<T>(dst: *mut T, c: int, count: uint) {
119119
/**
120120
Transform a region pointer - &T - to an unsafe pointer - *T.
121121
This is safe, but is implemented with an unsafe block due to
122-
reinterpret_cast.
122+
transmute.
123123
*/
124124
#[inline(always)]
125125
pub fn to_unsafe_ptr<T>(thing: &T) -> *T {
@@ -129,7 +129,7 @@ pub fn to_unsafe_ptr<T>(thing: &T) -> *T {
129129
/**
130130
Transform a const region pointer - &const T - to a const unsafe pointer -
131131
*const T. This is safe, but is implemented with an unsafe block due to
132-
reinterpret_cast.
132+
transmute.
133133
*/
134134
#[inline(always)]
135135
pub fn to_const_unsafe_ptr<T>(thing: &const T) -> *const T {
@@ -139,7 +139,7 @@ pub fn to_const_unsafe_ptr<T>(thing: &const T) -> *const T {
139139
/**
140140
Transform a mutable region pointer - &mut T - to a mutable unsafe pointer -
141141
*mut T. This is safe, but is implemented with an unsafe block due to
142-
reinterpret_cast.
142+
transmute.
143143
*/
144144
#[inline(always)]
145145
pub fn to_mut_unsafe_ptr<T>(thing: &mut T) -> *mut T {
@@ -149,7 +149,7 @@ pub fn to_mut_unsafe_ptr<T>(thing: &mut T) -> *mut T {
149149
/**
150150
Cast a region pointer - &T - to a uint.
151151
This is safe, but is implemented with an unsafe block due to
152-
reinterpret_cast.
152+
transmute.
153153
154154
(I couldn't think of a cutesy name for this one.)
155155
*/

src/libcore/sys.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ pub fn pref_align_of_val<T>(_val: &T) -> uint {
154154
#[inline(always)]
155155
pub fn refcount<T>(t: @T) -> uint {
156156
unsafe {
157-
let ref_ptr: *uint = cast::reinterpret_cast(&t);
158-
*ref_ptr - 1
157+
let ref_ptr: *uint = cast::transmute(t);
158+
*ref_ptr
159159
}
160160
}
161161

src/libcore/task/local_data_priv.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ unsafe fn get_task_local_map(task: *rust_task) -> TaskLocalMap {
5959
let map_ptr = rt::rust_get_task_local_data(task);
6060
if map_ptr.is_null() {
6161
let map: TaskLocalMap = @mut ~[];
62-
// Use reinterpret_cast -- transmute would take map away from us also.
63-
rt::rust_set_task_local_data(
64-
task, cast::transmute(map));
62+
rt::rust_set_task_local_data(task, cast::transmute(map));
6563
rt::rust_task_local_data_atexit(task, cleanup_task_local_map);
6664
// Also need to reference it an extra time to keep it for now.
6765
let nonmut = cast::transmute::<TaskLocalMap,
@@ -77,12 +75,10 @@ unsafe fn get_task_local_map(task: *rust_task) -> TaskLocalMap {
7775
}
7876
}
7977

80-
unsafe fn key_to_key_value<T:Durable>(
81-
key: LocalDataKey<T>) -> *libc::c_void {
82-
78+
unsafe fn key_to_key_value<T:Durable>(key: LocalDataKey<T>) -> *libc::c_void {
8379
// Keys are closures, which are (fnptr,envptr) pairs. Use fnptr.
8480
// Use reintepret_cast -- transmute would leak (forget) the closure.
85-
let pair: (*libc::c_void, *libc::c_void) = cast::reinterpret_cast(&key);
81+
let pair: (*libc::c_void, *libc::c_void) = cast::transmute_copy(&key);
8682
pair.first()
8783
}
8884

src/libcore/unstable/intrinsics.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ pub extern "rust-intrinsic" {
4747
pub fn forget<T>(_: T) -> ();
4848

4949
// XXX: intrinsic uses legacy modes
50+
#[cfg(stage0)]
5051
fn reinterpret_cast<T,U>(&&src: T) -> U;
5152

5253
pub fn needs_drop<T>() -> bool;

src/librustc/middle/trans/foreign.rs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -718,35 +718,6 @@ pub fn trans_intrinsic(ccx: @CrateContext,
718718
}
719719
}
720720
~"forget" => {}
721-
~"reinterpret_cast" => {
722-
let tp_ty = substs.tys[0];
723-
let lltp_ty = type_of::type_of(ccx, tp_ty);
724-
let llout_ty = type_of::type_of(ccx, substs.tys[1]);
725-
let tp_sz = machine::llbitsize_of_real(ccx, lltp_ty),
726-
out_sz = machine::llbitsize_of_real(ccx, llout_ty);
727-
if tp_sz != out_sz {
728-
let sp = match *ccx.tcx.items.get(&ref_id.get()) {
729-
ast_map::node_expr(e) => e.span,
730-
_ => fail!(~"reinterpret_cast or forget has non-expr arg")
731-
};
732-
ccx.sess.span_fatal(
733-
sp, fmt!("reinterpret_cast called on types \
734-
with different size: %s (%u bit(s)) to %s \
735-
(%u bit(s))",
736-
ty_to_str(ccx.tcx, tp_ty), tp_sz,
737-
ty_to_str(ccx.tcx, substs.tys[1]), out_sz));
738-
}
739-
if !ty::type_is_nil(substs.tys[1]) {
740-
// NB: Do not use a Load and Store here. This causes
741-
// massive code bloat when reinterpret_cast is used on
742-
// large structural types.
743-
let llretptr = fcx.llretptr.get();
744-
let llretptr = PointerCast(bcx, llretptr, T_ptr(T_i8()));
745-
let llcast = get_param(decl, first_real_arg);
746-
let llcast = PointerCast(bcx, llcast, T_ptr(T_i8()));
747-
call_memcpy(bcx, llretptr, llcast, llsize_of(ccx, lltp_ty));
748-
}
749-
}
750721
~"transmute" => {
751722
let (in_type, out_type) = (substs.tys[0], substs.tys[1]);
752723
let llintype = type_of::type_of(ccx, in_type);

src/librustc/middle/trans/type_use.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,9 @@ pub fn type_uses_for(ccx: @CrateContext, fn_id: def_id, n_tps: uint)
124124
_) => {
125125
if abi.is_intrinsic() {
126126
let flags = match *cx.ccx.sess.str_of(i.ident) {
127-
~"size_of" | ~"pref_align_of" | ~"min_align_of" |
128-
~"init" | ~"reinterpret_cast" | ~"transmute" |
129-
~"move_val" | ~"move_val_init" => use_repr,
127+
~"size_of" | ~"pref_align_of" | ~"min_align_of" |
128+
~"init" | ~"transmute" | ~"move_val" |
129+
~"move_val_init" => use_repr,
130130

131131
~"get_tydesc" | ~"needs_drop" => use_tydesc,
132132

src/librustc/middle/typeck/check/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3445,8 +3445,6 @@ pub fn check_intrinsic_type(ccx: @mut CrateCtxt, it: @ast::foreign_item) {
34453445
~"init" => (1u, ~[], param(ccx, 0u)),
34463446
~"forget" => (1u, ~[arg(ast::by_copy, param(ccx, 0u))],
34473447
ty::mk_nil()),
3448-
~"reinterpret_cast" => (2u, ~[arg(ast::by_ref, param(ccx, 0u))],
3449-
param(ccx, 1u)),
34503448
~"transmute" => (2, ~[arg(ast::by_copy, param(ccx, 0))], param(ccx, 1)),
34513449
~"move_val" | ~"move_val_init" => {
34523450
(1u, ~[arg(ast::by_copy,

src/test/compile-fail/packed-struct-generic-transmute.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// the error points to the start of the file, not the line with the
1414
// transmute
1515

16-
// error-pattern: reinterpret_cast called on types with different size
16+
// error-pattern: transmute called on types with different size
1717

1818
#[packed]
1919
struct Foo<T,S> {

src/test/compile-fail/packed-struct-transmute.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// the error points to the start of the file, not the line with the
1414
// transmute
1515

16-
// error-pattern: reinterpret_cast called on types with different size
16+
// error-pattern: transmute called on types with different size
1717

1818
#[packed]
1919
struct Foo {

src/test/run-fail/unwind-box-res.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct r {
2121
impl Drop for r {
2222
fn finalize(&self) {
2323
unsafe {
24-
let _v2: ~int = cast::reinterpret_cast(&self.v);
24+
let _v2: ~int = cast::transmute(self.v);
2525
}
2626
}
2727
}
@@ -35,7 +35,7 @@ fn r(v: *int) -> r {
3535
fn main() {
3636
unsafe {
3737
let i1 = ~0;
38-
let i1p = cast::reinterpret_cast(&i1);
38+
let i1p = cast::transmute_copy(&i1);
3939
cast::forget(i1);
4040
let x = @r(i1p);
4141
failfn();

src/test/run-pass/binops.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ fn test_box() {
6464

6565
fn test_ptr() {
6666
unsafe {
67-
let p1: *u8 = ::core::cast::reinterpret_cast(&0);
68-
let p2: *u8 = ::core::cast::reinterpret_cast(&0);
69-
let p3: *u8 = ::core::cast::reinterpret_cast(&1);
67+
let p1: *u8 = ::core::cast::transmute(0);
68+
let p2: *u8 = ::core::cast::transmute(0);
69+
let p3: *u8 = ::core::cast::transmute(1);
7070

7171
assert!(p1 == p2);
7272
assert!(p1 != p3);
@@ -107,8 +107,8 @@ fn test_class() {
107107

108108
unsafe {
109109
error!("q = %x, r = %x",
110-
(::core::cast::reinterpret_cast::<*p, uint>(& &q)),
111-
(::core::cast::reinterpret_cast::<*p, uint>(& &r)));
110+
(::core::cast::transmute::<*p, uint>(&q)),
111+
(::core::cast::transmute::<*p, uint>(&r)));
112112
}
113113
assert!((q == r));
114114
r.y = 17;

src/test/run-pass/enum-alignment.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn addr_of<T>(ptr: &T) -> uint {
1414
}
1515

1616
fn is_aligned<T>(ptr: &T) -> bool {
17-
(to_unsafe_ptr(ptr) % sys::min_align_of::<T>()) == 0
17+
(ptr::to_unsafe_ptr(ptr) % sys::min_align_of::<T>()) == 0
1818
}
1919

2020
pub fn main() {

src/test/run-pass/issue-2214.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use core::f64::*;
1616

1717
fn to_c_int(v: &mut int) -> &mut c_int {
1818
unsafe {
19-
cast::reinterpret_cast(&v)
19+
cast::transmute_copy(&v)
2020
}
2121
}
2222

src/test/run-pass/regions-mock-trans.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ struct Ccx {
2525

2626
fn alloc<'a>(_bcx : &'a arena) -> &'a Bcx<'a> {
2727
unsafe {
28-
return cast::reinterpret_cast(
29-
&libc::malloc(sys::size_of::<Bcx<'blk>>() as libc::size_t));
28+
cast::transmute(libc::malloc(sys::size_of::<Bcx<'blk>>()
29+
as libc::size_t));
3030
}
3131
}
3232

@@ -38,7 +38,7 @@ fn g(fcx : &Fcx) {
3838
let bcx = Bcx { fcx: fcx };
3939
let bcx2 = h(&bcx);
4040
unsafe {
41-
libc::free(cast::reinterpret_cast(&bcx2));
41+
libc::free(cast::transmute(bcx2));
4242
}
4343
}
4444

0 commit comments

Comments
 (0)