Skip to content

Commit c0fe47d

Browse files
committed
use strict_ instead of checked_ in a few more places
1 parent 654ad52 commit c0fe47d

File tree

4 files changed

+11
-15
lines changed

4 files changed

+11
-15
lines changed

src/tools/miri/src/concurrency/sync.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ macro_rules! declare_id {
4545
// We use 0 as a sentinel value (see the comment above) and,
4646
// therefore, need to shift by one when converting from an index
4747
// into a vector.
48-
let shifted_idx = u32::try_from(idx).unwrap().checked_add(1).unwrap();
48+
let shifted_idx = u32::try_from(idx).unwrap().strict_add(1);
4949
$name(std::num::NonZero::new(shifted_idx).unwrap())
5050
}
5151
fn index(self) -> usize {
@@ -350,7 +350,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
350350
} else {
351351
mutex.owner = Some(thread);
352352
}
353-
mutex.lock_count = mutex.lock_count.checked_add(1).unwrap();
353+
mutex.lock_count = mutex.lock_count.strict_add(1);
354354
if let Some(data_race) = &this.machine.data_race {
355355
data_race.acquire_clock(&mutex.clock, &this.machine.threads);
356356
}
@@ -370,9 +370,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
370370
return Ok(None);
371371
}
372372
let old_lock_count = mutex.lock_count;
373-
mutex.lock_count = old_lock_count
374-
.checked_sub(1)
375-
.expect("invariant violation: lock_count == 0 iff the thread is unlocked");
373+
mutex.lock_count = old_lock_count.strict_sub(1);
376374
if mutex.lock_count == 0 {
377375
mutex.owner = None;
378376
// The mutex is completely unlocked. Try transferring ownership
@@ -450,7 +448,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
450448
trace!("rwlock_reader_lock: {:?} now also held (one more time) by {:?}", id, thread);
451449
let rwlock = &mut this.machine.sync.rwlocks[id];
452450
let count = rwlock.readers.entry(thread).or_insert(0);
453-
*count = count.checked_add(1).expect("the reader counter overflowed");
451+
*count = count.strict_add(1);
454452
if let Some(data_race) = &this.machine.data_race {
455453
data_race.acquire_clock(&rwlock.clock_unlocked, &this.machine.threads);
456454
}

src/tools/miri/src/shims/os_str.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
142142
os_str: &OsStr,
143143
memkind: MemoryKind,
144144
) -> InterpResult<'tcx, Pointer> {
145-
let size = u64::try_from(os_str.len()).unwrap().checked_add(1).unwrap(); // Make space for `0` terminator.
145+
let size = u64::try_from(os_str.len()).unwrap().strict_add(1); // Make space for `0` terminator.
146146
let this = self.eval_context_mut();
147147

148148
let arg_type = Ty::new_array(this.tcx.tcx, this.tcx.types.u8, size);
@@ -158,7 +158,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
158158
os_str: &OsStr,
159159
memkind: MemoryKind,
160160
) -> InterpResult<'tcx, Pointer> {
161-
let size = u64::try_from(os_str.len()).unwrap().checked_add(1).unwrap(); // Make space for `0x0000` terminator.
161+
let size = u64::try_from(os_str.len()).unwrap().strict_add(1); // Make space for `0x0000` terminator.
162162
let this = self.eval_context_mut();
163163

164164
let arg_type = Ty::new_array(this.tcx.tcx, this.tcx.types.u16, size);

src/tools/miri/src/shims/unix/fs.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
893893

894894
let dirent64_layout = this.libc_ty_layout("dirent64");
895895
let d_name_offset = dirent64_layout.fields.offset(4 /* d_name */).bytes();
896-
let size = d_name_offset.checked_add(name_len).unwrap();
896+
let size = d_name_offset.strict_add(name_len);
897897

898898
let entry = this.allocate_ptr(
899899
Size::from_bytes(size),
@@ -994,7 +994,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
994994
name_place.ptr(),
995995
name_place.layout.size.bytes(),
996996
)?;
997-
let file_name_len = file_name_buf_len.checked_sub(1).unwrap();
997+
let file_name_len = file_name_buf_len.strict_sub(1);
998998
if !name_fits {
999999
throw_unsup_format!(
10001000
"a directory entry had a name too large to fit in libc::dirent"

src/tools/miri/src/shims/x86/avx.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,7 @@ pub(super) trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
176176
// of 4.
177177
let chunk_base = i & !0b11;
178178
let src_i = u64::from(this.read_scalar(&control)?.to_u32()? & 0b11)
179-
.checked_add(chunk_base)
180-
.unwrap();
179+
.strict_add(chunk_base);
181180

182181
this.copy_op(
183182
&this.project_index(&data, src_i)?,
@@ -210,9 +209,8 @@ pub(super) trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
210209
// second instead of the first, ask Intel). To read the value from the current
211210
// chunk, add the destination index truncated to a multiple of 2.
212211
let chunk_base = i & !1;
213-
let src_i = ((this.read_scalar(&control)?.to_u64()? >> 1) & 1)
214-
.checked_add(chunk_base)
215-
.unwrap();
212+
let src_i =
213+
((this.read_scalar(&control)?.to_u64()? >> 1) & 1).strict_add(chunk_base);
216214

217215
this.copy_op(
218216
&this.project_index(&data, src_i)?,

0 commit comments

Comments
 (0)