about summary refs log tree commit diff
path: root/src/liballoc
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2019-01-28 22:25:41 +0100
committerGitHub <noreply@github.com>2019-01-28 22:25:41 +0100
commitf21f83d11710f13e285dab4bea7b9bebd1d47814 (patch)
tree5107c096fdcbfc3170fc34efe95b28b02ac3033e /src/liballoc
parentd8a0dd7ae88023bd09fa4b86c9ca1f6ed8095b43 (diff)
parent489a79247ddf5da8090019ff4da4b688ad55afa7 (diff)
downloadrust-f21f83d11710f13e285dab4bea7b9bebd1d47814.tar.gz
rust-f21f83d11710f13e285dab4bea7b9bebd1d47814.zip
Rollup merge of #57045 - RalfJung:kill-more-uninit, r=SimonSapin
Kill remaining uses of mem::uninitialized in libcore, liballoc

Kill remaining uses of mem::uninitialized in libcore and liballoc, and enable a lint that will warn when uses are added again in the future.

To avoid casting raw pointers around (which is always very dangerous because it is not typechecked at all -- it doesn't even get the "same size" sanity check that `transmute` gets), I also added two new functions to `MaybeUninit`:

```rust
    /// Get a pointer to the first contained values.
    pub fn first_ptr(this: &[MaybeUninit<T>]) -> *const T {
        this as *const [MaybeUninit<T>] as *const T
    }

    /// Get a mutable pointer to the first contained values.
    pub fn first_mut_ptr(this: &mut [MaybeUninit<T>]) -> *mut T {
        this as *mut [MaybeUninit<T>] as *mut T
    }
```

I changed some of the existing code to use array-of-`MaybeUninit` instead of `MaybeUninit`-of-array, successfully removing raw pointer casts there as well.
Diffstat (limited to 'src/liballoc')
-rw-r--r--src/liballoc/borrow.rs1
-rw-r--r--src/liballoc/collections/btree/node.rs36
-rw-r--r--src/liballoc/lib.rs5
-rw-r--r--src/liballoc/rc.rs2
4 files changed, 23 insertions, 21 deletions
diff --git a/src/liballoc/borrow.rs b/src/liballoc/borrow.rs
index 603d73100a8..b47337e44b2 100644
--- a/src/liballoc/borrow.rs
+++ b/src/liballoc/borrow.rs
@@ -380,7 +380,6 @@ impl<'a, B: ?Sized> Hash for Cow<'a, B>
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]
-#[allow(deprecated)]
 impl<'a, T: ?Sized + ToOwned> AsRef<T> for Cow<'a, T> {
     fn as_ref(&self) -> &T {
         self
diff --git a/src/liballoc/collections/btree/node.rs b/src/liballoc/collections/btree/node.rs
index f9a21aa95db..e969e119dbe 100644
--- a/src/liballoc/collections/btree/node.rs
+++ b/src/liballoc/collections/btree/node.rs
@@ -95,8 +95,8 @@ struct LeafNode<K, V> {
 
     /// The arrays storing the actual data of the node. Only the first `len` elements of each
     /// array are initialized and valid.
-    keys: MaybeUninit<[K; CAPACITY]>,
-    vals: MaybeUninit<[V; CAPACITY]>,
+    keys: [MaybeUninit<K>; CAPACITY],
+    vals: [MaybeUninit<V>; CAPACITY],
 }
 
 impl<K, V> LeafNode<K, V> {
@@ -106,8 +106,8 @@ impl<K, V> LeafNode<K, V> {
         LeafNode {
             // As a general policy, we leave fields uninitialized if they can be, as this should
             // be both slightly faster and easier to track in Valgrind.
-            keys: MaybeUninit::uninitialized(),
-            vals: MaybeUninit::uninitialized(),
+            keys: uninitialized_array![_; CAPACITY],
+            vals: uninitialized_array![_; CAPACITY],
             parent: ptr::null(),
             parent_idx: MaybeUninit::uninitialized(),
             len: 0
@@ -145,7 +145,7 @@ struct InternalNode<K, V> {
 
     /// The pointers to the children of this node. `len + 1` of these are considered
     /// initialized and valid.
-    edges: [BoxedNode<K, V>; 2 * B],
+    edges: [MaybeUninit<BoxedNode<K, V>>; 2 * B],
 }
 
 impl<K, V> InternalNode<K, V> {
@@ -159,7 +159,7 @@ impl<K, V> InternalNode<K, V> {
     unsafe fn new() -> Self {
         InternalNode {
             data: LeafNode::new(),
-            edges: mem::uninitialized()
+            edges: uninitialized_array![_; 2*B],
         }
     }
 }
@@ -261,7 +261,7 @@ impl<K, V> Root<K, V> {
             -> NodeRef<marker::Mut, K, V, marker::Internal> {
         debug_assert!(!self.is_shared_root());
         let mut new_node = Box::new(unsafe { InternalNode::new() });
-        new_node.edges[0] = unsafe { BoxedNode::from_ptr(self.node.as_ptr()) };
+        new_node.edges[0].set(unsafe { BoxedNode::from_ptr(self.node.as_ptr()) });
 
         self.node = BoxedNode::from_internal(new_node);
         self.height += 1;
@@ -623,7 +623,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Immut<'a>, K, V, Type> {
         // We cannot be the root, so `as_leaf` is okay
         unsafe {
             slice::from_raw_parts(
-                self.as_leaf().vals.as_ptr() as *const V,
+                MaybeUninit::first_ptr(&self.as_leaf().vals),
                 self.len()
             )
         }
@@ -650,7 +650,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
         } else {
             unsafe {
                 slice::from_raw_parts_mut(
-                    (*self.as_leaf_mut()).keys.as_mut_ptr() as *mut K,
+                    MaybeUninit::first_ptr_mut(&mut (*self.as_leaf_mut()).keys),
                     self.len()
                 )
             }
@@ -661,7 +661,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
         debug_assert!(!self.is_shared_root());
         unsafe {
             slice::from_raw_parts_mut(
-                (*self.as_leaf_mut()).vals.as_mut_ptr() as *mut V,
+                MaybeUninit::first_ptr_mut(&mut (*self.as_leaf_mut()).vals),
                 self.len()
             )
         }
@@ -718,7 +718,7 @@ impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
         unsafe {
             ptr::write(self.keys_mut().get_unchecked_mut(idx), key);
             ptr::write(self.vals_mut().get_unchecked_mut(idx), val);
-            ptr::write(self.as_internal_mut().edges.get_unchecked_mut(idx + 1), edge.node);
+            self.as_internal_mut().edges.get_unchecked_mut(idx + 1).set(edge.node);
 
             (*self.as_leaf_mut()).len += 1;
 
@@ -749,7 +749,7 @@ impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
             slice_insert(self.vals_mut(), 0, val);
             slice_insert(
                 slice::from_raw_parts_mut(
-                    self.as_internal_mut().edges.as_mut_ptr(),
+                    MaybeUninit::first_ptr_mut(&mut self.as_internal_mut().edges),
                     self.len()+1
                 ),
                 0,
@@ -778,7 +778,9 @@ impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
             let edge = match self.reborrow_mut().force() {
                 ForceResult::Leaf(_) => None,
                 ForceResult::Internal(internal) => {
-                    let edge = ptr::read(internal.as_internal().edges.get_unchecked(idx + 1));
+                    let edge = ptr::read(
+                        internal.as_internal().edges.get_unchecked(idx + 1).as_ptr()
+                    );
                     let mut new_root = Root { node: edge, height: internal.height - 1 };
                     (*new_root.as_mut().as_leaf_mut()).parent = ptr::null();
                     Some(new_root)
@@ -806,7 +808,7 @@ impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
                 ForceResult::Internal(mut internal) => {
                     let edge = slice_remove(
                         slice::from_raw_parts_mut(
-                            internal.as_internal_mut().edges.as_mut_ptr(),
+                            MaybeUninit::first_ptr_mut(&mut internal.as_internal_mut().edges),
                             old_len+1
                         ),
                         0
@@ -1085,7 +1087,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
 
             slice_insert(
                 slice::from_raw_parts_mut(
-                    self.node.as_internal_mut().edges.as_mut_ptr(),
+                    MaybeUninit::first_ptr_mut(&mut self.node.as_internal_mut().edges),
                     self.node.len()
                 ),
                 self.idx + 1,
@@ -1140,7 +1142,9 @@ impl<BorrowType, K, V>
     pub fn descend(self) -> NodeRef<BorrowType, K, V, marker::LeafOrInternal> {
         NodeRef {
             height: self.node.height - 1,
-            node: unsafe { self.node.as_internal().edges.get_unchecked(self.idx).as_ptr() },
+            node: unsafe {
+                self.node.as_internal().edges.get_unchecked(self.idx).get_ref().as_ptr()
+            },
             root: self.node.root,
             _marker: PhantomData
         }
diff --git a/src/liballoc/lib.rs b/src/liballoc/lib.rs
index 3050a93ef39..d2ff1bae635 100644
--- a/src/liballoc/lib.rs
+++ b/src/liballoc/lib.rs
@@ -63,8 +63,9 @@
 #![no_std]
 #![needs_allocator]
 
-#![deny(intra_doc_link_resolution_failure)]
-#![deny(missing_debug_implementations)]
+#![warn(deprecated_in_future)]
+#![warn(intra_doc_link_resolution_failure)]
+#![warn(missing_debug_implementations)]
 
 #![cfg_attr(not(test), feature(fn_traits))]
 #![cfg_attr(not(test), feature(generator_trait))]
diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs
index d3a55c59ff6..c0dc010fe59 100644
--- a/src/liballoc/rc.rs
+++ b/src/liballoc/rc.rs
@@ -1,5 +1,3 @@
-#![allow(deprecated)]
-
 //! Single-threaded reference-counting pointers. 'Rc' stands for 'Reference
 //! Counted'.
 //!