Skip to content

Commit 2f23a0f

Browse files
committed
fix debug assertion
The InPlaceIterable debug assert checks that the write pointer did not advance beyond the read pointer. But TrustedRandomAccess never advances the read pointer, thus triggering the assert. Skip the assert if the source pointer did not change during iteration.
1 parent 8e5fe55 commit 2f23a0f

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

library/alloc/src/vec.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -2285,9 +2285,15 @@ where
22852285
return SpecFromIterNested::from_iter(iterator);
22862286
}
22872287

2288-
let (src_buf, dst_buf, dst_end, cap) = unsafe {
2288+
let (src_buf, src_ptr, dst_buf, dst_end, cap) = unsafe {
22892289
let inner = iterator.as_inner().as_into_iter();
2290-
(inner.buf.as_ptr(), inner.buf.as_ptr() as *mut T, inner.end as *const T, inner.cap)
2290+
(
2291+
inner.buf.as_ptr(),
2292+
inner.ptr,
2293+
inner.buf.as_ptr() as *mut T,
2294+
inner.end as *const T,
2295+
inner.cap,
2296+
)
22912297
};
22922298

22932299
// use try-fold since
@@ -2302,10 +2308,18 @@ where
23022308
let dst = mem::ManuallyDrop::new(sink).dst;
23032309

23042310
let src = unsafe { iterator.as_inner().as_into_iter() };
2305-
// check if SourceIter and InPlaceIterable contracts were upheld.
2311+
// check if SourceIter contract was upheld
23062312
// caveat: if they weren't we may not even make it to this point
23072313
debug_assert_eq!(src_buf, src.buf.as_ptr());
2308-
debug_assert!(dst as *const _ <= src.ptr, "InPlaceIterable contract violation");
2314+
// check InPlaceIterable contract. This is only possible if the iterator advanced the
2315+
// source pointer at all. If it uses unchecked access via TrustedRandomAccess
2316+
// then the source pointer will stay in its initial position and we can't use it as reference
2317+
if src.ptr != src_ptr {
2318+
debug_assert!(
2319+
dst as *const _ <= src.ptr,
2320+
"InPlaceIterable contract violation, write pointer advanced beyond read pointer"
2321+
);
2322+
}
23092323

23102324
// drop any remaining values at the tail of the source
23112325
src.drop_remaining();

0 commit comments

Comments
 (0)