Skip to content

Commit ff0eff4

Browse files
authored
Rollup merge of rust-lang#65749 - Centril:insurance-policy, r=RalfJung
Insurance policy in case `iter.size_hint()` lies. Follow up to https://github.com/rust-lang/rust/pull/64949/files#r334235076. (If the perf impact is bad we can use `debug_assert!` instead.) The good news is that the UI tests pass locally so `iter.size_hint()` seems to be honest *thus far*. On the other hand, with the status quo we do not have an insurance policy should that change in some case. This is problematic because a) this could possibly make some program be accepted which shouldn't, b) the compiler itself could have memory unsafety if the correctness of the iterator is assumed in `unsafe { ... }` code (even though the blame lies with the `unsafe { ... }` block in question.) r? @RalfJung cc @nnethercote
2 parents c0db0db + dfcfca2 commit ff0eff4

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/librustc/ty/context.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -2930,14 +2930,18 @@ impl<T, R, E> InternIteratorElement<T, R> for Result<T, E> {
29302930
// lower bounds from `size_hint` agree they are correct.
29312931
Ok(match iter.size_hint() {
29322932
(1, Some(1)) => {
2933-
f(&[iter.next().unwrap()?])
2933+
let t0 = iter.next().unwrap()?;
2934+
assert!(iter.next().is_none());
2935+
f(&[t0])
29342936
}
29352937
(2, Some(2)) => {
29362938
let t0 = iter.next().unwrap()?;
29372939
let t1 = iter.next().unwrap()?;
2940+
assert!(iter.next().is_none());
29382941
f(&[t0, t1])
29392942
}
29402943
(0, Some(0)) => {
2944+
assert!(iter.next().is_none());
29412945
f(&[])
29422946
}
29432947
_ => {

0 commit comments

Comments
 (0)