Skip to content

Commit 18a6d45

Browse files
committed
Documentation improvements
1 parent cdddb4c commit 18a6d45

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

library/alloc/src/vec/source_iter_marker.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ where
7171
// drop any remaining values at the tail of the source
7272
// but prevent drop of the allocation itself once IntoIter goes out of scope
7373
// if the drop panics then we also leak any elements collected into dst_buf
74+
//
75+
// FIXME: Since `SpecInPlaceCollect::collect_in_place` above might use
76+
// `__iterator_get_unchecked` internally, this call might be operating on
77+
// a `vec::IntoIter` with incorrect internal state regarding which elements
78+
// have already been “consumed”. However, the `TrustedRandomIteratorNoCoerce`
79+
// implementation of `vec::IntoIter` is only present if the `Vec` elements
80+
// don’t have a destructor, so it doesn’t matter if elements are “dropped multiple times”
81+
// in this case.
82+
// This argument technically currently lacks justification from the `# Safety` docs for
83+
// `SourceIter`/`InPlaceIterable` and/or `TrustedRandomAccess`, so it might be possible that
84+
// someone could inadvertently create new library unsoundness
85+
// involving this `.forget_allocation_drop_remaining()` call.
7486
src.forget_allocation_drop_remaining();
7587

7688
let vec = unsafe { Vec::from_raw_parts(dst_buf, len, cap) };
@@ -101,8 +113,11 @@ fn write_in_place_with_drop<T>(
101113
trait SpecInPlaceCollect<T, I>: Iterator<Item = T> {
102114
/// Collects an iterator (`self`) into the destination buffer (`dst`) and returns the number of items
103115
/// collected. `end` is the last writable element of the allocation and used for bounds checks.
104-
// FIXME: Clarify safety conditions. Iterator must not be coerced to a subtype
105-
// after this call due to potential use of [`TrustedRandomAccessNoCoerce`].
116+
///
117+
/// This method is specialized and one of its implementations makes use of
118+
/// `Iterator::__iterator_get_unchecked` calls with a `TrustedRandomAccessNoCoerce` bound
119+
/// on `I` which means the caller of this method must take the safety conditions
120+
/// of that trait into consideration.
106121
fn collect_in_place(&mut self, dst: *mut T, end: *const T) -> usize;
107122
}
108123

library/core/src/iter/adapters/zip.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,12 @@ pub unsafe trait TrustedRandomAccess: TrustedRandomAccessNoCoerce {}
524524

525525
/// Like [`TrustedRandomAccess`] but without any of the requirements / guarantees around
526526
/// coercions to subtypes after `__iterator_get_unchecked` (they aren’t allowed here!), and
527-
/// without the requirement that subtypes / supertypes implement [`TrustedRandomAccessNoCoerce`].
527+
/// without the requirement that subtypes / supertypes implement `TrustedRandomAccessNoCoerce`.
528+
///
529+
/// This trait was created in PR #85874 to fix soundness issue #85873 without performance regressions.
530+
/// It is subject to change as we might want to build a more generally useful (for performance
531+
/// optimizations) and more sophisticated trait or trait hierarchy that replaces or extends
532+
/// [`TrustedRandomAccess`] and `TrustedRandomAccessNoCoerce`.
528533
#[doc(hidden)]
529534
#[unstable(feature = "trusted_random_access", issue = "none")]
530535
#[rustc_specialization_trait]

0 commit comments

Comments
 (0)