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

0 commit comments

Comments
 (0)