Skip to content

Commit 20d6cb3

Browse files
committed
Mitigate focused memory leaks in alloc doctests for Miri.
If/when `-Zmiri-disable-leak-check` is able to be used at test-granularity, it should applied to these tests instead of unleaking.
1 parent cac8902 commit 20d6cb3

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

alloc/src/boxed.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,9 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
12131213
/// let static_ref: &'static mut usize = Box::leak(x);
12141214
/// *static_ref += 1;
12151215
/// assert_eq!(*static_ref, 42);
1216+
/// # // FIXME(https://github.com/rust-lang/miri/issues/3670):
1217+
/// # // use -Zmiri-disable-leak-check instead of unleaking in tests meant to leak.
1218+
/// # drop(unsafe { Box::from_raw(static_ref) });
12161219
/// ```
12171220
///
12181221
/// Unsized data:
@@ -1222,6 +1225,9 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
12221225
/// let static_ref = Box::leak(x);
12231226
/// static_ref[0] = 4;
12241227
/// assert_eq!(*static_ref, [4, 2, 3]);
1228+
/// # // FIXME(https://github.com/rust-lang/miri/issues/3670):
1229+
/// # // use -Zmiri-disable-leak-check instead of unleaking in tests meant to leak.
1230+
/// # drop(unsafe { Box::from_raw(static_ref) });
12251231
/// ```
12261232
#[stable(feature = "box_leak", since = "1.26.0")]
12271233
#[inline]

alloc/src/string.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1984,6 +1984,9 @@ impl String {
19841984
/// let x = String::from("bucket");
19851985
/// let static_ref: &'static mut str = x.leak();
19861986
/// assert_eq!(static_ref, "bucket");
1987+
/// # // FIXME(https://github.com/rust-lang/miri/issues/3670):
1988+
/// # // use -Zmiri-disable-leak-check instead of unleaking in tests meant to leak.
1989+
/// # drop(unsafe { Box::from_raw(static_ref) });
19871990
/// ```
19881991
#[stable(feature = "string_leak", since = "1.72.0")]
19891992
#[inline]

alloc/src/vec/into_iter.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,15 @@ impl<T, A: Allocator> IntoIter<T, A> {
120120
/// This is roughly equivalent to the following, but more efficient
121121
///
122122
/// ```
123-
/// # let mut into_iter = Vec::<u8>::with_capacity(10).into_iter();
123+
/// # let mut vec = Vec::<u8>::with_capacity(10);
124+
/// # let ptr = vec.as_mut_ptr();
125+
/// # let mut into_iter = vec.into_iter();
124126
/// let mut into_iter = std::mem::replace(&mut into_iter, Vec::new().into_iter());
125127
/// (&mut into_iter).for_each(drop);
126128
/// std::mem::forget(into_iter);
129+
/// # // FIXME(https://github.com/rust-lang/miri/issues/3670):
130+
/// # // use -Zmiri-disable-leak-check instead of unleaking in tests meant to leak.
131+
/// # drop(unsafe { Vec::<u8>::from_raw_parts(ptr, 0, 10) });
127132
/// ```
128133
///
129134
/// This method is used by in-place iteration, refer to the vec::in_place_collect

alloc/src/vec/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1473,6 +1473,9 @@ impl<T, A: Allocator> Vec<T, A> {
14731473
/// // 2. `0 <= capacity` always holds whatever `capacity` is.
14741474
/// unsafe {
14751475
/// vec.set_len(0);
1476+
/// # // FIXME(https://github.com/rust-lang/miri/issues/3670):
1477+
/// # // use -Zmiri-disable-leak-check instead of unleaking in tests meant to leak.
1478+
/// # vec.set_len(3);
14761479
/// }
14771480
/// ```
14781481
///
@@ -2391,6 +2394,9 @@ impl<T, A: Allocator> Vec<T, A> {
23912394
/// let static_ref: &'static mut [usize] = x.leak();
23922395
/// static_ref[0] += 1;
23932396
/// assert_eq!(static_ref, &[2, 2, 3]);
2397+
/// # // FIXME(https://github.com/rust-lang/miri/issues/3670):
2398+
/// # // use -Zmiri-disable-leak-check instead of unleaking in tests meant to leak.
2399+
/// # drop(unsafe { Box::from_raw(static_ref) });
23942400
/// ```
23952401
#[stable(feature = "vec_leak", since = "1.47.0")]
23962402
#[inline]

0 commit comments

Comments
 (0)