Skip to content

Commit 028f141

Browse files
promote debug_assert to assert
Signed-off-by: Vincenzo Palazzo <[email protected]>
1 parent 29c5a02 commit 028f141

File tree

16 files changed

+86
-43
lines changed

16 files changed

+86
-43
lines changed

library/std/src/alloc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ impl System {
154154
new_layout: Layout,
155155
zeroed: bool,
156156
) -> Result<NonNull<[u8]>, AllocError> {
157-
debug_assert!(
157+
assert!(
158158
new_layout.size() >= old_layout.size(),
159159
"`new_layout.size()` must be greater than or equal to `old_layout.size()`"
160160
);
@@ -246,7 +246,7 @@ unsafe impl Allocator for System {
246246
old_layout: Layout,
247247
new_layout: Layout,
248248
) -> Result<NonNull<[u8]>, AllocError> {
249-
debug_assert!(
249+
assert!(
250250
new_layout.size() <= old_layout.size(),
251251
"`new_layout.size()` must be smaller than or equal to `old_layout.size()`"
252252
);

library/std/src/io/buffered/bufreader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ impl<R: Read> BufRead for BufReader<R> {
376376
// Branch using `>=` instead of the more correct `==`
377377
// to tell the compiler that the pos..cap slice is always valid.
378378
if self.pos >= self.cap {
379-
debug_assert!(self.pos == self.cap);
379+
assert!(self.pos == self.cap, "unexpected end of buffer");
380380

381381
let mut readbuf = ReadBuf::uninit(&mut self.buf);
382382

library/std/src/io/buffered/bufwriter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ impl<W: Write> BufWriter<W> {
428428
// i.e., that input buffer length is less than or equal to spare capacity.
429429
#[inline]
430430
unsafe fn write_to_buffer_unchecked(&mut self, buf: &[u8]) {
431-
debug_assert!(buf.len() <= self.spare_capacity());
431+
assert!(buf.len() <= self.spare_capacity(), "unexpected write operation");
432432
let old_len = self.buf.len();
433433
let buf_len = buf.len();
434434
let src = buf.as_ptr();
@@ -610,7 +610,7 @@ impl<W: Write> Write for BufWriter<W> {
610610
} else {
611611
return Ok(0);
612612
};
613-
debug_assert!(total_written != 0);
613+
assert!(total_written != 0, "unexpected `total_written` value: {:?}", total_written);
614614
for buf in iter {
615615
if buf.len() <= self.spare_capacity() {
616616
// SAFETY: safe by above conditional.

library/std/src/io/cursor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ fn reserve_and_pad<A: Allocator>(
426426
// realise the `reserve` it does can be eliminated. So we do it manually
427427
// to eliminate that extra branch
428428
let spare = vec.spare_capacity_mut();
429-
debug_assert!(spare.len() >= diff);
429+
assert!(spare.len() >= diff, "unexpected allocated capacity");
430430
// Safety: we have allocated enough capacity for this.
431431
// And we are only writing, not reading
432432
unsafe {
@@ -444,7 +444,7 @@ unsafe fn vec_write_unchecked<A>(pos: usize, vec: &mut Vec<u8, A>, buf: &[u8]) -
444444
where
445445
A: Allocator,
446446
{
447-
debug_assert!(vec.capacity() >= pos + buf.len());
447+
assert!(vec.capacity() >= pos + buf.len(), "unexpected write out of bound");
448448
vec.as_mut_ptr().add(pos).copy_from(buf.as_ptr(), buf.len());
449449
pos + buf.len()
450450
}

library/std/src/path.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ impl<'a> Components<'a> {
749749
// parse a component from the left, saying how many bytes to consume to
750750
// remove the component
751751
fn parse_next_component(&self) -> (usize, Option<Component<'a>>) {
752-
debug_assert!(self.front == State::Body);
752+
assert!(self.front == State::Body, "unexpected component during parsing: {:?}", self.front);
753753
let (extra, comp) = match self.path.iter().position(|b| self.is_sep_byte(*b)) {
754754
None => (0, self.path),
755755
Some(i) => (1, &self.path[..i]),
@@ -760,7 +760,7 @@ impl<'a> Components<'a> {
760760
// parse a component from the right, saying how many bytes to consume to
761761
// remove the component
762762
fn parse_next_component_back(&self) -> (usize, Option<Component<'a>>) {
763-
debug_assert!(self.back == State::Body);
763+
assert!(self.back == State::Body, "unexpected component during parsing: {:?}", self.back);
764764
let start = self.len_before_body();
765765
let (extra, comp) = match self.path[start..].iter().rposition(|b| self.is_sep_byte(*b)) {
766766
None => (0, &self.path[start..]),
@@ -893,7 +893,11 @@ impl<'a> Iterator for Components<'a> {
893893
match self.front {
894894
State::Prefix if self.prefix_len() > 0 => {
895895
self.front = State::StartDir;
896-
debug_assert!(self.prefix_len() <= self.path.len());
896+
assert!(
897+
self.prefix_len() <= self.path.len(),
898+
"unexpected prefix len: {:?}",
899+
self.prefix_len()
900+
);
897901
let raw = &self.path[..self.prefix_len()];
898902
self.path = &self.path[self.prefix_len()..];
899903
return Some(Component::Prefix(PrefixComponent {
@@ -907,15 +911,15 @@ impl<'a> Iterator for Components<'a> {
907911
State::StartDir => {
908912
self.front = State::Body;
909913
if self.has_physical_root {
910-
debug_assert!(!self.path.is_empty());
914+
assert!(!self.path.is_empty(), "unexpected path value");
911915
self.path = &self.path[1..];
912916
return Some(Component::RootDir);
913917
} else if let Some(p) = self.prefix {
914918
if p.has_implicit_root() && !p.is_verbatim() {
915919
return Some(Component::RootDir);
916920
}
917921
} else if self.include_cur_dir() {
918-
debug_assert!(!self.path.is_empty());
922+
assert!(!self.path.is_empty(), "unexpected path value");
919923
self.path = &self.path[1..];
920924
return Some(Component::CurDir);
921925
}
@@ -1406,7 +1410,7 @@ impl PathBuf {
14061410
fn _set_file_name(&mut self, file_name: &OsStr) {
14071411
if self.file_name().is_some() {
14081412
let popped = self.pop();
1409-
debug_assert!(popped);
1413+
assert!(popped, "unexpected error during unwrap file name");
14101414
}
14111415
self.push(file_name);
14121416
}

library/std/src/sys/unix/fs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ impl Iterator for ReadDir {
636636
impl Drop for Dir {
637637
fn drop(&mut self) {
638638
let r = unsafe { libc::closedir(self.0) };
639-
debug_assert_eq!(r, 0);
639+
assert_eq!(r, 0, "unexpected error during closedir: {:?}", r);
640640
}
641641
}
642642

library/std/src/sys/unix/locks/pthread_condvar.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,19 @@ impl Condvar {
8080
#[inline]
8181
pub unsafe fn notify_one(&self) {
8282
let r = libc::pthread_cond_signal(self.inner.get());
83-
debug_assert_eq!(r, 0);
83+
assert_eq!(r, 0, "unexpected error during pthread_cond_signal: {:?}", r);
8484
}
8585

8686
#[inline]
8787
pub unsafe fn notify_all(&self) {
8888
let r = libc::pthread_cond_broadcast(self.inner.get());
89-
debug_assert_eq!(r, 0);
89+
assert_eq!(r, 0, "unexpected error during pthread_cond_broadcast: {:?}", r);
9090
}
9191

9292
#[inline]
9393
pub unsafe fn wait(&self, mutex: &Mutex) {
9494
let r = libc::pthread_cond_wait(self.inner.get(), pthread_mutex::raw(mutex));
95-
debug_assert_eq!(r, 0);
95+
assert_eq!(r, 0, "unexpected error during pthread_cond_wait: {:?}");
9696
}
9797

9898
// This implementation is used on systems that support pthread_condattr_setclock
@@ -168,7 +168,7 @@ impl Condvar {
168168
let mut sys_now = libc::timeval { tv_sec: 0, tv_usec: 0 };
169169
let stable_now = Instant::now();
170170
let r = libc::gettimeofday(&mut sys_now, ptr::null_mut());
171-
debug_assert_eq!(r, 0);
171+
assert_eq!(r, 0, "unexpected error during gettimeofday: {:?}", r);
172172

173173
let nsec = dur.subsec_nanos() as libc::c_long + (sys_now.tv_usec * 1000) as libc::c_long;
174174
let extra = (nsec / 1_000_000_000) as libc::time_t;
@@ -184,7 +184,11 @@ impl Condvar {
184184

185185
// And wait!
186186
let r = libc::pthread_cond_timedwait(self.inner.get(), pthread_mutex::raw(mutex), &timeout);
187-
debug_assert!(r == libc::ETIMEDOUT || r == 0);
187+
assert!(
188+
r == libc::ETIMEDOUT || r == 0,
189+
"unexpected error during pthread_conf_timedwait: {:?}",
190+
r
191+
);
188192

189193
// ETIMEDOUT is not a totally reliable method of determining timeout due
190194
// to clock shifts, so do the check ourselves
@@ -195,7 +199,7 @@ impl Condvar {
195199
#[cfg(not(target_os = "dragonfly"))]
196200
unsafe fn destroy(&mut self) {
197201
let r = libc::pthread_cond_destroy(self.inner.get());
198-
debug_assert_eq!(r, 0);
202+
assert_eq!(r, 0, "unexpected error during pthread_cond_destroy: {:?}", r);
199203
}
200204

201205
#[inline]
@@ -206,7 +210,11 @@ impl Condvar {
206210
// a condvar that was just initialized with
207211
// libc::PTHREAD_COND_INITIALIZER. Once it is used or
208212
// pthread_cond_init() is called, this behaviour no longer occurs.
209-
debug_assert!(r == 0 || r == libc::EINVAL);
213+
assert!(
214+
r == 0 || r == libc::EINVAL,
215+
"unexpected error during pthread_cond_destroy: {:?}",
216+
r
217+
);
210218
}
211219
}
212220

library/std/src/sys/unix/locks/pthread_mutex.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,12 @@ impl Mutex {
8787
#[inline]
8888
pub unsafe fn lock(&self) {
8989
let r = libc::pthread_mutex_lock(self.inner.get());
90-
debug_assert_eq!(r, 0);
90+
assert_eq!(r, 0, "unexpected error during pthread_mutex_lock: {:?}", r);
9191
}
9292
#[inline]
9393
pub unsafe fn unlock(&self) {
9494
let r = libc::pthread_mutex_unlock(self.inner.get());
95-
debug_assert_eq!(r, 0);
95+
assert_eq!(r, 0, "unexpected error during pthread_mutex_unlock: {:?}", r);
9696
}
9797
#[inline]
9898
pub unsafe fn try_lock(&self) -> bool {
@@ -102,7 +102,7 @@ impl Mutex {
102102
#[cfg(not(target_os = "dragonfly"))]
103103
unsafe fn destroy(&mut self) {
104104
let r = libc::pthread_mutex_destroy(self.inner.get());
105-
debug_assert_eq!(r, 0);
105+
assert_eq!(r, 0, "unexpected error during pthread_mutex_destroy: {:?}", r);
106106
}
107107
#[inline]
108108
#[cfg(target_os = "dragonfly")]
@@ -112,7 +112,11 @@ impl Mutex {
112112
// mutex that was just initialized with libc::PTHREAD_MUTEX_INITIALIZER.
113113
// Once it is used (locked/unlocked) or pthread_mutex_init() is called,
114114
// this behaviour no longer occurs.
115-
debug_assert!(r == 0 || r == libc::EINVAL);
115+
assert!(
116+
r == 0 || r == libc::EINVAL,
117+
"unexpected error during pthread_mutex_destroy: {:?}",
118+
r
119+
);
116120
}
117121
}
118122

@@ -129,7 +133,11 @@ impl Drop for PthreadMutexAttr<'_> {
129133
fn drop(&mut self) {
130134
unsafe {
131135
let result = libc::pthread_mutexattr_destroy(self.0.as_mut_ptr());
132-
debug_assert_eq!(result, 0);
136+
assert_eq!(
137+
result, 0,
138+
"unexpected error during: pthread_mutexattr_destroy: {:?}",
139+
result
140+
);
133141
}
134142
}
135143
}

library/std/src/sys/unix/locks/pthread_rwlock.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ impl RwLock {
112112
} else {
113113
// According to POSIX, for a properly initialized rwlock this can only
114114
// return EDEADLK or 0. We rely on that.
115-
debug_assert_eq!(r, 0);
115+
assert_eq!(r, 0, "unexpected error during pthread_rwlock_wrlock: {:?}", r);
116116
}
117117
*self.write_locked.get() = true;
118118
}
@@ -135,18 +135,18 @@ impl RwLock {
135135
#[inline]
136136
unsafe fn raw_unlock(&self) {
137137
let r = libc::pthread_rwlock_unlock(self.inner.get());
138-
debug_assert_eq!(r, 0);
138+
assert_eq!(r, 0, "unexpected error during pthread_rwlock_unlock: {:?}", r);
139139
}
140140
#[inline]
141141
pub unsafe fn read_unlock(&self) {
142-
debug_assert!(!*self.write_locked.get());
142+
assert!(!*self.write_locked.get(), "trying to unlock a thread already unlocked");
143143
self.num_readers.fetch_sub(1, Ordering::Relaxed);
144144
self.raw_unlock();
145145
}
146146
#[inline]
147147
pub unsafe fn write_unlock(&self) {
148148
debug_assert_eq!(self.num_readers.load(Ordering::Relaxed), 0);
149-
debug_assert!(*self.write_locked.get());
149+
assert!(*self.write_locked.get(), "trying to unlock a thread already unlocked");
150150
*self.write_locked.get() = false;
151151
self.raw_unlock();
152152
}
@@ -158,9 +158,13 @@ impl RwLock {
158158
// libc::PTHREAD_RWLOCK_INITIALIZER. Once it is used (locked/unlocked)
159159
// or pthread_rwlock_init() is called, this behaviour no longer occurs.
160160
if cfg!(target_os = "dragonfly") {
161-
debug_assert!(r == 0 || r == libc::EINVAL);
161+
assert!(
162+
r == 0 || r == libc::EINVAL,
163+
"unexpected error during pthread_rwlock_destroy: {:?}",
164+
r
165+
);
162166
} else {
163-
debug_assert_eq!(r, 0);
167+
assert_eq!(r, 0, "unexpected error during pthread_rwlock_destroy: {:?}", r);
164168
}
165169
}
166170
}

library/std/src/sys/unix/thread.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl Thread {
113113

114114
pub fn yield_now() {
115115
let ret = unsafe { libc::sched_yield() };
116-
debug_assert_eq!(ret, 0);
116+
assert_eq!(ret, 0, "unexpected error during sched_yield: {:?}", ret);
117117
}
118118

119119
#[cfg(any(target_os = "linux", target_os = "android"))]
@@ -267,7 +267,7 @@ impl Thread {
267267
impl Drop for Thread {
268268
fn drop(&mut self) {
269269
let ret = unsafe { libc::pthread_detach(self.id) };
270-
debug_assert_eq!(ret, 0);
270+
assert_eq!(ret, 0, "unexpected error during pthread_detach: {:?}", ret);
271271
}
272272
}
273273

library/std/src/sys/unix/thread_local_key.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub unsafe fn create(dtor: Option<unsafe extern "C" fn(*mut u8)>) -> Key {
1414
#[inline]
1515
pub unsafe fn set(key: Key, value: *mut u8) {
1616
let r = libc::pthread_setspecific(key, value as *mut _);
17-
debug_assert_eq!(r, 0);
17+
assert_eq!(r, 0, "unexpected error during pthread_setspecific: {:?}", r);
1818
}
1919

2020
#[inline]
@@ -25,7 +25,7 @@ pub unsafe fn get(key: Key) -> *mut u8 {
2525
#[inline]
2626
pub unsafe fn destroy(key: Key) {
2727
let r = libc::pthread_key_delete(key);
28-
debug_assert_eq!(r, 0);
28+
assert_eq!(r, 0, "unexpected error during pthread_key_delete: {:?}", r);
2929
}
3030

3131
#[inline]

library/std/src/sys/windows/fs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ impl Iterator for ReadDir {
123123
impl Drop for FindNextFileHandle {
124124
fn drop(&mut self) {
125125
let r = unsafe { c::FindClose(self.0) };
126-
debug_assert!(r != 0);
126+
assert!(r != 0, "unexpected error during FindClose: {:?}", r);
127127
}
128128
}
129129

library/std/src/sys/windows/locks/condvar.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl Condvar {
2222
#[inline]
2323
pub unsafe fn wait(&self, mutex: &Mutex) {
2424
let r = c::SleepConditionVariableSRW(self.inner.get(), mutex::raw(mutex), c::INFINITE, 0);
25-
debug_assert!(r != 0);
25+
assert!(r != 0, "unexpected error during SleepConditionVariableSRW: {:?}", r);
2626
}
2727

2828
pub unsafe fn wait_timeout(&self, mutex: &Mutex, dur: Duration) -> bool {
@@ -33,7 +33,12 @@ impl Condvar {
3333
0,
3434
);
3535
if r == 0 {
36-
debug_assert_eq!(os::errno() as usize, c::ERROR_TIMEOUT as usize);
36+
assert_eq!(
37+
os::errno() as usize,
38+
c::ERROR_TIMEOUT as usize,
39+
"unexpected error during SleepConditionariableSRW: {:?}",
40+
r
41+
);
3742
false
3843
} else {
3944
true

library/std/src/sys/windows/stdio.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,11 @@ fn write_valid_utf8_to_console(handle: c::HANDLE, utf8: &str) -> io::Result<usiz
205205
_ => 3,
206206
};
207207
}
208-
debug_assert!(String::from_utf16(&utf16[..written]).unwrap() == utf8[..count]);
208+
assert_eq!(
209+
String::from_utf16(&utf16[..written]).unwrap(),
210+
utf8[..count],
211+
"unexpected partial lenght written"
212+
);
209213
Ok(count)
210214
}
211215
}

library/std/src/sys/windows/thread_local_key.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub unsafe fn create(dtor: Option<Dtor>) -> Key {
5858
#[inline]
5959
pub unsafe fn set(key: Key, value: *mut u8) {
6060
let r = c::TlsSetValue(key, value as c::LPVOID);
61-
debug_assert!(r != 0);
61+
assert!(r != 0, "unexpected error during TlsSetValue: {:?}", r);
6262
}
6363

6464
#[inline]

0 commit comments

Comments
 (0)