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 3c74d02

Browse files
committedMar 11, 2025
debug-assert that the size_hint is well-formed in collect
1 parent 3ea711f commit 3c74d02

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
@@ -1968,6 +1968,15 @@ pub trait Iterator {
19681968
where
19691969
Self: Sized,
19701970
{
1971+
// This is too aggressive to turn on for everything all the time, but PR#137908
1972+
// accidentally noticed that some rustc iterators had malformed `size_hint`s,
1973+
// so this will help catch such things in debug-assertions-std runners,
1974+
// even if users won't actually ever see it.
1975+
if cfg!(debug_assertions) {
1976+
let hint = self.size_hint();
1977+
assert!(hint.1.is_none_or(|high| high >= hint.0), "Malformed size_hint {hint:?}");
1978+
}
1979+
19711980
FromIterator::from_iter(self)
19721981
}
19731982

0 commit comments

Comments
 (0)
Failed to load comments.