diff options
| author | Scott McMurray <scottmcm@users.noreply.github.com> | 2025-03-10 18:22:28 -0700 |
|---|---|---|
| committer | Scott McMurray <scottmcm@users.noreply.github.com> | 2025-03-10 18:22:28 -0700 |
| commit | 3c74d02319ea8ebbfd16f70f3154ce0aef87ae5c (patch) | |
| tree | 9827dd55f0045e269b0af432a56e62bdb2213814 | |
| parent | 3ea711f17e3946ac3f4df11691584e2c56b4b0cf (diff) | |
| download | rust-3c74d02319ea8ebbfd16f70f3154ce0aef87ae5c.tar.gz rust-3c74d02319ea8ebbfd16f70f3154ce0aef87ae5c.zip | |
debug-assert that the size_hint is well-formed in `collect`
| -rw-r--r-- | library/core/src/iter/traits/iterator.rs | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/library/core/src/iter/traits/iterator.rs b/library/core/src/iter/traits/iterator.rs index 42886e90f99..25cb4795b4d 100644 --- a/library/core/src/iter/traits/iterator.rs +++ b/library/core/src/iter/traits/iterator.rs @@ -1968,6 +1968,15 @@ pub trait Iterator { where Self: Sized, { + // This is too aggressive to turn on for everything all the time, but PR#137908 + // accidentally noticed that some rustc iterators had malformed `size_hint`s, + // so this will help catch such things in debug-assertions-std runners, + // even if users won't actually ever see it. + if cfg!(debug_assertions) { + let hint = self.size_hint(); + assert!(hint.1.is_none_or(|high| high >= hint.0), "Malformed size_hint {hint:?}"); + } + FromIterator::from_iter(self) } |
