stdlib: Make reinterpret_cast and leak unsafe

This commit is contained in:
Brian Anderson 2011-10-28 14:19:17 -07:00
parent ad66d72e6c
commit f96ad30dfc
7 changed files with 23 additions and 17 deletions

View File

@ -81,7 +81,7 @@ fn IndirectBr(cx: @block_ctxt, Addr: ValueRef, NumDests: uint) {
// This is a really awful way to get a zero-length c-string, but better (and a
// lot more efficient) than doing str::as_buf("", ...) every time.
fn noname() -> sbuf {
fn noname() -> sbuf unsafe {
const cnull: uint = 0u;
ret std::unsafe::reinterpret_cast(std::ptr::addr_of(cnull));
}
@ -480,9 +480,11 @@ fn Phi(cx: @block_ctxt, Ty: TypeRef, vals: [ValueRef], bbs: [BasicBlockRef])
fn AddIncomingToPhi(phi: ValueRef, val: ValueRef, bb: BasicBlockRef) {
if llvm::LLVMIsUndef(phi) == lib::llvm::True { ret; }
let valptr = std::unsafe::reinterpret_cast(std::ptr::addr_of(val));
let bbptr = std::unsafe::reinterpret_cast(std::ptr::addr_of(bb));
llvm::LLVMAddIncoming(phi, valptr, bbptr, 1u);
unsafe {
let valptr = std::unsafe::reinterpret_cast(std::ptr::addr_of(val));
let bbptr = std::unsafe::reinterpret_cast(std::ptr::addr_of(bb));
llvm::LLVMAddIncoming(phi, valptr, bbptr, 1u);
}
}
fn _UndefReturn(Fn: ValueRef) -> ValueRef {

View File

@ -11,7 +11,10 @@ Function: ptr_eq
Determine if two shared boxes point to the same object
*/
fn ptr_eq<T>(a: @T, b: @T) -> bool {
let a_ptr: uint = unsafe::reinterpret_cast(a);
let b_ptr: uint = unsafe::reinterpret_cast(b);
ret a_ptr == b_ptr;
// FIXME: ptr::addr_of
unsafe {
let a_ptr: uint = unsafe::reinterpret_cast(a);
let b_ptr: uint = unsafe::reinterpret_cast(b);
ret a_ptr == b_ptr;
}
}

View File

@ -29,4 +29,4 @@ Function: null
Create an unsafe null pointer
*/
fn null<T>() -> *T { ret unsafe::reinterpret_cast(0u); }
fn null<T>() -> *T unsafe { ret unsafe::reinterpret_cast(0u); }

View File

@ -92,7 +92,8 @@ type program = obj {
fn arg_vec(prog: str, args: [@str]) -> [sbuf] {
let argptrs = str::as_buf(prog, {|buf| [buf] });
for arg in args { argptrs += str::as_buf(*arg, {|buf| [buf] }); }
argptrs += [unsafe::reinterpret_cast(0)];
// FIXME: ptr::null instead of cast
argptrs += [unsafe {unsafe::reinterpret_cast(0)}];
ret argptrs;
}

View File

@ -127,7 +127,7 @@ Function: byte_len
Returns the length in bytes of a string
*/
fn byte_len(s: str) -> uint {
fn byte_len(s: str) -> uint unsafe {
let v: [u8] = unsafe::reinterpret_cast(s);
let vlen = vec::len(v);
unsafe::leak(v);
@ -141,7 +141,7 @@ Function: bytes
Converts a string to a vector of bytes
*/
fn bytes(s: str) -> [u8] {
fn bytes(s: str) -> [u8] unsafe {
let v = unsafe::reinterpret_cast(s);
let vcopy = vec::slice(v, 0u, vec::len(v) - 1u);
unsafe::leak(v);
@ -154,7 +154,7 @@ Function: unsafe_from_bytes
Converts a vector of bytes to a string. Does not verify that the
vector contains valid UTF-8.
*/
fn unsafe_from_bytes(v: [mutable? u8]) -> str {
fn unsafe_from_bytes(v: [mutable? u8]) -> str unsafe {
let vcopy: [u8] = v + [0u8];
let scopy: str = unsafe::reinterpret_cast(vcopy);
unsafe::leak(vcopy);
@ -520,7 +520,7 @@ Failure:
- If begin is greater than end.
- If end is greater than the length of the string.
*/
fn slice(s: str, begin: uint, end: uint) -> str {
fn slice(s: str, begin: uint, end: uint) -> str unsafe {
// FIXME: Typestate precondition
assert (begin <= end);
assert (end <= byte_len(s));

View File

@ -273,9 +273,9 @@ fn spawn_joinable<uniq T>(-data: T, f: fn(T)) -> joinable_task {
fn spawn_inner<uniq T>(-data: T, f: fn(T),
notify: option<comm::chan<task_notification>>)
-> task {
-> task unsafe {
fn wrapper<uniq T>(-data: *u8, f: fn(T)) {
fn wrapper<uniq T>(-data: *u8, f: fn(T)) unsafe {
let data: ~T = unsafe::reinterpret_cast(data);
f(*data);
}

View File

@ -17,7 +17,7 @@ Function: reinterpret_cast
Casts the value at `src` to U. The two types must have the same length.
*/
fn reinterpret_cast<T, U>(src: T) -> U { ret rusti::cast(src); }
unsafe fn reinterpret_cast<T, U>(src: T) -> U { ret rusti::cast(src); }
/*
Function: leak
@ -29,4 +29,4 @@ to run any required cleanup or memory-management operations on it. This
can be used for various acts of magick, particularly when using
reinterpret_cast on managed pointer types.
*/
fn leak<T>(-thing: T) { rustrt::leak(thing); }
unsafe fn leak<T>(-thing: T) { rustrt::leak(thing); }