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 7047d14

Browse files
authoredMar 16, 2025
Unrolled build for rust-lang#138329
Rollup merge of rust-lang#138329 - scottmcm:assert-hint, r=Mark-Simulacrum debug-assert that the size_hint is well-formed in `collect` Closes rust-lang#137919 In the hopes of helping to catch any future accidentally-incorrect rustc or stdlib iterators (like the ones rust-lang#137908 accidentally found), this has `Iterator::collect` call `size_hint` and check its `low` doesn't exceed its `Some(high)`. There's of course a bazillion more places this *could* be checked, but the hope is that this one is a good tradeoff of being likely to catch lots of things while having minimal maintenance cost (especially compared to putting it in *every* container's `from_iter`).
2 parents 9f274ba + 3c74d02 commit 7047d14

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed
 

‎library/core/src/iter/traits/iterator.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1977,6 +1977,15 @@ pub trait Iterator {
19771977
where
19781978
Self: Sized,
19791979
{
1980+
// This is too aggressive to turn on for everything all the time, but PR#137908
1981+
// accidentally noticed that some rustc iterators had malformed `size_hint`s,
1982+
// so this will help catch such things in debug-assertions-std runners,
1983+
// even if users won't actually ever see it.
1984+
if cfg!(debug_assertions) {
1985+
let hint = self.size_hint();
1986+
assert!(hint.1.is_none_or(|high| high >= hint.0), "Malformed size_hint {hint:?}");
1987+
}
1988+
19801989
FromIterator::from_iter(self)
19811990
}
19821991

0 commit comments

Comments
 (0)
Failed to load comments.