diff options
Diffstat (limited to 'compiler/rustc_index')
| -rw-r--r-- | compiler/rustc_index/Cargo.toml | 2 | ||||
| -rw-r--r-- | compiler/rustc_index/src/bit_set.rs | 22 | ||||
| -rw-r--r-- | compiler/rustc_index/src/idx.rs | 91 | ||||
| -rw-r--r-- | compiler/rustc_index/src/interval.rs | 4 | ||||
| -rw-r--r-- | compiler/rustc_index/src/lib.rs | 3 | ||||
| -rw-r--r-- | compiler/rustc_index/src/slice.rs | 39 | ||||
| -rw-r--r-- | compiler/rustc_index/src/vec.rs | 4 |
7 files changed, 125 insertions, 40 deletions
diff --git a/compiler/rustc_index/Cargo.toml b/compiler/rustc_index/Cargo.toml index f27db7a5400..3d83a3c98da 100644 --- a/compiler/rustc_index/Cargo.toml +++ b/compiler/rustc_index/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "rustc_index" version = "0.0.0" -edition = "2021" +edition = "2024" [dependencies] # tidy-alphabetical-start diff --git a/compiler/rustc_index/src/bit_set.rs b/compiler/rustc_index/src/bit_set.rs index f12df831cb5..6dc044a6d38 100644 --- a/compiler/rustc_index/src/bit_set.rs +++ b/compiler/rustc_index/src/bit_set.rs @@ -723,7 +723,7 @@ impl<T: Idx> ChunkedBitSet<T> { match self.chunks.get(chunk_index) { Some(Zeros(_chunk_domain_size)) => ChunkIter::Zeros, Some(Ones(chunk_domain_size)) => ChunkIter::Ones(0..*chunk_domain_size as usize), - Some(Mixed(chunk_domain_size, _, ref words)) => { + Some(Mixed(chunk_domain_size, _, words)) => { let num_words = num_words(*chunk_domain_size as usize); ChunkIter::Mixed(BitIter::new(&words[0..num_words])) } @@ -752,11 +752,7 @@ impl<T: Idx> BitRelations<ChunkedBitSet<T>> for ChunkedBitSet<T> { changed = true; } ( - Mixed( - self_chunk_domain_size, - ref mut self_chunk_count, - ref mut self_chunk_words, - ), + Mixed(self_chunk_domain_size, self_chunk_count, self_chunk_words), Mixed(_other_chunk_domain_size, _other_chunk_count, other_chunk_words), ) => { // First check if the operation would change @@ -836,11 +832,7 @@ impl<T: Idx> BitRelations<ChunkedBitSet<T>> for ChunkedBitSet<T> { Mixed(*self_chunk_domain_size, self_chunk_count, Rc::new(self_chunk_words)); } ( - Mixed( - self_chunk_domain_size, - ref mut self_chunk_count, - ref mut self_chunk_words, - ), + Mixed(self_chunk_domain_size, self_chunk_count, self_chunk_words), Mixed(_other_chunk_domain_size, _other_chunk_count, other_chunk_words), ) => { // See [`<Self as BitRelations<ChunkedBitSet<T>>>::union`] for the explanation @@ -891,11 +883,7 @@ impl<T: Idx> BitRelations<ChunkedBitSet<T>> for ChunkedBitSet<T> { *self_chunk = other_chunk.clone(); } ( - Mixed( - self_chunk_domain_size, - ref mut self_chunk_count, - ref mut self_chunk_words, - ), + Mixed(self_chunk_domain_size, self_chunk_count, self_chunk_words), Mixed(_other_chunk_domain_size, _other_chunk_count, other_chunk_words), ) => { // See [`<Self as BitRelations<ChunkedBitSet<T>>>::union`] for the explanation @@ -1678,7 +1666,7 @@ impl<R: Idx, C: Idx> SparseBitMatrix<R, C> { /// Iterates through all the columns set to true in a given row of /// the matrix. - pub fn iter(&self, row: R) -> impl Iterator<Item = C> + '_ { + pub fn iter(&self, row: R) -> impl Iterator<Item = C> { self.row(row).into_iter().flat_map(|r| r.iter()) } diff --git a/compiler/rustc_index/src/idx.rs b/compiler/rustc_index/src/idx.rs index b85160540d8..33f406e2113 100644 --- a/compiler/rustc_index/src/idx.rs +++ b/compiler/rustc_index/src/idx.rs @@ -1,5 +1,7 @@ use std::fmt::Debug; use std::hash::Hash; +use std::ops; +use std::slice::SliceIndex; /// Represents some newtyped `usize` wrapper. /// @@ -43,3 +45,92 @@ impl Idx for u32 { self as usize } } + +/// Helper trait for indexing operations with a custom index type. +pub trait IntoSliceIdx<I, T: ?Sized> { + type Output: SliceIndex<T>; + fn into_slice_idx(self) -> Self::Output; +} + +impl<I: Idx, T> IntoSliceIdx<I, [T]> for I { + type Output = usize; + #[inline] + fn into_slice_idx(self) -> Self::Output { + self.index() + } +} + +impl<I, T> IntoSliceIdx<I, [T]> for ops::RangeFull { + type Output = ops::RangeFull; + #[inline] + fn into_slice_idx(self) -> Self::Output { + self + } +} + +impl<I: Idx, T> IntoSliceIdx<I, [T]> for ops::Range<I> { + type Output = ops::Range<usize>; + #[inline] + fn into_slice_idx(self) -> Self::Output { + ops::Range { start: self.start.index(), end: self.end.index() } + } +} + +impl<I: Idx, T> IntoSliceIdx<I, [T]> for ops::RangeFrom<I> { + type Output = ops::RangeFrom<usize>; + #[inline] + fn into_slice_idx(self) -> Self::Output { + ops::RangeFrom { start: self.start.index() } + } +} + +impl<I: Idx, T> IntoSliceIdx<I, [T]> for ops::RangeTo<I> { + type Output = ops::RangeTo<usize>; + #[inline] + fn into_slice_idx(self) -> Self::Output { + ..self.end.index() + } +} + +impl<I: Idx, T> IntoSliceIdx<I, [T]> for ops::RangeInclusive<I> { + type Output = ops::RangeInclusive<usize>; + #[inline] + fn into_slice_idx(self) -> Self::Output { + ops::RangeInclusive::new(self.start().index(), self.end().index()) + } +} + +impl<I: Idx, T> IntoSliceIdx<I, [T]> for ops::RangeToInclusive<I> { + type Output = ops::RangeToInclusive<usize>; + #[inline] + fn into_slice_idx(self) -> Self::Output { + ..=self.end.index() + } +} + +#[cfg(feature = "nightly")] +impl<I: Idx, T> IntoSliceIdx<I, [T]> for core::range::Range<I> { + type Output = core::range::Range<usize>; + #[inline] + fn into_slice_idx(self) -> Self::Output { + core::range::Range { start: self.start.index(), end: self.end.index() } + } +} + +#[cfg(feature = "nightly")] +impl<I: Idx, T> IntoSliceIdx<I, [T]> for core::range::RangeFrom<I> { + type Output = core::range::RangeFrom<usize>; + #[inline] + fn into_slice_idx(self) -> Self::Output { + core::range::RangeFrom { start: self.start.index() } + } +} + +#[cfg(feature = "nightly")] +impl<I: Idx, T> IntoSliceIdx<I, [T]> for core::range::RangeInclusive<I> { + type Output = core::range::RangeInclusive<usize>; + #[inline] + fn into_slice_idx(self) -> Self::Output { + core::range::RangeInclusive { start: self.start.index(), end: self.end.index() } + } +} diff --git a/compiler/rustc_index/src/interval.rs b/compiler/rustc_index/src/interval.rs index a3d87f59567..0225c5c4f32 100644 --- a/compiler/rustc_index/src/interval.rs +++ b/compiler/rustc_index/src/interval.rs @@ -51,7 +51,7 @@ impl<I: Idx> IntervalSet<I> { self.map.clear(); } - pub fn iter(&self) -> impl Iterator<Item = I> + '_ + pub fn iter(&self) -> impl Iterator<Item = I> where I: Step, { @@ -59,7 +59,7 @@ impl<I: Idx> IntervalSet<I> { } /// Iterates through intervals stored in the set, in order. - pub fn iter_intervals(&self) -> impl Iterator<Item = std::ops::Range<I>> + '_ + pub fn iter_intervals(&self) -> impl Iterator<Item = std::ops::Range<I>> where I: Step, { diff --git a/compiler/rustc_index/src/lib.rs b/compiler/rustc_index/src/lib.rs index cae55230b06..3441a5f65c7 100644 --- a/compiler/rustc_index/src/lib.rs +++ b/compiler/rustc_index/src/lib.rs @@ -2,6 +2,7 @@ #![cfg_attr(all(feature = "nightly", test), feature(stmt_expr_attributes))] #![cfg_attr(feature = "nightly", allow(internal_features))] #![cfg_attr(feature = "nightly", feature(extend_one, step_trait, test))] +#![cfg_attr(feature = "nightly", feature(new_range_api))] #![cfg_attr(feature = "nightly", feature(new_zeroed_alloc))] #![warn(unreachable_pub)] // tidy-alphabetical-end @@ -14,7 +15,7 @@ mod idx; mod slice; mod vec; -pub use idx::Idx; +pub use idx::{Idx, IntoSliceIdx}; pub use rustc_index_macros::newtype_index; pub use slice::IndexSlice; #[doc(no_inline)] diff --git a/compiler/rustc_index/src/slice.rs b/compiler/rustc_index/src/slice.rs index 956d32c9a69..f17ea9e4b59 100644 --- a/compiler/rustc_index/src/slice.rs +++ b/compiler/rustc_index/src/slice.rs @@ -1,8 +1,9 @@ +use std::fmt; use std::marker::PhantomData; use std::ops::{Index, IndexMut}; -use std::{fmt, slice}; +use std::slice::{self, SliceIndex}; -use crate::{Idx, IndexVec}; +use crate::{Idx, IndexVec, IntoSliceIdx}; /// A view into contiguous `T`s, indexed by `I` rather than by `usize`. /// @@ -63,9 +64,7 @@ impl<I: Idx, T> IndexSlice<I, T> { } #[inline] - pub fn iter_enumerated( - &self, - ) -> impl DoubleEndedIterator<Item = (I, &T)> + ExactSizeIterator + '_ { + pub fn iter_enumerated(&self) -> impl DoubleEndedIterator<Item = (I, &T)> + ExactSizeIterator { self.raw.iter().enumerate().map(|(n, t)| (I::new(n), t)) } @@ -84,7 +83,7 @@ impl<I: Idx, T> IndexSlice<I, T> { #[inline] pub fn iter_enumerated_mut( &mut self, - ) -> impl DoubleEndedIterator<Item = (I, &mut T)> + ExactSizeIterator + '_ { + ) -> impl DoubleEndedIterator<Item = (I, &mut T)> + ExactSizeIterator { self.raw.iter_mut().enumerate().map(|(n, t)| (I::new(n), t)) } @@ -99,13 +98,19 @@ impl<I: Idx, T> IndexSlice<I, T> { } #[inline] - pub fn get(&self, index: I) -> Option<&T> { - self.raw.get(index.index()) + pub fn get<R: IntoSliceIdx<I, [T]>>( + &self, + index: R, + ) -> Option<&<R::Output as SliceIndex<[T]>>::Output> { + self.raw.get(index.into_slice_idx()) } #[inline] - pub fn get_mut(&mut self, index: I) -> Option<&mut T> { - self.raw.get_mut(index.index()) + pub fn get_mut<R: IntoSliceIdx<I, [T]>>( + &mut self, + index: R, + ) -> Option<&mut <R::Output as SliceIndex<[T]>>::Output> { + self.raw.get_mut(index.into_slice_idx()) } /// Returns mutable references to two distinct elements, `a` and `b`. @@ -186,19 +191,19 @@ impl<I: Idx, T: fmt::Debug> fmt::Debug for IndexSlice<I, T> { } } -impl<I: Idx, T> Index<I> for IndexSlice<I, T> { - type Output = T; +impl<I: Idx, T, R: IntoSliceIdx<I, [T]>> Index<R> for IndexSlice<I, T> { + type Output = <R::Output as SliceIndex<[T]>>::Output; #[inline] - fn index(&self, index: I) -> &T { - &self.raw[index.index()] + fn index(&self, index: R) -> &Self::Output { + &self.raw[index.into_slice_idx()] } } -impl<I: Idx, T> IndexMut<I> for IndexSlice<I, T> { +impl<I: Idx, T, R: IntoSliceIdx<I, [T]>> IndexMut<R> for IndexSlice<I, T> { #[inline] - fn index_mut(&mut self, index: I) -> &mut T { - &mut self.raw[index.index()] + fn index_mut(&mut self, index: R) -> &mut Self::Output { + &mut self.raw[index.into_slice_idx()] } } diff --git a/compiler/rustc_index/src/vec.rs b/compiler/rustc_index/src/vec.rs index 1cb8bc861af..7f3f3ead5f2 100644 --- a/compiler/rustc_index/src/vec.rs +++ b/compiler/rustc_index/src/vec.rs @@ -132,7 +132,7 @@ impl<I: Idx, T> IndexVec<I, T> { } #[inline] - pub fn drain<R: RangeBounds<usize>>(&mut self, range: R) -> impl Iterator<Item = T> + '_ { + pub fn drain<R: RangeBounds<usize>>(&mut self, range: R) -> impl Iterator<Item = T> { self.raw.drain(range) } @@ -140,7 +140,7 @@ impl<I: Idx, T> IndexVec<I, T> { pub fn drain_enumerated<R: RangeBounds<usize>>( &mut self, range: R, - ) -> impl Iterator<Item = (I, T)> + '_ { + ) -> impl Iterator<Item = (I, T)> { let begin = match range.start_bound() { std::ops::Bound::Included(i) => *i, std::ops::Bound::Excluded(i) => i.checked_add(1).unwrap(), |
