From cc8902994248930ef0902ff68483ce1991fa4e24 Mon Sep 17 00:00:00 2001 From: Luqman Aden Date: Thu, 14 Feb 2013 19:00:04 -0500 Subject: [PATCH 1/2] libcore: Remove ptr::mut_addr_of since &mut is coerced to *mut --- src/libcore/os.rs | 16 +++++++--------- src/libcore/path.rs | 4 ++-- src/libcore/ptr.rs | 12 ++---------- src/libcore/str.rs | 2 +- src/test/compile-fail/mutable-huh-ptr-assign.rs | 4 ++-- .../compile-fail/mutable-huh-variance-ptr.rs | 4 ++-- .../run-fail/too-much-recursion-unwinding.rs | 2 +- 7 files changed, 17 insertions(+), 27 deletions(-) diff --git a/src/libcore/os.rs b/src/libcore/os.rs index 8a6a241d870..b16cc1a1a1c 100644 --- a/src/libcore/os.rs +++ b/src/libcore/os.rs @@ -306,10 +306,9 @@ pub fn waitpid(pid: pid_t) -> c_int { pub fn waitpid(pid: pid_t) -> c_int { unsafe { use libc::funcs::posix01::wait::*; - let status = 0 as c_int; + let mut status = 0 as c_int; - assert (waitpid(pid, ptr::mut_addr_of(&status), - 0 as c_int) != (-1 as c_int)); + assert (waitpid(pid, &mut status, 0 as c_int) != (-1 as c_int)); return status; } } @@ -322,7 +321,7 @@ pub fn pipe() -> Pipe { unsafe { let mut fds = Pipe {mut in: 0 as c_int, mut out: 0 as c_int }; - assert (libc::pipe(ptr::mut_addr_of(&(fds.in))) == (0 as c_int)); + assert (libc::pipe(&mut fds.in) == (0 as c_int)); return Pipe {in: fds.in, out: fds.out}; } } @@ -339,8 +338,7 @@ pub fn pipe() -> Pipe { // first, as in rust_run_program. let mut fds = Pipe { mut in: 0 as c_int, mut out: 0 as c_int }; - let res = libc::pipe(ptr::mut_addr_of(&(fds.in)), - 1024 as c_uint, + let res = libc::pipe(&mut fds.in, 1024 as c_uint, (libc::O_BINARY | libc::O_NOINHERIT) as c_int); assert (res == 0 as c_int); assert (fds.in != -1 as c_int && fds.in != 0 as c_int); @@ -374,8 +372,8 @@ pub fn self_exe_path() -> Option { KERN_PROC as c_int, KERN_PROC_PATHNAME as c_int, -1 as c_int]; sysctl(vec::raw::to_ptr(mib), vec::len(mib) as c_uint, - buf as *mut c_void, ptr::mut_addr_of(&sz), - ptr::null(), 0u as size_t) == (0 as c_int) + buf, &mut sz, ptr::null(), + 0u as size_t) == (0 as c_int) } } } @@ -407,7 +405,7 @@ pub fn self_exe_path() -> Option { unsafe { do fill_charp_buf() |buf, sz| { libc::funcs::extra::_NSGetExecutablePath( - buf, ptr::mut_addr_of(&(sz as u32))) == (0 as c_int) + buf, &mut (sz as u32)) == (0 as c_int) } } } diff --git a/src/libcore/path.rs b/src/libcore/path.rs index 91690b6b5b0..f196b97e5e5 100644 --- a/src/libcore/path.rs +++ b/src/libcore/path.rs @@ -243,7 +243,7 @@ impl Path { unsafe { do str::as_c_str(self.to_str()) |buf| { let mut st = stat::arch::default_stat(); - let r = libc::stat(buf, ptr::mut_addr_of(&st)); + let r = libc::stat(buf, &mut st); if r == 0 { Some(move st) } else { None } } @@ -255,7 +255,7 @@ impl Path { unsafe { do str::as_c_str(self.to_str()) |buf| { let mut st = stat::arch::default_stat(); - let r = libc::lstat(buf, ptr::mut_addr_of(&st)); + let r = libc::lstat(buf, &mut st); if r == 0 { Some(move st) } else { None } } diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index acadf079b3b..c6617bdd516 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -44,14 +44,6 @@ extern mod rusti { #[inline(always)] pub pure fn addr_of(val: &T) -> *T { unsafe { rusti::addr_of(*val) } } -/// Get an unsafe mut pointer to a value -#[inline(always)] -pub pure fn mut_addr_of(val: &T) -> *mut T { - unsafe { - cast::reinterpret_cast(&rusti::addr_of(*val)) - } -} - /// Calculate the offset from a pointer #[inline(always)] pub pure fn offset(ptr: *T, count: uint) -> *T { @@ -313,8 +305,8 @@ impl Ord for &const T { pub fn test() { unsafe { struct Pair {mut fst: int, mut snd: int}; - let p = Pair {mut fst: 10, mut snd: 20}; - let pptr: *mut Pair = mut_addr_of(&p); + let mut p = Pair {mut fst: 10, mut snd: 20}; + let pptr: *mut Pair = &mut p; let iptr: *mut int = cast::reinterpret_cast(&pptr); assert (*iptr == 10);; *iptr = 30; diff --git a/src/libcore/str.rs b/src/libcore/str.rs index 7e7a34f1bab..a95d4236ce9 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -2110,7 +2110,7 @@ pub mod raw { let v: **vec::raw::VecRepr = cast::transmute(v); let repr: *vec::raw::VecRepr = *v; (*repr).unboxed.fill = new_len + 1u; - let null = ptr::mut_offset(ptr::mut_addr_of(&((*repr).unboxed.data)), + let null = ptr::mut_offset(cast::transmute(&((*repr).unboxed.data)), new_len); *null = 0u8; } diff --git a/src/test/compile-fail/mutable-huh-ptr-assign.rs b/src/test/compile-fail/mutable-huh-ptr-assign.rs index d0e7c2339bc..ed356f4001d 100644 --- a/src/test/compile-fail/mutable-huh-ptr-assign.rs +++ b/src/test/compile-fail/mutable-huh-ptr-assign.rs @@ -16,8 +16,8 @@ fn main() { } unsafe { - let a = 0; - let v = ptr::mut_addr_of(&a); + let mut a = 0; + let v = &mut a; f(v); } } diff --git a/src/test/compile-fail/mutable-huh-variance-ptr.rs b/src/test/compile-fail/mutable-huh-variance-ptr.rs index e2299597c2f..dba6f9ae3fa 100644 --- a/src/test/compile-fail/mutable-huh-variance-ptr.rs +++ b/src/test/compile-fail/mutable-huh-variance-ptr.rs @@ -13,8 +13,8 @@ extern mod std; fn main() { - let a = ~[0]; - let v: *mut ~[int] = ptr::mut_addr_of(&a); + let mut a = ~[0]; + let v: *mut ~[int] = &mut a; fn f(&&v: *mut ~[const int]) { unsafe { diff --git a/src/test/run-fail/too-much-recursion-unwinding.rs b/src/test/run-fail/too-much-recursion-unwinding.rs index b6a9bce934f..fbea8022cfc 100644 --- a/src/test/run-fail/too-much-recursion-unwinding.rs +++ b/src/test/run-fail/too-much-recursion-unwinding.rs @@ -39,6 +39,6 @@ fn r(recursed: *mut bool) -> r { fn main() { let mut recursed = false; - let _r = r(ptr::mut_addr_of(&recursed)); + let _r = r(&mut recursed); recurse(); } From 206757f8688bfe9958854952e7f1f21b5374a508 Mon Sep 17 00:00:00 2001 From: Luqman Aden Date: Thu, 14 Feb 2013 18:34:54 -0800 Subject: [PATCH 2/2] libcore: replace mut_addr_of properly on mac --- src/libcore/os.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libcore/os.rs b/src/libcore/os.rs index b16cc1a1a1c..91cc20d97f3 100644 --- a/src/libcore/os.rs +++ b/src/libcore/os.rs @@ -404,8 +404,9 @@ pub fn self_exe_path() -> Option { fn load_self() -> Option<~str> { unsafe { do fill_charp_buf() |buf, sz| { + let mut sz = sz as u32; libc::funcs::extra::_NSGetExecutablePath( - buf, &mut (sz as u32)) == (0 as c_int) + buf, &mut sz) == (0 as c_int) } } }