Rollup merge of #76825 - lcnr:array-windows-apply, r=varkor

use `array_windows` instead of `windows` in the compiler

I do think these changes are beautiful, but do have to admit that using type inference for the window length
can easily be confusing. This seems like a general issue with const generics, where inferring constants adds an additional
complexity which users have to learn and keep in mind.
This commit is contained in:
Ralf Jung 2020-09-20 12:08:26 +02:00 committed by GitHub
commit 50d56bc774
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 24 additions and 20 deletions

View File

@ -8,6 +8,7 @@
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/")] #![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
#![allow(incomplete_features)] #![allow(incomplete_features)]
#![feature(array_windows)]
#![feature(control_flow_enum)] #![feature(control_flow_enum)]
#![feature(in_band_lifetimes)] #![feature(in_band_lifetimes)]
#![feature(unboxed_closures)] #![feature(unboxed_closures)]

View File

@ -34,7 +34,7 @@ impl<K: Ord, V> SortedMap<K, V> {
/// and that there are no duplicates. /// and that there are no duplicates.
#[inline] #[inline]
pub fn from_presorted_elements(elements: Vec<(K, V)>) -> SortedMap<K, V> { pub fn from_presorted_elements(elements: Vec<(K, V)>) -> SortedMap<K, V> {
debug_assert!(elements.windows(2).all(|w| w[0].0 < w[1].0)); debug_assert!(elements.array_windows().all(|[fst, snd]| fst.0 < snd.0));
SortedMap { data: elements } SortedMap { data: elements }
} }
@ -159,7 +159,7 @@ impl<K: Ord, V> SortedMap<K, V> {
return; return;
} }
debug_assert!(elements.windows(2).all(|w| w[0].0 < w[1].0)); debug_assert!(elements.array_windows().all(|[fst, snd]| fst.0 < snd.0));
let start_index = self.lookup_index_for(&elements[0].0); let start_index = self.lookup_index_for(&elements[0].0);

View File

@ -27,6 +27,7 @@
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/")] #![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
#![cfg_attr(test, feature(test))] #![cfg_attr(test, feature(test))]
#![feature(array_windows)]
#![feature(bool_to_option)] #![feature(bool_to_option)]
#![feature(box_syntax)] #![feature(box_syntax)]
#![feature(crate_visibility_modifier)] #![feature(crate_visibility_modifier)]

View File

@ -70,9 +70,9 @@ fn is_camel_case(name: &str) -> bool {
// ones (some scripts don't have a concept of upper/lowercase) // ones (some scripts don't have a concept of upper/lowercase)
!name.chars().next().unwrap().is_lowercase() !name.chars().next().unwrap().is_lowercase()
&& !name.contains("__") && !name.contains("__")
&& !name.chars().collect::<Vec<_>>().windows(2).any(|pair| { && !name.chars().collect::<Vec<_>>().array_windows().any(|&[fst, snd]| {
// contains a capitalisable character followed by, or preceded by, an underscore // contains a capitalisable character followed by, or preceded by, an underscore
char_has_case(pair[0]) && pair[1] == '_' || char_has_case(pair[1]) && pair[0] == '_' char_has_case(fst) && snd == '_' || char_has_case(snd) && fst == '_'
}) })
} }

View File

@ -23,6 +23,7 @@
//! This API is completely unstable and subject to change. //! This API is completely unstable and subject to change.
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/")] #![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
#![feature(array_windows)]
#![feature(backtrace)] #![feature(backtrace)]
#![feature(bool_to_option)] #![feature(bool_to_option)]
#![feature(box_patterns)] #![feature(box_patterns)]

View File

@ -2419,7 +2419,7 @@ impl<'tcx> TyCtxt<'tcx> {
eps: &[ExistentialPredicate<'tcx>], eps: &[ExistentialPredicate<'tcx>],
) -> &'tcx List<ExistentialPredicate<'tcx>> { ) -> &'tcx List<ExistentialPredicate<'tcx>> {
assert!(!eps.is_empty()); assert!(!eps.is_empty());
assert!(eps.windows(2).all(|w| w[0].stable_cmp(self, &w[1]) != Ordering::Greater)); assert!(eps.array_windows().all(|[a, b]| a.stable_cmp(self, b) != Ordering::Greater));
self._intern_existential_predicates(eps) self._intern_existential_predicates(eps)
} }

