Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6b39be3

Browse files
committedMar 3, 2025
Auto merge of #137923 - scottmcm:fix-postorder-size-hint, r=<try>
Simplify `<Postorder as Iterator>::size_hint` The current version is sometimes malformed (cc #137919); let's see if we can get away with a loose but trivially-correct one.
2 parents 81d8edc + e403654 commit 6b39be3

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed
 

‎compiler/rustc_middle/src/mir/traversal.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ pub struct Postorder<'a, 'tcx> {
108108
basic_blocks: &'a IndexSlice<BasicBlock, BasicBlockData<'tcx>>,
109109
visited: DenseBitSet<BasicBlock>,
110110
visit_stack: Vec<(BasicBlock, Successors<'a>)>,
111-
root_is_start_block: bool,
112111
/// A non-empty `extra` allows for a precise calculation of the successors.
113112
extra: Option<(TyCtxt<'tcx>, Instance<'tcx>)>,
114113
}
@@ -123,7 +122,6 @@ impl<'a, 'tcx> Postorder<'a, 'tcx> {
123122
basic_blocks,
124123
visited: DenseBitSet::new_empty(basic_blocks.len()),
125124
visit_stack: Vec::new(),
126-
root_is_start_block: root == START_BLOCK,
127125
extra,
128126
};
129127

@@ -211,16 +209,13 @@ impl<'tcx> Iterator for Postorder<'_, 'tcx> {
211209
}
212210

213211
fn size_hint(&self) -> (usize, Option<usize>) {
214-
// All the blocks, minus the number of blocks we've visited.
215-
let upper = self.basic_blocks.len() - self.visited.count();
216-
217-
let lower = if self.root_is_start_block {
218-
// We will visit all remaining blocks exactly once.
219-
upper
220-
} else {
221-
self.visit_stack.len()
222-
};
212+
// These bounds are not at all tight, but that's fine.
213+
// It's not worth a popcnt loop in `DenseBitSet` to improve the upper,
214+
// and in mono-reachable we can't be precise anyway.
215+
// Leaning on amortized growth is fine.
223216

217+
let lower = self.visit_stack.len();
218+
let upper = self.basic_blocks.len();
224219
(lower, Some(upper))
225220
}
226221
}

0 commit comments

Comments
 (0)
Failed to load comments.