Use Itertools::all_equal_value() where applicable

This commit is contained in:
Yotam Ofek
2025-09-05 18:43:43 +00:00
parent 9cd272dc85
commit f279ae1b05
5 changed files with 35 additions and 46 deletions

View File

@@ -1,3 +1,4 @@
use itertools::Itertools as _;
use rustc_abi::{self as abi, FIRST_VARIANT}; use rustc_abi::{self as abi, FIRST_VARIANT};
use rustc_middle::ty::adjustment::PointerCoercion; use rustc_middle::ty::adjustment::PointerCoercion;
use rustc_middle::ty::layout::{HasTyCtxt, HasTypingEnv, LayoutOf, TyAndLayout}; use rustc_middle::ty::layout::{HasTyCtxt, HasTypingEnv, LayoutOf, TyAndLayout};
@@ -111,15 +112,14 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
let size = bx.const_usize(dest.layout.size.bytes()); let size = bx.const_usize(dest.layout.size.bytes());
// Use llvm.memset.p0i8.* to initialize all same byte arrays // Use llvm.memset.p0i8.* to initialize all same byte arrays
if let Some(int) = bx.cx().const_to_opt_u128(v, false) { if let Some(int) = bx.cx().const_to_opt_u128(v, false)
let bytes = &int.to_le_bytes()[..cg_elem.layout.size.bytes_usize()]; && let bytes = &int.to_le_bytes()[..cg_elem.layout.size.bytes_usize()]
let first = bytes[0]; && let Ok(&byte) = bytes.iter().all_equal_value()
if bytes[1..].iter().all(|&b| b == first) { {
let fill = bx.cx().const_u8(first); let fill = bx.cx().const_u8(byte);
bx.memset(start, fill, size, dest.val.align, MemFlags::empty()); bx.memset(start, fill, size, dest.val.align, MemFlags::empty());
return true; return true;
} }
}
// Use llvm.memset.p0i8.* to initialize byte arrays // Use llvm.memset.p0i8.* to initialize byte arrays
let v = bx.from_immediate(v); let v = bx.from_immediate(v);
@@ -130,14 +130,11 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
false false
}; };
match cg_elem.val { if let OperandValue::Immediate(v) = cg_elem.val
OperandValue::Immediate(v) => { && try_init_all_same(bx, v)
if try_init_all_same(bx, v) { {
return; return;
} }
}
_ => (),
}
let count = self let count = self
.monomorphize(count) .monomorphize(count)

View File

@@ -87,6 +87,7 @@
use std::borrow::Cow; use std::borrow::Cow;
use either::Either; use either::Either;
use itertools::Itertools as _;
use rustc_abi::{self as abi, BackendRepr, FIRST_VARIANT, FieldIdx, Primitive, Size, VariantIdx}; use rustc_abi::{self as abi, BackendRepr, FIRST_VARIANT, FieldIdx, Primitive, Size, VariantIdx};
use rustc_const_eval::const_eval::DummyMachine; use rustc_const_eval::const_eval::DummyMachine;
use rustc_const_eval::interpret::{ use rustc_const_eval::interpret::{
@@ -1023,16 +1024,16 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
} }
}; };
if ty.is_array() && fields.len() > 4 { if ty.is_array()
let first = fields[0]; && fields.len() > 4
if fields.iter().all(|&v| v == first) { && let Ok(&first) = fields.iter().all_equal_value()
{
let len = ty::Const::from_target_usize(self.tcx, fields.len().try_into().unwrap()); let len = ty::Const::from_target_usize(self.tcx, fields.len().try_into().unwrap());
if let Some(op) = self.try_as_operand(first, location) { if let Some(op) = self.try_as_operand(first, location) {
*rvalue = Rvalue::Repeat(op, len); *rvalue = Rvalue::Repeat(op, len);
} }
return Some(self.insert(ty, Value::Repeat(first, len))); return Some(self.insert(ty, Value::Repeat(first, len)));
} }
}
if let Some(value) = if let Some(value) =
self.simplify_aggregate_to_copy(lhs, rvalue, location, &fields, variant_index) self.simplify_aggregate_to_copy(lhs, rvalue, location, &fields, variant_index)