View File

@ -6,6 +6,7 @@ Rust MIR: a lowered representation of Rust.
#![feature(nll)] #![feature(nll)]
#![feature(in_band_lifetimes)] #![feature(in_band_lifetimes)]
#![feature(array_windows)]
#![feature(bindings_after_at)] #![feature(bindings_after_at)]
#![feature(bool_to_option)] #![feature(bool_to_option)]
#![feature(box_patterns)] #![feature(box_patterns)]

View File

@ -277,14 +277,8 @@ where
symbols.sort_by_key(|sym| sym.1); symbols.sort_by_key(|sym| sym.1);
for pair in symbols.windows(2) { for &[(mono_item1, ref sym1), (mono_item2, ref sym2)] in symbols.array_windows() {
let sym1 = &pair[0].1;
let sym2 = &pair[1].1;
if sym1 == sym2 { if sym1 == sym2 {
let mono_item1 = pair[0].0;
let mono_item2 = pair[1].0;
let span1 = mono_item1.local_span(tcx); let span1 = mono_item1.local_span(tcx);
let span2 = mono_item2.local_span(tcx); let span2 = mono_item2.local_span(tcx);

View File

@ -1,7 +1,7 @@
//! Construction of MIR from HIR. //! Construction of MIR from HIR.
//! //!
//! This crate also contains the match exhaustiveness and usefulness checking. //! This crate also contains the match exhaustiveness and usefulness checking.
#![feature(array_windows)]
#![feature(box_patterns)] #![feature(box_patterns)]
#![feature(box_syntax)] #![feature(box_syntax)]
#![feature(const_fn)] #![feature(const_fn)]

View File

@ -2299,19 +2299,19 @@ fn split_grouped_constructors<'p, 'tcx>(
// interval into a constructor. // interval into a constructor.
split_ctors.extend( split_ctors.extend(
borders borders
.windows(2) .array_windows()
.filter_map(|window| match (window[0], window[1]) { .filter_map(|&pair| match pair {
(Border::JustBefore(n), Border::JustBefore(m)) => { [Border::JustBefore(n), Border::JustBefore(m)] => {
if n < m { if n < m {
Some(IntRange { range: n..=(m - 1), ty, span }) Some(IntRange { range: n..=(m - 1), ty, span })
} else { } else {
None None
} }
} }
(Border::JustBefore(n), Border::AfterMax) => { [Border::JustBefore(n), Border::AfterMax] => {
Some(IntRange { range: n..=u128::MAX, ty, span }) Some(IntRange { range: n..=u128::MAX, ty, span })
} }
(Border::AfterMax, _) => None, [Border::AfterMax, _] => None,
}) })
.map(IntRange), .map(IntRange),
); );

View File

@ -5,6 +5,7 @@
//! This API is completely unstable and subject to change. //! This API is completely unstable and subject to change.
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/")] #![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
#![feature(array_windows)]
#![feature(crate_visibility_modifier)] #![feature(crate_visibility_modifier)]
#![feature(const_fn)] #![feature(const_fn)]
#![feature(const_panic)] #![feature(const_panic)]
@ -1156,7 +1157,12 @@ impl<S: Encoder> Encodable<S> for SourceFile {
let max_line_length = if lines.len() == 1 { let max_line_length = if lines.len() == 1 {
0 0
} else { } else {
lines.windows(2).map(|w| w[1] - w[0]).map(|bp| bp.to_usize()).max().unwrap() lines
.array_windows()
.map(|&[fst, snd]| snd - fst)
.map(|bp| bp.to_usize())
.max()
.unwrap()
}; };
let bytes_per_diff: u8 = match max_line_length { let bytes_per_diff: u8 = match max_line_length {
@ -1171,7 +1177,7 @@ impl<S: Encoder> Encodable<S> for SourceFile {
// Encode the first element. // Encode the first element.
lines[0].encode(s)?; lines[0].encode(s)?;
let diff_iter = (&lines[..]).windows(2).map(|w| (w[1] - w[0])); let diff_iter = lines[..].array_windows().map(|&[fst, snd]| snd - fst);
match bytes_per_diff { match bytes_per_diff {
1 => { 1 => {