Skip to content

Commit cb8b5fa

Browse files
authored
Rollup merge of #146254 - yotamofek:pr/itertools-all-equal-value, r=cjgillot
Use `Itertools::all_equal_value()` where applicable Just a small cleanup. We already have `itertools` as a dep in these crates, so might as well use another of its features. Makes the code simpler IMHO :)
2 parents f4b2f68 + f279ae1 commit cb8b5fa

File tree

5 files changed

+35
-46
lines changed

5 files changed

+35
-46
lines changed

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use itertools::Itertools as _;
12
use rustc_abi::{self as abi, FIRST_VARIANT};
23
use rustc_middle::ty::adjustment::PointerCoercion;
34
use rustc_middle::ty::layout::{HasTyCtxt, HasTypingEnv, LayoutOf, TyAndLayout};
@@ -111,14 +112,13 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
111112
let size = bx.const_usize(dest.layout.size.bytes());
112113

113114
// Use llvm.memset.p0i8.* to initialize all same byte arrays
114-
if let Some(int) = bx.cx().const_to_opt_u128(v, false) {
115-
let bytes = &int.to_le_bytes()[..cg_elem.layout.size.bytes_usize()];
116-
let first = bytes[0];
117-
if bytes[1..].iter().all(|&b| b == first) {
118-
let fill = bx.cx().const_u8(first);
119-
bx.memset(start, fill, size, dest.val.align, MemFlags::empty());
120-
return true;
121-
}
115+
if let Some(int) = bx.cx().const_to_opt_u128(v, false)
116+
&& let bytes = &int.to_le_bytes()[..cg_elem.layout.size.bytes_usize()]
117+
&& let Ok(&byte) = bytes.iter().all_equal_value()
118+
{
119+
let fill = bx.cx().const_u8(byte);
120+
bx.memset(start, fill, size, dest.val.align, MemFlags::empty());
121+
return true;
122122
}
123123

124124
// Use llvm.memset.p0i8.* to initialize byte arrays
@@ -130,13 +130,10 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
130130
false
131131
};
132132

133-
match cg_elem.val {
134-
OperandValue::Immediate(v) => {
135-
if try_init_all_same(bx, v) {
136-
return;
137-
}
138-
}
139-
_ => (),
133+
if let OperandValue::Immediate(v) = cg_elem.val
134+
&& try_init_all_same(bx, v)
135+
{
136+
return;
140137
}
141138

142139
let count = self

compiler/rustc_mir_transform/src/gvn.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
use std::borrow::Cow;
8888

8989
use either::Either;
90+
use itertools::Itertools as _;
9091
use rustc_abi::{self as abi, BackendRepr, FIRST_VARIANT, FieldIdx, Primitive, Size, VariantIdx};
9192
use rustc_const_eval::const_eval::DummyMachine;
9293
use rustc_const_eval::interpret::{
@@ -1023,15 +1024,15 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
10231024
}
10241025
};
10251026

1026-
if ty.is_array() && fields.len() > 4 {
1027-
let first = fields[0];
1028-
if fields.iter().all(|&v| v == first) {
1029-
let len = ty::Const::from_target_usize(self.tcx, fields.len().try_into().unwrap());
1030-
if let Some(op) = self.try_as_operand(first, location) {
1031-
*rvalue = Rvalue::Repeat(op, len);
1032-
}
1033-
return Some(self.insert(ty, Value::Repeat(first, len)));
1027+
if ty.is_array()
1028+
&& fields.len() > 4
1029+
&& let Ok(&first) = fields.iter().all_equal_value()
1030+
{
1031+
let len = ty::Const::from_target_usize(self.tcx, fields.len().try_into().unwrap());
1032+
if let Some(op) = self.try_as_operand(first, location) {
1033+
*rvalue = Rvalue::Repeat(op, len);
10341034
}
1035+
return Some(self.insert(ty, Value::Repeat(first, len)));
10351036
}
10361037

10371038
if let Some(value) =

compiler/rustc_mir_transform/src/lint_tail_expr_drop_order.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::cell::RefCell;
22
use std::collections::hash_map;
33
use std::rc::Rc;
44

5+
use itertools::Itertools as _;
56
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
67
use rustc_data_structures::unord::{UnordMap, UnordSet};
78
use rustc_errors::Subdiagnostic;
@@ -339,9 +340,9 @@ pub(crate) fn run_lint<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId, body: &Body<
339340
// Suppose that all BIDs point into the same local,
340341
// we can remove the this local from the observed drops,
341342
// so that we can focus our diagnosis more on the others.
342-
if candidates.iter().all(|&(_, place)| candidates[0].1.local == place.local) {
343+
if let Ok(local) = candidates.iter().map(|&(_, place)| place.local).all_equal_value() {
343344
for path_idx in all_locals_dropped.iter() {
344-
if move_data.move_paths[path_idx].place.local == candidates[0].1.local {
345+
if move_data.move_paths[path_idx].place.local == local {
345346
to_exclude.insert(path_idx);
346347
}
347348
}

compiler/rustc_mir_transform/src/simplify.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
//! The normal logic that a program with UB can be changed to do anything does not apply to
3535
//! pre-"runtime" MIR!
3636
37+
use itertools::Itertools as _;
3738
use rustc_index::{Idx, IndexSlice, IndexVec};
3839
use rustc_middle::mir::visit::{MutVisitor, MutatingUseContext, PlaceContext, Visitor};
3940
use rustc_middle::mir::*;
@@ -288,20 +289,13 @@ impl<'a, 'tcx> CfgSimplifier<'a, 'tcx> {
288289
return false;
289290
};
290291

291-
let first_succ = {
292-
if let Some(first_succ) = terminator.successors().next() {
293-
if terminator.successors().all(|s| s == first_succ) {
294-
let count = terminator.successors().count();
295-
self.pred_count[first_succ] -= (count - 1) as u32;
296-
first_succ
297-
} else {
298-
return false;
299-
}
300-
} else {
301-
return false;
302-
}
292+
let Ok(first_succ) = terminator.successors().all_equal_value() else {
293+
return false;
303294
};
304295

296+
let count = terminator.successors().count();
297+
self.pred_count[first_succ] -= (count - 1) as u32;
298+
305299
debug!("simplifying branch {:?}", terminator);
306300
terminator.kind = TerminatorKind::Goto { target: first_succ };
307301
true

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use itertools::Itertools as _;
12
use rustc_ast::visit::{self, Visitor};
23
use rustc_ast::{
34
self as ast, CRATE_NODE_ID, Crate, ItemKind, ModKind, NodeId, Path, join_path_idents,
@@ -3469,16 +3470,11 @@ fn show_candidates(
34693470
err.note(note.to_string());
34703471
}
34713472
} else {
3472-
let (_, descr_first, _, _, _) = &inaccessible_path_strings[0];
3473-
let descr = if inaccessible_path_strings
3473+
let descr = inaccessible_path_strings
34743474
.iter()
3475-
.skip(1)
3476-
.all(|(_, descr, _, _, _)| descr == descr_first)
3477-
{
3478-
descr_first
3479-
} else {
3480-
"item"
3481-
};
3475+
.map(|&(_, descr, _, _, _)| descr)
3476+
.all_equal_value()
3477+
.unwrap_or("item");
34823478
let plural_descr =
34833479
if descr.ends_with('s') { format!("{descr}es") } else { format!("{descr}s") };
34843480

0 commit comments

Comments
 (0)