Skip to content

Commit 649aad9

Browse files
remove unecessary assert, and improve others
Signed-off-by: Vincenzo Palazzo <[email protected]>
1 parent 028f141 commit 649aad9

File tree

17 files changed

+62
-42
lines changed

17 files changed

+62
-42
lines changed

library/std/src/io/error/repr_bitpacked.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ impl Repr {
145145
let p = Box::into_raw(b).cast::<u8>();
146146
// Should only be possible if an allocator handed out a pointer with
147147
// wrong alignment.
148-
debug_assert_eq!(p.addr() & TAG_MASK, 0);
148+
assert_eq!(p.addr() & TAG_MASK, 0);
149149
// Note: We know `TAG_CUSTOM <= size_of::<Custom>()` (static_assert at
150150
// end of file), and both the start and end of the expression must be
151151
// valid without address space wraparound due to `Box`'s semantics.
@@ -167,7 +167,7 @@ impl Repr {
167167
let res = Self(unsafe { NonNull::new_unchecked(tagged) }, PhantomData);
168168
// quickly smoke-check we encoded the right thing (This generally will
169169
// only run in libstd's tests, unless the user uses -Zbuild-std)
170-
debug_assert!(matches!(res.data(), ErrorData::Custom(_)), "repr(custom) encoding failed");
170+
assert!(matches!(res.data(), ErrorData::Custom(_)), "repr(custom) encoding failed");
171171
res
172172
}
173173

@@ -178,7 +178,7 @@ impl Repr {
178178
let res = Self(unsafe { NonNull::new_unchecked(ptr::invalid_mut(utagged)) }, PhantomData);
179179
// quickly smoke-check we encoded the right thing (This generally will
180180
// only run in libstd's tests, unless the user uses -Zbuild-std)
181-
debug_assert!(
181+
assert!(
182182
matches!(res.data(), ErrorData::Os(c) if c == code),
183183
"repr(os) encoding failed for {code}"
184184
);
@@ -192,7 +192,7 @@ impl Repr {
192192
let res = Self(unsafe { NonNull::new_unchecked(ptr::invalid_mut(utagged)) }, PhantomData);
193193
// quickly smoke-check we encoded the right thing (This generally will
194194
// only run in libstd's tests, unless the user uses -Zbuild-std)
195-
debug_assert!(
195+
assert!(
196196
matches!(res.data(), ErrorData::Simple(k) if k == kind),
197197
"repr(simple) encoding failed {:?}",
198198
kind,
@@ -256,7 +256,7 @@ where
256256
TAG_SIMPLE => {
257257
let kind_bits = (bits >> 32) as u32;
258258
let kind = kind_from_prim(kind_bits).unwrap_or_else(|| {
259-
debug_assert!(false, "Invalid io::error::Repr bits: `Repr({:#018x})`", bits);
259+
assert!(false, "Invalid io::error::Repr bits: `Repr({:#018x})`", bits);
260260
// This means the `ptr` passed in was not valid, which violates
261261
// the unsafe contract of `decode_repr`.
262262
//

library/std/src/os/windows/io/socket.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ impl IntoRawSocket for OwnedSocket {
193193
impl FromRawSocket for OwnedSocket {
194194
#[inline]
195195
unsafe fn from_raw_socket(socket: RawSocket) -> Self {
196-
debug_assert_ne!(socket, c::INVALID_SOCKET as RawSocket);
196+
assert_ne!(socket, c::INVALID_SOCKET as RawSocket);
197197
Self { socket }
198198
}
199199
}

library/std/src/sync/mpsc/oneshot.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ impl<T> Packet<T> {
138138
}
139139
} else {
140140
wait_token.wait();
141-
debug_assert!(self.state.load(Ordering::SeqCst) != EMPTY);
141+
assert!(self.state.load(Ordering::SeqCst) != EMPTY, "packet state not empity");
142142
}
143143
} else {
144144
// drop the signal token, since we never blocked

library/std/src/sync/once_lock.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ impl<T> OnceLock<T> {
217217
}
218218
self.initialize(f)?;
219219

220-
debug_assert!(self.is_initialized());
220+
assert!(self.is_initialized(), "bad initialization");
221221

222222
// SAFETY: The inner value has been initialized
223223
Ok(unsafe { self.get_unchecked() })
@@ -369,15 +369,15 @@ impl<T> OnceLock<T> {
369369
///
370370
/// The value must be initialized
371371
unsafe fn get_unchecked(&self) -> &T {
372-
debug_assert!(self.is_initialized());
372+
assert!(self.is_initialized(), "value not initialized");
373373
(&*self.value.get()).assume_init_ref()
374374
}
375375

376376
/// # Safety
377377
///
378378
/// The value must be initialized
379379
unsafe fn get_unchecked_mut(&mut self) -> &mut T {
380-
debug_assert!(self.is_initialized());
380+
assert!(self.is_initialized(), "value not initialized");
381381
(&mut *self.value.get()).assume_init_mut()
382382
}
383383
}

library/std/src/sys/itron/condvar.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ mod waiter_queue {
157157
// Zeroness of `Waiter::task` indicates whether the `Waiter` is
158158
// linked to a queue or not. This invariant is important for
159159
// the correctness.
160-
debug_assert_ne!(task, 0);
160+
assert_ne!(task, 0);
161161

162162
Self { task, priority, prev: None, next: None }
163163
}
@@ -182,8 +182,8 @@ mod waiter_queue {
182182
unsafe {
183183
let waiter = waiter_ptr.as_mut();
184184

185-
debug_assert!(waiter.prev.is_none());
186-
debug_assert!(waiter.next.is_none());
185+
assert!(waiter.prev.is_none());
186+
assert!(waiter.next.is_none());
187187

188188
if let Some(head) = &mut self.head {
189189
// Find the insertion position and insert `waiter`

library/std/src/sys/itron/spin.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl<T> SpinMutex<T> {
3434
let _guard;
3535
if unsafe { abi::sns_dsp() } == 0 {
3636
let er = unsafe { abi::dis_dsp() };
37-
debug_assert!(er >= 0);
37+
assert!(er >= 0, "unexpected error during abi::dis_dsp(): {:?}", ret);
3838

3939
// Wait until the current processor acquires a lock.
4040
while self.locked.swap(true, Ordering::Acquire) {}
@@ -95,11 +95,11 @@ impl<T> SpinIdOnceCell<T> {
9595
/// Assign the content without checking if it's already initialized or
9696
/// being initialized.
9797
pub unsafe fn set_unchecked(&self, (id, extra): (abi::ID, T)) {
98-
debug_assert!(self.get().is_none());
98+
assert!(self.get().is_none());
9999

100100
// Assumption: A positive `abi::ID` fits in `usize`.
101-
debug_assert!(id >= 0);
102-
debug_assert!(usize::try_from(id).is_ok());
101+
assert!(id >= 0, "negative `abi::ID` received: {:?}", id);
102+
assert!(usize::try_from(id).is_ok(), "fails to conver `abi::ID` to `usize`: {:?}", id);
103103
let id = id as usize;
104104

105105
unsafe { *self.extra.get() = MaybeUninit::new(extra) };
@@ -124,7 +124,7 @@ impl<T> SpinIdOnceCell<T> {
124124

125125
self.initialize(f)?;
126126

127-
debug_assert!(self.get().is_some());
127+
assert!(self.get().is_some());
128128

129129
// Safety: The inner value has been initialized
130130
Ok(unsafe { self.get_unchecked() })
@@ -139,8 +139,11 @@ impl<T> SpinIdOnceCell<T> {
139139
let (initialized_id, initialized_extra) = f()?;
140140

141141
// Assumption: A positive `abi::ID` fits in `usize`.
142-
debug_assert!(initialized_id >= 0);
143-
debug_assert!(usize::try_from(initialized_id).is_ok());
142+
assert!(initialized_id >= 0, "negative `abi::ID`");
143+
assert!(
144+
usize::try_from(initialized_id).is_ok(),
145+
"fails to conver `abi::ID` to `usize`: {:?}"
146+
);
144147
let initialized_id = initialized_id as usize;
145148

146149
// Store the initialized contents. Use the release ordering to

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,21 @@ impl Thread {
202202
// Get the current task ID. Panicking here would cause a resource leak,
203203
// so just abort on failure.
204204
let current_task = task::current_task_id_aborting();
205-
debug_assert!(usize::try_from(current_task).is_ok());
206-
debug_assert_ne!(current_task as usize, LIFECYCLE_INIT);
207-
debug_assert_ne!(current_task as usize, LIFECYCLE_DETACHED);
205+
assert!(
206+
usize::try_from(current_task).is_ok(),
207+
"fails to convert `current_task_id` to `usize`: {:?}",
208+
current_task
209+
);
210+
assert_ne!(
211+
current_task as usize, LIFECYCLE_INIT,
212+
"`current_task` is not in a valid state: {:?}",
213+
current_task
214+
);
215+
assert_ne!(
216+
current_task as usize, LIFECYCLE_DETACHED,
217+
"`current_task` is not in a valid state: {:?}",
218+
current_task
219+
);
208220

209221
let current_task = current_task as usize;
210222

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-
assert_eq!(r, 0, "unexpected error during closedir: {:?}", r);
639+
assert!(r == 0 || r == libc::EINTR, "unexpected error during closedir: {:?}", r);
640640
}
641641
}
642642

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ impl RwLock {
9595

9696
// It's impossible for a reader to be waiting on a read-locked RwLock,
9797
// except if there is also a writer waiting.
98-
debug_assert!(!has_readers_waiting(state) || has_writers_waiting(state));
98+
assert!(
99+
!has_readers_waiting(state) || has_writers_waiting(state),
100+
"invalid read state: {:?}",
101+
state
102+
);
99103

100104
// Wake up a writer if we were the last reader and there's a writer waiting.
101105
if is_unlocked(state) && has_writers_waiting(state) {
@@ -161,7 +165,7 @@ impl RwLock {
161165
pub unsafe fn write_unlock(&self) {
162166
let state = self.state.fetch_sub(WRITE_LOCKED, Release) - WRITE_LOCKED;
163167

164-
debug_assert!(is_unlocked(state));
168+
assert!(is_unlocked(state), "trying to write while the state is invalid: {:?}", state);
165169

166170
if has_writers_waiting(state) || has_readers_waiting(state) {
167171
self.wake_writer_or_readers(state);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ impl Condvar {
186186
let r = libc::pthread_cond_timedwait(self.inner.get(), pthread_mutex::raw(mutex), &timeout);
187187
assert!(
188188
r == libc::ETIMEDOUT || r == 0,
189-
"unexpected error during pthread_conf_timedwait: {:?}",
189+
"unexpected error during pthread_cond_timedwait: {:?}",
190190
r
191191
);
192192

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,6 @@ impl RwLock {
110110
}
111111
panic!("rwlock write lock would result in deadlock");
112112
} else {
113-
// According to POSIX, for a properly initialized rwlock this can only
114-
// return EDEADLK or 0. We rely on that.
115113
assert_eq!(r, 0, "unexpected error during pthread_rwlock_wrlock: {:?}", r);
116114
}
117115
*self.write_locked.get() = true;
@@ -145,7 +143,7 @@ impl RwLock {
145143
}
146144
#[inline]
147145
pub unsafe fn write_unlock(&self) {
148-
debug_assert_eq!(self.num_readers.load(Ordering::Relaxed), 0);
146+
assert_eq!(self.num_readers.load(Ordering::Relaxed), 0);
149147
assert!(*self.write_locked.get(), "trying to unlock a thread already unlocked");
150148
*self.write_locked.get() = false;
151149
self.raw_unlock();

library/std/src/sys/unix/process/process_common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ impl Command {
177177
pub fn set_arg_0(&mut self, arg: &OsStr) {
178178
// Set a new arg0
179179
let arg = os2c(arg, &mut self.saw_nul);
180-
debug_assert!(self.argv.0.len() > 1);
180+
assert!(self.argv.0.len() > 1, "invalid number of argument: {:?}", self.argv.0);
181181
self.argv.0[0] = arg.as_ptr();
182182
self.args[0] = arg;
183183
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ impl Thread {
112112
}
113113

114114
pub fn yield_now() {
115-
let ret = unsafe { libc::sched_yield() };
116-
assert_eq!(ret, 0, "unexpected error during sched_yield: {:?}", ret);
115+
let _ = unsafe { libc::sched_yield() };
117116
}
118117

119118
#[cfg(any(target_os = "linux", target_os = "android"))]

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,22 @@ const NOTIFIED: usize = 2;
2424

2525
unsafe fn lock(lock: *mut libc::pthread_mutex_t) {
2626
let r = libc::pthread_mutex_lock(lock);
27-
debug_assert_eq!(r, 0);
27+
assert_eq!(r, 0, "unexpected error during pthread_mutex_lock: {:?}", r);
2828
}
2929

3030
unsafe fn unlock(lock: *mut libc::pthread_mutex_t) {
3131
let r = libc::pthread_mutex_unlock(lock);
32-
debug_assert_eq!(r, 0);
32+
assert_eq!(r, 0, "unexpected error during pthread_mutex_ulock: {:?}", r);
3333
}
3434

3535
unsafe fn notify_one(cond: *mut libc::pthread_cond_t) {
3636
let r = libc::pthread_cond_signal(cond);
37-
debug_assert_eq!(r, 0);
37+
assert_eq!(r, 0, "unexpected error during pthread_cond_signal: {:?}", r);
3838
}
3939

4040
unsafe fn wait(cond: *mut libc::pthread_cond_t, lock: *mut libc::pthread_mutex_t) {
4141
let r = libc::pthread_cond_wait(cond, lock);
42-
debug_assert_eq!(r, 0);
42+
assert_eq!(r, 0, "unexpected error during pthread_cond_wait: {:?}", r);
4343
}
4444

4545
const TIMESPEC_MAX: libc::timespec =
@@ -83,7 +83,11 @@ unsafe fn wait_timeout(
8383
let timeout =
8484
now.checked_add_duration(&dur).and_then(|t| t.to_timespec()).unwrap_or(TIMESPEC_MAX);
8585
let r = libc::pthread_cond_timedwait(cond, lock, &timeout);
86-
debug_assert!(r == libc::ETIMEDOUT || r == 0);
86+
assert!(
87+
r == libc::ETIMEDOUT || r == 0,
88+
"unexpected error during pthread_cond_timedwait: {:?}",
89+
ret
90+
);
8791
}
8892

8993
pub struct Parker {

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

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

2020
pub fn yield_now() {
2121
let ret = unsafe { wasi::sched_yield() };
22-
debug_assert_eq!(ret, Ok(()));
22+
assert_eq!(ret, Ok(()), "unexpected error wasi::sched_yield: {:?}", ret);
2323
}
2424

2525
pub fn set_name(_name: &CStr) {

library/std/src/sys/wasm/alloc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ mod lock {
7979
// 1, // expected value
8080
// -1, // timeout
8181
// );
82-
// debug_assert!(r == 0 || r == 1);
82+
// assert!(r == 0 || r == 1);
8383
// }
8484
//
8585
// Unfortunately though in doing so we would cause issues for the
@@ -144,7 +144,7 @@ mod lock {
144144
impl Drop for DropLock {
145145
fn drop(&mut self) {
146146
let r = LOCKED.swap(0, SeqCst);
147-
debug_assert_eq!(r, 1);
147+
assert_eq!(r, 1, "unexpected error during swap: {:?}", r);
148148

149149
// Note that due to the above logic we don't actually need to wake
150150
// anyone up, but if we did it'd likely look something like this:

library/std/src/sys/wasm/atomics/thread.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl Thread {
3232
let amt = cmp::min(i64::MAX as u128, nanos);
3333
let mut x = 0;
3434
let val = unsafe { wasm32::memory_atomic_wait32(&mut x, 0, amt as i64) };
35-
debug_assert_eq!(val, 2);
35+
assert_eq!(val, 2, "unexpected value during wasm32::memory_atomic_wait32: {:?}", val);
3636
nanos -= amt;
3737
}
3838
}

0 commit comments

Comments
 (0)