Skip to content

Commit f96ad30

Browse files
committed
stdlib: Make reinterpret_cast and leak unsafe
1 parent ad66d72 commit f96ad30

File tree

7 files changed

+23
-17
lines changed

7 files changed

+23
-17
lines changed

src/comp/middle/trans_build.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ fn IndirectBr(cx: @block_ctxt, Addr: ValueRef, NumDests: uint) {
8181

8282
// This is a really awful way to get a zero-length c-string, but better (and a
8383
// lot more efficient) than doing str::as_buf("", ...) every time.
84-
fn noname() -> sbuf {
84+
fn noname() -> sbuf unsafe {
8585
const cnull: uint = 0u;
8686
ret std::unsafe::reinterpret_cast(std::ptr::addr_of(cnull));
8787
}
@@ -480,9 +480,11 @@ fn Phi(cx: @block_ctxt, Ty: TypeRef, vals: [ValueRef], bbs: [BasicBlockRef])
480480

481481
fn AddIncomingToPhi(phi: ValueRef, val: ValueRef, bb: BasicBlockRef) {
482482
if llvm::LLVMIsUndef(phi) == lib::llvm::True { ret; }
483-
let valptr = std::unsafe::reinterpret_cast(std::ptr::addr_of(val));
484-
let bbptr = std::unsafe::reinterpret_cast(std::ptr::addr_of(bb));
485-
llvm::LLVMAddIncoming(phi, valptr, bbptr, 1u);
483+
unsafe {
484+
let valptr = std::unsafe::reinterpret_cast(std::ptr::addr_of(val));
485+
let bbptr = std::unsafe::reinterpret_cast(std::ptr::addr_of(bb));
486+
llvm::LLVMAddIncoming(phi, valptr, bbptr, 1u);
487+
}
486488
}
487489

488490
fn _UndefReturn(Fn: ValueRef) -> ValueRef {

src/lib/box.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ Function: ptr_eq
1111
Determine if two shared boxes point to the same object
1212
*/
1313
fn ptr_eq<T>(a: @T, b: @T) -> bool {
14-
let a_ptr: uint = unsafe::reinterpret_cast(a);
15-
let b_ptr: uint = unsafe::reinterpret_cast(b);
16-
ret a_ptr == b_ptr;
14+
// FIXME: ptr::addr_of
15+
unsafe {
16+
let a_ptr: uint = unsafe::reinterpret_cast(a);
17+
let b_ptr: uint = unsafe::reinterpret_cast(b);
18+
ret a_ptr == b_ptr;
19+
}
1720
}

src/lib/ptr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ Function: null
2929
3030
Create an unsafe null pointer
3131
*/
32-
fn null<T>() -> *T { ret unsafe::reinterpret_cast(0u); }
32+
fn null<T>() -> *T unsafe { ret unsafe::reinterpret_cast(0u); }

src/lib/run_program.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ type program = obj {
9292
fn arg_vec(prog: str, args: [@str]) -> [sbuf] {
9393
let argptrs = str::as_buf(prog, {|buf| [buf] });
9494
for arg in args { argptrs += str::as_buf(*arg, {|buf| [buf] }); }
95-
argptrs += [unsafe::reinterpret_cast(0)];
95+
// FIXME: ptr::null instead of cast
96+
argptrs += [unsafe {unsafe::reinterpret_cast(0)}];
9697
ret argptrs;
9798
}
9899

src/lib/str.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ Function: byte_len
127127
128128
Returns the length in bytes of a string
129129
*/
130-
fn byte_len(s: str) -> uint {
130+
fn byte_len(s: str) -> uint unsafe {
131131
let v: [u8] = unsafe::reinterpret_cast(s);
132132
let vlen = vec::len(v);
133133
unsafe::leak(v);
@@ -141,7 +141,7 @@ Function: bytes
141141
142142
Converts a string to a vector of bytes
143143
*/
144-
fn bytes(s: str) -> [u8] {
144+
fn bytes(s: str) -> [u8] unsafe {
145145
let v = unsafe::reinterpret_cast(s);
146146
let vcopy = vec::slice(v, 0u, vec::len(v) - 1u);
147147
unsafe::leak(v);
@@ -154,7 +154,7 @@ Function: unsafe_from_bytes
154154
Converts a vector of bytes to a string. Does not verify that the
155155
vector contains valid UTF-8.
156156
*/
157-
fn unsafe_from_bytes(v: [mutable? u8]) -> str {
157+
fn unsafe_from_bytes(v: [mutable? u8]) -> str unsafe {
158158
let vcopy: [u8] = v + [0u8];
159159
let scopy: str = unsafe::reinterpret_cast(vcopy);
160160
unsafe::leak(vcopy);
@@ -520,7 +520,7 @@ Failure:
520520
- If begin is greater than end.
521521
- If end is greater than the length of the string.
522522
*/
523-
fn slice(s: str, begin: uint, end: uint) -> str {
523+
fn slice(s: str, begin: uint, end: uint) -> str unsafe {
524524
// FIXME: Typestate precondition
525525
assert (begin <= end);
526526
assert (end <= byte_len(s));

src/lib/task.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,9 @@ fn spawn_joinable<uniq T>(-data: T, f: fn(T)) -> joinable_task {
273273

274274
fn spawn_inner<uniq T>(-data: T, f: fn(T),
275275
notify: option<comm::chan<task_notification>>)
276-
-> task {
276+
-> task unsafe {
277277

278-
fn wrapper<uniq T>(-data: *u8, f: fn(T)) {
278+
fn wrapper<uniq T>(-data: *u8, f: fn(T)) unsafe {
279279
let data: ~T = unsafe::reinterpret_cast(data);
280280
f(*data);
281281
}

src/lib/unsafe.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Function: reinterpret_cast
1717
1818
Casts the value at `src` to U. The two types must have the same length.
1919
*/
20-
fn reinterpret_cast<T, U>(src: T) -> U { ret rusti::cast(src); }
20+
unsafe fn reinterpret_cast<T, U>(src: T) -> U { ret rusti::cast(src); }
2121

2222
/*
2323
Function: leak
@@ -29,4 +29,4 @@ to run any required cleanup or memory-management operations on it. This
2929
can be used for various acts of magick, particularly when using
3030
reinterpret_cast on managed pointer types.
3131
*/
32-
fn leak<T>(-thing: T) { rustrt::leak(thing); }
32+
unsafe fn leak<T>(-thing: T) { rustrt::leak(thing); }

0 commit comments

Comments
 (0)