about summary refs log tree commit diff
path: root/src/liballoc/tests
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2019-05-26 02:13:25 +0200
committerGitHub <noreply@github.com>2019-05-26 02:13:25 +0200
commitf185ee5fda65a82d0efdfe0504e2712cecf18cdf (patch)
treedca8502ddbd51edb1ff99893a7ae193121ad73da /src/liballoc/tests
parent24cc368118e434719bee711ad34f0a370b27231d (diff)
parent428ab7e1bd77b82db30dc574b3f9c4fe0a7c77f6 (diff)
downloadrust-f185ee5fda65a82d0efdfe0504e2712cecf18cdf.tar.gz
rust-f185ee5fda65a82d0efdfe0504e2712cecf18cdf.zip
Rollup merge of #61114 - RalfJung:vec, r=Gankro
Vec: avoid creating slices to the elements

Instead of `self.deref_mut().as_mut_ptr()` to get a raw pointer to the buffer, use `self.buf.ptr_mut()`. This (a) avoids creating a unique reference to all existing elements without any need, and (b) creates a pointer that can actually be used for the *entire* buffer, and not just for the part of it covered by `self.deref_mut()`.

I also got worried about `RawVec::ptr` returning a `*mut T` from an `&self`, so I added both a mutable and an immutable version.

Cc @Gankro in particular for the `assume` changes -- I don't know why that is not in `Unique`, but I moved it up from `Vec::deref` to `RawVec::ptr` to avoid having to repeat it everywhere.

Fixes https://github.com/rust-lang/rust/issues/60847
Diffstat (limited to 'src/liballoc/tests')
-rw-r--r--src/liballoc/tests/vec.rs21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/liballoc/tests/vec.rs b/src/liballoc/tests/vec.rs
index 3307bdf94f9..5ddac673c9f 100644
--- a/src/liballoc/tests/vec.rs
+++ b/src/liballoc/tests/vec.rs
@@ -1152,3 +1152,24 @@ fn test_try_reserve_exact() {
     }
 
 }
+
+#[test]
+fn test_stable_push_pop() {
+    // Test that, if we reserved enough space, adding and removing elements does not
+    // invalidate references into the vector (such as `v0`).  This test also
+    // runs in Miri, which would detect such problems.
+    let mut v = Vec::with_capacity(10);
+    v.push(13);
+
+    // laundering the lifetime -- we take care that `v` does not reallocate, so that's okay.
+    let v0 = unsafe { &*(&v[0] as *const _) };
+
+    // Now do a bunch of things and occasionally use `v0` again to assert it is still valid.
+    v.push(1);
+    v.push(2);
+    v.insert(1, 1);
+    assert_eq!(*v0, 13);
+    v.remove(1);
+    v.pop().unwrap();
+    assert_eq!(*v0, 13);
+}