Replace confusing is_sorted_by in format_args implementation

This commit is contained in:
David Tolnay 2022-01-14 19:57:45 -08:00
parent abf1d94b1a
commit 1b5ff95edb
No known key found for this signature in database
GPG key ID: F9BA143B95FF6D82
2 changed files with 16 additions and 13 deletions

View file

@ -11,9 +11,9 @@ use rustc_expand::base::{self, *};
use rustc_parse_format as parse; use rustc_parse_format as parse;
use rustc_span::symbol::{sym, Ident, Symbol}; use rustc_span::symbol::{sym, Ident, Symbol};
use rustc_span::{MultiSpan, Span}; use rustc_span::{MultiSpan, Span};
use smallvec::SmallVec;
use std::borrow::Cow; use std::borrow::Cow;
use std::cmp::Ordering;
use std::collections::hash_map::Entry; use std::collections::hash_map::Entry;
#[derive(PartialEq)] #[derive(PartialEq)]
@ -760,21 +760,23 @@ impl<'a, 'b> Context<'a, 'b> {
// "{1} {0}"), or may have multiple entries referring to the same // "{1} {0}"), or may have multiple entries referring to the same
// element of original_args ("{0} {0}"). // element of original_args ("{0} {0}").
// //
// The following Iterator<Item = (usize, &ArgumentType)> has one item // The following vector has one item per element of our output slice,
// per element of our output slice, identifying the index of which // identifying the index of which element of original_args it's passing,
// element of original_args it's passing, and that argument's type. // and that argument's type.
let fmt_arg_index_and_ty = self let mut fmt_arg_index_and_ty = SmallVec::<[(usize, &ArgumentType); 8]>::new();
.arg_unique_types for (i, unique_types) in self.arg_unique_types.iter().enumerate() {
.iter() fmt_arg_index_and_ty.extend(unique_types.iter().map(|ty| (i, ty)));
.enumerate() }
.flat_map(|(i, unique_types)| unique_types.iter().map(move |ty| (i, ty))) fmt_arg_index_and_ty.extend(self.count_args.iter().map(|&i| (i, &Count)));
.chain(self.count_args.iter().map(|i| (*i, &Count)));
// Figure out whether there are permuted or repeated elements. If not, // Figure out whether there are permuted or repeated elements. If not,
// we can generate simpler code. // we can generate simpler code.
let nicely_ordered = fmt_arg_index_and_ty //
.clone() // The sequence has no indices out of order or repeated if: for every
.is_sorted_by(|(i, _), (j, _)| (i < j).then_some(Ordering::Less)); // adjacent pair of elements, the first one's index is less than the
// second one's index.
let nicely_ordered =
fmt_arg_index_and_ty.array_windows().all(|[(i, _i_ty), (j, _j_ty)]| i < j);
// We want to emit: // We want to emit:
// //

View file

@ -2,6 +2,7 @@
//! injecting code into the crate before it is lowered to HIR. //! injecting code into the crate before it is lowered to HIR.
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")] #![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
#![feature(array_windows)]
#![feature(box_patterns)] #![feature(box_patterns)]
#![feature(bool_to_option)] #![feature(bool_to_option)]
#![feature(crate_visibility_modifier)] #![feature(crate_visibility_modifier)]