Skip to content

Commit 8d0d244

Browse files
authored
Rollup merge of #73910 - cuviper:while-indexing, r=oli-obk
Rewrite a few manual index loops with while-let There were a few instances of this pattern: ```rust while index < vec.len() { let item = &vec[index]; // ... } ``` These can be indexed at once: ```rust while let Some(item) = vec.get(index) { // ... } ``` Particularly in `ObligationForest::process_obligations`, this mitigates a codegen regression found with LLVM 11 (#73526).
2 parents 5cd52e0 + 47425e4 commit 8d0d244

File tree

2 files changed

+6
-10
lines changed

2 files changed

+6
-10
lines changed

src/librustc_data_structures/obligation_forest/mod.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -412,9 +412,7 @@ impl<O: ForestObligation> ObligationForest<O> {
412412
// be computed with the initial length, and we would miss the appended
413413
// nodes. Therefore we use a `while` loop.
414414
let mut index = 0;
415-
while index < self.nodes.len() {
416-
let node = &mut self.nodes[index];
417-
415+
while let Some(node) = self.nodes.get_mut(index) {
418416
// `processor.process_obligation` can modify the predicate within
419417
// `node.obligation`, and that predicate is the key used for
420418
// `self.active_cache`. This means that `self.active_cache` can get
@@ -666,16 +664,16 @@ impl<O: ForestObligation> ObligationForest<O> {
666664

667665
for node in &mut self.nodes {
668666
let mut i = 0;
669-
while i < node.dependents.len() {
670-
let new_index = node_rewrites[node.dependents[i]];
667+
while let Some(dependent) = node.dependents.get_mut(i) {
668+
let new_index = node_rewrites[*dependent];
671669
if new_index >= orig_nodes_len {
672670
node.dependents.swap_remove(i);
673671
if i == 0 && node.has_parent {
674672
// We just removed the parent.
675673
node.has_parent = false;
676674
}
677675
} else {
678-
node.dependents[i] = new_index;
676+
*dependent = new_index;
679677
i += 1;
680678
}
681679
}

src/librustc_data_structures/transitive_relation.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -391,14 +391,12 @@ impl<T: Clone + Debug + Eq + Hash> TransitiveRelation<T> {
391391
/// - Input: `[a, x, b, y]`. Output: `[a, x]`.
392392
fn pare_down(candidates: &mut Vec<usize>, closure: &BitMatrix<usize, usize>) {
393393
let mut i = 0;
394-
while i < candidates.len() {
395-
let candidate_i = candidates[i];
394+
while let Some(&candidate_i) = candidates.get(i) {
396395
i += 1;
397396

398397
let mut j = i;
399398
let mut dead = 0;
400-
while j < candidates.len() {
401-
let candidate_j = candidates[j];
399+
while let Some(&candidate_j) = candidates.get(j) {
402400
if closure.contains(candidate_i, candidate_j) {
403401
// If `i` can reach `j`, then we can remove `j`. So just
404402
// mark it as dead and move on; subsequent indices will be

0 commit comments

Comments
 (0)