diff --git a/src/shims/sync.rs b/src/shims/sync.rs index 97afbbe98f6..b0605b4e814 100644 --- a/src/shims/sync.rs +++ b/src/shims/sync.rs @@ -158,7 +158,7 @@ fn mutex_get_or_create_blockset<'mir, 'tcx: 'mir>( mutex_set_blockset(ecx, mutex_op, blockset.to_u32_scalar())?; Ok(blockset) } else { - Ok(blockset.into()) + Ok(BlockSetId::new(blockset)) } } @@ -233,7 +233,7 @@ fn rwlock_get_or_create_writer_blockset<'mir, 'tcx: 'mir>( rwlock_set_writer_blockset(ecx, rwlock_op, blockset.to_u32_scalar())?; Ok(blockset) } else { - Ok(blockset.into()) + Ok(BlockSetId::new(blockset)) } } @@ -264,7 +264,7 @@ fn rwlock_get_or_create_reader_blockset<'mir, 'tcx: 'mir>( rwlock_set_reader_blockset(ecx, rwlock_op, blockset.to_u32_scalar())?; Ok(blockset) } else { - Ok(blockset.into()) + Ok(BlockSetId::new(blockset)) } } diff --git a/src/thread.rs b/src/thread.rs index f9094d771e6..749d6bf955f 100644 --- a/src/thread.rs +++ b/src/thread.rs @@ -30,7 +30,7 @@ pub enum SchedulingAction { /// A thread identifier. #[derive(Clone, Copy, Debug, PartialOrd, Ord, PartialEq, Eq, Hash)] -pub struct ThreadId(usize); +pub struct ThreadId(u32); /// The main thread. When it terminates, the whole application terminates. const MAIN_THREAD: ThreadId = ThreadId(0); @@ -43,22 +43,22 @@ pub fn to_u128(self) -> u128 { impl Idx for ThreadId { fn new(idx: usize) -> Self { - ThreadId(idx) + ThreadId(u32::try_from(idx).unwrap()) } fn index(self) -> usize { - self.0 + usize::try_from(self.0).unwrap() } } impl From for ThreadId { fn from(id: u64) -> Self { - Self(usize::try_from(id).unwrap()) + Self(u32::try_from(id).unwrap()) } } impl From for ThreadId { fn from(id: u32) -> Self { - Self(usize::try_from(id).unwrap()) + Self(u32::try_from(id).unwrap()) } } @@ -73,13 +73,11 @@ pub fn to_u32_scalar<'tcx>(&self) -> Scalar { #[derive(Clone, Copy, Debug, PartialOrd, Ord, PartialEq, Eq, Hash)] pub struct BlockSetId(NonZeroU32); -impl From for BlockSetId { - fn from(id: u32) -> Self { +impl BlockSetId { + /// Panics if `id` is 0. + pub fn new(id: u32) -> Self { Self(NonZeroU32::new(id).expect("0 is not a valid blockset id")) } -} - -impl BlockSetId { pub fn to_u32_scalar<'tcx>(&self) -> Scalar { Scalar::from_u32(self.0.get()) } @@ -325,7 +323,7 @@ fn get_thread_name(&self) -> InterpResult<'tcx, &[u8]> { /// Allocate a new blockset id. fn create_blockset(&mut self) -> BlockSetId { self.blockset_counter = self.blockset_counter.checked_add(1).unwrap(); - self.blockset_counter.into() + BlockSetId::new(self.blockset_counter) } /// Block the currently active thread and put it into the given blockset.