Skip to content

Range experiment #118769

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3010,9 +3010,9 @@ checksum = "dc375e1527247fe1a97d8b7156678dfe7c1af2fc075c9a4db3690ecd2a148068"

[[package]]
name = "proc-macro2"
version = "1.0.63"
version = "1.0.70"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b368fba921b0dce7e60f5e04ec15e565b3303972b42bcfde1d0713b881959eb"
checksum = "39278fbbf5fb4f646ce651690877f89d1c5811a3d4acb27700c1cb3cdb78fd3b"
dependencies = [
"unicode-ident",
]
Expand Down Expand Up @@ -3068,9 +3068,8 @@ checksum = "07589615d719a60c8dd8a4622e7946465dfef20d1a428f969e3443e7386d5f45"

[[package]]
name = "quote"
version = "1.0.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "573015e8ab27661678357f27dc26460738fd2b6c86e46f386fde94cb5d913105"
version = "1.0.33"
source = "git+https://github.com/pitaj/quote#44feebf0594b255a511ff20890a7acbf4d6aeed1"
dependencies = [
"proc-macro2",
]
Expand Down Expand Up @@ -3353,11 +3352,10 @@ dependencies = [
[[package]]
name = "rustc-rayon"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb81aadc8837ca6ecebe0fe1353f15df83b3b3cc2cf7a8afd571bc22aa121710"
source = "git+https://github.com/pitaj/rustc-rayon#e76e554512ce25abb48f4118576ede5d7a457918"
dependencies = [
"either",
"rustc-rayon-core",
"rustc-rayon-core 0.5.0 (git+https://github.com/pitaj/rustc-rayon)",
]

[[package]]
Expand All @@ -3372,6 +3370,17 @@ dependencies = [
"num_cpus",
]

[[package]]
name = "rustc-rayon-core"
version = "0.5.0"
source = "git+https://github.com/pitaj/rustc-rayon#e76e554512ce25abb48f4118576ede5d7a457918"
dependencies = [
"crossbeam-channel",
"crossbeam-deque",
"crossbeam-utils",
"num_cpus",
]

[[package]]
name = "rustc-semver"
version = "1.1.0"
Expand Down Expand Up @@ -3701,7 +3710,7 @@ dependencies = [
"portable-atomic",
"rustc-hash",
"rustc-rayon",
"rustc-rayon-core",
"rustc-rayon-core 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc_arena",
"rustc_graphviz",
"rustc_index",
Expand Down Expand Up @@ -4027,7 +4036,7 @@ version = "0.0.0"
dependencies = [
"libloading 0.7.4",
"rustc-rayon",
"rustc-rayon-core",
"rustc-rayon-core 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc_ast",
"rustc_ast_lowering",
"rustc_ast_passes",
Expand Down Expand Up @@ -4192,7 +4201,7 @@ dependencies = [
"measureme",
"polonius-engine",
"rustc-rayon",
"rustc-rayon-core",
"rustc-rayon-core 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc_apfloat",
"rustc_arena",
"rustc_ast",
Expand Down Expand Up @@ -4386,7 +4395,7 @@ version = "0.0.0"
dependencies = [
"field-offset",
"measureme",
"rustc-rayon-core",
"rustc-rayon-core 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc_data_structures",
"rustc_errors",
"rustc_hir",
Expand All @@ -4405,7 +4414,7 @@ name = "rustc_query_system"
version = "0.0.0"
dependencies = [
"parking_lot 0.12.1",
"rustc-rayon-core",
"rustc-rayon-core 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc_ast",
"rustc_data_structures",
"rustc_errors",
Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,8 @@ rustc-std-workspace-core = { path = 'library/rustc-std-workspace-core' }
rustc-std-workspace-alloc = { path = 'library/rustc-std-workspace-alloc' }
rustc-std-workspace-std = { path = 'library/rustc-std-workspace-std' }

rustc-rayon = { git = "https://github.com/pitaj/rustc-rayon" }
quote = { git = "https://github.com/pitaj/quote" }

[patch."https://github.com/rust-lang/rust-clippy"]
clippy_lints = { path = "src/tools/clippy/clippy_lints" }
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/borrow_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ impl<'tcx> BorrowSet<'tcx> {
}

pub(crate) fn indices(&self) -> impl Iterator<Item = BorrowIndex> {
BorrowIndex::from_usize(0)..BorrowIndex::from_usize(self.len())
(BorrowIndex::from_usize(0)..BorrowIndex::from_usize(self.len())).into_iter()
}

pub(crate) fn iter_enumerated(&self) -> impl Iterator<Item = (BorrowIndex, &BorrowData<'tcx>)> {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/region_infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2140,7 +2140,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
// we still want to screen for an "interesting" point to
// highlight (e.g., a call site or something).
let target_scc = self.constraint_sccs.scc(target_region);
let mut range = 0..path.len();
let mut range = (0..path.len()).into_iter();

// As noted above, when reporting an error, there is typically a chain of constraints
// leading from some "source" region which must outlive some "target" region.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn expand_deriving_partial_ord(
// No data, placing the tag check first makes codegen simpler
0 => true,
1..=2 => false,
_ => (0..dataful.len() - 1).any(|i| {
_ => (0..dataful.len() - 1).into_iter().any(|i| {
if dataful[i]
&& let Some(idx) = dataful[i + 1..].iter().position(|v| *v)
{
Expand Down
10 changes: 8 additions & 2 deletions compiler/rustc_const_eval/src/interpret/projection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub trait Projectable<'tcx, Prov: Provenance>: Sized + std::fmt::Debug {
/// A type representing iteration over the elements of an array.
pub struct ArrayIterator<'tcx, 'a, Prov: Provenance, P: Projectable<'tcx, Prov>> {
base: &'a P,
range: Range<u64>,
range: <Range<u64> as IntoIterator>::IntoIter,
stride: Size,
field_layout: TyAndLayout<'tcx>,
_phantom: PhantomData<Prov>, // otherwise it says `Prov` is never used...
Expand Down Expand Up @@ -280,7 +280,13 @@ where
debug!("project_array_fields: {base:?} {len}");
base.offset(len * stride, self.layout_of(self.tcx.types.unit).unwrap(), self)?;
// Create the iterator.
Ok(ArrayIterator { base, range: 0..len, stride, field_layout, _phantom: PhantomData })
Ok(ArrayIterator {
base,
range: (0..len).into_iter(),
stride,
field_layout,
_phantom: PhantomData,
})
}

/// Subslicing
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_error_codes/src/error_codes/E0562.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Erroneous code example:

```compile_fail,E0562
fn main() {
let count_to_ten: impl Iterator<Item=usize> = 0..10;
let count_to_ten: impl IntoIterator<Item=usize> = 0..10;
// error: `impl Trait` not allowed outside of function and inherent method
// return types
for i in count_to_ten {
Expand All @@ -17,7 +17,7 @@ fn main() {
Make sure `impl Trait` only appears in return-type position.

```
fn count_to_n(n: usize) -> impl Iterator<Item=usize> {
fn count_to_n(n: usize) -> impl IntoIterator<Item=usize> {
0..n
}

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ impl<T: ParameterizedOverTcx> LazyValue<T> {
}

struct DecodeIterator<'a, 'tcx, T> {
elem_counter: std::ops::Range<usize>,
elem_counter: <std::ops::Range<usize> as IntoIterator>::IntoIter,
dcx: DecodeContext<'a, 'tcx>,
_phantom: PhantomData<fn() -> T>,
}
Expand Down Expand Up @@ -304,7 +304,7 @@ impl<T: ParameterizedOverTcx> LazyArray<T> {
{
let mut dcx = metadata.decoder(self.position.get());
dcx.lazy_state = LazyState::NodeStart(self.position);
DecodeIterator { elem_counter: (0..self.num_elems), dcx, _phantom: PhantomData }
DecodeIterator { elem_counter: (0..self.num_elems).into_iter(), dcx, _phantom: PhantomData }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ impl InitMaskMaterialized {
end: Size,
is_init: bool,
) -> Option<Size> {
(start..end).find(|&i| init_mask.get(i) == is_init)
(start..end).into_iter().find(|&i| init_mask.get(i) == is_init)
}

let result = find_bit_fast(self, start, end, is_init);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2910,7 +2910,7 @@ impl<'a> Parser<'a> {
}

pub fn is_diff_marker(&mut self, long_kind: &TokenKind, short_kind: &TokenKind) -> bool {
(0..3).all(|i| self.look_ahead(i, |tok| tok == long_kind))
(0..3).into_iter().all(|i| self.look_ahead(i, |tok| tok == long_kind))
&& self.look_ahead(3, |tok| tok == short_kind)
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_parse/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,7 @@ impl<'a> Parser<'a> {
// we are in has non-skipped delimiters. Look for skipped
// delimiters in the lookahead range.
let tree_cursor = &self.token_cursor.tree_cursor;
let all_normal = (0..dist).all(|i| {
let all_normal = (0..dist).into_iter().all(|i| {
let token = tree_cursor.look_ahead(i);
!matches!(token, Some(TokenTree::Delimited(_, Delimiter::Invisible, _)))
});
Expand Down
2 changes: 1 addition & 1 deletion library/alloc/src/collections/btree/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
/// # Safety
/// Every item returned by `range` is a valid edge index for the node.
unsafe fn correct_childrens_parent_links<R: Iterator<Item = usize>>(&mut self, range: R) {
unsafe fn correct_childrens_parent_links<R: IntoIterator<Item = usize>>(&mut self, range: R) {
for i in range {
debug_assert!(i <= self.len());
unsafe { Handle::new_edge(self.reborrow_mut(), i) }.correct_parent_link();
Expand Down
7 changes: 5 additions & 2 deletions library/alloc/src/collections/vec_deque/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -899,10 +899,13 @@ fn test_extend_impl(trusted_len: bool) {
Self { test: VecDeque::new(), expected: VecDeque::new(), trusted_len }
}

fn test_extend<I>(&mut self, iter: I)
fn test_extend<I, IntoIter>(&mut self, iter: I)
where
I: Iterator<Item = usize> + TrustedLen + Clone,
I: IntoIterator<IntoIter = IntoIter>,
IntoIter: Iterator<Item = usize> + TrustedLen + Clone,
{
let iter = iter.into_iter();

struct BasicIterator<I>(I);
impl<I> Iterator for BasicIterator<I>
where
Expand Down
2 changes: 1 addition & 1 deletion library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1943,7 +1943,7 @@ impl<T, A: Allocator> Vec<T, A> {
/// #![feature(vec_push_within_capacity)]
///
/// use std::collections::TryReserveError;
/// fn from_iter_fallible<T>(iter: impl Iterator<Item=T>) -> Result<Vec<T>, TryReserveError> {
/// fn from_iter_fallible<T>(iter: impl IntoIterator<Item=T>) -> Result<Vec<T>, TryReserveError> {
/// let mut vec = Vec::new();
/// for value in iter {
/// if let Err(value) = vec.push_within_capacity(value) {
Expand Down
4 changes: 2 additions & 2 deletions library/alloc/tests/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,15 +590,15 @@ mod slice_index {

good: data[0..=5] == "abcdef";
good: data[{
let mut iter = 0..=5;
let mut iter = core::ops::range::legacy::RangeInclusive::from(0..=5);
iter.by_ref().count(); // exhaust it
iter
}] == "";

// 0..=6 is out of bounds before exhaustion, so it
// stands to reason that it still would be after.
bad: data[{
let mut iter = 0..=6;
let mut iter = core::ops::range::legacy::RangeInclusive::from(0..=6);
iter.by_ref().count(); // exhaust it
iter
}];
Expand Down
12 changes: 6 additions & 6 deletions library/core/benches/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,42 +493,42 @@ fn bench_next_chunk_trusted_random_access(b: &mut Bencher) {

#[bench]
fn bench_next_chunk_filter_even(b: &mut Bencher) {
let a = (0..1024).next_chunk::<1024>().unwrap();
let a = (0..1024).into_iter().next_chunk::<1024>().unwrap();

b.iter(|| black_box(&a).iter().filter(|&&i| i % 2 == 0).next_chunk::<32>())
}

#[bench]
fn bench_next_chunk_filter_predictably_true(b: &mut Bencher) {
let a = (0..1024).next_chunk::<1024>().unwrap();
let a = (0..1024).into_iter().next_chunk::<1024>().unwrap();

b.iter(|| black_box(&a).iter().filter(|&&i| i < 100).next_chunk::<32>())
}

#[bench]
fn bench_next_chunk_filter_mostly_false(b: &mut Bencher) {
let a = (0..1024).next_chunk::<1024>().unwrap();
let a = (0..1024).into_iter().next_chunk::<1024>().unwrap();

b.iter(|| black_box(&a).iter().filter(|&&i| i > 900).next_chunk::<32>())
}

#[bench]
fn bench_next_chunk_filter_map_even(b: &mut Bencher) {
let a = (0..1024).next_chunk::<1024>().unwrap();
let a = (0..1024).into_iter().next_chunk::<1024>().unwrap();

b.iter(|| black_box(&a).iter().filter_map(|&i| (i % 2 == 0).then(|| i)).next_chunk::<32>())
}

#[bench]
fn bench_next_chunk_filter_map_predictably_true(b: &mut Bencher) {
let a = (0..1024).next_chunk::<1024>().unwrap();
let a = (0..1024).into_iter().next_chunk::<1024>().unwrap();

b.iter(|| black_box(&a).iter().filter_map(|&i| (i < 100).then(|| i)).next_chunk::<32>())
}

#[bench]
fn bench_next_chunk_filter_map_mostly_false(b: &mut Bencher) {
let a = (0..1024).next_chunk::<1024>().unwrap();
let a = (0..1024).into_iter().next_chunk::<1024>().unwrap();

b.iter(|| black_box(&a).iter().filter_map(|&i| (i > 900).then(|| i)).next_chunk::<32>())
}
4 changes: 2 additions & 2 deletions library/core/src/array/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ impl<T, const N: usize> IntoIter<T, N> {
/// unsafe { Ok(buffer.transpose().assume_init()) }
/// }
///
/// let r: [_; 4] = next_chunk(&mut (10..16)).unwrap();
/// let r: [_; 4] = next_chunk(&mut (10..16).into_iter()).unwrap();
/// assert_eq!(r, [10, 11, 12, 13]);
/// let r: IntoIter<_, 40> = next_chunk(&mut (10..16)).unwrap_err();
/// let r: IntoIter<_, 40> = next_chunk(&mut (10..16).into_iter()).unwrap_err();
/// assert_eq!(r.collect::<Vec<_>>(), vec![10, 11, 12, 13, 14, 15]);
/// ```
#[unstable(feature = "array_into_iter_constructors", issue = "91583")]
Expand Down
9 changes: 5 additions & 4 deletions library/core/src/escape.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Helper code for character escaping.

use crate::ascii;
use crate::iter::range::RangeIter;
use crate::num::NonZeroUsize;
use crate::ops::Range;

Expand Down Expand Up @@ -68,14 +69,14 @@ pub(crate) struct EscapeIterInner<const N: usize> {
pub(crate) data: [ascii::Char; N],

// Invariant: alive.start <= alive.end <= N.
pub(crate) alive: Range<u8>,
pub(crate) alive: RangeIter<u8>,
}

impl<const N: usize> EscapeIterInner<N> {
pub fn new(data: [ascii::Char; N], alive: Range<u8>) -> Self {
const { assert!(N < 256) };
debug_assert!(alive.start <= alive.end && usize::from(alive.end) <= N, "{alive:?}");
Self { data, alive }
Self { data, alive: alive.into_iter() }
}

pub fn from_array<const M: usize>(array: [ascii::Char; M]) -> Self {
Expand All @@ -87,15 +88,15 @@ impl<const N: usize> EscapeIterInner<N> {
}

pub fn as_ascii(&self) -> &[ascii::Char] {
&self.data[usize::from(self.alive.start)..usize::from(self.alive.end)]
&self.data[usize::from(self.alive.inner.start)..usize::from(self.alive.inner.end)]
}

pub fn as_str(&self) -> &str {
self.as_ascii().as_str()
}

pub fn len(&self) -> usize {
usize::from(self.alive.end - self.alive.start)
usize::from(self.alive.inner.end - self.alive.inner.start)
}

pub fn next(&mut self) -> Option<u8> {
Expand Down
Loading