Skip to content

Commit 829d240

Browse files
committed
Rollup merge of rust-lang#28187 - petrochenkov:null, r=aturon
And replace more `0 as *const T`/`0 as *mut T`s with `null()`/`null_mut()`s I'm not sure what is the general policy about making simple functions `const`, but `null()` and `null_mut()` seem to be good candidates.
2 parents 21f2f15 + 06fb196 commit 829d240

File tree

23 files changed

+69
-56
lines changed

23 files changed

+69
-56
lines changed

src/libcollections/btree/node.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ impl<K, V> Drop for Node<K, V> {
296296
self.destroy();
297297
}
298298

299-
self.keys = unsafe { Unique::new(0 as *mut K) };
299+
self.keys = unsafe { Unique::new(ptr::null_mut()) };
300300
}
301301
}
302302

src/libcollections/vec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,7 @@ impl<T> ops::Deref for Vec<T> {
11351135
fn deref(&self) -> &[T] {
11361136
unsafe {
11371137
let p = self.buf.ptr();
1138-
assume(p != 0 as *mut T);
1138+
assume(!p.is_null());
11391139
slice::from_raw_parts(p, self.len)
11401140
}
11411141
}

src/libcore/ptr.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub use intrinsics::write_bytes;
5151
/// ```
5252
#[inline]
5353
#[stable(feature = "rust1", since = "1.0.0")]
54-
pub fn null<T>() -> *const T { 0 as *const T }
54+
pub const fn null<T>() -> *const T { 0 as *const T }
5555

5656
/// Creates a null mutable raw pointer.
5757
///
@@ -65,7 +65,7 @@ pub fn null<T>() -> *const T { 0 as *const T }
6565
/// ```
6666
#[inline]
6767
#[stable(feature = "rust1", since = "1.0.0")]
68-
pub fn null_mut<T>() -> *mut T { 0 as *mut T }
68+
pub const fn null_mut<T>() -> *mut T { 0 as *mut T }
6969

7070
/// Swaps the values at two mutable locations of the same type, without
7171
/// deinitialising either. They may overlap, unlike `mem::swap` which is
@@ -163,7 +163,7 @@ impl<T: ?Sized> *const T {
163163
#[stable(feature = "rust1", since = "1.0.0")]
164164
#[inline]
165165
pub fn is_null(self) -> bool where T: Sized {
166-
self == 0 as *const T
166+
self == null()
167167
}
168168

169169
/// Returns `None` if the pointer is null, or else returns a reference to
@@ -212,7 +212,7 @@ impl<T: ?Sized> *mut T {
212212
#[stable(feature = "rust1", since = "1.0.0")]
213213
#[inline]
214214
pub fn is_null(self) -> bool where T: Sized {
215-
self == 0 as *mut T
215+
self == null_mut()
216216
}
217217

218218
/// Returns `None` if the pointer is null, or else returns a reference to

src/liblog/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ use std::io::{self, Stderr};
184184
use std::io::prelude::*;
185185
use std::mem;
186186
use std::env;
187+
use std::ptr;
187188
use std::rt;
188189
use std::slice;
189190
use std::sync::{Once, StaticMutex};
@@ -209,11 +210,10 @@ static LOCK: StaticMutex = StaticMutex::new();
209210
/// logging statement should be run.
210211
static mut LOG_LEVEL: u32 = MAX_LOG_LEVEL;
211212

212-
static mut DIRECTIVES: *mut Vec<directive::LogDirective> =
213-
0 as *mut Vec<directive::LogDirective>;
213+
static mut DIRECTIVES: *mut Vec<directive::LogDirective> = ptr::null_mut();
214214

215215
/// Optional filter.
216-
static mut FILTER: *mut String = 0 as *mut _;
216+
static mut FILTER: *mut String = ptr::null_mut();
217217

218218
/// Debug log level
219219
pub const DEBUG: u32 = 4;

src/librustc_trans/back/archive.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use std::io;
1818
use std::mem;
1919
use std::path::{Path, PathBuf};
2020
use std::process::{Command, Output, Stdio};
21+
use std::ptr;
2122
use std::str;
2223

2324
use libc;
@@ -449,7 +450,7 @@ impl<'a> ArchiveBuilder<'a> {
449450
}
450451

451452
let name = try!(CString::new(child_name));
452-
members.push(llvm::LLVMRustArchiveMemberNew(0 as *const _,
453+
members.push(llvm::LLVMRustArchiveMemberNew(ptr::null(),
453454
name.as_ptr(),
454455
child.raw()));
455456
strings.push(name);
@@ -462,7 +463,7 @@ impl<'a> ArchiveBuilder<'a> {
462463
let name = try!(CString::new(name_in_archive));
463464
members.push(llvm::LLVMRustArchiveMemberNew(path.as_ptr(),
464465
name.as_ptr(),
465-
0 as *mut _));
466+
ptr::null_mut()));
466467
strings.push(path);
467468
strings.push(name);
468469
}
@@ -472,7 +473,7 @@ impl<'a> ArchiveBuilder<'a> {
472473
if skip(child_name) { continue }
473474

474475
let name = try!(CString::new(child_name));
475-
let m = llvm::LLVMRustArchiveMemberNew(0 as *const _,
476+
let m = llvm::LLVMRustArchiveMemberNew(ptr::null(),
476477
name.as_ptr(),
477478
child.raw());
478479
members.push(m);

src/librustc_trans/back/msvc/registry.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use std::io;
1212
use std::ffi::{OsString, OsStr};
1313
use std::os::windows::prelude::*;
1414
use std::ops::RangeFrom;
15+
use std::ptr;
1516
use libc::{DWORD, LPCWSTR, LONG, LPDWORD, LPBYTE, ERROR_SUCCESS};
1617
use libc::c_void;
1718

@@ -88,7 +89,7 @@ impl RegistryKey {
8889

8990
pub fn open(&self, key: &OsStr) -> io::Result<RegistryKey> {
9091
let key = key.encode_wide().chain(Some(0)).collect::<Vec<_>>();
91-
let mut ret = 0 as *mut _;
92+
let mut ret = ptr::null_mut();
9293
let err = unsafe {
9394
RegOpenKeyExW(self.raw(), key.as_ptr(), 0,
9495
KEY_READ | KEY_WOW64_32KEY, &mut ret)
@@ -110,8 +111,8 @@ impl RegistryKey {
110111
let mut len = 0;
111112
let mut kind = 0;
112113
unsafe {
113-
let err = RegQueryValueExW(self.raw(), name.as_ptr(), 0 as *mut _,
114-
&mut kind, 0 as *mut _, &mut len);
114+
let err = RegQueryValueExW(self.raw(), name.as_ptr(), ptr::null_mut(),
115+
&mut kind, ptr::null_mut(), &mut len);
115116
if err != ERROR_SUCCESS {
116117
return Err(io::Error::from_raw_os_error(err as i32))
117118
}
@@ -124,8 +125,8 @@ impl RegistryKey {
124125
// characters so we need to be sure to halve it for the capacity
125126
// passed in.
126127
let mut v = Vec::with_capacity(len as usize / 2);
127-
let err = RegQueryValueExW(self.raw(), name.as_ptr(), 0 as *mut _,
128-
0 as *mut _, v.as_mut_ptr() as *mut _,
128+
let err = RegQueryValueExW(self.raw(), name.as_ptr(), ptr::null_mut(),
129+
ptr::null_mut(), v.as_mut_ptr() as *mut _,
129130
&mut len);
130131
if err != ERROR_SUCCESS {
131132
return Err(io::Error::from_raw_os_error(err as i32))
@@ -156,8 +157,8 @@ impl<'a> Iterator for Iter<'a> {
156157
let mut v = Vec::with_capacity(256);
157158
let mut len = v.capacity() as DWORD;
158159
let ret = RegEnumKeyExW(self.key.raw(), i, v.as_mut_ptr(), &mut len,
159-
0 as *mut _, 0 as *mut _, 0 as *mut _,
160-
0 as *mut _);
160+
ptr::null_mut(), ptr::null_mut(), ptr::null_mut(),
161+
ptr::null_mut());
161162
if ret == ERROR_NO_MORE_ITEMS as LONG {
162163
None
163164
} else if ret != ERROR_SUCCESS {

src/libstd/io/lazy.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use prelude::v1::*;
1212

1313
use cell::Cell;
14+
use ptr;
1415
use rt;
1516
use sync::{StaticMutex, Arc};
1617

@@ -26,7 +27,7 @@ impl<T: Send + Sync + 'static> Lazy<T> {
2627
pub const fn new(init: fn() -> Arc<T>) -> Lazy<T> {
2728
Lazy {
2829
lock: StaticMutex::new(),
29-
ptr: Cell::new(0 as *mut _),
30+
ptr: Cell::new(ptr::null_mut()),
3031
init: init
3132
}
3233
}

src/libstd/rand/os.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ mod imp {
185185

186186
use io;
187187
use mem;
188+
use ptr;
188189
use rand::Rng;
189190
use libc::{c_int, size_t};
190191

@@ -207,7 +208,7 @@ mod imp {
207208
enum SecRandom {}
208209

209210
#[allow(non_upper_case_globals)]
210-
const kSecRandomDefault: *const SecRandom = 0 as *const SecRandom;
211+
const kSecRandomDefault: *const SecRandom = ptr::null();
211212

212213
#[link(name = "Security", kind = "framework")]
213214
extern "C" {

src/libstd/rt/at_exit_imp.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
use alloc::boxed::FnBox;
2020
use boxed::Box;
21+
use ptr;
2122
use sys_common::mutex::Mutex;
2223
use vec::Vec;
2324

@@ -28,7 +29,7 @@ type Queue = Vec<Box<FnBox()>>;
2829
// the thread infrastructure to be in place (useful on the borders of
2930
// initialization/destruction).
3031
static LOCK: Mutex = Mutex::new();
31-
static mut QUEUE: *mut Queue = 0 as *mut Queue;
32+
static mut QUEUE: *mut Queue = ptr::null_mut();
3233

3334
// The maximum number of times the cleanup routines will be run. While running
3435
// the at_exit closures new ones may be registered, and this count is the number

src/libstd/rt/unwind/seh.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ use prelude::v1::*;
5353

5454
use any::Any;
5555
use libc::{c_ulong, DWORD, c_void};
56+
use ptr;
5657
use sys_common::thread_local::StaticKey;
5758

5859
// 0x R U S T
@@ -98,7 +99,7 @@ pub unsafe fn panic(data: Box<Any + Send + 'static>) -> ! {
9899
rtassert!(PANIC_DATA.get().is_null());
99100
PANIC_DATA.set(Box::into_raw(exception) as *mut u8);
100101

101-
RaiseException(RUST_PANIC, 0, 0, 0 as *const _);
102+
RaiseException(RUST_PANIC, 0, 0, ptr::null());
102103
rtabort!("could not unwind stack");
103104
}
104105

@@ -108,7 +109,7 @@ pub unsafe fn cleanup(ptr: *mut u8) -> Box<Any + Send + 'static> {
108109
rtassert!(ptr as DWORD == RUST_PANIC);
109110

110111
let data = PANIC_DATA.get() as *mut Box<Any + Send + 'static>;
111-
PANIC_DATA.set(0 as *mut u8);
112+
PANIC_DATA.set(ptr::null_mut());
112113
rtassert!(!data.is_null());
113114

114115
*Box::from_raw(data)

src/libstd/sys/common/net.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use io::{self, Error, ErrorKind};
1616
use libc::{self, c_int, c_char, c_void, socklen_t};
1717
use mem;
1818
use net::{SocketAddr, Shutdown, IpAddr};
19+
use ptr;
1920
use str::from_utf8;
2021
use sys::c;
2122
use sys::net::{cvt, cvt_r, cvt_gai, Socket, init, wrlen_t};
@@ -123,9 +124,9 @@ pub fn lookup_host(host: &str) -> io::Result<LookupHost> {
123124
init();
124125

125126
let c_host = try!(CString::new(host));
126-
let mut res = 0 as *mut _;
127+
let mut res = ptr::null_mut();
127128
unsafe {
128-
try!(cvt_gai(getaddrinfo(c_host.as_ptr(), 0 as *const _, 0 as *const _,
129+
try!(cvt_gai(getaddrinfo(c_host.as_ptr(), ptr::null(), ptr::null(),
129130
&mut res)));
130131
Ok(LookupHost { original: res, cur: res })
131132
}
@@ -154,7 +155,7 @@ pub fn lookup_addr(addr: &IpAddr) -> io::Result<String> {
154155
let data = unsafe {
155156
try!(cvt_gai(getnameinfo(inner, len,
156157
hostbuf.as_mut_ptr(), NI_MAXHOST as libc::size_t,
157-
0 as *mut _, 0, 0)));
158+
ptr::null_mut(), 0, 0)));
158159

159160
CStr::from_ptr(hostbuf.as_ptr())
160161
};

src/libstd/sys/unix/backtrace/printing/libbacktrace.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ pub fn print(w: &mut Write, idx: isize, addr: *mut libc::c_void,
123123
// FIXME: We also call self_exe_name() on DragonFly BSD. I haven't
124124
// tested if this is required or not.
125125
unsafe fn init_state() -> *mut backtrace_state {
126-
static mut STATE: *mut backtrace_state = 0 as *mut backtrace_state;
126+
static mut STATE: *mut backtrace_state = ptr::null_mut();
127127
static mut LAST_FILENAME: [libc::c_char; 256] = [0; 256];
128128
if !STATE.is_null() { return STATE }
129129
let selfname = if cfg!(target_os = "freebsd") ||

src/libstd/sys/unix/os.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ pub fn args() -> Args {
291291
};
292292
Args {
293293
iter: vec.into_iter(),
294-
_dont_send_or_sync_me: 0 as *mut (),
294+
_dont_send_or_sync_me: ptr::null_mut(),
295295
}
296296
}
297297

@@ -347,7 +347,7 @@ pub fn args() -> Args {
347347
}
348348
}
349349

350-
Args { iter: res.into_iter(), _dont_send_or_sync_me: 0 as *mut _ }
350+
Args { iter: res.into_iter(), _dont_send_or_sync_me: ptr::null_mut() }
351351
}
352352

353353
#[cfg(any(target_os = "linux",
@@ -363,7 +363,7 @@ pub fn args() -> Args {
363363
let v: Vec<OsString> = bytes.into_iter().map(|v| {
364364
OsStringExt::from_vec(v)
365365
}).collect();
366-
Args { iter: v.into_iter(), _dont_send_or_sync_me: 0 as *mut _ }
366+
Args { iter: v.into_iter(), _dont_send_or_sync_me: ptr::null_mut() }
367367
}
368368

369369
pub struct Env {
@@ -403,7 +403,7 @@ pub fn env() -> Env {
403403
result.push(parse(CStr::from_ptr(*environ).to_bytes()));
404404
environ = environ.offset(1);
405405
}
406-
Env { iter: result.into_iter(), _dont_send_or_sync_me: 0 as *mut _ }
406+
Env { iter: result.into_iter(), _dont_send_or_sync_me: ptr::null_mut() }
407407
};
408408

409409
fn parse(input: &[u8]) -> (OsString, OsString) {
@@ -481,7 +481,7 @@ pub fn home_dir() -> Option<PathBuf> {
481481
loop {
482482
let mut buf = Vec::with_capacity(amt);
483483
let mut passwd: c::passwd = mem::zeroed();
484-
let mut result = 0 as *mut _;
484+
let mut result = ptr::null_mut();
485485
match c::getpwuid_r(me, &mut passwd, buf.as_mut_ptr(),
486486
buf.capacity() as libc::size_t,
487487
&mut result) {

src/libstd/sys/unix/process.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ fn make_envp(env: Option<&HashMap<OsString, OsString>>)
405405

406406
(ptrs.as_ptr() as *const _, tmps, ptrs)
407407
} else {
408-
(0 as *const _, Vec::new(), Vec::new())
408+
(ptr::null(), Vec::new(), Vec::new())
409409
}
410410
}
411411

src/libstd/sys/unix/stack_overflow.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ mod imp {
9393
// See comment above for why this function returns.
9494
}
9595

96-
static mut MAIN_ALTSTACK: *mut libc::c_void = 0 as *mut libc::c_void;
96+
static mut MAIN_ALTSTACK: *mut libc::c_void = ptr::null_mut();
9797

9898
pub unsafe fn init() {
9999
PAGE_SIZE = ::sys::os::page_size();
@@ -155,7 +155,7 @@ mod imp {
155155
}
156156

157157
pub unsafe fn make_handler() -> super::Handler {
158-
super::Handler { _data: 0 as *mut libc::c_void }
158+
super::Handler { _data: ptr::null_mut() }
159159
}
160160

161161
pub unsafe fn drop_handler(_handler: &mut super::Handler) {

src/libstd/sys/unix/sync.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,16 @@ extern {
5959
target_os = "openbsd"))]
6060
mod os {
6161
use libc;
62+
use ptr;
6263

6364
pub type pthread_mutex_t = *mut libc::c_void;
6465
pub type pthread_mutexattr_t = *mut libc::c_void;
6566
pub type pthread_cond_t = *mut libc::c_void;
6667
pub type pthread_rwlock_t = *mut libc::c_void;
6768

68-
pub const PTHREAD_MUTEX_INITIALIZER: pthread_mutex_t = 0 as *mut _;
69-
pub const PTHREAD_COND_INITIALIZER: pthread_cond_t = 0 as *mut _;
70-
pub const PTHREAD_RWLOCK_INITIALIZER: pthread_rwlock_t = 0 as *mut _;
69+
pub const PTHREAD_MUTEX_INITIALIZER: pthread_mutex_t = ptr::null_mut();
70+
pub const PTHREAD_COND_INITIALIZER: pthread_cond_t = ptr::null_mut();
71+
pub const PTHREAD_RWLOCK_INITIALIZER: pthread_rwlock_t = ptr::null_mut();
7172
pub const PTHREAD_MUTEX_RECURSIVE: libc::c_int = 2;
7273
}
7374

@@ -213,6 +214,7 @@ mod os {
213214
#[cfg(target_os = "android")]
214215
mod os {
215216
use libc;
217+
use ptr;
216218

217219
#[repr(C)]
218220
pub struct pthread_mutex_t { value: libc::c_int }
@@ -243,7 +245,7 @@ mod os {
243245
writerThreadId: 0,
244246
pendingReaders: 0,
245247
pendingWriters: 0,
246-
reserved: [0 as *mut _; 4],
248+
reserved: [ptr::null_mut(); 4],
247249
};
248250
pub const PTHREAD_MUTEX_RECURSIVE: libc::c_int = 1;
249251
}

src/libstd/sys/unix/thread.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl Thread {
7272

7373
extern fn thread_start(main: *mut libc::c_void) -> *mut libc::c_void {
7474
unsafe { start_thread(main); }
75-
0 as *mut _
75+
ptr::null_mut()
7676
}
7777
}
7878

0 commit comments

Comments
 (0)