Skip to content

Commit 1b5ff95

Browse files
committed
Replace confusing is_sorted_by in format_args implementation
1 parent abf1d94 commit 1b5ff95

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

compiler/rustc_builtin_macros/src/format.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ use rustc_expand::base::{self, *};
1111
use rustc_parse_format as parse;
1212
use rustc_span::symbol::{sym, Ident, Symbol};
1313
use rustc_span::{MultiSpan, Span};
14+
use smallvec::SmallVec;
1415

1516
use std::borrow::Cow;
16-
use std::cmp::Ordering;
1717
use std::collections::hash_map::Entry;
1818

1919
#[derive(PartialEq)]
@@ -760,21 +760,23 @@ impl<'a, 'b> Context<'a, 'b> {
760760
// "{1} {0}"), or may have multiple entries referring to the same
761761
// element of original_args ("{0} {0}").
762762
//
763-
// The following Iterator<Item = (usize, &ArgumentType)> has one item
764-
// per element of our output slice, identifying the index of which
765-
// element of original_args it's passing, and that argument's type.
766-
let fmt_arg_index_and_ty = self
767-
.arg_unique_types
768-
.iter()
769-
.enumerate()
770-
.flat_map(|(i, unique_types)| unique_types.iter().map(move |ty| (i, ty)))
771-
.chain(self.count_args.iter().map(|i| (*i, &Count)));
763+
// The following vector has one item per element of our output slice,
764+
// identifying the index of which element of original_args it's passing,
765+
// and that argument's type.
766+
let mut fmt_arg_index_and_ty = SmallVec::<[(usize, &ArgumentType); 8]>::new();
767+
for (i, unique_types) in self.arg_unique_types.iter().enumerate() {
768+
fmt_arg_index_and_ty.extend(unique_types.iter().map(|ty| (i, ty)));
769+
}
770+
fmt_arg_index_and_ty.extend(self.count_args.iter().map(|&i| (i, &Count)));
772771

773772
// Figure out whether there are permuted or repeated elements. If not,
774773
// we can generate simpler code.
775-
let nicely_ordered = fmt_arg_index_and_ty
776-
.clone()
777-
.is_sorted_by(|(i, _), (j, _)| (i < j).then_some(Ordering::Less));
774+
//
775+
// The sequence has no indices out of order or repeated if: for every
776+
// adjacent pair of elements, the first one's index is less than the
777+
// second one's index.
778+
let nicely_ordered =
779+
fmt_arg_index_and_ty.array_windows().all(|[(i, _i_ty), (j, _j_ty)]| i < j);
778780

779781
// We want to emit:
780782
//

compiler/rustc_builtin_macros/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//! injecting code into the crate before it is lowered to HIR.
33
44
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
5+
#![feature(array_windows)]
56
#![feature(box_patterns)]
67
#![feature(bool_to_option)]
78
#![feature(crate_visibility_modifier)]

0 commit comments

Comments
 (0)