From c11e514e9dfd44eebe435408b32de4193d87860c Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Thu, 20 Dec 2018 22:51:08 +0100 Subject: liballoc: remove unneeded allow(deprecated) --- src/liballoc/borrow.rs | 1 - src/liballoc/rc.rs | 2 -- 2 files changed, 3 deletions(-) (limited to 'src/liballoc') 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 for Cow<'a, T> { fn as_ref(&self) -> &T { self 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'. //! -- cgit 1.4.1-3-g733a5 From ffd73df7559be73398679d7f09447379dd0c2098 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 21 Dec 2018 11:22:18 +0100 Subject: avoid mem::uninitialized in BTreeMap --- src/liballoc/collections/btree/node.rs | 25 ++++++++++++++++--------- src/liballoc/lib.rs | 1 + src/libcore/lib.rs | 2 +- 3 files changed, 18 insertions(+), 10 deletions(-) (limited to 'src/liballoc') diff --git a/src/liballoc/collections/btree/node.rs b/src/liballoc/collections/btree/node.rs index f9a21aa95db..cc46bae01cb 100644 --- a/src/liballoc/collections/btree/node.rs +++ b/src/liballoc/collections/btree/node.rs @@ -145,7 +145,7 @@ struct InternalNode { /// The pointers to the children of this node. `len + 1` of these are considered /// initialized and valid. - edges: [BoxedNode; 2 * B], + edges: [MaybeUninit>; 2 * B], } impl InternalNode { @@ -159,7 +159,10 @@ impl InternalNode { unsafe fn new() -> Self { InternalNode { data: LeafNode::new(), - edges: mem::uninitialized() + // Creating a `[MaybeUninit; N]` array by first creating a + // `MaybeUninit<[MaybeUninit; N]>`; the `into_inner` is safe because the inner + // array does not require initialization. + edges: MaybeUninit::uninitialized().into_inner(), } } } @@ -261,7 +264,7 @@ impl Root { -> NodeRef { 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; @@ -718,7 +721,7 @@ impl<'a, K, V> NodeRef, 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 +752,7 @@ impl<'a, K, V> NodeRef, 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_mut_ptr(&mut self.as_internal_mut().edges), self.len()+1 ), 0, @@ -778,7 +781,9 @@ impl<'a, K, V> NodeRef, 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 +811,7 @@ impl<'a, K, V> NodeRef, 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_mut_ptr(&mut internal.as_internal_mut().edges), old_len+1 ), 0 @@ -1085,7 +1090,7 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: slice_insert( slice::from_raw_parts_mut( - self.node.as_internal_mut().edges.as_mut_ptr(), + MaybeUninit::first_mut_ptr(&mut self.node.as_internal_mut().edges), self.node.len() ), self.idx + 1, @@ -1140,7 +1145,9 @@ impl pub fn descend(self) -> NodeRef { 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..cf9d89ee05a 100644 --- a/src/liballoc/lib.rs +++ b/src/liballoc/lib.rs @@ -63,6 +63,7 @@ #![no_std] #![needs_allocator] +#![warn(deprecated_in_future)] #![deny(intra_doc_link_resolution_failure)] #![deny(missing_debug_implementations)] diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index dbf0b158189..4d7da3692fd 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -58,8 +58,8 @@ issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/", test(no_crate_inject, attr(deny(warnings))), test(attr(allow(dead_code, deprecated, unused_variables, unused_mut))))] - #![no_core] + #![warn(deprecated_in_future)] #![deny(missing_docs)] #![deny(intra_doc_link_resolution_failure)] -- cgit 1.4.1-3-g733a5 From 630aaa4e801393c55c8327f642aec2349adfaee3 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 21 Dec 2018 15:10:19 +0100 Subject: avoid some raw ptr casts in BTreeMap --- src/liballoc/collections/btree/node.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'src/liballoc') diff --git a/src/liballoc/collections/btree/node.rs b/src/liballoc/collections/btree/node.rs index cc46bae01cb..8dd4aec136a 100644 --- a/src/liballoc/collections/btree/node.rs +++ b/src/liballoc/collections/btree/node.rs @@ -95,8 +95,8 @@ struct LeafNode { /// 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; CAPACITY], + vals: [MaybeUninit; CAPACITY], } impl LeafNode { @@ -106,8 +106,11 @@ impl LeafNode { 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(), + // Creating a `[MaybeUninit; N]` array by first creating a + // `MaybeUninit<[MaybeUninit; N]>`; the `into_inner` is safe because the inner + // array does not require initialization. + keys: MaybeUninit::uninitialized().into_inner(), + vals: MaybeUninit::uninitialized().into_inner(), parent: ptr::null(), parent_idx: MaybeUninit::uninitialized(), len: 0 @@ -626,7 +629,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef, 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() ) } @@ -653,7 +656,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef, K, V, Type> { } else { unsafe { slice::from_raw_parts_mut( - (*self.as_leaf_mut()).keys.as_mut_ptr() as *mut K, + MaybeUninit::first_mut_ptr(&mut (*self.as_leaf_mut()).keys), self.len() ) } @@ -664,7 +667,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef, 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_mut_ptr(&mut (*self.as_leaf_mut()).vals), self.len() ) } -- cgit 1.4.1-3-g733a5 From 22a947f3aa4b990efa135e3593fe7365bc7c36b9 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 22 Dec 2018 11:02:06 +0100 Subject: add macro for creating uninitialized array --- src/liballoc/collections/btree/node.rs | 12 +++--------- src/libcore/fmt/num.rs | 14 ++------------ src/libcore/lib.rs | 1 + src/libcore/macros.rs | 17 +++++++++++++++++ src/libcore/slice/sort.rs | 11 ++--------- 5 files changed, 25 insertions(+), 30 deletions(-) (limited to 'src/liballoc') diff --git a/src/liballoc/collections/btree/node.rs b/src/liballoc/collections/btree/node.rs index 8dd4aec136a..25810d680fa 100644 --- a/src/liballoc/collections/btree/node.rs +++ b/src/liballoc/collections/btree/node.rs @@ -106,11 +106,8 @@ impl LeafNode { 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. - // Creating a `[MaybeUninit; N]` array by first creating a - // `MaybeUninit<[MaybeUninit; N]>`; the `into_inner` is safe because the inner - // array does not require initialization. - keys: MaybeUninit::uninitialized().into_inner(), - vals: MaybeUninit::uninitialized().into_inner(), + keys: uninitialized_array![_; CAPACITY], + vals: uninitialized_array![_; CAPACITY], parent: ptr::null(), parent_idx: MaybeUninit::uninitialized(), len: 0 @@ -162,10 +159,7 @@ impl InternalNode { unsafe fn new() -> Self { InternalNode { data: LeafNode::new(), - // Creating a `[MaybeUninit; N]` array by first creating a - // `MaybeUninit<[MaybeUninit; N]>`; the `into_inner` is safe because the inner - // array does not require initialization. - edges: MaybeUninit::uninitialized().into_inner(), + edges: uninitialized_array![_; 2*B], } } } diff --git a/src/libcore/fmt/num.rs b/src/libcore/fmt/num.rs index ff284d2978e..5283c6d7ef3 100644 --- a/src/libcore/fmt/num.rs +++ b/src/libcore/fmt/num.rs @@ -51,12 +51,7 @@ trait GenericRadix { // characters for a base 2 number. let zero = T::zero(); let is_nonnegative = x >= zero; - // Creating a `[MaybeUninit; N]` array by first creating a - // `MaybeUninit<[MaybeUninit; N]>`; the `into_inner` is safe because the inner - // array does not require initialization. - let mut buf: [MaybeUninit; 128] = unsafe { - MaybeUninit::uninitialized().into_inner() - }; + let mut buf = uninitialized_array![u8; 128]; let mut curr = buf.len(); let base = T::from_u8(Self::BASE); if is_nonnegative { @@ -203,12 +198,7 @@ macro_rules! impl_Display { // convert the negative num to positive by summing 1 to it's 2 complement (!self.$conv_fn()).wrapping_add(1) }; - // Creating a `[MaybeUninit; N]` array by first creating a - // `MaybeUninit<[MaybeUninit; N]>`; the `into_inner` is safe because the inner - // array does not require initialization. - let mut buf: [MaybeUninit; 39] = unsafe { - MaybeUninit::uninitialized().into_inner() - }; + let mut buf = uninitialized_array![u8; 39]; let mut curr = buf.len() as isize; let buf_ptr = MaybeUninit::first_mut_ptr(&mut buf); let lut_ptr = DEC_DIGITS_LUT.as_ptr(); diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index 4d7da3692fd..1295acb44a5 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -123,6 +123,7 @@ #![feature(structural_match)] #![feature(abi_unadjusted)] #![feature(adx_target_feature)] +#![feature(maybe_uninit)] #[prelude_import] #[allow(unused)] diff --git a/src/libcore/macros.rs b/src/libcore/macros.rs index 2f350df2f5c..12b7adb8a9d 100644 --- a/src/libcore/macros.rs +++ b/src/libcore/macros.rs @@ -547,6 +547,23 @@ macro_rules! unimplemented { ($($arg:tt)+) => (panic!("not yet implemented: {}", format_args!($($arg)*))); } +/// A macro to create an array of [`MaybeUninit`] +/// +/// This macro constructs and uninitialized array of the type `[MaybeUninit; N]`. +/// +/// [`MaybeUninit`]: mem/union.MaybeUninit.html +#[macro_export] +#[unstable(feature = "maybe_uninit", issue = "53491")] +macro_rules! uninitialized_array { + // This `into_inner` is safe because an array of `MaybeUninit` does not + // require initialization. + // FIXME(#49147): Could be replaced by an array initializer, once those can + // be any const expression. + ($t:ty; $size:expr) => (unsafe { + MaybeUninit::<[MaybeUninit<$t>; $size]>::uninitialized().into_inner() + }); +} + /// Built-in macros to the compiler itself. /// /// These macros do not have any corresponding definition with a `macro_rules!` diff --git a/src/libcore/slice/sort.rs b/src/libcore/slice/sort.rs index 8eb6a4d54c0..2ff67a4934f 100644 --- a/src/libcore/slice/sort.rs +++ b/src/libcore/slice/sort.rs @@ -216,21 +216,14 @@ fn partition_in_blocks(v: &mut [T], pivot: &T, is_less: &mut F) -> usize let mut block_l = BLOCK; let mut start_l = ptr::null_mut(); let mut end_l = ptr::null_mut(); - // Creating a `[MaybeUninit; N]` array by first creating a - // `MaybeUninit<[MaybeUninit; N]>`; the `into_inner` is safe because the inner - // array does not require initialization. - let mut offsets_l: [MaybeUninit; BLOCK] = unsafe { - MaybeUninit::uninitialized().into_inner() - }; + let mut offsets_l: [MaybeUninit; BLOCK] = uninitialized_array![u8; BLOCK]; // The current block on the right side (from `r.sub(block_r)` to `r`). let mut r = unsafe { l.add(v.len()) }; let mut block_r = BLOCK; let mut start_r = ptr::null_mut(); let mut end_r = ptr::null_mut(); - let mut offsets_r: [MaybeUninit; BLOCK] = unsafe { - MaybeUninit::uninitialized().into_inner() - }; + let mut offsets_r: [MaybeUninit; BLOCK] = uninitialized_array![u8; BLOCK]; // FIXME: When we get VLAs, try creating one array of length `min(v.len(), 2 * BLOCK)` rather // than two fixed-size arrays of length `BLOCK`. VLAs might be more cache-efficient. -- cgit 1.4.1-3-g733a5 From 0e8fb93249ee63edba83cd7f2f5f1b09819efa15 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Mon, 28 Jan 2019 10:49:11 +0100 Subject: Use warn() for extra diagnostics; with -D warnings this leads to errors This is needed to properly respect "deny_warnings = false" in config.toml --- src/liballoc/lib.rs | 4 ++-- src/libcore/lib.rs | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'src/liballoc') diff --git a/src/liballoc/lib.rs b/src/liballoc/lib.rs index cf9d89ee05a..d2ff1bae635 100644 --- a/src/liballoc/lib.rs +++ b/src/liballoc/lib.rs @@ -64,8 +64,8 @@ #![needs_allocator] #![warn(deprecated_in_future)] -#![deny(intra_doc_link_resolution_failure)] -#![deny(missing_debug_implementations)] +#![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/libcore/lib.rs b/src/libcore/lib.rs index 1295acb44a5..1ef21832592 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -61,9 +61,9 @@ #![no_core] #![warn(deprecated_in_future)] -#![deny(missing_docs)] -#![deny(intra_doc_link_resolution_failure)] -#![deny(missing_debug_implementations)] +#![warn(missing_docs)] +#![warn(intra_doc_link_resolution_failure)] +#![warn(missing_debug_implementations)] #![feature(allow_internal_unstable)] #![feature(arbitrary_self_types)] -- cgit 1.4.1-3-g733a5 From 6a52ca3fb465544f62cee70bec9499b967939cc9 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Mon, 28 Jan 2019 12:37:29 +0100 Subject: rename first_mut_ptr -> first_ptr_mut --- src/liballoc/collections/btree/node.rs | 10 +++++----- src/libcore/fmt/num.rs | 2 +- src/libcore/mem.rs | 2 +- src/libcore/slice/sort.rs | 8 ++++---- 4 files changed, 11 insertions(+), 11 deletions(-) (limited to 'src/liballoc') diff --git a/src/liballoc/collections/btree/node.rs b/src/liballoc/collections/btree/node.rs index 25810d680fa..e969e119dbe 100644 --- a/src/liballoc/collections/btree/node.rs +++ b/src/liballoc/collections/btree/node.rs @@ -650,7 +650,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef, K, V, Type> { } else { unsafe { slice::from_raw_parts_mut( - MaybeUninit::first_mut_ptr(&mut (*self.as_leaf_mut()).keys), + MaybeUninit::first_ptr_mut(&mut (*self.as_leaf_mut()).keys), self.len() ) } @@ -661,7 +661,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef, K, V, Type> { debug_assert!(!self.is_shared_root()); unsafe { slice::from_raw_parts_mut( - MaybeUninit::first_mut_ptr(&mut (*self.as_leaf_mut()).vals), + MaybeUninit::first_ptr_mut(&mut (*self.as_leaf_mut()).vals), self.len() ) } @@ -749,7 +749,7 @@ impl<'a, K, V> NodeRef, K, V, marker::Internal> { slice_insert(self.vals_mut(), 0, val); slice_insert( slice::from_raw_parts_mut( - MaybeUninit::first_mut_ptr(&mut self.as_internal_mut().edges), + MaybeUninit::first_ptr_mut(&mut self.as_internal_mut().edges), self.len()+1 ), 0, @@ -808,7 +808,7 @@ impl<'a, K, V> NodeRef, K, V, marker::LeafOrInternal> { ForceResult::Internal(mut internal) => { let edge = slice_remove( slice::from_raw_parts_mut( - MaybeUninit::first_mut_ptr(&mut internal.as_internal_mut().edges), + MaybeUninit::first_ptr_mut(&mut internal.as_internal_mut().edges), old_len+1 ), 0 @@ -1087,7 +1087,7 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: slice_insert( slice::from_raw_parts_mut( - MaybeUninit::first_mut_ptr(&mut self.node.as_internal_mut().edges), + MaybeUninit::first_ptr_mut(&mut self.node.as_internal_mut().edges), self.node.len() ), self.idx + 1, diff --git a/src/libcore/fmt/num.rs b/src/libcore/fmt/num.rs index 5283c6d7ef3..3a812337bb1 100644 --- a/src/libcore/fmt/num.rs +++ b/src/libcore/fmt/num.rs @@ -200,7 +200,7 @@ macro_rules! impl_Display { }; let mut buf = uninitialized_array![u8; 39]; let mut curr = buf.len() as isize; - let buf_ptr = MaybeUninit::first_mut_ptr(&mut buf); + let buf_ptr = MaybeUninit::first_ptr_mut(&mut buf); let lut_ptr = DEC_DIGITS_LUT.as_ptr(); unsafe { diff --git a/src/libcore/mem.rs b/src/libcore/mem.rs index fdee86064e6..8b6d9d882b5 100644 --- a/src/libcore/mem.rs +++ b/src/libcore/mem.rs @@ -1159,7 +1159,7 @@ impl MaybeUninit { /// Get a mutable pointer to the first element of the array. #[unstable(feature = "maybe_uninit", issue = "53491")] #[inline(always)] - pub fn first_mut_ptr(this: &mut [MaybeUninit]) -> *mut T { + pub fn first_ptr_mut(this: &mut [MaybeUninit]) -> *mut T { this as *mut [MaybeUninit] as *mut T } } diff --git a/src/libcore/slice/sort.rs b/src/libcore/slice/sort.rs index 2ff67a4934f..3f84faa0499 100644 --- a/src/libcore/slice/sort.rs +++ b/src/libcore/slice/sort.rs @@ -262,8 +262,8 @@ fn partition_in_blocks(v: &mut [T], pivot: &T, is_less: &mut F) -> usize if start_l == end_l { // Trace `block_l` elements from the left side. - start_l = MaybeUninit::first_mut_ptr(&mut offsets_l); - end_l = MaybeUninit::first_mut_ptr(&mut offsets_l); + start_l = MaybeUninit::first_ptr_mut(&mut offsets_l); + end_l = MaybeUninit::first_ptr_mut(&mut offsets_l); let mut elem = l; for i in 0..block_l { @@ -278,8 +278,8 @@ fn partition_in_blocks(v: &mut [T], pivot: &T, is_less: &mut F) -> usize if start_r == end_r { // Trace `block_r` elements from the right side. - start_r = MaybeUninit::first_mut_ptr(&mut offsets_r); - end_r = MaybeUninit::first_mut_ptr(&mut offsets_r); + start_r = MaybeUninit::first_ptr_mut(&mut offsets_r); + end_r = MaybeUninit::first_ptr_mut(&mut offsets_r); let mut elem = r; for i in 0..block_r { -- cgit 1.4.1-3-g733a5