From 98c50ebfa124f311f3c889f3cc885df21aaeb31f Mon Sep 17 00:00:00 2001 From: Tomasz Różański Date: Sat, 27 Jul 2019 17:55:28 +0200 Subject: Change the placement of two functions. Right now, the order is as follows: `pop_front()` `push_front()` `push_back()` `pop_back()` `swap_remove_back()` `swap_remove_front()` I believe it would be more natural, and easier to follow, if we place `pop_back()` right after the `pop_front()`, and `swap_remove_back()` after the `swap_remove_front()` like this: `pop_front()` `pop_back()` `push_front()` `push_back()` `swap_remove_front()` `swap_remove_back()` The rest of the documentation (at least in this module) adheres to the same logic, where the 'front' function always precedes its 'back' equivalent. --- src/liballoc/collections/vec_deque.rs | 86 +++++++++++++++++------------------ 1 file changed, 43 insertions(+), 43 deletions(-) (limited to 'src/liballoc') diff --git a/src/liballoc/collections/vec_deque.rs b/src/liballoc/collections/vec_deque.rs index d149f742b01..b2fe703521e 100644 --- a/src/liballoc/collections/vec_deque.rs +++ b/src/liballoc/collections/vec_deque.rs @@ -1197,6 +1197,31 @@ impl VecDeque { } } + /// Removes the last element from the `VecDeque` and returns it, or `None` if + /// it is empty. + /// + /// # Examples + /// + /// ``` + /// use std::collections::VecDeque; + /// + /// let mut buf = VecDeque::new(); + /// assert_eq!(buf.pop_back(), None); + /// buf.push_back(1); + /// buf.push_back(3); + /// assert_eq!(buf.pop_back(), Some(3)); + /// ``` + #[stable(feature = "rust1", since = "1.0.0")] + pub fn pop_back(&mut self) -> Option { + if self.is_empty() { + None + } else { + self.head = self.wrap_sub(self.head, 1); + let head = self.head; + unsafe { Some(self.buffer_read(head)) } + } + } + /// Prepends an element to the `VecDeque`. /// /// # Examples @@ -1241,38 +1266,13 @@ impl VecDeque { unsafe { self.buffer_write(head, value) } } - /// Removes the last element from the `VecDeque` and returns it, or `None` if - /// it is empty. - /// - /// # Examples - /// - /// ``` - /// use std::collections::VecDeque; - /// - /// let mut buf = VecDeque::new(); - /// assert_eq!(buf.pop_back(), None); - /// buf.push_back(1); - /// buf.push_back(3); - /// assert_eq!(buf.pop_back(), Some(3)); - /// ``` - #[stable(feature = "rust1", since = "1.0.0")] - pub fn pop_back(&mut self) -> Option { - if self.is_empty() { - None - } else { - self.head = self.wrap_sub(self.head, 1); - let head = self.head; - unsafe { Some(self.buffer_read(head)) } - } - } - #[inline] fn is_contiguous(&self) -> bool { self.tail <= self.head } - /// Removes an element from anywhere in the `VecDeque` and returns it, replacing it with the - /// last element. + /// Removes an element from anywhere in the `VecDeque` and returns it, + /// replacing it with the first element. /// /// This does not preserve ordering, but is O(1). /// @@ -1286,28 +1286,28 @@ impl VecDeque { /// use std::collections::VecDeque; /// /// let mut buf = VecDeque::new(); - /// assert_eq!(buf.swap_remove_back(0), None); + /// assert_eq!(buf.swap_remove_front(0), None); /// buf.push_back(1); /// buf.push_back(2); /// buf.push_back(3); /// assert_eq!(buf, [1, 2, 3]); /// - /// assert_eq!(buf.swap_remove_back(0), Some(1)); - /// assert_eq!(buf, [3, 2]); + /// assert_eq!(buf.swap_remove_front(2), Some(3)); + /// assert_eq!(buf, [2, 1]); /// ``` #[stable(feature = "deque_extras_15", since = "1.5.0")] - pub fn swap_remove_back(&mut self, index: usize) -> Option { + pub fn swap_remove_front(&mut self, index: usize) -> Option { let length = self.len(); - if length > 0 && index < length - 1 { - self.swap(index, length - 1); + if length > 0 && index < length && index != 0 { + self.swap(index, 0); } else if index >= length { return None; } - self.pop_back() + self.pop_front() } - /// Removes an element from anywhere in the `VecDeque` and returns it, - /// replacing it with the first element. + /// Removes an element from anywhere in the `VecDeque` and returns it, replacing it with the + /// last element. /// /// This does not preserve ordering, but is O(1). /// @@ -1321,24 +1321,24 @@ impl VecDeque { /// use std::collections::VecDeque; /// /// let mut buf = VecDeque::new(); - /// assert_eq!(buf.swap_remove_front(0), None); + /// assert_eq!(buf.swap_remove_back(0), None); /// buf.push_back(1); /// buf.push_back(2); /// buf.push_back(3); /// assert_eq!(buf, [1, 2, 3]); /// - /// assert_eq!(buf.swap_remove_front(2), Some(3)); - /// assert_eq!(buf, [2, 1]); + /// assert_eq!(buf.swap_remove_back(0), Some(1)); + /// assert_eq!(buf, [3, 2]); /// ``` #[stable(feature = "deque_extras_15", since = "1.5.0")] - pub fn swap_remove_front(&mut self, index: usize) -> Option { + pub fn swap_remove_back(&mut self, index: usize) -> Option { let length = self.len(); - if length > 0 && index < length && index != 0 { - self.swap(index, 0); + if length > 0 && index < length - 1 { + self.swap(index, length - 1); } else if index >= length { return None; } - self.pop_front() + self.pop_back() } /// Inserts an element at `index` within the `VecDeque`, shifting all elements with indices -- cgit 1.4.1-3-g733a5 From 47b16b656376021041110df42e71c551fb2c4881 Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Sun, 4 Aug 2019 21:50:05 +1200 Subject: Remove recommendation about idiomatic syntax for Arc::Clone Signed-off-by: Nick Cameron --- src/liballoc/sync.rs | 4 ---- 1 file changed, 4 deletions(-) (limited to 'src/liballoc') diff --git a/src/liballoc/sync.rs b/src/liballoc/sync.rs index e11873218e8..de8db01ce8b 100644 --- a/src/liballoc/sync.rs +++ b/src/liballoc/sync.rs @@ -106,10 +106,6 @@ const MAX_REFCOUNT: usize = (isize::MAX) as usize; /// // a, b, and foo are all Arcs that point to the same memory location /// ``` /// -/// The [`Arc::clone(&from)`] syntax is the most idiomatic because it conveys more explicitly -/// the meaning of the code. In the example above, this syntax makes it easier to see that -/// this code is creating a new reference rather than copying the whole content of foo. -/// /// ## `Deref` behavior /// /// `Arc` automatically dereferences to `T` (via the [`Deref`][deref] trait), -- cgit 1.4.1-3-g733a5 From fa7a40cc6ee7e16c948a804dbb487d36b3d0ccc9 Mon Sep 17 00:00:00 2001 From: Observer42 Date: Mon, 12 Aug 2019 16:07:13 +0800 Subject: Document From trait for BinaryHeap --- src/liballoc/collections/binary_heap.rs | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/liballoc') diff --git a/src/liballoc/collections/binary_heap.rs b/src/liballoc/collections/binary_heap.rs index 9f531f5b83c..cc6c69d5738 100644 --- a/src/liballoc/collections/binary_heap.rs +++ b/src/liballoc/collections/binary_heap.rs @@ -1163,6 +1163,9 @@ impl FusedIterator for Drain<'_, T> {} #[stable(feature = "binary_heap_extras_15", since = "1.5.0")] impl From> for BinaryHeap { + /// Converts a `Vec` into a `BinaryHeap`. + /// + /// This conversion happens in-place, and has O(n) time complexity. fn from(vec: Vec) -> BinaryHeap { let mut heap = BinaryHeap { data: vec }; heap.rebuild(); -- cgit 1.4.1-3-g733a5 From e30480ca864c5810c89bbf1f475eb0bc2999c251 Mon Sep 17 00:00:00 2001 From: Observer42 Date: Mon, 12 Aug 2019 18:07:14 +0800 Subject: Apply suggestions from code review Co-Authored-By: Mazdak Farrokhzad --- src/liballoc/collections/binary_heap.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/liballoc') diff --git a/src/liballoc/collections/binary_heap.rs b/src/liballoc/collections/binary_heap.rs index cc6c69d5738..3d04f30e7bd 100644 --- a/src/liballoc/collections/binary_heap.rs +++ b/src/liballoc/collections/binary_heap.rs @@ -1163,9 +1163,9 @@ impl FusedIterator for Drain<'_, T> {} #[stable(feature = "binary_heap_extras_15", since = "1.5.0")] impl From> for BinaryHeap { - /// Converts a `Vec` into a `BinaryHeap`. + /// Converts a `Vec` into a `BinaryHeap`. /// - /// This conversion happens in-place, and has O(n) time complexity. + /// This conversion happens in-place, and has `O(n)` time complexity. fn from(vec: Vec) -> BinaryHeap { let mut heap = BinaryHeap { data: vec }; heap.rebuild(); -- cgit 1.4.1-3-g733a5 From 2601c864878412814134f417b7a738e5ee8898f2 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Sun, 11 Aug 2019 12:55:14 -0400 Subject: Handle cfg(bootstrap) throughout --- src/bootstrap/bin/rustc.rs | 5 +--- src/bootstrap/builder.rs | 2 +- src/liballoc/collections/btree/node.rs | 6 ++--- src/liballoc/lib.rs | 6 ++--- src/libcore/any.rs | 5 ---- src/libcore/char/methods.rs | 14 ++-------- src/libcore/clone.rs | 1 - src/libcore/cmp.rs | 4 --- src/libcore/default.rs | 1 - src/libcore/fmt/mod.rs | 2 -- src/libcore/hash/mod.rs | 2 -- src/libcore/lib.rs | 4 +-- src/libcore/macros.rs | 35 ------------------------- src/libcore/marker.rs | 1 - src/libcore/mem/maybe_uninit.rs | 2 +- src/libcore/mem/mod.rs | 4 +-- src/libcore/prelude/v1.rs | 4 --- src/librustc/lint/context.rs | 4 +-- src/librustc/lint/internal.rs | 2 +- src/librustc/ty/codec.rs | 4 +-- src/librustc/ty/context.rs | 6 ++--- src/librustc/ty/flags.rs | 4 +-- src/librustc/ty/mod.rs | 2 +- src/librustc/ty/sty.rs | 2 +- src/librustc_data_structures/lib.rs | 2 +- src/librustc_macros/src/lib.rs | 2 +- src/librustc_target/abi/mod.rs | 19 +++++--------- src/librustc_typeck/check/mod.rs | 4 +-- src/libstd/lib.rs | 5 ++-- src/libstd/os/raw/mod.rs | 48 ++++++++++++---------------------- src/libstd/prelude/v1.rs | 18 ------------- src/libstd/sync/mod.rs | 1 - src/libstd/sys/cloudabi/mod.rs | 2 +- src/libunwind/build.rs | 8 +++--- src/libunwind/libunwind.rs | 10 +++---- src/test/ui/lint/lint-qualification.rs | 2 +- 36 files changed, 64 insertions(+), 179 deletions(-) (limited to 'src/liballoc') diff --git a/src/bootstrap/bin/rustc.rs b/src/bootstrap/bin/rustc.rs index 54b689fb062..04a3dea5c87 100644 --- a/src/bootstrap/bin/rustc.rs +++ b/src/bootstrap/bin/rustc.rs @@ -132,10 +132,7 @@ fn main() { cmd.arg("-Dwarnings"); cmd.arg("-Drust_2018_idioms"); cmd.arg("-Dunused_lifetimes"); - // cfg(not(bootstrap)): Remove this during the next stage 0 compiler update. - // `-Drustc::internal` is a new feature and `rustc_version` mis-reports the `stage`. - let cfg_not_bootstrap = stage != "0" && crate_name != Some("rustc_version"); - if cfg_not_bootstrap && use_internal_lints(crate_name) { + if use_internal_lints(crate_name) { cmd.arg("-Zunstable-options"); cmd.arg("-Drustc::internal"); } diff --git a/src/bootstrap/builder.rs b/src/bootstrap/builder.rs index e54c9360bae..9e4cd5ebca7 100644 --- a/src/bootstrap/builder.rs +++ b/src/bootstrap/builder.rs @@ -145,7 +145,7 @@ impl StepDescription { only_hosts: S::ONLY_HOSTS, should_run: S::should_run, make_run: S::make_run, - name: unsafe { ::std::intrinsics::type_name::() }, + name: std::any::type_name::(), } } diff --git a/src/liballoc/collections/btree/node.rs b/src/liballoc/collections/btree/node.rs index e067096f0c7..0b5a271dbea 100644 --- a/src/liballoc/collections/btree/node.rs +++ b/src/liballoc/collections/btree/node.rs @@ -106,8 +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. - keys: uninit_array![_; CAPACITY], - vals: uninit_array![_; CAPACITY], + keys: [MaybeUninit::UNINIT; CAPACITY], + vals: [MaybeUninit::UNINIT; CAPACITY], parent: ptr::null(), parent_idx: MaybeUninit::uninit(), len: 0 @@ -159,7 +159,7 @@ impl InternalNode { unsafe fn new() -> Self { InternalNode { data: LeafNode::new(), - edges: uninit_array![_; 2*B], + edges: [MaybeUninit::UNINIT; 2*B] } } } diff --git a/src/liballoc/lib.rs b/src/liballoc/lib.rs index a1936b36ac6..7421beddd95 100644 --- a/src/liballoc/lib.rs +++ b/src/liballoc/lib.rs @@ -69,7 +69,7 @@ #![warn(missing_debug_implementations)] #![deny(intra_doc_link_resolution_failure)] // rustdoc is run without -D warnings #![allow(explicit_outlives_requirements)] -#![cfg_attr(not(bootstrap), allow(incomplete_features))] +#![allow(incomplete_features)] #![cfg_attr(not(test), feature(generator_trait))] #![cfg_attr(test, feature(test))] @@ -84,7 +84,7 @@ #![feature(coerce_unsized)] #![feature(const_generic_impls_guard)] #![feature(const_generics)] -#![cfg_attr(not(bootstrap), feature(const_in_array_repeat_expressions))] +#![feature(const_in_array_repeat_expressions)] #![feature(dispatch_from_dyn)] #![feature(core_intrinsics)] #![feature(dropck_eyepatch)] @@ -118,7 +118,7 @@ #![feature(rustc_const_unstable)] #![feature(const_vec_new)] #![feature(slice_partition_dedup)] -#![feature(maybe_uninit_extra, maybe_uninit_slice, maybe_uninit_array)] +#![feature(maybe_uninit_extra, maybe_uninit_slice)] #![feature(alloc_layout_extra)] #![feature(try_trait)] #![feature(mem_take)] diff --git a/src/libcore/any.rs b/src/libcore/any.rs index 078091a9b54..e8a0a88f12a 100644 --- a/src/libcore/any.rs +++ b/src/libcore/any.rs @@ -470,10 +470,5 @@ impl TypeId { #[stable(feature = "type_name", since = "1.38.0")] #[rustc_const_unstable(feature = "const_type_name")] pub const fn type_name() -> &'static str { - #[cfg(bootstrap)] - unsafe { - intrinsics::type_name::() - } - #[cfg(not(bootstrap))] intrinsics::type_name::() } diff --git a/src/libcore/char/methods.rs b/src/libcore/char/methods.rs index aa834db2b9b..e91bf53c5b4 100644 --- a/src/libcore/char/methods.rs +++ b/src/libcore/char/methods.rs @@ -553,12 +553,7 @@ impl char { /// `XID_Start` is a Unicode Derived Property specified in /// [UAX #31](http://unicode.org/reports/tr31/#NFKC_Modifications), /// mostly similar to `ID_Start` but modified for closure under `NFKx`. - #[cfg_attr(bootstrap, - unstable(feature = "rustc_private", - reason = "mainly needed for compiler internals", - issue = "27812"))] - #[cfg_attr(not(bootstrap), - unstable(feature = "unicode_internals", issue = "0"))] + #[unstable(feature = "unicode_internals", issue = "0")] pub fn is_xid_start(self) -> bool { derived_property::XID_Start(self) } @@ -569,12 +564,7 @@ impl char { /// `XID_Continue` is a Unicode Derived Property specified in /// [UAX #31](http://unicode.org/reports/tr31/#NFKC_Modifications), /// mostly similar to `ID_Continue` but modified for closure under NFKx. - #[cfg_attr(bootstrap, - unstable(feature = "rustc_private", - reason = "mainly needed for compiler internals", - issue = "27812"))] - #[cfg_attr(not(bootstrap), - unstable(feature = "unicode_internals", issue = "0"))] + #[unstable(feature = "unicode_internals", issue = "0")] #[inline] pub fn is_xid_continue(self) -> bool { derived_property::XID_Continue(self) diff --git a/src/libcore/clone.rs b/src/libcore/clone.rs index 0c99356390b..ec22a7ccfe4 100644 --- a/src/libcore/clone.rs +++ b/src/libcore/clone.rs @@ -134,7 +134,6 @@ pub trait Clone : Sized { } /// Derive macro generating an impl of the trait `Clone`. -#[cfg(not(bootstrap))] #[rustc_builtin_macro] #[rustc_macro_transparency = "semitransparent"] #[stable(feature = "builtin_macro_prelude", since = "1.38.0")] diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index 38a52d97da2..b802216036b 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -201,7 +201,6 @@ pub trait PartialEq { } /// Derive macro generating an impl of the trait `PartialEq`. -#[cfg(not(bootstrap))] #[rustc_builtin_macro] #[rustc_macro_transparency = "semitransparent"] #[stable(feature = "builtin_macro_prelude", since = "1.38.0")] @@ -265,7 +264,6 @@ pub trait Eq: PartialEq { } /// Derive macro generating an impl of the trait `Eq`. -#[cfg(not(bootstrap))] #[rustc_builtin_macro] #[rustc_macro_transparency = "semitransparent"] #[stable(feature = "builtin_macro_prelude", since = "1.38.0")] @@ -617,7 +615,6 @@ pub trait Ord: Eq + PartialOrd { } /// Derive macro generating an impl of the trait `Ord`. -#[cfg(not(bootstrap))] #[rustc_builtin_macro] #[rustc_macro_transparency = "semitransparent"] #[stable(feature = "builtin_macro_prelude", since = "1.38.0")] @@ -867,7 +864,6 @@ pub trait PartialOrd: PartialEq { } /// Derive macro generating an impl of the trait `PartialOrd`. -#[cfg(not(bootstrap))] #[rustc_builtin_macro] #[rustc_macro_transparency = "semitransparent"] #[stable(feature = "builtin_macro_prelude", since = "1.38.0")] diff --git a/src/libcore/default.rs b/src/libcore/default.rs index 8d95e9de158..9b1616ccce4 100644 --- a/src/libcore/default.rs +++ b/src/libcore/default.rs @@ -116,7 +116,6 @@ pub trait Default: Sized { } /// Derive macro generating an impl of the trait `Default`. -#[cfg(not(bootstrap))] #[rustc_builtin_macro] #[rustc_macro_transparency = "semitransparent"] #[stable(feature = "builtin_macro_prelude", since = "1.38.0")] diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs index 0ea01d4b84a..d5fae9e7401 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -546,7 +546,6 @@ pub trait Debug { } // Separate module to reexport the macro `Debug` from prelude without the trait `Debug`. -#[cfg(not(bootstrap))] pub(crate) mod macros { /// Derive macro generating an impl of the trait `Debug`. #[rustc_builtin_macro] @@ -555,7 +554,6 @@ pub(crate) mod macros { #[allow_internal_unstable(core_intrinsics)] pub macro Debug($item:item) { /* compiler built-in */ } } -#[cfg(not(bootstrap))] #[stable(feature = "builtin_macro_prelude", since = "1.38.0")] #[doc(inline)] pub use macros::Debug; diff --git a/src/libcore/hash/mod.rs b/src/libcore/hash/mod.rs index c4cbf40a93a..40b827b8787 100644 --- a/src/libcore/hash/mod.rs +++ b/src/libcore/hash/mod.rs @@ -199,7 +199,6 @@ pub trait Hash { } // Separate module to reexport the macro `Hash` from prelude without the trait `Hash`. -#[cfg(not(bootstrap))] pub(crate) mod macros { /// Derive macro generating an impl of the trait `Hash`. #[rustc_builtin_macro] @@ -208,7 +207,6 @@ pub(crate) mod macros { #[allow_internal_unstable(core_intrinsics)] pub macro Hash($item:item) { /* compiler built-in */ } } -#[cfg(not(bootstrap))] #[stable(feature = "builtin_macro_prelude", since = "1.38.0")] #[doc(inline)] pub use macros::Hash; diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index 678ff768792..c168d5c8a2e 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -63,7 +63,7 @@ #![warn(missing_debug_implementations)] #![deny(intra_doc_link_resolution_failure)] // rustdoc is run without -D warnings #![allow(explicit_outlives_requirements)] -#![cfg_attr(not(bootstrap), allow(incomplete_features))] +#![allow(incomplete_features)] #![feature(allow_internal_unstable)] #![feature(arbitrary_self_types)] @@ -129,7 +129,7 @@ #![feature(structural_match)] #![feature(abi_unadjusted)] #![feature(adx_target_feature)] -#![feature(maybe_uninit_slice, maybe_uninit_array)] +#![feature(maybe_uninit_slice)] #![feature(external_doc)] #![feature(mem_take)] #![feature(associated_type_bounds)] diff --git a/src/libcore/macros.rs b/src/libcore/macros.rs index f9dc53874ac..bbed9516716 100644 --- a/src/libcore/macros.rs +++ b/src/libcore/macros.rs @@ -635,46 +635,11 @@ macro_rules! todo { ($($arg:tt)+) => (panic!("not yet implemented: {}", format_args!($($arg)+))); } -/// Creates an array of [`MaybeUninit`]. -/// -/// This macro constructs an uninitialized array of the type `[MaybeUninit; N]`. -/// It exists solely because bootstrap does not yet support const array-init expressions. -/// -/// [`MaybeUninit`]: mem/union.MaybeUninit.html -// FIXME: Remove both versions of this macro once bootstrap is 1.38. -#[macro_export] -#[unstable(feature = "maybe_uninit_array", issue = "53491")] -#[cfg(bootstrap)] -macro_rules! uninit_array { - // This `assume_init` is safe because an array of `MaybeUninit` does not - // require initialization. - ($t:ty; $size:expr) => (unsafe { - MaybeUninit::<[MaybeUninit<$t>; $size]>::uninit().assume_init() - }); -} - -/// Creates an array of [`MaybeUninit`]. -/// -/// This macro constructs an uninitialized array of the type `[MaybeUninit; N]`. -/// It exists solely because bootstrap does not yet support const array-init expressions. -/// -/// [`MaybeUninit`]: mem/union.MaybeUninit.html -// FIXME: Just inline this version of the macro once bootstrap is 1.38. -#[macro_export] -#[unstable(feature = "maybe_uninit_array", issue = "53491")] -#[cfg(not(bootstrap))] -macro_rules! uninit_array { - ($t:ty; $size:expr) => ( - [MaybeUninit::<$t>::UNINIT; $size] - ); -} - /// Definitions of built-in macros. /// /// Most of the macro properties (stability, visibility, etc.) are taken from the source code here, /// with exception of expansion functions transforming macro inputs into outputs, /// those functions are provided by the compiler. -#[cfg(not(bootstrap))] pub(crate) mod builtin { /// Causes compilation to fail with the given error message when encountered. diff --git a/src/libcore/marker.rs b/src/libcore/marker.rs index 78a27361165..3befd421b01 100644 --- a/src/libcore/marker.rs +++ b/src/libcore/marker.rs @@ -289,7 +289,6 @@ pub trait Copy : Clone { } /// Derive macro generating an impl of the trait `Copy`. -#[cfg(not(bootstrap))] #[rustc_builtin_macro] #[rustc_macro_transparency = "semitransparent"] #[stable(feature = "builtin_macro_prelude", since = "1.38.0")] diff --git a/src/libcore/mem/maybe_uninit.rs b/src/libcore/mem/maybe_uninit.rs index 1bbea02e0c7..49711607262 100644 --- a/src/libcore/mem/maybe_uninit.rs +++ b/src/libcore/mem/maybe_uninit.rs @@ -213,7 +213,7 @@ use crate::mem::ManuallyDrop; #[allow(missing_debug_implementations)] #[stable(feature = "maybe_uninit", since = "1.36.0")] // Lang item so we can wrap other types in it. This is useful for generators. -#[cfg_attr(not(bootstrap), lang = "maybe_uninit")] +#[lang = "maybe_uninit"] #[derive(Copy)] #[repr(transparent)] pub union MaybeUninit { diff --git a/src/libcore/mem/mod.rs b/src/libcore/mem/mod.rs index 2534400b833..87ec05a243d 100644 --- a/src/libcore/mem/mod.rs +++ b/src/libcore/mem/mod.rs @@ -453,7 +453,7 @@ pub const fn needs_drop() -> bool { /// ``` #[inline] #[stable(feature = "rust1", since = "1.0.0")] -#[cfg_attr(bootstrap, allow(deprecated_in_future))] +#[allow(deprecated_in_future)] #[allow(deprecated)] pub unsafe fn zeroed() -> T { intrinsics::panic_if_uninhabited::(); @@ -481,7 +481,7 @@ pub unsafe fn zeroed() -> T { #[inline] #[rustc_deprecated(since = "1.39.0", reason = "use `mem::MaybeUninit` instead")] #[stable(feature = "rust1", since = "1.0.0")] -#[cfg_attr(bootstrap, allow(deprecated_in_future))] +#[allow(deprecated_in_future)] #[allow(deprecated)] pub unsafe fn uninitialized() -> T { intrinsics::panic_if_uninhabited::(); diff --git a/src/libcore/prelude/v1.rs b/src/libcore/prelude/v1.rs index 84cf85f339c..76240379040 100644 --- a/src/libcore/prelude/v1.rs +++ b/src/libcore/prelude/v1.rs @@ -46,16 +46,13 @@ pub use crate::option::Option::{self, Some, None}; pub use crate::result::Result::{self, Ok, Err}; // Re-exported built-in macros -#[cfg(not(bootstrap))] #[stable(feature = "builtin_macro_prelude", since = "1.38.0")] #[doc(no_inline)] pub use crate::fmt::macros::Debug; -#[cfg(not(bootstrap))] #[stable(feature = "builtin_macro_prelude", since = "1.38.0")] #[doc(no_inline)] pub use crate::hash::macros::Hash; -#[cfg(not(bootstrap))] #[stable(feature = "builtin_macro_prelude", since = "1.38.0")] #[doc(no_inline)] pub use crate::{ @@ -83,7 +80,6 @@ pub use crate::{ trace_macros, }; -#[cfg(not(bootstrap))] #[stable(feature = "builtin_macro_prelude", since = "1.38.0")] #[allow(deprecated)] #[doc(no_inline)] diff --git a/src/librustc/lint/context.rs b/src/librustc/lint/context.rs index de812410e8b..3584a128290 100644 --- a/src/librustc/lint/context.rs +++ b/src/librustc/lint/context.rs @@ -1355,7 +1355,7 @@ struct LateLintPassObjects<'a> { lints: &'a mut [LateLintPassObject], } -#[cfg_attr(not(bootstrap), allow(rustc::lint_pass_impl_without_macro))] +#[allow(rustc::lint_pass_impl_without_macro)] impl LintPass for LateLintPassObjects<'_> { fn name(&self) -> &'static str { panic!() @@ -1525,7 +1525,7 @@ struct EarlyLintPassObjects<'a> { lints: &'a mut [EarlyLintPassObject], } -#[cfg_attr(not(bootstrap), allow(rustc::lint_pass_impl_without_macro))] +#[allow(rustc::lint_pass_impl_without_macro)] impl LintPass for EarlyLintPassObjects<'_> { fn name(&self) -> &'static str { panic!() diff --git a/src/librustc/lint/internal.rs b/src/librustc/lint/internal.rs index 0b514f5927d..dea1cc6601b 100644 --- a/src/librustc/lint/internal.rs +++ b/src/librustc/lint/internal.rs @@ -23,7 +23,7 @@ pub struct DefaultHashTypes { impl DefaultHashTypes { // we are allowed to use `HashMap` and `HashSet` as identifiers for implementing the lint itself - #[cfg_attr(not(bootstrap), allow(rustc::default_hash_types))] + #[allow(rustc::default_hash_types)] pub fn new() -> Self { let mut map = FxHashMap::default(); map.insert(sym::HashMap, sym::FxHashMap); diff --git a/src/librustc/ty/codec.rs b/src/librustc/ty/codec.rs index e3c6eca02d5..1ddc6780aca 100644 --- a/src/librustc/ty/codec.rs +++ b/src/librustc/ty/codec.rs @@ -27,7 +27,7 @@ pub trait EncodableWithShorthand: Clone + Eq + Hash { fn variant(&self) -> &Self::Variant; } -#[cfg_attr(not(bootstrap), allow(rustc::usage_of_ty_tykind))] +#[allow(rustc::usage_of_ty_tykind)] impl<'tcx> EncodableWithShorthand for Ty<'tcx> { type Variant = ty::TyKind<'tcx>; fn variant(&self) -> &Self::Variant { @@ -160,7 +160,7 @@ where Ok(decoder.map_encoded_cnum_to_current(cnum)) } -#[cfg_attr(not(bootstrap), allow(rustc::usage_of_ty_tykind))] +#[allow(rustc::usage_of_ty_tykind)] #[inline] pub fn decode_ty(decoder: &mut D) -> Result, D::Error> where diff --git a/src/librustc/ty/context.rs b/src/librustc/ty/context.rs index ef74d9e5b28..d504ba4dfe0 100644 --- a/src/librustc/ty/context.rs +++ b/src/librustc/ty/context.rs @@ -130,7 +130,7 @@ impl<'tcx> CtxtInterners<'tcx> { } /// Intern a type - #[cfg_attr(not(bootstrap), allow(rustc::usage_of_ty_tykind))] + #[allow(rustc::usage_of_ty_tykind)] #[inline(never)] fn intern_ty(&self, st: TyKind<'tcx> @@ -2076,7 +2076,7 @@ impl<'tcx> Hash for Interned<'tcx, TyS<'tcx>> { } } -#[cfg_attr(not(bootstrap), allow(rustc::usage_of_ty_tykind))] +#[allow(rustc::usage_of_ty_tykind)] impl<'tcx> Borrow> for Interned<'tcx, TyS<'tcx>> { fn borrow<'a>(&'a self) -> &'a TyKind<'tcx> { &self.0.sty @@ -2291,7 +2291,7 @@ impl<'tcx> TyCtxt<'tcx> { self.mk_fn_ptr(converted_sig) } - #[cfg_attr(not(bootstrap), allow(rustc::usage_of_ty_tykind))] + #[allow(rustc::usage_of_ty_tykind)] #[inline] pub fn mk_ty(&self, st: TyKind<'tcx>) -> Ty<'tcx> { self.interners.intern_ty(st) diff --git a/src/librustc/ty/flags.rs b/src/librustc/ty/flags.rs index 411b18e043a..9119505acd1 100644 --- a/src/librustc/ty/flags.rs +++ b/src/librustc/ty/flags.rs @@ -18,7 +18,7 @@ impl FlagComputation { } } - #[cfg_attr(not(bootstrap), allow(rustc::usage_of_ty_tykind))] + #[allow(rustc::usage_of_ty_tykind)] pub fn for_sty(st: &ty::TyKind<'_>) -> FlagComputation { let mut result = FlagComputation::new(); result.add_sty(st); @@ -62,7 +62,7 @@ impl FlagComputation { } // otherwise, this binder captures nothing } - #[cfg_attr(not(bootstrap), allow(rustc::usage_of_ty_tykind))] + #[allow(rustc::usage_of_ty_tykind)] fn add_sty(&mut self, st: &ty::TyKind<'_>) { match st { &ty::Bool | diff --git a/src/librustc/ty/mod.rs b/src/librustc/ty/mod.rs index 9d563e290de..a8ff36a3946 100644 --- a/src/librustc/ty/mod.rs +++ b/src/librustc/ty/mod.rs @@ -483,7 +483,7 @@ bitflags! { } } -#[cfg_attr(not(bootstrap), allow(rustc::usage_of_ty_tykind))] +#[allow(rustc::usage_of_ty_tykind)] pub struct TyS<'tcx> { pub sty: TyKind<'tcx>, pub flags: TypeFlags, diff --git a/src/librustc/ty/sty.rs b/src/librustc/ty/sty.rs index 129ea9b5b67..42e367b8518 100644 --- a/src/librustc/ty/sty.rs +++ b/src/librustc/ty/sty.rs @@ -1,6 +1,6 @@ //! This module contains `TyKind` and its major components. -#![cfg_attr(not(bootstrap), allow(rustc::usage_of_ty_tykind))] +#![allow(rustc::usage_of_ty_tykind)] use crate::hir; use crate::hir::def_id::DefId; diff --git a/src/librustc_data_structures/lib.rs b/src/librustc_data_structures/lib.rs index 9f103437d36..f7593501959 100644 --- a/src/librustc_data_structures/lib.rs +++ b/src/librustc_data_structures/lib.rs @@ -27,7 +27,7 @@ #![cfg_attr(unix, feature(libc))] -#![cfg_attr(not(bootstrap), allow(rustc::default_hash_types))] +#![allow(rustc::default_hash_types)] #[macro_use] extern crate log; diff --git a/src/librustc_macros/src/lib.rs b/src/librustc_macros/src/lib.rs index 85e2247ebd7..3d3a020ef0c 100644 --- a/src/librustc_macros/src/lib.rs +++ b/src/librustc_macros/src/lib.rs @@ -1,5 +1,5 @@ #![feature(proc_macro_hygiene)] -#![cfg_attr(not(bootstrap), allow(rustc::default_hash_types))] +#![allow(rustc::default_hash_types)] #![recursion_limit="128"] diff --git a/src/librustc_target/abi/mod.rs b/src/librustc_target/abi/mod.rs index dd7ae742a63..dafa8661176 100644 --- a/src/librustc_target/abi/mod.rs +++ b/src/librustc_target/abi/mod.rs @@ -114,26 +114,20 @@ impl TargetDataLayout { [p] if p.starts_with("P") => { dl.instruction_address_space = parse_address_space(&p[1..], "P")? } - // FIXME: Ping cfg(bootstrap) -- Use `ref a @ ..` with new bootstrap compiler. - ["a", ..] => { - let a = &spec_parts[1..]; // FIXME inline into pattern. + ["a", ref a @ ..] => { dl.aggregate_align = align(a, "a")? } - ["f32", ..] => { - let a = &spec_parts[1..]; // FIXME inline into pattern. + ["f32", ref a @ ..] => { dl.f32_align = align(a, "f32")? } - ["f64", ..] => { - let a = &spec_parts[1..]; // FIXME inline into pattern. + ["f64", ref a @ ..] => { dl.f64_align = align(a, "f64")? } - [p @ "p", s, ..] | [p @ "p0", s, ..] => { - let a = &spec_parts[2..]; // FIXME inline into pattern. + [p @ "p", s, ref a @ ..] | [p @ "p0", s, ref a @ ..] => { dl.pointer_size = size(s, p)?; dl.pointer_align = align(a, p)?; } - [s, ..] if s.starts_with("i") => { - let a = &spec_parts[1..]; // FIXME inline into pattern. + [s, ref a @ ..] if s.starts_with("i") => { let bits = match s[1..].parse::() { Ok(bits) => bits, Err(_) => { @@ -157,8 +151,7 @@ impl TargetDataLayout { dl.i128_align = a; } } - [s, ..] if s.starts_with("v") => { - let a = &spec_parts[1..]; // FIXME inline into pattern. + [s, ref a @ ..] if s.starts_with("v") => { let v_size = size(&s[1..], "v")?; let a = align(a, s)?; if let Some(v) = dl.vector_align.iter_mut().find(|v| v.0 == v_size) { diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 4fb28db6e94..daad2f28fda 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -1834,9 +1834,7 @@ fn bad_variant_count<'tcx>(tcx: TyCtxt<'tcx>, adt: &'tcx ty::AdtDef, sp: Span, d ); let mut err = struct_span_err!(tcx.sess, sp, E0731, "transparent enum {}", msg); err.span_label(sp, &msg); - if let &[.., ref end] = &variant_spans[..] { - // FIXME: Ping cfg(bootstrap) -- Use `ref start @ ..` with new bootstrap compiler. - let start = &variant_spans[..variant_spans.len() - 1]; + if let &[ref start @ .., ref end] = &variant_spans[..] { for variant_span in start { err.span_label(*variant_span, ""); } diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index ba80d1b7004..1f48315d3f8 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -228,7 +228,7 @@ // std is implemented with unstable features, many of which are internal // compiler details that will never be stable // NB: the following list is sorted to minimize merge conflicts. -#![cfg_attr(not(bootstrap), feature(__rust_unstable_column))] +#![feature(__rust_unstable_column)] #![feature(alloc_error_handler)] #![feature(alloc_layout_extra)] #![feature(allocator_api)] @@ -513,7 +513,7 @@ pub use std_detect::detect; // Re-export macros defined in libcore. #[stable(feature = "rust1", since = "1.0.0")] -#[allow(deprecated_in_future)] +#[allow(deprecated, deprecated_in_future)] pub use core::{ // Stable assert_eq, @@ -531,7 +531,6 @@ pub use core::{ }; // Re-export built-in macros defined through libcore. -#[cfg(not(bootstrap))] #[stable(feature = "builtin_macro_prelude", since = "1.38.0")] pub use core::{ // Stable diff --git a/src/libstd/os/raw/mod.rs b/src/libstd/os/raw/mod.rs index 0761c50f4b2..611a1709c8d 100644 --- a/src/libstd/os/raw/mod.rs +++ b/src/libstd/os/raw/mod.rs @@ -8,8 +8,7 @@ #![stable(feature = "raw_os", since = "1.1.0")] -#[cfg_attr(bootstrap, doc(include = "os/raw/char.md"))] -#[cfg_attr(not(bootstrap), doc(include = "char.md"))] +#[doc(include = "char.md")] #[cfg(any(all(target_os = "linux", any(target_arch = "aarch64", target_arch = "arm", target_arch = "hexagon", @@ -33,8 +32,7 @@ target_arch = "powerpc")), all(target_os = "fuchsia", target_arch = "aarch64")))] #[stable(feature = "raw_os", since = "1.1.0")] pub type c_char = u8; -#[cfg_attr(bootstrap, doc(include = "os/raw/char.md"))] -#[cfg_attr(not(bootstrap), doc(include = "char.md"))] +#[doc(include = "char.md")] #[cfg(not(any(all(target_os = "linux", any(target_arch = "aarch64", target_arch = "arm", target_arch = "hexagon", @@ -58,51 +56,37 @@ target_arch = "powerpc")), all(target_os = "fuchsia", target_arch = "aarch64"))))] #[stable(feature = "raw_os", since = "1.1.0")] pub type c_char = i8; -#[cfg_attr(bootstrap, doc(include = "os/raw/schar.md"))] -#[cfg_attr(not(bootstrap), doc(include = "schar.md"))] +#[doc(include = "schar.md")] #[stable(feature = "raw_os", since = "1.1.0")] pub type c_schar = i8; -#[cfg_attr(bootstrap, doc(include = "os/raw/uchar.md"))] -#[cfg_attr(not(bootstrap), doc(include = "uchar.md"))] +#[doc(include = "uchar.md")] #[stable(feature = "raw_os", since = "1.1.0")] pub type c_uchar = u8; -#[cfg_attr(bootstrap, doc(include = "os/raw/short.md"))] -#[cfg_attr(not(bootstrap), doc(include = "short.md"))] +#[doc(include = "short.md")] #[stable(feature = "raw_os", since = "1.1.0")] pub type c_short = i16; -#[cfg_attr(bootstrap, doc(include = "os/raw/ushort.md"))] -#[cfg_attr(not(bootstrap), doc(include = "ushort.md"))] +#[doc(include = "ushort.md")] #[stable(feature = "raw_os", since = "1.1.0")] pub type c_ushort = u16; -#[cfg_attr(bootstrap, doc(include = "os/raw/int.md"))] -#[cfg_attr(not(bootstrap), doc(include = "int.md"))] +#[doc(include = "int.md")] #[stable(feature = "raw_os", since = "1.1.0")] pub type c_int = i32; -#[cfg_attr(bootstrap, doc(include = "os/raw/uint.md"))] -#[cfg_attr(not(bootstrap), doc(include = "uint.md"))] +#[doc(include = "uint.md")] #[stable(feature = "raw_os", since = "1.1.0")] pub type c_uint = u32; -#[cfg_attr(bootstrap, doc(include = "os/raw/long.md"))] -#[cfg_attr(not(bootstrap), doc(include = "long.md"))] +#[doc(include = "long.md")] #[cfg(any(target_pointer_width = "32", windows))] #[stable(feature = "raw_os", since = "1.1.0")] pub type c_long = i32; -#[cfg_attr(bootstrap, doc(include = "os/raw/ulong.md"))] -#[cfg_attr(not(bootstrap), doc(include = "ulong.md"))] +#[doc(include = "ulong.md")] #[cfg(any(target_pointer_width = "32", windows))] #[stable(feature = "raw_os", since = "1.1.0")] pub type c_ulong = u32; -#[cfg_attr(bootstrap, doc(include = "os/raw/long.md"))] -#[cfg_attr(not(bootstrap), doc(include = "long.md"))] +#[doc(include = "long.md")] #[cfg(all(target_pointer_width = "64", not(windows)))] #[stable(feature = "raw_os", since = "1.1.0")] pub type c_long = i64; -#[cfg_attr(bootstrap, doc(include = "os/raw/ulong.md"))] -#[cfg_attr(not(bootstrap), doc(include = "ulong.md"))] +#[doc(include = "ulong.md")] #[cfg(all(target_pointer_width = "64", not(windows)))] #[stable(feature = "raw_os", since = "1.1.0")] pub type c_ulong = u64; -#[cfg_attr(bootstrap, doc(include = "os/raw/longlong.md"))] -#[cfg_attr(not(bootstrap), doc(include = "longlong.md"))] +#[doc(include = "longlong.md")] #[stable(feature = "raw_os", since = "1.1.0")] pub type c_longlong = i64; -#[cfg_attr(bootstrap, doc(include = "os/raw/ulonglong.md"))] -#[cfg_attr(not(bootstrap), doc(include = "ulonglong.md"))] +#[doc(include = "ulonglong.md")] #[stable(feature = "raw_os", since = "1.1.0")] pub type c_ulonglong = u64; -#[cfg_attr(bootstrap, doc(include = "os/raw/float.md"))] -#[cfg_attr(not(bootstrap), doc(include = "float.md"))] +#[doc(include = "float.md")] #[stable(feature = "raw_os", since = "1.1.0")] pub type c_float = f32; -#[cfg_attr(bootstrap, doc(include = "os/raw/double.md"))] -#[cfg_attr(not(bootstrap), doc(include = "double.md"))] +#[doc(include = "double.md")] #[stable(feature = "raw_os", since = "1.1.0")] pub type c_double = f64; #[stable(feature = "raw_os", since = "1.1.0")] diff --git a/src/libstd/prelude/v1.rs b/src/libstd/prelude/v1.rs index 1c61f21f7df..752c6202ee4 100644 --- a/src/libstd/prelude/v1.rs +++ b/src/libstd/prelude/v1.rs @@ -7,10 +7,6 @@ #![stable(feature = "rust1", since = "1.0.0")] // Re-exported core operators -#[cfg(bootstrap)] -#[stable(feature = "rust1", since = "1.0.0")] -#[doc(no_inline)] -pub use crate::marker::Copy; #[stable(feature = "rust1", since = "1.0.0")] #[doc(no_inline)] pub use crate::marker::{Send, Sized, Sync, Unpin}; @@ -24,21 +20,9 @@ pub use crate::ops::{Drop, Fn, FnMut, FnOnce}; pub use crate::mem::drop; // Re-exported types and traits -#[cfg(bootstrap)] -#[stable(feature = "rust1", since = "1.0.0")] -#[doc(no_inline)] -pub use crate::clone::Clone; -#[cfg(bootstrap)] -#[stable(feature = "rust1", since = "1.0.0")] -#[doc(no_inline)] -pub use crate::cmp::{PartialEq, PartialOrd, Eq, Ord}; #[stable(feature = "rust1", since = "1.0.0")] #[doc(no_inline)] pub use crate::convert::{AsRef, AsMut, Into, From}; -#[cfg(bootstrap)] -#[stable(feature = "rust1", since = "1.0.0")] -#[doc(no_inline)] -pub use crate::default::Default; #[stable(feature = "rust1", since = "1.0.0")] #[doc(no_inline)] pub use crate::iter::{Iterator, Extend, IntoIterator}; @@ -53,7 +37,6 @@ pub use crate::option::Option::{self, Some, None}; pub use crate::result::Result::{self, Ok, Err}; // Re-exported built-in macros -#[cfg(not(bootstrap))] #[stable(feature = "builtin_macro_prelude", since = "1.38.0")] #[doc(no_inline)] pub use core::prelude::v1::{ @@ -83,7 +66,6 @@ pub use core::prelude::v1::{ // FIXME: Attribute and derive macros are not documented because for them rustdoc generates // dead links which fail link checker testing. -#[cfg(not(bootstrap))] #[stable(feature = "builtin_macro_prelude", since = "1.38.0")] #[allow(deprecated)] #[doc(hidden)] diff --git a/src/libstd/sync/mod.rs b/src/libstd/sync/mod.rs index e29faf18d83..fd6e46fd61d 100644 --- a/src/libstd/sync/mod.rs +++ b/src/libstd/sync/mod.rs @@ -163,7 +163,6 @@ pub use self::condvar::{Condvar, WaitTimeoutResult}; #[stable(feature = "rust1", since = "1.0.0")] pub use self::mutex::{Mutex, MutexGuard}; #[stable(feature = "rust1", since = "1.0.0")] -#[cfg_attr(bootstrap, allow(deprecated_in_future))] #[allow(deprecated)] pub use self::once::{Once, OnceState, ONCE_INIT}; #[stable(feature = "rust1", since = "1.0.0")] diff --git a/src/libstd/sys/cloudabi/mod.rs b/src/libstd/sys/cloudabi/mod.rs index 6e147612eb4..518aef4ed5e 100644 --- a/src/libstd/sys/cloudabi/mod.rs +++ b/src/libstd/sys/cloudabi/mod.rs @@ -1,4 +1,4 @@ -#![allow(deprecated_in_future)] // mem::uninitialized; becomes `deprecated` when nightly is 1.39 +#![allow(deprecated)] // mem::uninitialized use crate::io::ErrorKind; use crate::mem; diff --git a/src/libunwind/build.rs b/src/libunwind/build.rs index da31a49ddf2..c1b0dbc0881 100644 --- a/src/libunwind/build.rs +++ b/src/libunwind/build.rs @@ -4,13 +4,11 @@ fn main() { println!("cargo:rerun-if-changed=build.rs"); let target = env::var("TARGET").expect("TARGET was not set"); - // FIXME: the not(bootstrap) part is needed because of the issue addressed by #62286, - // and could be removed once that change is in beta. - if cfg!(all(not(bootstrap), feature = "llvm-libunwind")) && + if cfg!(feature = "llvm-libunwind") && (target.contains("linux") || target.contains("fuchsia")) { // Build the unwinding from libunwind C/C++ source code. - #[cfg(all(not(bootstrap), feature = "llvm-libunwind"))] + #[cfg(feature = "llvm-libunwind")] llvm_libunwind::compile(); } else if target.contains("linux") { if target.contains("musl") { @@ -46,7 +44,7 @@ fn main() { } } -#[cfg(all(not(bootstrap), feature = "llvm-libunwind"))] +#[cfg(feature = "llvm-libunwind")] mod llvm_libunwind { use std::env; use std::path::Path; diff --git a/src/libunwind/libunwind.rs b/src/libunwind/libunwind.rs index aacbfc547d4..7c9eaa51fd9 100644 --- a/src/libunwind/libunwind.rs +++ b/src/libunwind/libunwind.rs @@ -70,7 +70,7 @@ pub enum _Unwind_Context {} pub type _Unwind_Exception_Cleanup_Fn = extern "C" fn(unwind_code: _Unwind_Reason_Code, exception: *mut _Unwind_Exception); -#[cfg_attr(all(not(bootstrap), feature = "llvm-libunwind", +#[cfg_attr(all(feature = "llvm-libunwind", any(target_os = "fuchsia", target_os = "linux")), link(name = "unwind", kind = "static"))] extern "C" { @@ -97,7 +97,7 @@ if #[cfg(all(any(target_os = "ios", target_os = "netbsd", not(target_arch = "arm } pub use _Unwind_Action::*; - #[cfg_attr(all(not(bootstrap), feature = "llvm-libunwind", + #[cfg_attr(all(feature = "llvm-libunwind", any(target_os = "fuchsia", target_os = "linux")), link(name = "unwind", kind = "static"))] extern "C" { @@ -153,7 +153,7 @@ if #[cfg(all(any(target_os = "ios", target_os = "netbsd", not(target_arch = "arm pub const UNWIND_POINTER_REG: c_int = 12; pub const UNWIND_IP_REG: c_int = 15; - #[cfg_attr(all(not(bootstrap), feature = "llvm-libunwind", + #[cfg_attr(all(feature = "llvm-libunwind", any(target_os = "fuchsia", target_os = "linux")), link(name = "unwind", kind = "static"))] extern "C" { @@ -218,7 +218,7 @@ if #[cfg(all(any(target_os = "ios", target_os = "netbsd", not(target_arch = "arm cfg_if::cfg_if! { if #[cfg(not(all(target_os = "ios", target_arch = "arm")))] { // Not 32-bit iOS - #[cfg_attr(all(not(bootstrap), feature = "llvm-libunwind", + #[cfg_attr(all(feature = "llvm-libunwind", any(target_os = "fuchsia", target_os = "linux")), link(name = "unwind", kind = "static"))] extern "C" { @@ -230,7 +230,7 @@ if #[cfg(not(all(target_os = "ios", target_arch = "arm")))] { } } else { // 32-bit iOS uses SjLj and does not provide _Unwind_Backtrace() - #[cfg_attr(all(not(bootstrap), feature = "llvm-libunwind", + #[cfg_attr(all(feature = "llvm-libunwind", any(target_os = "fuchsia", target_os = "linux")), link(name = "unwind", kind = "static"))] extern "C" { diff --git a/src/test/ui/lint/lint-qualification.rs b/src/test/ui/lint/lint-qualification.rs index 1b24191a111..0cace0ca035 100644 --- a/src/test/ui/lint/lint-qualification.rs +++ b/src/test/ui/lint/lint-qualification.rs @@ -1,5 +1,5 @@ #![deny(unused_qualifications)] -#[allow(deprecated)] +#![allow(deprecated)] mod foo { pub fn bar() {} -- cgit 1.4.1-3-g733a5 From a9ecfd729507d19946f0b66c78cebca1e23e9a15 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Thu, 15 Aug 2019 21:23:11 +0300 Subject: Hygienize use of built-in macros in the standard library --- src/liballoc/macros.rs | 2 +- src/libcore/macros.rs | 26 +++++++++++++------------- src/libstd/macros.rs | 18 +++++++++--------- src/test/ui/macros/trace-macro.stderr | 2 +- 4 files changed, 24 insertions(+), 24 deletions(-) (limited to 'src/liballoc') diff --git a/src/liballoc/macros.rs b/src/liballoc/macros.rs index 250c419c531..0b5e186d4c7 100644 --- a/src/liballoc/macros.rs +++ b/src/liballoc/macros.rs @@ -98,5 +98,5 @@ macro_rules! vec { #[macro_export] #[stable(feature = "rust1", since = "1.0.0")] macro_rules! format { - ($($arg:tt)*) => ($crate::fmt::format(format_args!($($arg)*))) + ($($arg:tt)*) => ($crate::fmt::format(::core::format_args!($($arg)*))) } diff --git a/src/libcore/macros.rs b/src/libcore/macros.rs index bbed9516716..c41763756b9 100644 --- a/src/libcore/macros.rs +++ b/src/libcore/macros.rs @@ -9,14 +9,14 @@ macro_rules! panic { $crate::panic!("explicit panic") ); ($msg:expr) => ({ - $crate::panicking::panic(&($msg, file!(), line!(), __rust_unstable_column!())) + $crate::panicking::panic(&($msg, $crate::file!(), $crate::line!(), $crate::column!())) }); ($msg:expr,) => ( $crate::panic!($msg) ); ($fmt:expr, $($arg:tt)+) => ({ - $crate::panicking::panic_fmt(format_args!($fmt, $($arg)+), - &(file!(), line!(), __rust_unstable_column!())) + $crate::panicking::panic_fmt($crate::format_args!($fmt, $($arg)+), + &($crate::file!(), $crate::line!(), $crate::column!())) }); } @@ -70,7 +70,7 @@ macro_rules! assert_eq { panic!(r#"assertion failed: `(left == right)` left: `{:?}`, right: `{:?}`: {}"#, &*left_val, &*right_val, - format_args!($($arg)+)) + $crate::format_args!($($arg)+)) } } } @@ -127,7 +127,7 @@ macro_rules! assert_ne { panic!(r#"assertion failed: `(left != right)` left: `{:?}`, right: `{:?}`: {}"#, &*left_val, &*right_val, - format_args!($($arg)+)) + $crate::format_args!($($arg)+)) } } } @@ -181,7 +181,7 @@ macro_rules! assert_ne { #[macro_export] #[stable(feature = "rust1", since = "1.0.0")] macro_rules! debug_assert { - ($($arg:tt)*) => (if cfg!(debug_assertions) { assert!($($arg)*); }) + ($($arg:tt)*) => (if $crate::cfg!(debug_assertions) { $crate::assert!($($arg)*); }) } /// Asserts that two expressions are equal to each other. @@ -208,7 +208,7 @@ macro_rules! debug_assert { #[macro_export] #[stable(feature = "rust1", since = "1.0.0")] macro_rules! debug_assert_eq { - ($($arg:tt)*) => (if cfg!(debug_assertions) { $crate::assert_eq!($($arg)*); }) + ($($arg:tt)*) => (if $crate::cfg!(debug_assertions) { $crate::assert_eq!($($arg)*); }) } /// Asserts that two expressions are not equal to each other. @@ -235,7 +235,7 @@ macro_rules! debug_assert_eq { #[macro_export] #[stable(feature = "assert_ne", since = "1.13.0")] macro_rules! debug_assert_ne { - ($($arg:tt)*) => (if cfg!(debug_assertions) { $crate::assert_ne!($($arg)*); }) + ($($arg:tt)*) => (if $crate::cfg!(debug_assertions) { $crate::assert_ne!($($arg)*); }) } /// Unwraps a result or propagates its error. @@ -386,7 +386,7 @@ macro_rules! r#try { #[macro_export] #[stable(feature = "rust1", since = "1.0.0")] macro_rules! write { - ($dst:expr, $($arg:tt)*) => ($dst.write_fmt(format_args!($($arg)*))) + ($dst:expr, $($arg:tt)*) => ($dst.write_fmt($crate::format_args!($($arg)*))) } /// Write formatted data into a buffer, with a newline appended. @@ -446,7 +446,7 @@ macro_rules! writeln { $crate::writeln!($dst) ); ($dst:expr, $($arg:tt)*) => ( - $dst.write_fmt(format_args_nl!($($arg)*)) + $dst.write_fmt($crate::format_args_nl!($($arg)*)) ); } @@ -515,7 +515,7 @@ macro_rules! unreachable { $crate::unreachable!($msg) }); ($fmt:expr, $($arg:tt)*) => ({ - panic!(concat!("internal error: entered unreachable code: ", $fmt), $($arg)*) + panic!($crate::concat!("internal error: entered unreachable code: ", $fmt), $($arg)*) }); } @@ -573,7 +573,7 @@ macro_rules! unreachable { #[stable(feature = "rust1", since = "1.0.0")] macro_rules! unimplemented { () => (panic!("not yet implemented")); - ($($arg:tt)+) => (panic!("not yet implemented: {}", format_args!($($arg)+))); + ($($arg:tt)+) => (panic!("not yet implemented: {}", $crate::format_args!($($arg)+))); } /// Indicates unfinished code. @@ -632,7 +632,7 @@ macro_rules! unimplemented { #[unstable(feature = "todo_macro", issue = "59277")] macro_rules! todo { () => (panic!("not yet implemented")); - ($($arg:tt)+) => (panic!("not yet implemented: {}", format_args!($($arg)+))); + ($($arg:tt)+) => (panic!("not yet implemented: {}", $crate::format_args!($($arg)+))); } /// Definitions of built-in macros. diff --git a/src/libstd/macros.rs b/src/libstd/macros.rs index f2000936b9a..9fafe26104a 100644 --- a/src/libstd/macros.rs +++ b/src/libstd/macros.rs @@ -59,14 +59,14 @@ macro_rules! panic { $crate::panic!("explicit panic") }); ($msg:expr) => ({ - $crate::rt::begin_panic($msg, &(file!(), line!(), __rust_unstable_column!())) + $crate::rt::begin_panic($msg, &($crate::file!(), $crate::line!(), $crate::column!())) }); ($msg:expr,) => ({ $crate::panic!($msg) }); ($fmt:expr, $($arg:tt)+) => ({ - $crate::rt::begin_panic_fmt(&format_args!($fmt, $($arg)+), - &(file!(), line!(), __rust_unstable_column!())) + $crate::rt::begin_panic_fmt(&$crate::format_args!($fmt, $($arg)+), + &($crate::file!(), $crate::line!(), $crate::column!())) }); } @@ -113,7 +113,7 @@ macro_rules! panic { #[stable(feature = "rust1", since = "1.0.0")] #[allow_internal_unstable(print_internals)] macro_rules! print { - ($($arg:tt)*) => ($crate::io::_print(format_args!($($arg)*))); + ($($arg:tt)*) => ($crate::io::_print($crate::format_args!($($arg)*))); } /// Prints to the standard output, with a newline. @@ -147,7 +147,7 @@ macro_rules! print { macro_rules! println { () => ($crate::print!("\n")); ($($arg:tt)*) => ({ - $crate::io::_print(format_args_nl!($($arg)*)); + $crate::io::_print($crate::format_args_nl!($($arg)*)); }) } @@ -176,7 +176,7 @@ macro_rules! println { #[stable(feature = "eprint", since = "1.19.0")] #[allow_internal_unstable(print_internals)] macro_rules! eprint { - ($($arg:tt)*) => ($crate::io::_eprint(format_args!($($arg)*))); + ($($arg:tt)*) => ($crate::io::_eprint($crate::format_args!($($arg)*))); } /// Prints to the standard error, with a newline. @@ -206,7 +206,7 @@ macro_rules! eprint { macro_rules! eprintln { () => ($crate::eprint!("\n")); ($($arg:tt)*) => ({ - $crate::io::_eprint(format_args_nl!($($arg)*)); + $crate::io::_eprint($crate::format_args_nl!($($arg)*)); }) } @@ -337,7 +337,7 @@ macro_rules! eprintln { #[stable(feature = "dbg_macro", since = "1.32.0")] macro_rules! dbg { () => { - $crate::eprintln!("[{}:{}]", file!(), line!()); + $crate::eprintln!("[{}:{}]", $crate::file!(), $crate::line!()); }; ($val:expr) => { // Use of `match` here is intentional because it affects the lifetimes @@ -345,7 +345,7 @@ macro_rules! dbg { match $val { tmp => { $crate::eprintln!("[{}:{}] {} = {:#?}", - file!(), line!(), stringify!($val), &tmp); + $crate::file!(), $crate::line!(), $crate::stringify!($val), &tmp); tmp } } diff --git a/src/test/ui/macros/trace-macro.stderr b/src/test/ui/macros/trace-macro.stderr index 287f7b297d5..202a9235adb 100644 --- a/src/test/ui/macros/trace-macro.stderr +++ b/src/test/ui/macros/trace-macro.stderr @@ -5,5 +5,5 @@ LL | println!("Hello, World!"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: expanding `println! { "Hello, World!" }` - = note: to `{ $crate :: io :: _print (format_args_nl ! ("Hello, World!")) ; }` + = note: to `{ $crate :: io :: _print ($crate :: format_args_nl ! ("Hello, World!")) ; }` -- cgit 1.4.1-3-g733a5 From 1613fdae37df044f2c254d25d356b3a57eb61a50 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Sat, 6 Jul 2019 18:17:24 +0200 Subject: Add Rc::get_mut_unchecked, Arc::get_mut_unchecked --- src/liballoc/rc.rs | 31 ++++++++++++++++++++++++++++++- src/liballoc/sync.rs | 31 ++++++++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 2 deletions(-) (limited to 'src/liballoc') diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index 0c406a92029..22d06180d8d 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -560,13 +560,42 @@ impl Rc { pub fn get_mut(this: &mut Self) -> Option<&mut T> { if Rc::is_unique(this) { unsafe { - Some(&mut this.ptr.as_mut().value) + Some(Rc::get_mut_unchecked(this)) } } else { None } } + /// Returns a mutable reference to the inner value, + /// without any check. + /// + /// See also [`get_mut`], which is safe and does appropriate checks. + /// + /// [`get_mut`]: struct.Rc.html#method.get_mut + /// + /// # Safety + /// + /// There must be no other `Rc` or [`Weak`][weak] pointers to the same value. + /// This is the case for example immediately after `Rc::new`. + /// + /// # Examples + /// + /// ``` + /// use std::rc::Rc; + /// + /// let mut x = Rc::new(String::new()); + /// unsafe { + /// Rc::get_mut_unchecked(&mut x).push_str("foo") + /// } + /// assert_eq!(*x, "foo"); + /// ``` + #[inline] + #[unstable(feature = "get_mut_unchecked", issue = "0")] + pub unsafe fn get_mut_unchecked(this: &mut Self) -> &mut T { + &mut this.ptr.as_mut().value + } + #[inline] #[stable(feature = "ptr_eq", since = "1.17.0")] /// Returns `true` if the two `Rc`s point to the same value (not diff --git a/src/liballoc/sync.rs b/src/liballoc/sync.rs index 7d3b2656a7b..4cd8e1fd4dd 100644 --- a/src/liballoc/sync.rs +++ b/src/liballoc/sync.rs @@ -945,13 +945,42 @@ impl Arc { // the Arc itself to be `mut`, so we're returning the only possible // reference to the inner data. unsafe { - Some(&mut this.ptr.as_mut().data) + Some(Arc::get_mut_unchecked(this)) } } else { None } } + /// Returns a mutable reference to the inner value, + /// without any check. + /// + /// See also [`get_mut`], which is safe and does appropriate checks. + /// + /// [`get_mut`]: struct.Arc.html#method.get_mut + /// + /// # Safety + /// + /// There must be no other `Arc` or [`Weak`][weak] pointers to the same value. + /// This is the case for example immediately after `Rc::new`. + /// + /// # Examples + /// + /// ``` + /// use std::sync::Arc; + /// + /// let mut x = Arc::new(String::new()); + /// unsafe { + /// Arc::get_mut_unchecked(&mut x).push_str("foo") + /// } + /// assert_eq!(*x, "foo"); + /// ``` + #[inline] + #[unstable(feature = "get_mut_unchecked", issue = "0")] + pub unsafe fn get_mut_unchecked(this: &mut Self) -> &mut T { + &mut this.ptr.as_mut().data + } + /// Determine whether this is the unique reference (including weak refs) to /// the underlying data. /// -- cgit 1.4.1-3-g733a5 From 7b02b9f8ec2850ac687ee5c7d869a626e09d22cb Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Sat, 6 Jul 2019 17:19:58 +0200 Subject: Add new_uninit and assume_init on Box, Rc, and Arc --- src/liballoc/boxed.rs | 61 +++++++++++++++++++++++++++++++++++ src/liballoc/rc.rs | 81 +++++++++++++++++++++++++++++++++++++++++++++++ src/liballoc/sync.rs | 81 +++++++++++++++++++++++++++++++++++++++++++++++ src/libcore/ptr/unique.rs | 8 +++++ 4 files changed, 231 insertions(+) (limited to 'src/liballoc') diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index c92db517cad..5ea7847ca45 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -93,6 +93,7 @@ use core::ops::{ use core::ptr::{self, NonNull, Unique}; use core::task::{Context, Poll}; +use crate::alloc; use crate::vec::Vec; use crate::raw_vec::RawVec; use crate::str::from_boxed_utf8_unchecked; @@ -121,6 +122,32 @@ impl Box { box x } + /// Construct a new box with uninitialized contents. + /// + /// # Examples + /// + /// ``` + /// #![feature(new_uninit)] + /// + /// let mut five = Box::::new_uninit(); + /// + /// let five = unsafe { + /// // Deferred initialization: + /// five.as_mut_ptr().write(5); + /// + /// Box::assume_init(five) + /// }; + /// + /// assert_eq!(*five, 5) + /// ``` + #[unstable(feature = "new_uninit", issue = "0")] + pub fn new_uninit() -> Box> { + let layout = alloc::Layout::new::>(); + let ptr = unsafe { alloc::alloc(layout) }; + let unique = Unique::new(ptr).unwrap_or_else(|| alloc::handle_alloc_error(layout)); + Box(unique.cast()) + } + /// Constructs a new `Pin>`. If `T` does not implement `Unpin`, then /// `x` will be pinned in memory and unable to be moved. #[stable(feature = "pin", since = "1.33.0")] @@ -130,6 +157,40 @@ impl Box { } } +impl Box> { + /// Convert to `Box`. + /// + /// # Safety + /// + /// As with [`MaybeUninit::assume_init`], + /// it is up to the caller to guarantee that the value + /// really is in an initialized state. + /// Calling this when the content is not yet fully initialized + /// causes immediate undefined behavior. + /// + /// # Examples + /// + /// ``` + /// #![feature(new_uninit)] + /// + /// let mut five = Box::::new_uninit(); + /// + /// let five: Box = unsafe { + /// // Deferred initialization: + /// five.as_mut_ptr().write(5); + /// + /// Box::assume_init(five) + /// }; + /// + /// assert_eq!(*five, 5) + /// ``` + #[unstable(feature = "new_uninit", issue = "0")] + #[inline] + pub unsafe fn assume_init(this: Self) -> Box { + Box(Box::into_unique(this).cast()) + } +} + impl Box { /// Constructs a box from a raw pointer. /// diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index 22d06180d8d..c902580354d 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -327,6 +327,43 @@ impl Rc { })) } + /// Construct a new Rc with uninitialized contents. + /// + /// # Examples + /// + /// ``` + /// #![feature(new_uninit)] + /// #![feature(get_mut_unchecked)] + /// + /// use std::rc::Rc; + /// + /// let mut five = Rc::::new_uninit(); + /// + /// let five = unsafe { + /// // Deferred initialization: + /// Rc::get_mut_unchecked(&mut five).as_mut_ptr().write(5); + /// + /// Rc::assume_init(five) + /// }; + /// + /// assert_eq!(*five, 5) + /// ``` + #[unstable(feature = "new_uninit", issue = "0")] + pub fn new_uninit() -> Rc> { + let layout = Layout::new::>>(); + unsafe { + let mut ptr = Global.alloc(layout) + .unwrap_or_else(|_| handle_alloc_error(layout)) + .cast::>>(); + ptr::write(&mut ptr.as_mut().strong, Cell::new(1)); + ptr::write(&mut ptr.as_mut().weak, Cell::new(1)); + Rc { + ptr, + phantom: PhantomData, + } + } + } + /// Constructs a new `Pin>`. If `T` does not implement `Unpin`, then /// `value` will be pinned in memory and unable to be moved. #[stable(feature = "pin", since = "1.33.0")] @@ -377,6 +414,48 @@ impl Rc { } } +impl Rc> { + /// Convert to `Rc`. + /// + /// # Safety + /// + /// As with [`MaybeUninit::assume_init`], + /// it is up to the caller to guarantee that the value + /// really is in an initialized state. + /// Calling this when the content is not yet fully initialized + /// causes immediate undefined behavior. + /// + /// # Examples + /// + /// ``` + /// #![feature(new_uninit)] + /// #![feature(get_mut_unchecked)] + /// + /// use std::rc::Rc; + /// + /// let mut five = Rc::::new_uninit(); + /// + /// let five = unsafe { + /// // Deferred initialization: + /// Rc::get_mut_unchecked(&mut five).as_mut_ptr().write(5); + /// + /// Rc::assume_init(five) + /// }; + /// + /// assert_eq!(*five, 5) + /// ``` + #[unstable(feature = "new_uninit", issue = "0")] + #[inline] + pub unsafe fn assume_init(this: Self) -> Rc { + let ptr = this.ptr.cast(); + mem::forget(this); + Rc { + ptr, + phantom: PhantomData, + } + } +} + impl Rc { /// Consumes the `Rc`, returning the wrapped pointer. /// @@ -582,6 +661,8 @@ impl Rc { /// # Examples /// /// ``` + /// #![feature(get_mut_unchecked)] + /// /// use std::rc::Rc; /// /// let mut x = Rc::new(String::new()); diff --git a/src/liballoc/sync.rs b/src/liballoc/sync.rs index 4cd8e1fd4dd..8c63c81ee27 100644 --- a/src/liballoc/sync.rs +++ b/src/liballoc/sync.rs @@ -311,6 +311,43 @@ impl Arc { Self::from_inner(Box::into_raw_non_null(x)) } + /// Construct a Arc box with uninitialized contents. + /// + /// # Examples + /// + /// ``` + /// #![feature(new_uninit)] + /// #![feature(get_mut_unchecked)] + /// + /// use std::sync::Arc; + /// + /// let mut five = Arc::::new_uninit(); + /// + /// let five = unsafe { + /// // Deferred initialization: + /// Arc::get_mut_unchecked(&mut five).as_mut_ptr().write(5); + /// + /// Arc::assume_init(five) + /// }; + /// + /// assert_eq!(*five, 5) + /// ``` + #[unstable(feature = "new_uninit", issue = "0")] + pub fn new_uninit() -> Arc> { + let layout = Layout::new::>>(); + unsafe { + let mut ptr = Global.alloc(layout) + .unwrap_or_else(|_| handle_alloc_error(layout)) + .cast::>>(); + ptr::write(&mut ptr.as_mut().strong, atomic::AtomicUsize::new(1)); + ptr::write(&mut ptr.as_mut().weak, atomic::AtomicUsize::new(1)); + Arc { + ptr, + phantom: PhantomData, + } + } + } + /// Constructs a new `Pin>`. If `T` does not implement `Unpin`, then /// `data` will be pinned in memory and unable to be moved. #[stable(feature = "pin", since = "1.33.0")] @@ -361,6 +398,48 @@ impl Arc { } } +impl Arc> { + /// Convert to `Arc`. + /// + /// # Safety + /// + /// As with [`MaybeUninit::assume_init`], + /// it is up to the caller to guarantee that the value + /// really is in an initialized state. + /// Calling this when the content is not yet fully initialized + /// causes immediate undefined behavior. + /// + /// # Examples + /// + /// ``` + /// #![feature(new_uninit)] + /// #![feature(get_mut_unchecked)] + /// + /// use std::sync::Arc; + /// + /// let mut five = Arc::::new_uninit(); + /// + /// let five = unsafe { + /// // Deferred initialization: + /// Arc::get_mut_unchecked(&mut five).as_mut_ptr().write(5); + /// + /// Arc::assume_init(five) + /// }; + /// + /// assert_eq!(*five, 5) + /// ``` + #[unstable(feature = "new_uninit", issue = "0")] + #[inline] + pub unsafe fn assume_init(this: Self) -> Arc { + let ptr = this.ptr.cast(); + mem::forget(this); + Arc { + ptr, + phantom: PhantomData, + } + } +} + impl Arc { /// Consumes the `Arc`, returning the wrapped pointer. /// @@ -967,6 +1046,8 @@ impl Arc { /// # Examples /// /// ``` + /// #![feature(get_mut_unchecked)] + /// /// use std::sync::Arc; /// /// let mut x = Arc::new(String::new()); diff --git a/src/libcore/ptr/unique.rs b/src/libcore/ptr/unique.rs index f0d011fe6b2..a739f3b6022 100644 --- a/src/libcore/ptr/unique.rs +++ b/src/libcore/ptr/unique.rs @@ -122,6 +122,14 @@ impl Unique { pub unsafe fn as_mut(&mut self) -> &mut T { &mut *self.as_ptr() } + + /// Cast to a pointer of another type + #[inline] + pub const fn cast(self) -> Unique { + unsafe { + Unique::new_unchecked(self.as_ptr() as *mut U) + } + } } #[unstable(feature = "ptr_internals", issue = "0")] -- cgit 1.4.1-3-g733a5 From bde19240594c229a19ac928b823101965f4a0cd8 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Sat, 6 Jul 2019 21:27:55 +0200 Subject: Add new_uninit_slice and assume_init on Box, Rc, and Arc of [T] --- src/liballoc/boxed.rs | 74 +++++++++++++++++++++++++++++++++++--- src/liballoc/rc.rs | 98 ++++++++++++++++++++++++++++++++++++++++++++++++--- src/liballoc/sync.rs | 98 ++++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 256 insertions(+), 14 deletions(-) (limited to 'src/liballoc') diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 5ea7847ca45..a759260bd8f 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -91,6 +91,7 @@ use core::ops::{ CoerceUnsized, DispatchFromDyn, Deref, DerefMut, Receiver, Generator, GeneratorState }; use core::ptr::{self, NonNull, Unique}; +use core::slice; use core::task::{Context, Poll}; use crate::alloc; @@ -135,7 +136,7 @@ impl Box { /// // Deferred initialization: /// five.as_mut_ptr().write(5); /// - /// Box::assume_init(five) + /// five.assume_init() /// }; /// /// assert_eq!(*five, 5) @@ -148,6 +149,35 @@ impl Box { Box(unique.cast()) } + /// Construct a new boxed slice with uninitialized contents. + /// + /// # Examples + /// + /// ``` + /// #![feature(new_uninit)] + /// + /// let mut values = Box::::new_uninit_slice(3); + /// + /// let values = unsafe { + /// // Deferred initialization: + /// values[0].as_mut_ptr().write(1); + /// values[1].as_mut_ptr().write(2); + /// values[2].as_mut_ptr().write(3); + /// + /// values.assume_init() + /// }; + /// + /// assert_eq!(*values, [1, 2, 3]) + /// ``` + #[unstable(feature = "new_uninit", issue = "0")] + pub fn new_uninit_slice(len: usize) -> Box<[mem::MaybeUninit]> { + let layout = alloc::Layout::array::>(len).unwrap(); + let ptr = unsafe { alloc::alloc(layout) }; + let unique = Unique::new(ptr).unwrap_or_else(|| alloc::handle_alloc_error(layout)); + let slice = unsafe { slice::from_raw_parts_mut(unique.cast().as_ptr(), len) }; + Box(Unique::from(slice)) + } + /// Constructs a new `Pin>`. If `T` does not implement `Unpin`, then /// `x` will be pinned in memory and unable to be moved. #[stable(feature = "pin", since = "1.33.0")] @@ -179,15 +209,51 @@ impl Box> { /// // Deferred initialization: /// five.as_mut_ptr().write(5); /// - /// Box::assume_init(five) + /// five.assume_init() /// }; /// /// assert_eq!(*five, 5) /// ``` #[unstable(feature = "new_uninit", issue = "0")] #[inline] - pub unsafe fn assume_init(this: Self) -> Box { - Box(Box::into_unique(this).cast()) + pub unsafe fn assume_init(self) -> Box { + Box(Box::into_unique(self).cast()) + } +} + +impl Box<[mem::MaybeUninit]> { + /// Convert to `Box<[T]>`. + /// + /// # Safety + /// + /// As with [`MaybeUninit::assume_init`], + /// it is up to the caller to guarantee that the values + /// really are in an initialized state. + /// Calling this when the content is not yet fully initialized + /// causes immediate undefined behavior. + /// + /// # Examples + /// + /// ``` + /// #![feature(new_uninit)] + /// + /// let mut values = Box::::new_uninit_slice(3); + /// + /// let values = unsafe { + /// // Deferred initialization: + /// values[0].as_mut_ptr().write(1); + /// values[1].as_mut_ptr().write(2); + /// values[2].as_mut_ptr().write(3); + /// + /// values.assume_init() + /// }; + /// + /// assert_eq!(*values, [1, 2, 3]) + /// ``` + #[unstable(feature = "new_uninit", issue = "0")] + #[inline] + pub unsafe fn assume_init(self) -> Box<[T]> { + Box(Unique::new_unchecked(Box::into_raw(self) as _)) } } diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index c902580354d..404c86a24da 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -343,7 +343,7 @@ impl Rc { /// // Deferred initialization: /// Rc::get_mut_unchecked(&mut five).as_mut_ptr().write(5); /// - /// Rc::assume_init(five) + /// five.assume_init() /// }; /// /// assert_eq!(*five, 5) @@ -364,6 +364,50 @@ impl Rc { } } + /// Construct a new reference-counted slice with uninitialized contents. + /// + /// # Examples + /// + /// ``` + /// #![feature(new_uninit)] + /// #![feature(get_mut_unchecked)] + /// + /// use std::rc::Rc; + /// + /// let mut values = Rc::::new_uninit_slice(3); + /// + /// let values = unsafe { + /// // Deferred initialization: + /// Rc::get_mut_unchecked(&mut values)[0].as_mut_ptr().write(1); + /// Rc::get_mut_unchecked(&mut values)[1].as_mut_ptr().write(2); + /// Rc::get_mut_unchecked(&mut values)[2].as_mut_ptr().write(3); + /// + /// values.assume_init() + /// }; + /// + /// assert_eq!(*values, [1, 2, 3]) + /// ``` + #[unstable(feature = "new_uninit", issue = "0")] + pub fn new_uninit_slice(len: usize) -> Rc<[mem::MaybeUninit]> { + let data_layout = Layout::array::>(len).unwrap(); + let (layout, offset) = Layout::new::>().extend(data_layout).unwrap(); + unsafe { + let allocated_ptr = Global.alloc(layout) + .unwrap_or_else(|_| handle_alloc_error(layout)) + .as_ptr(); + let data_ptr = allocated_ptr.add(offset) as *mut mem::MaybeUninit; + let slice: *mut [mem::MaybeUninit] = from_raw_parts_mut(data_ptr, len); + let wide_ptr = slice as *mut RcBox<[mem::MaybeUninit]>; + let wide_ptr = set_data_ptr(wide_ptr, allocated_ptr); + ptr::write(&mut (*wide_ptr).strong, Cell::new(1)); + ptr::write(&mut (*wide_ptr).weak, Cell::new(1)); + Rc { + ptr: NonNull::new_unchecked(wide_ptr), + phantom: PhantomData, + } + } + } + /// Constructs a new `Pin>`. If `T` does not implement `Unpin`, then /// `value` will be pinned in memory and unable to be moved. #[stable(feature = "pin", since = "1.33.0")] @@ -439,16 +483,60 @@ impl Rc> { /// // Deferred initialization: /// Rc::get_mut_unchecked(&mut five).as_mut_ptr().write(5); /// - /// Rc::assume_init(five) + /// five.assume_init() /// }; /// /// assert_eq!(*five, 5) /// ``` #[unstable(feature = "new_uninit", issue = "0")] #[inline] - pub unsafe fn assume_init(this: Self) -> Rc { - let ptr = this.ptr.cast(); - mem::forget(this); + pub unsafe fn assume_init(self) -> Rc { + let ptr = self.ptr.cast(); + mem::forget(self); + Rc { + ptr, + phantom: PhantomData, + } + } +} + +impl Rc<[mem::MaybeUninit]> { + /// Convert to `Rc<[T]>`. + /// + /// # Safety + /// + /// As with [`MaybeUninit::assume_init`], + /// it is up to the caller to guarantee that the value + /// really is in an initialized state. + /// Calling this when the content is not yet fully initialized + /// causes immediate undefined behavior. + /// + /// # Examples + /// + /// ``` + /// #![feature(new_uninit)] + /// #![feature(get_mut_unchecked)] + /// + /// use std::rc::Rc; + /// + /// let mut values = Rc::::new_uninit_slice(3); + /// + /// let values = unsafe { + /// // Deferred initialization: + /// Rc::get_mut_unchecked(&mut values)[0].as_mut_ptr().write(1); + /// Rc::get_mut_unchecked(&mut values)[1].as_mut_ptr().write(2); + /// Rc::get_mut_unchecked(&mut values)[2].as_mut_ptr().write(3); + /// + /// values.assume_init() + /// }; + /// + /// assert_eq!(*values, [1, 2, 3]) + /// ``` + #[unstable(feature = "new_uninit", issue = "0")] + #[inline] + pub unsafe fn assume_init(self) -> Rc<[T]> { + let ptr = NonNull::new_unchecked(self.ptr.as_ptr() as _); + mem::forget(self); Rc { ptr, phantom: PhantomData, diff --git a/src/liballoc/sync.rs b/src/liballoc/sync.rs index 8c63c81ee27..3f47b9e6c7d 100644 --- a/src/liballoc/sync.rs +++ b/src/liballoc/sync.rs @@ -327,7 +327,7 @@ impl Arc { /// // Deferred initialization: /// Arc::get_mut_unchecked(&mut five).as_mut_ptr().write(5); /// - /// Arc::assume_init(five) + /// five.assume_init() /// }; /// /// assert_eq!(*five, 5) @@ -348,6 +348,50 @@ impl Arc { } } + /// Construct a new reference-counted slice with uninitialized contents. + /// + /// # Examples + /// + /// ``` + /// #![feature(new_uninit)] + /// #![feature(get_mut_unchecked)] + /// + /// use std::sync::Arc; + /// + /// let mut values = Arc::::new_uninit_slice(3); + /// + /// let values = unsafe { + /// // Deferred initialization: + /// Arc::get_mut_unchecked(&mut values)[0].as_mut_ptr().write(1); + /// Arc::get_mut_unchecked(&mut values)[1].as_mut_ptr().write(2); + /// Arc::get_mut_unchecked(&mut values)[2].as_mut_ptr().write(3); + /// + /// values.assume_init() + /// }; + /// + /// assert_eq!(*values, [1, 2, 3]) + /// ``` + #[unstable(feature = "new_uninit", issue = "0")] + pub fn new_uninit_slice(len: usize) -> Arc<[mem::MaybeUninit]> { + let data_layout = Layout::array::>(len).unwrap(); + let (layout, offset) = Layout::new::>().extend(data_layout).unwrap(); + unsafe { + let allocated_ptr = Global.alloc(layout) + .unwrap_or_else(|_| handle_alloc_error(layout)) + .as_ptr(); + let data_ptr = allocated_ptr.add(offset) as *mut mem::MaybeUninit; + let slice: *mut [mem::MaybeUninit] = from_raw_parts_mut(data_ptr, len); + let wide_ptr = slice as *mut ArcInner<[mem::MaybeUninit]>; + let wide_ptr = set_data_ptr(wide_ptr, allocated_ptr); + ptr::write(&mut (*wide_ptr).strong, atomic::AtomicUsize::new(1)); + ptr::write(&mut (*wide_ptr).weak, atomic::AtomicUsize::new(1)); + Arc { + ptr: NonNull::new_unchecked(wide_ptr), + phantom: PhantomData, + } + } + } + /// Constructs a new `Pin>`. If `T` does not implement `Unpin`, then /// `data` will be pinned in memory and unable to be moved. #[stable(feature = "pin", since = "1.33.0")] @@ -423,16 +467,60 @@ impl Arc> { /// // Deferred initialization: /// Arc::get_mut_unchecked(&mut five).as_mut_ptr().write(5); /// - /// Arc::assume_init(five) + /// five.assume_init() /// }; /// /// assert_eq!(*five, 5) /// ``` #[unstable(feature = "new_uninit", issue = "0")] #[inline] - pub unsafe fn assume_init(this: Self) -> Arc { - let ptr = this.ptr.cast(); - mem::forget(this); + pub unsafe fn assume_init(self) -> Arc { + let ptr = self.ptr.cast(); + mem::forget(self); + Arc { + ptr, + phantom: PhantomData, + } + } +} + +impl Arc<[mem::MaybeUninit]> { + /// Convert to `Arc<[T]>`. + /// + /// # Safety + /// + /// As with [`MaybeUninit::assume_init`], + /// it is up to the caller to guarantee that the value + /// really is in an initialized state. + /// Calling this when the content is not yet fully initialized + /// causes immediate undefined behavior. + /// + /// # Examples + /// + /// ``` + /// #![feature(new_uninit)] + /// #![feature(get_mut_unchecked)] + /// + /// use std::sync::Arc; + /// + /// let mut values = Arc::::new_uninit_slice(3); + /// + /// let values = unsafe { + /// // Deferred initialization: + /// Arc::get_mut_unchecked(&mut values)[0].as_mut_ptr().write(1); + /// Arc::get_mut_unchecked(&mut values)[1].as_mut_ptr().write(2); + /// Arc::get_mut_unchecked(&mut values)[2].as_mut_ptr().write(3); + /// + /// values.assume_init() + /// }; + /// + /// assert_eq!(*values, [1, 2, 3]) + /// ``` + #[unstable(feature = "new_uninit", issue = "0")] + #[inline] + pub unsafe fn assume_init(self) -> Arc<[T]> { + let ptr = NonNull::new_unchecked(self.ptr.as_ptr() as _); + mem::forget(self); Arc { ptr, phantom: PhantomData, -- cgit 1.4.1-3-g733a5 From 170d933d2f8a7d4787355a86136093af86b03077 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Sat, 6 Jul 2019 21:52:15 +0200 Subject: Move constructors of boxed/rc’ed slices to matching `impl` blocks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/liballoc/boxed.rs | 22 ++++++------ src/liballoc/rc.rs | 92 ++++++++++++++++++++++++++------------------------- src/liballoc/sync.rs | 92 ++++++++++++++++++++++++++------------------------- 3 files changed, 106 insertions(+), 100 deletions(-) (limited to 'src/liballoc') diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index a759260bd8f..6e296907ce8 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -149,6 +149,16 @@ impl Box { Box(unique.cast()) } + /// Constructs a new `Pin>`. If `T` does not implement `Unpin`, then + /// `x` will be pinned in memory and unable to be moved. + #[stable(feature = "pin", since = "1.33.0")] + #[inline(always)] + pub fn pin(x: T) -> Pin> { + (box x).into() + } +} + +impl Box<[T]> { /// Construct a new boxed slice with uninitialized contents. /// /// # Examples @@ -156,7 +166,7 @@ impl Box { /// ``` /// #![feature(new_uninit)] /// - /// let mut values = Box::::new_uninit_slice(3); + /// let mut values = Box::<[u32]>::new_uninit_slice(3); /// /// let values = unsafe { /// // Deferred initialization: @@ -177,14 +187,6 @@ impl Box { let slice = unsafe { slice::from_raw_parts_mut(unique.cast().as_ptr(), len) }; Box(Unique::from(slice)) } - - /// Constructs a new `Pin>`. If `T` does not implement `Unpin`, then - /// `x` will be pinned in memory and unable to be moved. - #[stable(feature = "pin", since = "1.33.0")] - #[inline(always)] - pub fn pin(x: T) -> Pin> { - (box x).into() - } } impl Box> { @@ -237,7 +239,7 @@ impl Box<[mem::MaybeUninit]> { /// ``` /// #![feature(new_uninit)] /// - /// let mut values = Box::::new_uninit_slice(3); + /// let mut values = Box::<[u32]>::new_uninit_slice(3); /// /// let values = unsafe { /// // Deferred initialization: diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index 404c86a24da..aa1268ec051 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -364,50 +364,6 @@ impl Rc { } } - /// Construct a new reference-counted slice with uninitialized contents. - /// - /// # Examples - /// - /// ``` - /// #![feature(new_uninit)] - /// #![feature(get_mut_unchecked)] - /// - /// use std::rc::Rc; - /// - /// let mut values = Rc::::new_uninit_slice(3); - /// - /// let values = unsafe { - /// // Deferred initialization: - /// Rc::get_mut_unchecked(&mut values)[0].as_mut_ptr().write(1); - /// Rc::get_mut_unchecked(&mut values)[1].as_mut_ptr().write(2); - /// Rc::get_mut_unchecked(&mut values)[2].as_mut_ptr().write(3); - /// - /// values.assume_init() - /// }; - /// - /// assert_eq!(*values, [1, 2, 3]) - /// ``` - #[unstable(feature = "new_uninit", issue = "0")] - pub fn new_uninit_slice(len: usize) -> Rc<[mem::MaybeUninit]> { - let data_layout = Layout::array::>(len).unwrap(); - let (layout, offset) = Layout::new::>().extend(data_layout).unwrap(); - unsafe { - let allocated_ptr = Global.alloc(layout) - .unwrap_or_else(|_| handle_alloc_error(layout)) - .as_ptr(); - let data_ptr = allocated_ptr.add(offset) as *mut mem::MaybeUninit; - let slice: *mut [mem::MaybeUninit] = from_raw_parts_mut(data_ptr, len); - let wide_ptr = slice as *mut RcBox<[mem::MaybeUninit]>; - let wide_ptr = set_data_ptr(wide_ptr, allocated_ptr); - ptr::write(&mut (*wide_ptr).strong, Cell::new(1)); - ptr::write(&mut (*wide_ptr).weak, Cell::new(1)); - Rc { - ptr: NonNull::new_unchecked(wide_ptr), - phantom: PhantomData, - } - } - } - /// Constructs a new `Pin>`. If `T` does not implement `Unpin`, then /// `value` will be pinned in memory and unable to be moved. #[stable(feature = "pin", since = "1.33.0")] @@ -458,6 +414,52 @@ impl Rc { } } +impl Rc<[T]> { + /// Construct a new reference-counted slice with uninitialized contents. + /// + /// # Examples + /// + /// ``` + /// #![feature(new_uninit)] + /// #![feature(get_mut_unchecked)] + /// + /// use std::rc::Rc; + /// + /// let mut values = Rc::<[u32]>::new_uninit_slice(3); + /// + /// let values = unsafe { + /// // Deferred initialization: + /// Rc::get_mut_unchecked(&mut values)[0].as_mut_ptr().write(1); + /// Rc::get_mut_unchecked(&mut values)[1].as_mut_ptr().write(2); + /// Rc::get_mut_unchecked(&mut values)[2].as_mut_ptr().write(3); + /// + /// values.assume_init() + /// }; + /// + /// assert_eq!(*values, [1, 2, 3]) + /// ``` + #[unstable(feature = "new_uninit", issue = "0")] + pub fn new_uninit_slice(len: usize) -> Rc<[mem::MaybeUninit]> { + let data_layout = Layout::array::>(len).unwrap(); + let (layout, offset) = Layout::new::>().extend(data_layout).unwrap(); + unsafe { + let allocated_ptr = Global.alloc(layout) + .unwrap_or_else(|_| handle_alloc_error(layout)) + .as_ptr(); + let data_ptr = allocated_ptr.add(offset) as *mut mem::MaybeUninit; + let slice: *mut [mem::MaybeUninit] = from_raw_parts_mut(data_ptr, len); + let wide_ptr = slice as *mut RcBox<[mem::MaybeUninit]>; + let wide_ptr = set_data_ptr(wide_ptr, allocated_ptr); + ptr::write(&mut (*wide_ptr).strong, Cell::new(1)); + ptr::write(&mut (*wide_ptr).weak, Cell::new(1)); + Rc { + ptr: NonNull::new_unchecked(wide_ptr), + phantom: PhantomData, + } + } + } +} + impl Rc> { /// Convert to `Rc`. /// @@ -519,7 +521,7 @@ impl Rc<[mem::MaybeUninit]> { /// /// use std::rc::Rc; /// - /// let mut values = Rc::::new_uninit_slice(3); + /// let mut values = Rc::<[u32]>::new_uninit_slice(3); /// /// let values = unsafe { /// // Deferred initialization: diff --git a/src/liballoc/sync.rs b/src/liballoc/sync.rs index 3f47b9e6c7d..93441437e45 100644 --- a/src/liballoc/sync.rs +++ b/src/liballoc/sync.rs @@ -348,50 +348,6 @@ impl Arc { } } - /// Construct a new reference-counted slice with uninitialized contents. - /// - /// # Examples - /// - /// ``` - /// #![feature(new_uninit)] - /// #![feature(get_mut_unchecked)] - /// - /// use std::sync::Arc; - /// - /// let mut values = Arc::::new_uninit_slice(3); - /// - /// let values = unsafe { - /// // Deferred initialization: - /// Arc::get_mut_unchecked(&mut values)[0].as_mut_ptr().write(1); - /// Arc::get_mut_unchecked(&mut values)[1].as_mut_ptr().write(2); - /// Arc::get_mut_unchecked(&mut values)[2].as_mut_ptr().write(3); - /// - /// values.assume_init() - /// }; - /// - /// assert_eq!(*values, [1, 2, 3]) - /// ``` - #[unstable(feature = "new_uninit", issue = "0")] - pub fn new_uninit_slice(len: usize) -> Arc<[mem::MaybeUninit]> { - let data_layout = Layout::array::>(len).unwrap(); - let (layout, offset) = Layout::new::>().extend(data_layout).unwrap(); - unsafe { - let allocated_ptr = Global.alloc(layout) - .unwrap_or_else(|_| handle_alloc_error(layout)) - .as_ptr(); - let data_ptr = allocated_ptr.add(offset) as *mut mem::MaybeUninit; - let slice: *mut [mem::MaybeUninit] = from_raw_parts_mut(data_ptr, len); - let wide_ptr = slice as *mut ArcInner<[mem::MaybeUninit]>; - let wide_ptr = set_data_ptr(wide_ptr, allocated_ptr); - ptr::write(&mut (*wide_ptr).strong, atomic::AtomicUsize::new(1)); - ptr::write(&mut (*wide_ptr).weak, atomic::AtomicUsize::new(1)); - Arc { - ptr: NonNull::new_unchecked(wide_ptr), - phantom: PhantomData, - } - } - } - /// Constructs a new `Pin>`. If `T` does not implement `Unpin`, then /// `data` will be pinned in memory and unable to be moved. #[stable(feature = "pin", since = "1.33.0")] @@ -442,6 +398,52 @@ impl Arc { } } +impl Arc<[T]> { + /// Construct a new reference-counted slice with uninitialized contents. + /// + /// # Examples + /// + /// ``` + /// #![feature(new_uninit)] + /// #![feature(get_mut_unchecked)] + /// + /// use std::sync::Arc; + /// + /// let mut values = Arc::<[u32]>::new_uninit_slice(3); + /// + /// let values = unsafe { + /// // Deferred initialization: + /// Arc::get_mut_unchecked(&mut values)[0].as_mut_ptr().write(1); + /// Arc::get_mut_unchecked(&mut values)[1].as_mut_ptr().write(2); + /// Arc::get_mut_unchecked(&mut values)[2].as_mut_ptr().write(3); + /// + /// values.assume_init() + /// }; + /// + /// assert_eq!(*values, [1, 2, 3]) + /// ``` + #[unstable(feature = "new_uninit", issue = "0")] + pub fn new_uninit_slice(len: usize) -> Arc<[mem::MaybeUninit]> { + let data_layout = Layout::array::>(len).unwrap(); + let (layout, offset) = Layout::new::>().extend(data_layout).unwrap(); + unsafe { + let allocated_ptr = Global.alloc(layout) + .unwrap_or_else(|_| handle_alloc_error(layout)) + .as_ptr(); + let data_ptr = allocated_ptr.add(offset) as *mut mem::MaybeUninit; + let slice: *mut [mem::MaybeUninit] = from_raw_parts_mut(data_ptr, len); + let wide_ptr = slice as *mut ArcInner<[mem::MaybeUninit]>; + let wide_ptr = set_data_ptr(wide_ptr, allocated_ptr); + ptr::write(&mut (*wide_ptr).strong, atomic::AtomicUsize::new(1)); + ptr::write(&mut (*wide_ptr).weak, atomic::AtomicUsize::new(1)); + Arc { + ptr: NonNull::new_unchecked(wide_ptr), + phantom: PhantomData, + } + } + } +} + impl Arc> { /// Convert to `Arc`. /// @@ -503,7 +505,7 @@ impl Arc<[mem::MaybeUninit]> { /// /// use std::sync::Arc; /// - /// let mut values = Arc::::new_uninit_slice(3); + /// let mut values = Arc::<[u32]>::new_uninit_slice(3); /// /// let values = unsafe { /// // Deferred initialization: -- cgit 1.4.1-3-g733a5 From 4eeb623e9ee35db03ec3281dcfd1c61194312404 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Sat, 6 Jul 2019 23:30:32 +0200 Subject: Fix intra-rustdoc links --- src/liballoc/boxed.rs | 4 ++++ src/liballoc/rc.rs | 6 +++++- src/liballoc/sync.rs | 6 +++++- 3 files changed, 14 insertions(+), 2 deletions(-) (limited to 'src/liballoc') diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 6e296907ce8..3442fce467a 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -200,6 +200,8 @@ impl Box> { /// Calling this when the content is not yet fully initialized /// causes immediate undefined behavior. /// + /// [`MaybeUninit::assume_init`]: ../../std/mem/union.MaybeUninit.html#method.assume_init + /// /// # Examples /// /// ``` @@ -234,6 +236,8 @@ impl Box<[mem::MaybeUninit]> { /// Calling this when the content is not yet fully initialized /// causes immediate undefined behavior. /// + /// [`MaybeUninit::assume_init`]: ../../std/mem/union.MaybeUninit.html#method.assume_init + /// /// # Examples /// /// ``` diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index aa1268ec051..70911790be2 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -471,6 +471,8 @@ impl Rc> { /// Calling this when the content is not yet fully initialized /// causes immediate undefined behavior. /// + /// [`MaybeUninit::assume_init`]: ../../std/mem/union.MaybeUninit.html#method.assume_init + /// /// # Examples /// /// ``` @@ -513,6 +515,8 @@ impl Rc<[mem::MaybeUninit]> { /// Calling this when the content is not yet fully initialized /// causes immediate undefined behavior. /// + /// [`MaybeUninit::assume_init`]: ../../std/mem/union.MaybeUninit.html#method.assume_init + /// /// # Examples /// /// ``` @@ -745,7 +749,7 @@ impl Rc { /// /// # Safety /// - /// There must be no other `Rc` or [`Weak`][weak] pointers to the same value. + /// There must be no other `Rc` or [`Weak`] pointers to the same value. /// This is the case for example immediately after `Rc::new`. /// /// # Examples diff --git a/src/liballoc/sync.rs b/src/liballoc/sync.rs index 93441437e45..901d2d831d1 100644 --- a/src/liballoc/sync.rs +++ b/src/liballoc/sync.rs @@ -455,6 +455,8 @@ impl Arc> { /// Calling this when the content is not yet fully initialized /// causes immediate undefined behavior. /// + /// [`MaybeUninit::assume_init`]: ../../std/mem/union.MaybeUninit.html#method.assume_init + /// /// # Examples /// /// ``` @@ -497,6 +499,8 @@ impl Arc<[mem::MaybeUninit]> { /// Calling this when the content is not yet fully initialized /// causes immediate undefined behavior. /// + /// [`MaybeUninit::assume_init`]: ../../std/mem/union.MaybeUninit.html#method.assume_init + /// /// # Examples /// /// ``` @@ -1130,7 +1134,7 @@ impl Arc { /// /// # Safety /// - /// There must be no other `Arc` or [`Weak`][weak] pointers to the same value. + /// There must be no other `Arc` or [`Weak`] pointers to the same value. /// This is the case for example immediately after `Rc::new`. /// /// # Examples -- cgit 1.4.1-3-g733a5 From dab967afdc76904c971d5ac5f5f46e31272a9827 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Tue, 16 Jul 2019 09:02:36 +0200 Subject: Use `alloc::Global` in `Box::new_uninit` --- src/liballoc/boxed.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'src/liballoc') diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 3442fce467a..9f6b16bbef3 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -94,7 +94,7 @@ use core::ptr::{self, NonNull, Unique}; use core::slice; use core::task::{Context, Poll}; -use crate::alloc; +use crate::alloc::{self, Global, Alloc}; use crate::vec::Vec; use crate::raw_vec::RawVec; use crate::str::from_boxed_utf8_unchecked; @@ -144,9 +144,11 @@ impl Box { #[unstable(feature = "new_uninit", issue = "0")] pub fn new_uninit() -> Box> { let layout = alloc::Layout::new::>(); - let ptr = unsafe { alloc::alloc(layout) }; - let unique = Unique::new(ptr).unwrap_or_else(|| alloc::handle_alloc_error(layout)); - Box(unique.cast()) + let ptr = unsafe { + Global.alloc(layout) + .unwrap_or_else(|_| alloc::handle_alloc_error(layout)) + }; + Box(ptr.cast().into()) } /// Constructs a new `Pin>`. If `T` does not implement `Unpin`, then -- cgit 1.4.1-3-g733a5 From 1141136b90663cf9d5ee6325b2f9e47f02e70746 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Tue, 16 Jul 2019 09:17:35 +0200 Subject: Use ManuallyDrop instead of mem::forget Per https://github.com/rust-lang/rust/pull/62451#discussion_r303197278 --- src/liballoc/rc.rs | 8 ++------ src/liballoc/sync.rs | 8 ++------ 2 files changed, 4 insertions(+), 12 deletions(-) (limited to 'src/liballoc') diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index 70911790be2..c76c4b50c64 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -495,10 +495,8 @@ impl Rc> { #[unstable(feature = "new_uninit", issue = "0")] #[inline] pub unsafe fn assume_init(self) -> Rc { - let ptr = self.ptr.cast(); - mem::forget(self); Rc { - ptr, + ptr: mem::ManuallyDrop::new(self).ptr.cast(), phantom: PhantomData, } } @@ -541,10 +539,8 @@ impl Rc<[mem::MaybeUninit]> { #[unstable(feature = "new_uninit", issue = "0")] #[inline] pub unsafe fn assume_init(self) -> Rc<[T]> { - let ptr = NonNull::new_unchecked(self.ptr.as_ptr() as _); - mem::forget(self); Rc { - ptr, + ptr: NonNull::new_unchecked(mem::ManuallyDrop::new(self).ptr.as_ptr() as _), phantom: PhantomData, } } diff --git a/src/liballoc/sync.rs b/src/liballoc/sync.rs index 901d2d831d1..bc3acd27e8b 100644 --- a/src/liballoc/sync.rs +++ b/src/liballoc/sync.rs @@ -479,10 +479,8 @@ impl Arc> { #[unstable(feature = "new_uninit", issue = "0")] #[inline] pub unsafe fn assume_init(self) -> Arc { - let ptr = self.ptr.cast(); - mem::forget(self); Arc { - ptr, + ptr: mem::ManuallyDrop::new(self).ptr.cast(), phantom: PhantomData, } } @@ -525,10 +523,8 @@ impl Arc<[mem::MaybeUninit]> { #[unstable(feature = "new_uninit", issue = "0")] #[inline] pub unsafe fn assume_init(self) -> Arc<[T]> { - let ptr = NonNull::new_unchecked(self.ptr.as_ptr() as _); - mem::forget(self); Arc { - ptr, + ptr: NonNull::new_unchecked(mem::ManuallyDrop::new(self).ptr.as_ptr() as _), phantom: PhantomData, } } -- cgit 1.4.1-3-g733a5 From 78264f5e3c94592ff64aab831499f48497083a91 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Mon, 5 Aug 2019 17:45:30 +0200 Subject: Add tracking issue numbers --- src/liballoc/boxed.rs | 8 ++++---- src/liballoc/rc.rs | 10 +++++----- src/liballoc/sync.rs | 10 +++++----- 3 files changed, 14 insertions(+), 14 deletions(-) (limited to 'src/liballoc') diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 9f6b16bbef3..e315b9f3417 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -141,7 +141,7 @@ impl Box { /// /// assert_eq!(*five, 5) /// ``` - #[unstable(feature = "new_uninit", issue = "0")] + #[unstable(feature = "new_uninit", issue = "63291")] pub fn new_uninit() -> Box> { let layout = alloc::Layout::new::>(); let ptr = unsafe { @@ -181,7 +181,7 @@ impl Box<[T]> { /// /// assert_eq!(*values, [1, 2, 3]) /// ``` - #[unstable(feature = "new_uninit", issue = "0")] + #[unstable(feature = "new_uninit", issue = "63291")] pub fn new_uninit_slice(len: usize) -> Box<[mem::MaybeUninit]> { let layout = alloc::Layout::array::>(len).unwrap(); let ptr = unsafe { alloc::alloc(layout) }; @@ -220,7 +220,7 @@ impl Box> { /// /// assert_eq!(*five, 5) /// ``` - #[unstable(feature = "new_uninit", issue = "0")] + #[unstable(feature = "new_uninit", issue = "63291")] #[inline] pub unsafe fn assume_init(self) -> Box { Box(Box::into_unique(self).cast()) @@ -258,7 +258,7 @@ impl Box<[mem::MaybeUninit]> { /// /// assert_eq!(*values, [1, 2, 3]) /// ``` - #[unstable(feature = "new_uninit", issue = "0")] + #[unstable(feature = "new_uninit", issue = "63291")] #[inline] pub unsafe fn assume_init(self) -> Box<[T]> { Box(Unique::new_unchecked(Box::into_raw(self) as _)) diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index c76c4b50c64..a9aa822173f 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -348,7 +348,7 @@ impl Rc { /// /// assert_eq!(*five, 5) /// ``` - #[unstable(feature = "new_uninit", issue = "0")] + #[unstable(feature = "new_uninit", issue = "63291")] pub fn new_uninit() -> Rc> { let layout = Layout::new::>>(); unsafe { @@ -438,7 +438,7 @@ impl Rc<[T]> { /// /// assert_eq!(*values, [1, 2, 3]) /// ``` - #[unstable(feature = "new_uninit", issue = "0")] + #[unstable(feature = "new_uninit", issue = "63291")] pub fn new_uninit_slice(len: usize) -> Rc<[mem::MaybeUninit]> { let data_layout = Layout::array::>(len).unwrap(); let (layout, offset) = Layout::new::>().extend(data_layout).unwrap(); @@ -492,7 +492,7 @@ impl Rc> { /// /// assert_eq!(*five, 5) /// ``` - #[unstable(feature = "new_uninit", issue = "0")] + #[unstable(feature = "new_uninit", issue = "63291")] #[inline] pub unsafe fn assume_init(self) -> Rc { Rc { @@ -536,7 +536,7 @@ impl Rc<[mem::MaybeUninit]> { /// /// assert_eq!(*values, [1, 2, 3]) /// ``` - #[unstable(feature = "new_uninit", issue = "0")] + #[unstable(feature = "new_uninit", issue = "63291")] #[inline] pub unsafe fn assume_init(self) -> Rc<[T]> { Rc { @@ -762,7 +762,7 @@ impl Rc { /// assert_eq!(*x, "foo"); /// ``` #[inline] - #[unstable(feature = "get_mut_unchecked", issue = "0")] + #[unstable(feature = "get_mut_unchecked", issue = "63292")] pub unsafe fn get_mut_unchecked(this: &mut Self) -> &mut T { &mut this.ptr.as_mut().value } diff --git a/src/liballoc/sync.rs b/src/liballoc/sync.rs index bc3acd27e8b..50fd9850893 100644 --- a/src/liballoc/sync.rs +++ b/src/liballoc/sync.rs @@ -332,7 +332,7 @@ impl Arc { /// /// assert_eq!(*five, 5) /// ``` - #[unstable(feature = "new_uninit", issue = "0")] + #[unstable(feature = "new_uninit", issue = "63291")] pub fn new_uninit() -> Arc> { let layout = Layout::new::>>(); unsafe { @@ -422,7 +422,7 @@ impl Arc<[T]> { /// /// assert_eq!(*values, [1, 2, 3]) /// ``` - #[unstable(feature = "new_uninit", issue = "0")] + #[unstable(feature = "new_uninit", issue = "63291")] pub fn new_uninit_slice(len: usize) -> Arc<[mem::MaybeUninit]> { let data_layout = Layout::array::>(len).unwrap(); let (layout, offset) = Layout::new::>().extend(data_layout).unwrap(); @@ -476,7 +476,7 @@ impl Arc> { /// /// assert_eq!(*five, 5) /// ``` - #[unstable(feature = "new_uninit", issue = "0")] + #[unstable(feature = "new_uninit", issue = "63291")] #[inline] pub unsafe fn assume_init(self) -> Arc { Arc { @@ -520,7 +520,7 @@ impl Arc<[mem::MaybeUninit]> { /// /// assert_eq!(*values, [1, 2, 3]) /// ``` - #[unstable(feature = "new_uninit", issue = "0")] + #[unstable(feature = "new_uninit", issue = "63291")] #[inline] pub unsafe fn assume_init(self) -> Arc<[T]> { Arc { @@ -1147,7 +1147,7 @@ impl Arc { /// assert_eq!(*x, "foo"); /// ``` #[inline] - #[unstable(feature = "get_mut_unchecked", issue = "0")] + #[unstable(feature = "get_mut_unchecked", issue = "63292")] pub unsafe fn get_mut_unchecked(this: &mut Self) -> &mut T { &mut this.ptr.as_mut().data } -- cgit 1.4.1-3-g733a5 From ae1e201a0cd37a48bd3dabf1c643ccd5f53f7680 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Mon, 5 Aug 2019 17:50:44 +0200 Subject: Add a comment on the usage of Layout::new::>() --- src/liballoc/rc.rs | 2 ++ src/liballoc/sync.rs | 2 ++ 2 files changed, 4 insertions(+) (limited to 'src/liballoc') diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index a9aa822173f..9d608c886b8 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -441,6 +441,8 @@ impl Rc<[T]> { #[unstable(feature = "new_uninit", issue = "63291")] pub fn new_uninit_slice(len: usize) -> Rc<[mem::MaybeUninit]> { let data_layout = Layout::array::>(len).unwrap(); + // This relies on `value` being the last field of `RcBox` in memory, + // so that the layout of `RcBox` is the same as that of `RcBox<()>` followed by `T`. let (layout, offset) = Layout::new::>().extend(data_layout).unwrap(); unsafe { let allocated_ptr = Global.alloc(layout) diff --git a/src/liballoc/sync.rs b/src/liballoc/sync.rs index 50fd9850893..1bd177be0d5 100644 --- a/src/liballoc/sync.rs +++ b/src/liballoc/sync.rs @@ -425,6 +425,8 @@ impl Arc<[T]> { #[unstable(feature = "new_uninit", issue = "63291")] pub fn new_uninit_slice(len: usize) -> Arc<[mem::MaybeUninit]> { let data_layout = Layout::array::>(len).unwrap(); + // This relies on `value` being the last field of `RcBox` in memory, + // so that the layout of `RcBox` is the same as that of `RcBox<()>` followed by `T`. let (layout, offset) = Layout::new::>().extend(data_layout).unwrap(); unsafe { let allocated_ptr = Global.alloc(layout) -- cgit 1.4.1-3-g733a5 From 810dfd7cd451c370d0c47d6aa9e62b16292a29e0 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Fri, 16 Aug 2019 17:44:24 +0200 Subject: Reuse more internal Rc and Arc methods --- src/liballoc/rc.rs | 42 +++++++----------------------------------- src/liballoc/sync.rs | 42 +++++++----------------------------------- 2 files changed, 14 insertions(+), 70 deletions(-) (limited to 'src/liballoc') diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index 9d608c886b8..abb09684260 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -350,17 +350,11 @@ impl Rc { /// ``` #[unstable(feature = "new_uninit", issue = "63291")] pub fn new_uninit() -> Rc> { - let layout = Layout::new::>>(); unsafe { - let mut ptr = Global.alloc(layout) - .unwrap_or_else(|_| handle_alloc_error(layout)) - .cast::>>(); - ptr::write(&mut ptr.as_mut().strong, Cell::new(1)); - ptr::write(&mut ptr.as_mut().weak, Cell::new(1)); - Rc { - ptr, - phantom: PhantomData, - } + Rc::from_ptr(Rc::allocate_for_unsized( + Layout::new::(), + |mem| mem as *mut RcBox>, + )) } } @@ -440,24 +434,8 @@ impl Rc<[T]> { /// ``` #[unstable(feature = "new_uninit", issue = "63291")] pub fn new_uninit_slice(len: usize) -> Rc<[mem::MaybeUninit]> { - let data_layout = Layout::array::>(len).unwrap(); - // This relies on `value` being the last field of `RcBox` in memory, - // so that the layout of `RcBox` is the same as that of `RcBox<()>` followed by `T`. - let (layout, offset) = Layout::new::>().extend(data_layout).unwrap(); unsafe { - let allocated_ptr = Global.alloc(layout) - .unwrap_or_else(|_| handle_alloc_error(layout)) - .as_ptr(); - let data_ptr = allocated_ptr.add(offset) as *mut mem::MaybeUninit; - let slice: *mut [mem::MaybeUninit] = from_raw_parts_mut(data_ptr, len); - let wide_ptr = slice as *mut RcBox<[mem::MaybeUninit]>; - let wide_ptr = set_data_ptr(wide_ptr, allocated_ptr); - ptr::write(&mut (*wide_ptr).strong, Cell::new(1)); - ptr::write(&mut (*wide_ptr).weak, Cell::new(1)); - Rc { - ptr: NonNull::new_unchecked(wide_ptr), - phantom: PhantomData, - } + Rc::from_ptr(Rc::allocate_for_slice(len)) } } } @@ -497,10 +475,7 @@ impl Rc> { #[unstable(feature = "new_uninit", issue = "63291")] #[inline] pub unsafe fn assume_init(self) -> Rc { - Rc { - ptr: mem::ManuallyDrop::new(self).ptr.cast(), - phantom: PhantomData, - } + Rc::from_inner(mem::ManuallyDrop::new(self).ptr.cast()) } } @@ -541,10 +516,7 @@ impl Rc<[mem::MaybeUninit]> { #[unstable(feature = "new_uninit", issue = "63291")] #[inline] pub unsafe fn assume_init(self) -> Rc<[T]> { - Rc { - ptr: NonNull::new_unchecked(mem::ManuallyDrop::new(self).ptr.as_ptr() as _), - phantom: PhantomData, - } + Rc::from_ptr(mem::ManuallyDrop::new(self).ptr.as_ptr() as _) } } diff --git a/src/liballoc/sync.rs b/src/liballoc/sync.rs index 1bd177be0d5..bb4d1499014 100644 --- a/src/liballoc/sync.rs +++ b/src/liballoc/sync.rs @@ -334,17 +334,11 @@ impl Arc { /// ``` #[unstable(feature = "new_uninit", issue = "63291")] pub fn new_uninit() -> Arc> { - let layout = Layout::new::>>(); unsafe { - let mut ptr = Global.alloc(layout) - .unwrap_or_else(|_| handle_alloc_error(layout)) - .cast::>>(); - ptr::write(&mut ptr.as_mut().strong, atomic::AtomicUsize::new(1)); - ptr::write(&mut ptr.as_mut().weak, atomic::AtomicUsize::new(1)); - Arc { - ptr, - phantom: PhantomData, - } + Arc::from_ptr(Arc::allocate_for_unsized( + Layout::new::(), + |mem| mem as *mut ArcInner>, + )) } } @@ -424,24 +418,8 @@ impl Arc<[T]> { /// ``` #[unstable(feature = "new_uninit", issue = "63291")] pub fn new_uninit_slice(len: usize) -> Arc<[mem::MaybeUninit]> { - let data_layout = Layout::array::>(len).unwrap(); - // This relies on `value` being the last field of `RcBox` in memory, - // so that the layout of `RcBox` is the same as that of `RcBox<()>` followed by `T`. - let (layout, offset) = Layout::new::>().extend(data_layout).unwrap(); unsafe { - let allocated_ptr = Global.alloc(layout) - .unwrap_or_else(|_| handle_alloc_error(layout)) - .as_ptr(); - let data_ptr = allocated_ptr.add(offset) as *mut mem::MaybeUninit; - let slice: *mut [mem::MaybeUninit] = from_raw_parts_mut(data_ptr, len); - let wide_ptr = slice as *mut ArcInner<[mem::MaybeUninit]>; - let wide_ptr = set_data_ptr(wide_ptr, allocated_ptr); - ptr::write(&mut (*wide_ptr).strong, atomic::AtomicUsize::new(1)); - ptr::write(&mut (*wide_ptr).weak, atomic::AtomicUsize::new(1)); - Arc { - ptr: NonNull::new_unchecked(wide_ptr), - phantom: PhantomData, - } + Arc::from_ptr(Arc::allocate_for_slice(len)) } } } @@ -481,10 +459,7 @@ impl Arc> { #[unstable(feature = "new_uninit", issue = "63291")] #[inline] pub unsafe fn assume_init(self) -> Arc { - Arc { - ptr: mem::ManuallyDrop::new(self).ptr.cast(), - phantom: PhantomData, - } + Arc::from_inner(mem::ManuallyDrop::new(self).ptr.cast()) } } @@ -525,10 +500,7 @@ impl Arc<[mem::MaybeUninit]> { #[unstable(feature = "new_uninit", issue = "63291")] #[inline] pub unsafe fn assume_init(self) -> Arc<[T]> { - Arc { - ptr: NonNull::new_unchecked(mem::ManuallyDrop::new(self).ptr.as_ptr() as _), - phantom: PhantomData, - } + Arc::from_ptr(mem::ManuallyDrop::new(self).ptr.as_ptr() as _) } } -- cgit 1.4.1-3-g733a5 From 7a641f7c516ac666c72bf9edfbec8648aa0160c7 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Fri, 16 Aug 2019 17:45:44 +0200 Subject: Relax the safety condition for get_mut_unchecked --- src/liballoc/rc.rs | 6 ++++-- src/liballoc/sync.rs | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) (limited to 'src/liballoc') diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index abb09684260..1509afa4889 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -719,8 +719,10 @@ impl Rc { /// /// # Safety /// - /// There must be no other `Rc` or [`Weak`] pointers to the same value. - /// This is the case for example immediately after `Rc::new`. + /// Any other `Rc` or [`Weak`] pointers to the same value must not be dereferenced + /// for the duration of the returned borrow. + /// This is trivially the case if no such pointer exist, + /// for example immediately after `Rc::new`. /// /// # Examples /// diff --git a/src/liballoc/sync.rs b/src/liballoc/sync.rs index bb4d1499014..078ddc27da9 100644 --- a/src/liballoc/sync.rs +++ b/src/liballoc/sync.rs @@ -1104,8 +1104,10 @@ impl Arc { /// /// # Safety /// - /// There must be no other `Arc` or [`Weak`] pointers to the same value. - /// This is the case for example immediately after `Rc::new`. + /// Any other `Arc` or [`Weak`] pointers to the same value must not be dereferenced + /// for the duration of the returned borrow. + /// This is trivially the case if no such pointer exist, + /// for example immediately after `Arc::new`. /// /// # Examples /// -- cgit 1.4.1-3-g733a5 From 36b18a1901e50ab526fd03b5f4713c283f2e2fb6 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Wed, 12 Jun 2019 20:02:01 +0200 Subject: Rename CollectionAllocError to TryReserveError --- src/liballoc/collections/mod.rs | 10 +++++----- src/liballoc/collections/vec_deque.rs | 16 ++++++++-------- src/liballoc/raw_vec.rs | 12 ++++++------ src/liballoc/string.rs | 14 +++++++------- src/liballoc/tests/string.rs | 2 +- src/liballoc/tests/vec.rs | 2 +- src/liballoc/tests/vec_deque.rs | 2 +- src/liballoc/vec.rs | 14 +++++++------- src/libstd/collections/hash/map.rs | 12 ++++++------ src/libstd/collections/hash/set.rs | 4 ++-- src/libstd/collections/mod.rs | 2 +- 11 files changed, 45 insertions(+), 45 deletions(-) (limited to 'src/liballoc') diff --git a/src/liballoc/collections/mod.rs b/src/liballoc/collections/mod.rs index 5a33ddc14f0..48c3ed8a57d 100644 --- a/src/liballoc/collections/mod.rs +++ b/src/liballoc/collections/mod.rs @@ -46,7 +46,7 @@ use crate::alloc::{AllocErr, LayoutErr}; /// Augments `AllocErr` with a CapacityOverflow variant. #[derive(Clone, PartialEq, Eq, Debug)] #[unstable(feature = "try_reserve", reason = "new API", issue="48043")] -pub enum CollectionAllocErr { +pub enum TryReserveError { /// Error due to the computed capacity exceeding the collection's maximum /// (usually `isize::MAX` bytes). CapacityOverflow, @@ -55,18 +55,18 @@ pub enum CollectionAllocErr { } #[unstable(feature = "try_reserve", reason = "new API", issue="48043")] -impl From for CollectionAllocErr { +impl From for TryReserveError { #[inline] fn from(AllocErr: AllocErr) -> Self { - CollectionAllocErr::AllocErr + TryReserveError::AllocErr } } #[unstable(feature = "try_reserve", reason = "new API", issue="48043")] -impl From for CollectionAllocErr { +impl From for TryReserveError { #[inline] fn from(_: LayoutErr) -> Self { - CollectionAllocErr::CapacityOverflow + TryReserveError::CapacityOverflow } } diff --git a/src/liballoc/collections/vec_deque.rs b/src/liballoc/collections/vec_deque.rs index 9240346ace9..2fc87413367 100644 --- a/src/liballoc/collections/vec_deque.rs +++ b/src/liballoc/collections/vec_deque.rs @@ -18,7 +18,7 @@ use core::ptr::{self, NonNull}; use core::slice; use core::hash::{Hash, Hasher}; -use crate::collections::CollectionAllocErr; +use crate::collections::TryReserveError; use crate::raw_vec::RawVec; use crate::vec::Vec; @@ -576,10 +576,10 @@ impl VecDeque { /// /// ``` /// #![feature(try_reserve)] - /// use std::collections::CollectionAllocErr; + /// use std::collections::TryReserveError; /// use std::collections::VecDeque; /// - /// fn process_data(data: &[u32]) -> Result, CollectionAllocErr> { + /// fn process_data(data: &[u32]) -> Result, TryReserveError> { /// let mut output = VecDeque::new(); /// /// // Pre-reserve the memory, exiting if we can't @@ -595,7 +595,7 @@ impl VecDeque { /// # process_data(&[1, 2, 3]).expect("why is the test harness OOMing on 12 bytes?"); /// ``` #[unstable(feature = "try_reserve", reason = "new API", issue="48043")] - pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), CollectionAllocErr> { + pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> { self.try_reserve(additional) } @@ -614,10 +614,10 @@ impl VecDeque { /// /// ``` /// #![feature(try_reserve)] - /// use std::collections::CollectionAllocErr; + /// use std::collections::TryReserveError; /// use std::collections::VecDeque; /// - /// fn process_data(data: &[u32]) -> Result, CollectionAllocErr> { + /// fn process_data(data: &[u32]) -> Result, TryReserveError> { /// let mut output = VecDeque::new(); /// /// // Pre-reserve the memory, exiting if we can't @@ -633,12 +633,12 @@ impl VecDeque { /// # process_data(&[1, 2, 3]).expect("why is the test harness OOMing on 12 bytes?"); /// ``` #[unstable(feature = "try_reserve", reason = "new API", issue="48043")] - pub fn try_reserve(&mut self, additional: usize) -> Result<(), CollectionAllocErr> { + pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> { let old_cap = self.cap(); let used_cap = self.len() + 1; let new_cap = used_cap.checked_add(additional) .and_then(|needed_cap| needed_cap.checked_next_power_of_two()) - .ok_or(CollectionAllocErr::CapacityOverflow)?; + .ok_or(TryReserveError::CapacityOverflow)?; if new_cap > old_cap { self.buf.try_reserve_exact(used_cap, new_cap - used_cap)?; diff --git a/src/liballoc/raw_vec.rs b/src/liballoc/raw_vec.rs index 0abab45e920..fed5a16599d 100644 --- a/src/liballoc/raw_vec.rs +++ b/src/liballoc/raw_vec.rs @@ -8,7 +8,7 @@ use core::ptr::{self, NonNull, Unique}; use core::slice; use crate::alloc::{Alloc, Layout, Global, handle_alloc_error}; -use crate::collections::CollectionAllocErr::{self, *}; +use crate::collections::TryReserveError::{self, *}; use crate::boxed::Box; #[cfg(test)] @@ -385,7 +385,7 @@ impl RawVec { /// The same as `reserve_exact`, but returns on errors instead of panicking or aborting. pub fn try_reserve_exact(&mut self, used_capacity: usize, needed_extra_capacity: usize) - -> Result<(), CollectionAllocErr> { + -> Result<(), TryReserveError> { self.reserve_internal(used_capacity, needed_extra_capacity, Fallible, Exact) } @@ -422,7 +422,7 @@ impl RawVec { /// needed_extra_capacity` elements. This logic is used in amortized reserve methods. /// Returns `(new_capacity, new_alloc_size)`. fn amortized_new_size(&self, used_capacity: usize, needed_extra_capacity: usize) - -> Result { + -> Result { // Nothing we can really do about these checks :( let required_cap = used_capacity.checked_add(needed_extra_capacity) @@ -435,7 +435,7 @@ impl RawVec { /// The same as `reserve`, but returns on errors instead of panicking or aborting. pub fn try_reserve(&mut self, used_capacity: usize, needed_extra_capacity: usize) - -> Result<(), CollectionAllocErr> { + -> Result<(), TryReserveError> { self.reserve_internal(used_capacity, needed_extra_capacity, Fallible, Amortized) } @@ -640,7 +640,7 @@ impl RawVec { needed_extra_capacity: usize, fallibility: Fallibility, strategy: ReserveStrategy, - ) -> Result<(), CollectionAllocErr> { + ) -> Result<(), TryReserveError> { unsafe { use crate::alloc::AllocErr; @@ -737,7 +737,7 @@ unsafe impl<#[may_dangle] T, A: Alloc> Drop for RawVec { // all 4GB in user-space. e.g., PAE or x32 #[inline] -fn alloc_guard(alloc_size: usize) -> Result<(), CollectionAllocErr> { +fn alloc_guard(alloc_size: usize) -> Result<(), TryReserveError> { if mem::size_of::() < 8 && alloc_size > core::isize::MAX as usize { Err(CapacityOverflow) } else { diff --git a/src/liballoc/string.rs b/src/liballoc/string.rs index eca726cd410..b65f191836e 100644 --- a/src/liballoc/string.rs +++ b/src/liballoc/string.rs @@ -56,7 +56,7 @@ use core::ptr; use core::str::{pattern::Pattern, lossy}; use crate::borrow::{Cow, ToOwned}; -use crate::collections::CollectionAllocErr; +use crate::collections::TryReserveError; use crate::boxed::Box; use crate::str::{self, from_boxed_utf8_unchecked, FromStr, Utf8Error, Chars}; use crate::vec::Vec; @@ -937,9 +937,9 @@ impl String { /// /// ``` /// #![feature(try_reserve)] - /// use std::collections::CollectionAllocErr; + /// use std::collections::TryReserveError; /// - /// fn process_data(data: &str) -> Result { + /// fn process_data(data: &str) -> Result { /// let mut output = String::new(); /// /// // Pre-reserve the memory, exiting if we can't @@ -953,7 +953,7 @@ impl String { /// # process_data("rust").expect("why is the test harness OOMing on 4 bytes?"); /// ``` #[unstable(feature = "try_reserve", reason = "new API", issue="48043")] - pub fn try_reserve(&mut self, additional: usize) -> Result<(), CollectionAllocErr> { + pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> { self.vec.try_reserve(additional) } @@ -975,9 +975,9 @@ impl String { /// /// ``` /// #![feature(try_reserve)] - /// use std::collections::CollectionAllocErr; + /// use std::collections::TryReserveError; /// - /// fn process_data(data: &str) -> Result { + /// fn process_data(data: &str) -> Result { /// let mut output = String::new(); /// /// // Pre-reserve the memory, exiting if we can't @@ -991,7 +991,7 @@ impl String { /// # process_data("rust").expect("why is the test harness OOMing on 4 bytes?"); /// ``` #[unstable(feature = "try_reserve", reason = "new API", issue="48043")] - pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), CollectionAllocErr> { + pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> { self.vec.try_reserve_exact(additional) } diff --git a/src/liballoc/tests/string.rs b/src/liballoc/tests/string.rs index 765210e5aa6..4d2ea42fcae 100644 --- a/src/liballoc/tests/string.rs +++ b/src/liballoc/tests/string.rs @@ -1,5 +1,5 @@ use std::borrow::Cow; -use std::collections::CollectionAllocErr::*; +use std::collections::TryReserveError::*; use std::mem::size_of; use std::{usize, isize}; diff --git a/src/liballoc/tests/vec.rs b/src/liballoc/tests/vec.rs index 6e8ffe18522..c67ec32543b 100644 --- a/src/liballoc/tests/vec.rs +++ b/src/liballoc/tests/vec.rs @@ -2,7 +2,7 @@ use std::borrow::Cow; use std::mem::size_of; use std::{usize, isize}; use std::vec::{Drain, IntoIter}; -use std::collections::CollectionAllocErr::*; +use std::collections::TryReserveError::*; struct DropCounter<'a> { count: &'a mut u32, diff --git a/src/liballoc/tests/vec_deque.rs b/src/liballoc/tests/vec_deque.rs index 1bbcca97b3c..c7e743c2e94 100644 --- a/src/liballoc/tests/vec_deque.rs +++ b/src/liballoc/tests/vec_deque.rs @@ -1,6 +1,6 @@ use std::fmt::Debug; use std::collections::{VecDeque, vec_deque::Drain}; -use std::collections::CollectionAllocErr::*; +use std::collections::TryReserveError::*; use std::mem::size_of; use std::{usize, isize}; diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs index dac04e4e624..d2798955c46 100644 --- a/src/liballoc/vec.rs +++ b/src/liballoc/vec.rs @@ -70,7 +70,7 @@ use core::ptr::{self, NonNull}; use core::slice::{self, SliceIndex}; use crate::borrow::{ToOwned, Cow}; -use crate::collections::CollectionAllocErr; +use crate::collections::TryReserveError; use crate::boxed::Box; use crate::raw_vec::RawVec; @@ -498,9 +498,9 @@ impl Vec { /// /// ``` /// #![feature(try_reserve)] - /// use std::collections::CollectionAllocErr; + /// use std::collections::TryReserveError; /// - /// fn process_data(data: &[u32]) -> Result, CollectionAllocErr> { + /// fn process_data(data: &[u32]) -> Result, TryReserveError> { /// let mut output = Vec::new(); /// /// // Pre-reserve the memory, exiting if we can't @@ -516,7 +516,7 @@ impl Vec { /// # process_data(&[1, 2, 3]).expect("why is the test harness OOMing on 12 bytes?"); /// ``` #[unstable(feature = "try_reserve", reason = "new API", issue="48043")] - pub fn try_reserve(&mut self, additional: usize) -> Result<(), CollectionAllocErr> { + pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> { self.buf.try_reserve(self.len, additional) } @@ -538,9 +538,9 @@ impl Vec { /// /// ``` /// #![feature(try_reserve)] - /// use std::collections::CollectionAllocErr; + /// use std::collections::TryReserveError; /// - /// fn process_data(data: &[u32]) -> Result, CollectionAllocErr> { + /// fn process_data(data: &[u32]) -> Result, TryReserveError> { /// let mut output = Vec::new(); /// /// // Pre-reserve the memory, exiting if we can't @@ -556,7 +556,7 @@ impl Vec { /// # process_data(&[1, 2, 3]).expect("why is the test harness OOMing on 12 bytes?"); /// ``` #[unstable(feature = "try_reserve", reason = "new API", issue="48043")] - pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), CollectionAllocErr> { + pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> { self.buf.try_reserve_exact(self.len, additional) } diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index 1e28ee8da26..8581cf13b08 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -6,7 +6,7 @@ use hashbrown::hash_map as base; use crate::borrow::Borrow; use crate::cell::Cell; -use crate::collections::CollectionAllocErr; +use crate::collections::TryReserveError; use crate::fmt::{self, Debug}; #[allow(deprecated)] use crate::hash::{BuildHasher, Hash, Hasher, SipHasher13}; @@ -588,7 +588,7 @@ where /// ``` #[inline] #[unstable(feature = "try_reserve", reason = "new API", issue = "48043")] - pub fn try_reserve(&mut self, additional: usize) -> Result<(), CollectionAllocErr> { + pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> { self.base .try_reserve(additional) .map_err(map_collection_alloc_err) @@ -2542,10 +2542,10 @@ fn map_entry<'a, K: 'a, V: 'a>(raw: base::RustcEntry<'a, K, V>) -> Entry<'a, K, } #[inline] -fn map_collection_alloc_err(err: hashbrown::CollectionAllocErr) -> CollectionAllocErr { +fn map_collection_alloc_err(err: hashbrown::CollectionAllocErr) -> TryReserveError { match err { - hashbrown::CollectionAllocErr::CapacityOverflow => CollectionAllocErr::CapacityOverflow, - hashbrown::CollectionAllocErr::AllocErr => CollectionAllocErr::AllocErr, + hashbrown::CollectionAllocErr::CapacityOverflow => TryReserveError::CapacityOverflow, + hashbrown::CollectionAllocErr::AllocErr => TryReserveError::AllocErr, } } @@ -2605,7 +2605,7 @@ mod test_map { use super::RandomState; use crate::cell::RefCell; use rand::{thread_rng, Rng}; - use realstd::collections::CollectionAllocErr::*; + use realstd::collections::TryReserveError::*; use realstd::usize; // https://github.com/rust-lang/rust/issues/62301 diff --git a/src/libstd/collections/hash/set.rs b/src/libstd/collections/hash/set.rs index d243412405a..26db651ef89 100644 --- a/src/libstd/collections/hash/set.rs +++ b/src/libstd/collections/hash/set.rs @@ -1,5 +1,5 @@ use crate::borrow::Borrow; -use crate::collections::CollectionAllocErr; +use crate::collections::TryReserveError; use crate::fmt; use crate::hash::{Hash, BuildHasher}; use crate::iter::{Chain, FromIterator, FusedIterator}; @@ -383,7 +383,7 @@ impl HashSet /// ``` #[inline] #[unstable(feature = "try_reserve", reason = "new API", issue="48043")] - pub fn try_reserve(&mut self, additional: usize) -> Result<(), CollectionAllocErr> { + pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> { self.map.try_reserve(additional) } diff --git a/src/libstd/collections/mod.rs b/src/libstd/collections/mod.rs index 15c2532f8b4..f5957466be8 100644 --- a/src/libstd/collections/mod.rs +++ b/src/libstd/collections/mod.rs @@ -427,7 +427,7 @@ pub use self::hash_map::HashMap; pub use self::hash_set::HashSet; #[unstable(feature = "try_reserve", reason = "new API", issue="48043")] -pub use alloc_crate::collections::CollectionAllocErr; +pub use alloc_crate::collections::TryReserveError; mod hash; -- cgit 1.4.1-3-g733a5 From 59a340963fa5d8b5507d95cd015f7ca2855ba151 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Wed, 12 Jun 2019 18:47:58 +0200 Subject: Add the Layout of the failed allocation to TryReserveError::AllocError MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit … and add a separately-unstable field to force non-exhaustive matching (`#[non_exhaustive]` is no implemented yet on enum variants) so that we have the option to later expose the allocator’s error value. CC https://github.com/rust-lang/wg-allocators/issues/23 --- src/liballoc/collections/mod.rs | 25 ++++++++++++++----------- src/liballoc/lib.rs | 1 + src/liballoc/raw_vec.rs | 20 +++++++++++--------- src/liballoc/tests/string.rs | 12 ++++++------ src/liballoc/tests/vec.rs | 16 ++++++++-------- src/liballoc/tests/vec_deque.rs | 12 ++++++------ src/libstd/collections/hash/map.rs | 7 +++++-- src/libstd/lib.rs | 1 + 8 files changed, 52 insertions(+), 42 deletions(-) (limited to 'src/liballoc') diff --git a/src/liballoc/collections/mod.rs b/src/liballoc/collections/mod.rs index 48c3ed8a57d..f1f22fe48c5 100644 --- a/src/liballoc/collections/mod.rs +++ b/src/liballoc/collections/mod.rs @@ -41,25 +41,28 @@ pub use linked_list::LinkedList; #[doc(no_inline)] pub use vec_deque::VecDeque; -use crate::alloc::{AllocErr, LayoutErr}; +use crate::alloc::{Layout, LayoutErr}; -/// Augments `AllocErr` with a CapacityOverflow variant. +/// The error type for `try_reserve` methods. #[derive(Clone, PartialEq, Eq, Debug)] #[unstable(feature = "try_reserve", reason = "new API", issue="48043")] pub enum TryReserveError { /// Error due to the computed capacity exceeding the collection's maximum /// (usually `isize::MAX` bytes). CapacityOverflow, - /// Error due to the allocator (see the `AllocErr` type's docs). - AllocErr, -} -#[unstable(feature = "try_reserve", reason = "new API", issue="48043")] -impl From for TryReserveError { - #[inline] - fn from(AllocErr: AllocErr) -> Self { - TryReserveError::AllocErr - } + /// The memory allocator returned an error + AllocError { + /// The layout of allocation request that failed + layout: Layout, + + #[doc(hidden)] + #[unstable(feature = "container_error_extra", issue = "0", reason = "\ + Enable exposing the allocator’s custom error value \ + if an associated type is added in the future: \ + https://github.com/rust-lang/wg-allocators/issues/23")] + non_exhaustive: (), + }, } #[unstable(feature = "try_reserve", reason = "new API", issue="48043")] diff --git a/src/liballoc/lib.rs b/src/liballoc/lib.rs index 7421beddd95..4a48945adc3 100644 --- a/src/liballoc/lib.rs +++ b/src/liballoc/lib.rs @@ -87,6 +87,7 @@ #![feature(const_in_array_repeat_expressions)] #![feature(dispatch_from_dyn)] #![feature(core_intrinsics)] +#![feature(container_error_extra)] #![feature(dropck_eyepatch)] #![feature(exact_size_is_empty)] #![feature(fmt_internals)] diff --git a/src/liballoc/raw_vec.rs b/src/liballoc/raw_vec.rs index fed5a16599d..bc8a38f6b3a 100644 --- a/src/liballoc/raw_vec.rs +++ b/src/liballoc/raw_vec.rs @@ -7,7 +7,7 @@ use core::ops::Drop; use core::ptr::{self, NonNull, Unique}; use core::slice; -use crate::alloc::{Alloc, Layout, Global, handle_alloc_error}; +use crate::alloc::{Alloc, Layout, Global, AllocErr, handle_alloc_error}; use crate::collections::TryReserveError::{self, *}; use crate::boxed::Box; @@ -413,7 +413,7 @@ impl RawVec { pub fn reserve_exact(&mut self, used_capacity: usize, needed_extra_capacity: usize) { match self.reserve_internal(used_capacity, needed_extra_capacity, Infallible, Exact) { Err(CapacityOverflow) => capacity_overflow(), - Err(AllocErr) => unreachable!(), + Err(AllocError { .. }) => unreachable!(), Ok(()) => { /* yay */ } } } @@ -494,7 +494,7 @@ impl RawVec { pub fn reserve(&mut self, used_capacity: usize, needed_extra_capacity: usize) { match self.reserve_internal(used_capacity, needed_extra_capacity, Infallible, Amortized) { Err(CapacityOverflow) => capacity_overflow(), - Err(AllocErr) => unreachable!(), + Err(AllocError { .. }) => unreachable!(), Ok(()) => { /* yay */ } } } @@ -642,8 +642,6 @@ impl RawVec { strategy: ReserveStrategy, ) -> Result<(), TryReserveError> { unsafe { - use crate::alloc::AllocErr; - // NOTE: we don't early branch on ZSTs here because we want this // to actually catch "asking for more than usize::MAX" in that case. // If we make it past the first branch then we are guaranteed to @@ -672,12 +670,16 @@ impl RawVec { None => self.a.alloc(new_layout), }; - match (&res, fallibility) { + let ptr = match (res, fallibility) { (Err(AllocErr), Infallible) => handle_alloc_error(new_layout), - _ => {} - } + (Err(AllocErr), Fallible) => return Err(TryReserveError::AllocError { + layout: new_layout, + non_exhaustive: (), + }), + (Ok(ptr), _) => ptr, + }; - self.ptr = res?.cast().into(); + self.ptr = ptr.cast().into(); self.cap = new_cap; Ok(()) diff --git a/src/liballoc/tests/string.rs b/src/liballoc/tests/string.rs index 4d2ea42fcae..55edf56345b 100644 --- a/src/liballoc/tests/string.rs +++ b/src/liballoc/tests/string.rs @@ -566,11 +566,11 @@ fn test_try_reserve() { } else { panic!("usize::MAX should trigger an overflow!") } } else { // Check isize::MAX + 1 is an OOM - if let Err(AllocErr) = empty_string.try_reserve(MAX_CAP + 1) { + if let Err(AllocError { .. }) = empty_string.try_reserve(MAX_CAP + 1) { } else { panic!("isize::MAX + 1 should trigger an OOM!") } // Check usize::MAX is an OOM - if let Err(AllocErr) = empty_string.try_reserve(MAX_USIZE) { + if let Err(AllocError { .. }) = empty_string.try_reserve(MAX_USIZE) { } else { panic!("usize::MAX should trigger an OOM!") } } } @@ -590,7 +590,7 @@ fn test_try_reserve() { if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_CAP - 9) { } else { panic!("isize::MAX + 1 should trigger an overflow!"); } } else { - if let Err(AllocErr) = ten_bytes.try_reserve(MAX_CAP - 9) { + if let Err(AllocError { .. }) = ten_bytes.try_reserve(MAX_CAP - 9) { } else { panic!("isize::MAX + 1 should trigger an OOM!") } } // Should always overflow in the add-to-len @@ -629,10 +629,10 @@ fn test_try_reserve_exact() { if let Err(CapacityOverflow) = empty_string.try_reserve_exact(MAX_USIZE) { } else { panic!("usize::MAX should trigger an overflow!") } } else { - if let Err(AllocErr) = empty_string.try_reserve_exact(MAX_CAP + 1) { + if let Err(AllocError { .. }) = empty_string.try_reserve_exact(MAX_CAP + 1) { } else { panic!("isize::MAX + 1 should trigger an OOM!") } - if let Err(AllocErr) = empty_string.try_reserve_exact(MAX_USIZE) { + if let Err(AllocError { .. }) = empty_string.try_reserve_exact(MAX_USIZE) { } else { panic!("usize::MAX should trigger an OOM!") } } } @@ -651,7 +651,7 @@ fn test_try_reserve_exact() { if let Err(CapacityOverflow) = ten_bytes.try_reserve_exact(MAX_CAP - 9) { } else { panic!("isize::MAX + 1 should trigger an overflow!"); } } else { - if let Err(AllocErr) = ten_bytes.try_reserve_exact(MAX_CAP - 9) { + if let Err(AllocError { .. }) = ten_bytes.try_reserve_exact(MAX_CAP - 9) { } else { panic!("isize::MAX + 1 should trigger an OOM!") } } if let Err(CapacityOverflow) = ten_bytes.try_reserve_exact(MAX_USIZE) { diff --git a/src/liballoc/tests/vec.rs b/src/liballoc/tests/vec.rs index c67ec32543b..29a22aa0315 100644 --- a/src/liballoc/tests/vec.rs +++ b/src/liballoc/tests/vec.rs @@ -1121,11 +1121,11 @@ fn test_try_reserve() { } else { panic!("usize::MAX should trigger an overflow!") } } else { // Check isize::MAX + 1 is an OOM - if let Err(AllocErr) = empty_bytes.try_reserve(MAX_CAP + 1) { + if let Err(AllocError { .. }) = empty_bytes.try_reserve(MAX_CAP + 1) { } else { panic!("isize::MAX + 1 should trigger an OOM!") } // Check usize::MAX is an OOM - if let Err(AllocErr) = empty_bytes.try_reserve(MAX_USIZE) { + if let Err(AllocError { .. }) = empty_bytes.try_reserve(MAX_USIZE) { } else { panic!("usize::MAX should trigger an OOM!") } } } @@ -1145,7 +1145,7 @@ fn test_try_reserve() { if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_CAP - 9) { } else { panic!("isize::MAX + 1 should trigger an overflow!"); } } else { - if let Err(AllocErr) = ten_bytes.try_reserve(MAX_CAP - 9) { + if let Err(AllocError { .. }) = ten_bytes.try_reserve(MAX_CAP - 9) { } else { panic!("isize::MAX + 1 should trigger an OOM!") } } // Should always overflow in the add-to-len @@ -1168,7 +1168,7 @@ fn test_try_reserve() { if let Err(CapacityOverflow) = ten_u32s.try_reserve(MAX_CAP/4 - 9) { } else { panic!("isize::MAX + 1 should trigger an overflow!"); } } else { - if let Err(AllocErr) = ten_u32s.try_reserve(MAX_CAP/4 - 9) { + if let Err(AllocError { .. }) = ten_u32s.try_reserve(MAX_CAP/4 - 9) { } else { panic!("isize::MAX + 1 should trigger an OOM!") } } // Should fail in the mul-by-size @@ -1209,10 +1209,10 @@ fn test_try_reserve_exact() { if let Err(CapacityOverflow) = empty_bytes.try_reserve_exact(MAX_USIZE) { } else { panic!("usize::MAX should trigger an overflow!") } } else { - if let Err(AllocErr) = empty_bytes.try_reserve_exact(MAX_CAP + 1) { + if let Err(AllocError { .. }) = empty_bytes.try_reserve_exact(MAX_CAP + 1) { } else { panic!("isize::MAX + 1 should trigger an OOM!") } - if let Err(AllocErr) = empty_bytes.try_reserve_exact(MAX_USIZE) { + if let Err(AllocError { .. }) = empty_bytes.try_reserve_exact(MAX_USIZE) { } else { panic!("usize::MAX should trigger an OOM!") } } } @@ -1231,7 +1231,7 @@ fn test_try_reserve_exact() { if let Err(CapacityOverflow) = ten_bytes.try_reserve_exact(MAX_CAP - 9) { } else { panic!("isize::MAX + 1 should trigger an overflow!"); } } else { - if let Err(AllocErr) = ten_bytes.try_reserve_exact(MAX_CAP - 9) { + if let Err(AllocError { .. }) = ten_bytes.try_reserve_exact(MAX_CAP - 9) { } else { panic!("isize::MAX + 1 should trigger an OOM!") } } if let Err(CapacityOverflow) = ten_bytes.try_reserve_exact(MAX_USIZE) { @@ -1252,7 +1252,7 @@ fn test_try_reserve_exact() { if let Err(CapacityOverflow) = ten_u32s.try_reserve_exact(MAX_CAP/4 - 9) { } else { panic!("isize::MAX + 1 should trigger an overflow!"); } } else { - if let Err(AllocErr) = ten_u32s.try_reserve_exact(MAX_CAP/4 - 9) { + if let Err(AllocError { .. }) = ten_u32s.try_reserve_exact(MAX_CAP/4 - 9) { } else { panic!("isize::MAX + 1 should trigger an OOM!") } } if let Err(CapacityOverflow) = ten_u32s.try_reserve_exact(MAX_USIZE - 20) { diff --git a/src/liballoc/tests/vec_deque.rs b/src/liballoc/tests/vec_deque.rs index c7e743c2e94..d49b553fc02 100644 --- a/src/liballoc/tests/vec_deque.rs +++ b/src/liballoc/tests/vec_deque.rs @@ -1168,7 +1168,7 @@ fn test_try_reserve() { // VecDeque starts with capacity 7, always adds 1 to the capacity // and also rounds the number to next power of 2 so this is the // furthest we can go without triggering CapacityOverflow - if let Err(AllocErr) = empty_bytes.try_reserve(MAX_CAP) { + if let Err(AllocError { .. }) = empty_bytes.try_reserve(MAX_CAP) { } else { panic!("isize::MAX + 1 should trigger an OOM!") } } } @@ -1188,7 +1188,7 @@ fn test_try_reserve() { if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_CAP - 9) { } else { panic!("isize::MAX + 1 should trigger an overflow!"); } } else { - if let Err(AllocErr) = ten_bytes.try_reserve(MAX_CAP - 9) { + if let Err(AllocError { .. }) = ten_bytes.try_reserve(MAX_CAP - 9) { } else { panic!("isize::MAX + 1 should trigger an OOM!") } } // Should always overflow in the add-to-len @@ -1211,7 +1211,7 @@ fn test_try_reserve() { if let Err(CapacityOverflow) = ten_u32s.try_reserve(MAX_CAP/4 - 9) { } else { panic!("isize::MAX + 1 should trigger an overflow!"); } } else { - if let Err(AllocErr) = ten_u32s.try_reserve(MAX_CAP/4 - 9) { + if let Err(AllocError { .. }) = ten_u32s.try_reserve(MAX_CAP/4 - 9) { } else { panic!("isize::MAX + 1 should trigger an OOM!") } } // Should fail in the mul-by-size @@ -1256,7 +1256,7 @@ fn test_try_reserve_exact() { // VecDeque starts with capacity 7, always adds 1 to the capacity // and also rounds the number to next power of 2 so this is the // furthest we can go without triggering CapacityOverflow - if let Err(AllocErr) = empty_bytes.try_reserve_exact(MAX_CAP) { + if let Err(AllocError { .. }) = empty_bytes.try_reserve_exact(MAX_CAP) { } else { panic!("isize::MAX + 1 should trigger an OOM!") } } } @@ -1275,7 +1275,7 @@ fn test_try_reserve_exact() { if let Err(CapacityOverflow) = ten_bytes.try_reserve_exact(MAX_CAP - 9) { } else { panic!("isize::MAX + 1 should trigger an overflow!"); } } else { - if let Err(AllocErr) = ten_bytes.try_reserve_exact(MAX_CAP - 9) { + if let Err(AllocError { .. }) = ten_bytes.try_reserve_exact(MAX_CAP - 9) { } else { panic!("isize::MAX + 1 should trigger an OOM!") } } if let Err(CapacityOverflow) = ten_bytes.try_reserve_exact(MAX_USIZE) { @@ -1296,7 +1296,7 @@ fn test_try_reserve_exact() { if let Err(CapacityOverflow) = ten_u32s.try_reserve_exact(MAX_CAP/4 - 9) { } else { panic!("isize::MAX + 1 should trigger an overflow!"); } } else { - if let Err(AllocErr) = ten_u32s.try_reserve_exact(MAX_CAP/4 - 9) { + if let Err(AllocError { .. }) = ten_u32s.try_reserve_exact(MAX_CAP/4 - 9) { } else { panic!("isize::MAX + 1 should trigger an OOM!") } } if let Err(CapacityOverflow) = ten_u32s.try_reserve_exact(MAX_USIZE - 20) { diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index db637a108cb..a0538986a22 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -2545,7 +2545,10 @@ fn map_entry<'a, K: 'a, V: 'a>(raw: base::RustcEntry<'a, K, V>) -> Entry<'a, K, fn map_collection_alloc_err(err: hashbrown::CollectionAllocErr) -> TryReserveError { match err { hashbrown::CollectionAllocErr::CapacityOverflow => TryReserveError::CapacityOverflow, - hashbrown::CollectionAllocErr::AllocErr { .. } => TryReserveError::AllocErr, + hashbrown::CollectionAllocErr::AllocErr { layout } => TryReserveError::AllocError { + layout, + non_exhaustive: (), + }, } } @@ -3405,7 +3408,7 @@ mod test_map { panic!("usize::MAX should trigger an overflow!"); } - if let Err(AllocErr) = empty_bytes.try_reserve(MAX_USIZE / 8) { + if let Err(AllocError { .. }) = empty_bytes.try_reserve(MAX_USIZE / 8) { } else { panic!("usize::MAX / 8 should trigger an OOM!") } diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index 1f48315d3f8..760d92f1d7b 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -251,6 +251,7 @@ #![feature(concat_idents)] #![feature(const_cstr_unchecked)] #![feature(const_raw_ptr_deref)] +#![feature(container_error_extra)] #![feature(core_intrinsics)] #![feature(custom_test_frameworks)] #![feature(doc_alias)] -- cgit 1.4.1-3-g733a5 From ba0328327c8ce94f7bf380223d655c70de584e93 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Sat, 17 Aug 2019 15:15:17 +0200 Subject: Doc nits Co-Authored-By: Ralf Jung --- src/liballoc/boxed.rs | 8 ++++---- src/liballoc/rc.rs | 10 +++++----- src/liballoc/sync.rs | 10 +++++----- src/libcore/ptr/unique.rs | 2 +- 4 files changed, 15 insertions(+), 15 deletions(-) (limited to 'src/liballoc') diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index e315b9f3417..c61e3183409 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -123,7 +123,7 @@ impl Box { box x } - /// Construct a new box with uninitialized contents. + /// Constructs a new box with uninitialized contents. /// /// # Examples /// @@ -161,7 +161,7 @@ impl Box { } impl Box<[T]> { - /// Construct a new boxed slice with uninitialized contents. + /// Constructs a new boxed slice with uninitialized contents. /// /// # Examples /// @@ -192,7 +192,7 @@ impl Box<[T]> { } impl Box> { - /// Convert to `Box`. + /// Converts to `Box`. /// /// # Safety /// @@ -228,7 +228,7 @@ impl Box> { } impl Box<[mem::MaybeUninit]> { - /// Convert to `Box<[T]>`. + /// Converts to `Box<[T]>`. /// /// # Safety /// diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index 1509afa4889..7183336ba53 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -327,7 +327,7 @@ impl Rc { })) } - /// Construct a new Rc with uninitialized contents. + /// Constructs a new `Rc` with uninitialized contents. /// /// # Examples /// @@ -409,7 +409,7 @@ impl Rc { } impl Rc<[T]> { - /// Construct a new reference-counted slice with uninitialized contents. + /// Constructs a new reference-counted slice with uninitialized contents. /// /// # Examples /// @@ -441,7 +441,7 @@ impl Rc<[T]> { } impl Rc> { - /// Convert to `Rc`. + /// Converts to `Rc`. /// /// # Safety /// @@ -480,7 +480,7 @@ impl Rc> { } impl Rc<[mem::MaybeUninit]> { - /// Convert to `Rc<[T]>`. + /// Converts to `Rc<[T]>`. /// /// # Safety /// @@ -721,7 +721,7 @@ impl Rc { /// /// Any other `Rc` or [`Weak`] pointers to the same value must not be dereferenced /// for the duration of the returned borrow. - /// This is trivially the case if no such pointer exist, + /// This is trivially the case if no such pointers exist, /// for example immediately after `Rc::new`. /// /// # Examples diff --git a/src/liballoc/sync.rs b/src/liballoc/sync.rs index 078ddc27da9..ffab842c769 100644 --- a/src/liballoc/sync.rs +++ b/src/liballoc/sync.rs @@ -311,7 +311,7 @@ impl Arc { Self::from_inner(Box::into_raw_non_null(x)) } - /// Construct a Arc box with uninitialized contents. + /// Constructs a new `Arc` with uninitialized contents. /// /// # Examples /// @@ -393,7 +393,7 @@ impl Arc { } impl Arc<[T]> { - /// Construct a new reference-counted slice with uninitialized contents. + /// Constructs a new reference-counted slice with uninitialized contents. /// /// # Examples /// @@ -425,7 +425,7 @@ impl Arc<[T]> { } impl Arc> { - /// Convert to `Arc`. + /// Converts to `Arc`. /// /// # Safety /// @@ -464,7 +464,7 @@ impl Arc> { } impl Arc<[mem::MaybeUninit]> { - /// Convert to `Arc<[T]>`. + /// Converts to `Arc<[T]>`. /// /// # Safety /// @@ -1106,7 +1106,7 @@ impl Arc { /// /// Any other `Arc` or [`Weak`] pointers to the same value must not be dereferenced /// for the duration of the returned borrow. - /// This is trivially the case if no such pointer exist, + /// This is trivially the case if no such pointers exist, /// for example immediately after `Arc::new`. /// /// # Examples diff --git a/src/libcore/ptr/unique.rs b/src/libcore/ptr/unique.rs index a739f3b6022..24f45394a3b 100644 --- a/src/libcore/ptr/unique.rs +++ b/src/libcore/ptr/unique.rs @@ -123,7 +123,7 @@ impl Unique { &mut *self.as_ptr() } - /// Cast to a pointer of another type + /// Casts to a pointer of another type #[inline] pub const fn cast(self) -> Unique { unsafe { -- cgit 1.4.1-3-g733a5 From b79ce1b1b10d6de1dac516d5e129f8251725ebe2 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Sat, 17 Aug 2019 15:39:21 +0200 Subject: Rename private helper method allocate_for_unsized to allocate_for_layout --- src/liballoc/rc.rs | 10 +++++----- src/liballoc/sync.rs | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) (limited to 'src/liballoc') diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index 7183336ba53..2b222caf13f 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -351,7 +351,7 @@ impl Rc { #[unstable(feature = "new_uninit", issue = "63291")] pub fn new_uninit() -> Rc> { unsafe { - Rc::from_ptr(Rc::allocate_for_unsized( + Rc::from_ptr(Rc::allocate_for_layout( Layout::new::(), |mem| mem as *mut RcBox>, )) @@ -880,11 +880,11 @@ impl Rc { impl Rc { /// Allocates an `RcBox` with sufficient space for - /// an unsized value where the value has the layout provided. + /// a possibly-unsized value where the value has the layout provided. /// /// The function `mem_to_rcbox` is called with the data pointer /// and must return back a (potentially fat)-pointer for the `RcBox`. - unsafe fn allocate_for_unsized( + unsafe fn allocate_for_layout( value_layout: Layout, mem_to_rcbox: impl FnOnce(*mut u8) -> *mut RcBox ) -> *mut RcBox { @@ -913,7 +913,7 @@ impl Rc { /// Allocates an `RcBox` with sufficient space for an unsized value unsafe fn allocate_for_ptr(ptr: *const T) -> *mut RcBox { // Allocate for the `RcBox` using the given value. - Self::allocate_for_unsized( + Self::allocate_for_layout( Layout::for_value(&*ptr), |mem| set_data_ptr(ptr as *mut T, mem) as *mut RcBox, ) @@ -944,7 +944,7 @@ impl Rc { impl Rc<[T]> { /// Allocates an `RcBox<[T]>` with the given length. unsafe fn allocate_for_slice(len: usize) -> *mut RcBox<[T]> { - Self::allocate_for_unsized( + Self::allocate_for_layout( Layout::array::(len).unwrap(), |mem| ptr::slice_from_raw_parts_mut(mem as *mut T, len) as *mut RcBox<[T]>, ) diff --git a/src/liballoc/sync.rs b/src/liballoc/sync.rs index ffab842c769..341172136e2 100644 --- a/src/liballoc/sync.rs +++ b/src/liballoc/sync.rs @@ -335,7 +335,7 @@ impl Arc { #[unstable(feature = "new_uninit", issue = "63291")] pub fn new_uninit() -> Arc> { unsafe { - Arc::from_ptr(Arc::allocate_for_unsized( + Arc::from_ptr(Arc::allocate_for_layout( Layout::new::(), |mem| mem as *mut ArcInner>, )) @@ -736,11 +736,11 @@ impl Arc { impl Arc { /// Allocates an `ArcInner` with sufficient space for - /// an unsized value where the value has the layout provided. + /// a possibly-unsized value where the value has the layout provided. /// /// The function `mem_to_arcinner` is called with the data pointer /// and must return back a (potentially fat)-pointer for the `ArcInner`. - unsafe fn allocate_for_unsized( + unsafe fn allocate_for_layout( value_layout: Layout, mem_to_arcinner: impl FnOnce(*mut u8) -> *mut ArcInner ) -> *mut ArcInner { @@ -768,7 +768,7 @@ impl Arc { /// Allocates an `ArcInner` with sufficient space for an unsized value. unsafe fn allocate_for_ptr(ptr: *const T) -> *mut ArcInner { // Allocate for the `ArcInner` using the given value. - Self::allocate_for_unsized( + Self::allocate_for_layout( Layout::for_value(&*ptr), |mem| set_data_ptr(ptr as *mut T, mem) as *mut ArcInner, ) @@ -799,7 +799,7 @@ impl Arc { impl Arc<[T]> { /// Allocates an `ArcInner<[T]>` with the given length. unsafe fn allocate_for_slice(len: usize) -> *mut ArcInner<[T]> { - Self::allocate_for_unsized( + Self::allocate_for_layout( Layout::array::(len).unwrap(), |mem| ptr::slice_from_raw_parts_mut(mem as *mut T, len) as *mut ArcInner<[T]>, ) -- cgit 1.4.1-3-g733a5