View File

@@ -2,6 +2,7 @@ use std::cell::RefCell;
use std::collections::hash_map; use std::collections::hash_map;
use std::rc::Rc; use std::rc::Rc;
use itertools::Itertools as _;
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap}; use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
use rustc_data_structures::unord::{UnordMap, UnordSet}; use rustc_data_structures::unord::{UnordMap, UnordSet};
use rustc_errors::Subdiagnostic; use rustc_errors::Subdiagnostic;
@@ -339,9 +340,9 @@ pub(crate) fn run_lint<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId, body: &Body<
// Suppose that all BIDs point into the same local, // Suppose that all BIDs point into the same local,
// we can remove the this local from the observed drops, // we can remove the this local from the observed drops,
// so that we can focus our diagnosis more on the others. // so that we can focus our diagnosis more on the others.
if candidates.iter().all(|&(_, place)| candidates[0].1.local == place.local) { if let Ok(local) = candidates.iter().map(|&(_, place)| place.local).all_equal_value() {
for path_idx in all_locals_dropped.iter() { for path_idx in all_locals_dropped.iter() {
if move_data.move_paths[path_idx].place.local == candidates[0].1.local { if move_data.move_paths[path_idx].place.local == local {
to_exclude.insert(path_idx); to_exclude.insert(path_idx);
} }
} }

View File

@@ -34,6 +34,7 @@
//! The normal logic that a program with UB can be changed to do anything does not apply to //! The normal logic that a program with UB can be changed to do anything does not apply to
//! pre-"runtime" MIR! //! pre-"runtime" MIR!
use itertools::Itertools as _;
use rustc_index::{Idx, IndexSlice, IndexVec}; use rustc_index::{Idx, IndexSlice, IndexVec};
use rustc_middle::mir::visit::{MutVisitor, MutatingUseContext, PlaceContext, Visitor}; use rustc_middle::mir::visit::{MutVisitor, MutatingUseContext, PlaceContext, Visitor};
use rustc_middle::mir::*; use rustc_middle::mir::*;
@@ -288,19 +289,12 @@ impl<'a, 'tcx> CfgSimplifier<'a, 'tcx> {
return false; return false;
}; };
let first_succ = { let Ok(first_succ) = terminator.successors().all_equal_value() else {
if let Some(first_succ) = terminator.successors().next() { return false;
if terminator.successors().all(|s| s == first_succ) { };
let count = terminator.successors().count(); let count = terminator.successors().count();
self.pred_count[first_succ] -= (count - 1) as u32; self.pred_count[first_succ] -= (count - 1) as u32;
first_succ
} else {
return false;
}
} else {
return false;
}
};
debug!("simplifying branch {:?}", terminator); debug!("simplifying branch {:?}", terminator);
terminator.kind = TerminatorKind::Goto { target: first_succ }; terminator.kind = TerminatorKind::Goto { target: first_succ };

View File

@@ -1,3 +1,4 @@
use itertools::Itertools as _;
use rustc_ast::visit::{self, Visitor}; use rustc_ast::visit::{self, Visitor};
use rustc_ast::{ use rustc_ast::{
self as ast, CRATE_NODE_ID, Crate, ItemKind, ModKind, NodeId, Path, join_path_idents, self as ast, CRATE_NODE_ID, Crate, ItemKind, ModKind, NodeId, Path, join_path_idents,
@@ -3469,16 +3470,11 @@ fn show_candidates(
err.note(note.to_string()); err.note(note.to_string());
} }
} else { } else {
let (_, descr_first, _, _, _) = &inaccessible_path_strings[0]; let descr = inaccessible_path_strings
let descr = if inaccessible_path_strings
.iter() .iter()
.skip(1) .map(|&(_, descr, _, _, _)| descr)
.all(|(_, descr, _, _, _)| descr == descr_first) .all_equal_value()
{ .unwrap_or("item");
descr_first
} else {
"item"
};
let plural_descr = let plural_descr =
if descr.ends_with('s') { format!("{descr}es") } else { format!("{descr}s") }; if descr.ends_with('s') { format!("{descr}es") } else { format!("{descr}s") };