Skip to content

Commit 5f60208

Browse files
committed
Auto merge of #57957 - Centril:rollup, r=Centril
Rollup of 7 pull requests Successful merges: - #57045 (Kill remaining uses of mem::uninitialized in libcore, liballoc) - #57674 (Avoid erase_regions_ty queries if there are no regions to erase) - #57833 (Print a slightly clearer message when failing to launch a thread) - #57859 (Fix invalid background color) - #57904 (add typo suggestion to unknown attribute error) - #57915 (Pretty print `$crate` as `crate` or `crate_name` in more cases) - #57950 (Extend E0106, E0261) Failed merges: r? @ghost
2 parents d8a0dd7 + d77db2e commit 5f60208

28 files changed

+269
-92
lines changed

src/etc/gdb_rust_pretty_printing.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -330,19 +330,20 @@ def children_of_node(boxed_node, height, want_values):
330330
leaf = node_ptr['data']
331331
else:
332332
leaf = node_ptr.dereference()
333-
keys = leaf['keys']['value']['value']
333+
keys = leaf['keys']
334334
if want_values:
335-
values = leaf['vals']['value']['value']
335+
values = leaf['vals']
336336
length = int(leaf['len'])
337337
for i in xrange(0, length + 1):
338338
if height > 0:
339-
for child in children_of_node(node_ptr['edges'][i], height - 1, want_values):
339+
child_ptr = node_ptr['edges'][i]['value']['value']
340+
for child in children_of_node(child_ptr, height - 1, want_values):
340341
yield child
341342
if i < length:
342343
if want_values:
343-
yield (keys[i], values[i])
344+
yield (keys[i]['value']['value'], values[i]['value']['value'])
344345
else:
345-
yield keys[i]
346+
yield keys[i]['value']['value']
346347

347348
class RustStdBTreeSetPrinter(object):
348349
def __init__(self, val):

src/liballoc/borrow.rs

-1
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,6 @@ impl<'a, B: ?Sized> Hash for Cow<'a, B>
380380
}
381381

382382
#[stable(feature = "rust1", since = "1.0.0")]
383-
#[allow(deprecated)]
384383
impl<'a, T: ?Sized + ToOwned> AsRef<T> for Cow<'a, T> {
385384
fn as_ref(&self) -> &T {
386385
self

src/liballoc/collections/btree/node.rs

+20-16
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ struct LeafNode<K, V> {
9595

9696
/// The arrays storing the actual data of the node. Only the first `len` elements of each
9797
/// array are initialized and valid.
98-
keys: MaybeUninit<[K; CAPACITY]>,
99-
vals: MaybeUninit<[V; CAPACITY]>,
98+
keys: [MaybeUninit<K>; CAPACITY],
99+
vals: [MaybeUninit<V>; CAPACITY],
100100
}
101101

102102
impl<K, V> LeafNode<K, V> {
@@ -106,8 +106,8 @@ impl<K, V> LeafNode<K, V> {
106106
LeafNode {
107107
// As a general policy, we leave fields uninitialized if they can be, as this should
108108
// be both slightly faster and easier to track in Valgrind.
109-
keys: MaybeUninit::uninitialized(),
110-
vals: MaybeUninit::uninitialized(),
109+
keys: uninitialized_array![_; CAPACITY],
110+
vals: uninitialized_array![_; CAPACITY],
111111
parent: ptr::null(),
112112
parent_idx: MaybeUninit::uninitialized(),
113113
len: 0
@@ -145,7 +145,7 @@ struct InternalNode<K, V> {
145145

146146
/// The pointers to the children of this node. `len + 1` of these are considered
147147
/// initialized and valid.
148-
edges: [BoxedNode<K, V>; 2 * B],
148+
edges: [MaybeUninit<BoxedNode<K, V>>; 2 * B],
149149
}
150150

151151
impl<K, V> InternalNode<K, V> {
@@ -159,7 +159,7 @@ impl<K, V> InternalNode<K, V> {
159159
unsafe fn new() -> Self {
160160
InternalNode {
161161
data: LeafNode::new(),
162-
edges: mem::uninitialized()
162+
edges: uninitialized_array![_; 2*B],
163163
}
164164
}
165165
}
@@ -261,7 +261,7 @@ impl<K, V> Root<K, V> {
261261
-> NodeRef<marker::Mut, K, V, marker::Internal> {
262262
debug_assert!(!self.is_shared_root());
263263
let mut new_node = Box::new(unsafe { InternalNode::new() });
264-
new_node.edges[0] = unsafe { BoxedNode::from_ptr(self.node.as_ptr()) };
264+
new_node.edges[0].set(unsafe { BoxedNode::from_ptr(self.node.as_ptr()) });
265265

266266
self.node = BoxedNode::from_internal(new_node);
267267
self.height += 1;
@@ -623,7 +623,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Immut<'a>, K, V, Type> {
623623
// We cannot be the root, so `as_leaf` is okay
624624
unsafe {
625625
slice::from_raw_parts(
626-
self.as_leaf().vals.as_ptr() as *const V,
626+
MaybeUninit::first_ptr(&self.as_leaf().vals),
627627
self.len()
628628
)
629629
}
@@ -650,7 +650,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
650650
} else {
651651
unsafe {
652652
slice::from_raw_parts_mut(
653-
(*self.as_leaf_mut()).keys.as_mut_ptr() as *mut K,
653+
MaybeUninit::first_ptr_mut(&mut (*self.as_leaf_mut()).keys),
654654
self.len()
655655
)
656656
}
@@ -661,7 +661,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
661661
debug_assert!(!self.is_shared_root());
662662
unsafe {
663663
slice::from_raw_parts_mut(
664-
(*self.as_leaf_mut()).vals.as_mut_ptr() as *mut V,
664+
MaybeUninit::first_ptr_mut(&mut (*self.as_leaf_mut()).vals),
665665
self.len()
666666
)
667667
}
@@ -718,7 +718,7 @@ impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
718718
unsafe {
719719
ptr::write(self.keys_mut().get_unchecked_mut(idx), key);
720720
ptr::write(self.vals_mut().get_unchecked_mut(idx), val);
721-
ptr::write(self.as_internal_mut().edges.get_unchecked_mut(idx + 1), edge.node);
721+
self.as_internal_mut().edges.get_unchecked_mut(idx + 1).set(edge.node);
722722

723723
(*self.as_leaf_mut()).len += 1;
724724

@@ -749,7 +749,7 @@ impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
749749
slice_insert(self.vals_mut(), 0, val);
750750
slice_insert(
751751
slice::from_raw_parts_mut(
752-
self.as_internal_mut().edges.as_mut_ptr(),
752+
MaybeUninit::first_ptr_mut(&mut self.as_internal_mut().edges),
753753
self.len()+1
754754
),
755755
0,
@@ -778,7 +778,9 @@ impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
778778
let edge = match self.reborrow_mut().force() {
779779
ForceResult::Leaf(_) => None,
780780
ForceResult::Internal(internal) => {
781-
let edge = ptr::read(internal.as_internal().edges.get_unchecked(idx + 1));
781+
let edge = ptr::read(
782+
internal.as_internal().edges.get_unchecked(idx + 1).as_ptr()
783+
);
782784
let mut new_root = Root { node: edge, height: internal.height - 1 };
783785
(*new_root.as_mut().as_leaf_mut()).parent = ptr::null();
784786
Some(new_root)
@@ -806,7 +808,7 @@ impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
806808
ForceResult::Internal(mut internal) => {
807809
let edge = slice_remove(
808810
slice::from_raw_parts_mut(
809-
internal.as_internal_mut().edges.as_mut_ptr(),
811+
MaybeUninit::first_ptr_mut(&mut internal.as_internal_mut().edges),
810812
old_len+1
811813
),
812814
0
@@ -1085,7 +1087,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
10851087

10861088
slice_insert(
10871089
slice::from_raw_parts_mut(
1088-
self.node.as_internal_mut().edges.as_mut_ptr(),
1090+
MaybeUninit::first_ptr_mut(&mut self.node.as_internal_mut().edges),
10891091
self.node.len()
10901092
),
10911093
self.idx + 1,
@@ -1140,7 +1142,9 @@ impl<BorrowType, K, V>
11401142
pub fn descend(self) -> NodeRef<BorrowType, K, V, marker::LeafOrInternal> {
11411143
NodeRef {
11421144
height: self.node.height - 1,
1143-
node: unsafe { self.node.as_internal().edges.get_unchecked(self.idx).as_ptr() },
1145+
node: unsafe {
1146+
self.node.as_internal().edges.get_unchecked(self.idx).get_ref().as_ptr()
1147+
},
11441148
root: self.node.root,
11451149
_marker: PhantomData
11461150
}

src/liballoc/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@
6363
#![no_std]
6464
#![needs_allocator]
6565

66-
#![deny(intra_doc_link_resolution_failure)]
67-
#![deny(missing_debug_implementations)]
66+
#![warn(deprecated_in_future)]
67+
#![warn(intra_doc_link_resolution_failure)]
68+
#![warn(missing_debug_implementations)]
6869

6970
#![cfg_attr(not(test), feature(fn_traits))]
7071
#![cfg_attr(not(test), feature(generator_trait))]

src/liballoc/rc.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![allow(deprecated)]
2-
31
//! Single-threaded reference-counting pointers. 'Rc' stands for 'Reference
42
//! Counted'.
53
//!

src/libcore/fmt/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2048,7 +2048,7 @@ macro_rules! tuple {
20482048
( $($name:ident,)+ ) => (
20492049
#[stable(feature = "rust1", since = "1.0.0")]
20502050
impl<$($name:Debug),*> Debug for ($($name,)*) where last_type!($($name,)+): ?Sized {
2051-
#[allow(non_snake_case, unused_assignments, deprecated)]
2051+
#[allow(non_snake_case, unused_assignments)]
20522052
fn fmt(&self, f: &mut Formatter) -> Result {
20532053
let mut builder = f.debug_tuple("");
20542054
let ($(ref $name,)*) = *self;

src/libcore/fmt/num.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
//! Integer and floating-point number formatting
22
3-
#![allow(deprecated)]
4-
53

64
use fmt;
75
use ops::{Div, Rem, Sub};
86
use str;
97
use slice;
108
use ptr;
11-
use mem;
9+
use mem::MaybeUninit;
1210

1311
#[doc(hidden)]
1412
trait Int: PartialEq + PartialOrd + Div<Output=Self> + Rem<Output=Self> +
@@ -53,7 +51,7 @@ trait GenericRadix {
5351
// characters for a base 2 number.
5452
let zero = T::zero();
5553
let is_nonnegative = x >= zero;
56-
let mut buf: [u8; 128] = unsafe { mem::uninitialized() };
54+
let mut buf = uninitialized_array![u8; 128];
5755
let mut curr = buf.len();
5856
let base = T::from_u8(Self::BASE);
5957
if is_nonnegative {
@@ -62,7 +60,7 @@ trait GenericRadix {
6260
for byte in buf.iter_mut().rev() {
6361
let n = x % base; // Get the current place value.
6462
x = x / base; // Deaccumulate the number.
65-
*byte = Self::digit(n.to_u8()); // Store the digit in the buffer.
63+
byte.set(Self::digit(n.to_u8())); // Store the digit in the buffer.
6664
curr -= 1;
6765
if x == zero {
6866
// No more digits left to accumulate.
@@ -74,15 +72,19 @@ trait GenericRadix {
7472
for byte in buf.iter_mut().rev() {
7573
let n = zero - (x % base); // Get the current place value.
7674
x = x / base; // Deaccumulate the number.
77-
*byte = Self::digit(n.to_u8()); // Store the digit in the buffer.
75+
byte.set(Self::digit(n.to_u8())); // Store the digit in the buffer.
7876
curr -= 1;
7977
if x == zero {
8078
// No more digits left to accumulate.
8179
break
8280
};
8381
}
8482
}
85-
let buf = unsafe { str::from_utf8_unchecked(&buf[curr..]) };
83+
let buf = &buf[curr..];
84+
let buf = unsafe { str::from_utf8_unchecked(slice::from_raw_parts(
85+
MaybeUninit::first_ptr(buf),
86+
buf.len()
87+
)) };
8688
f.pad_integral(is_nonnegative, Self::PREFIX, buf)
8789
}
8890
}
@@ -196,9 +198,9 @@ macro_rules! impl_Display {
196198
// convert the negative num to positive by summing 1 to it's 2 complement
197199
(!self.$conv_fn()).wrapping_add(1)
198200
};
199-
let mut buf: [u8; 39] = unsafe { mem::uninitialized() };
201+
let mut buf = uninitialized_array![u8; 39];
200202
let mut curr = buf.len() as isize;
201-
let buf_ptr = buf.as_mut_ptr();
203+
let buf_ptr = MaybeUninit::first_ptr_mut(&mut buf);
202204
let lut_ptr = DEC_DIGITS_LUT.as_ptr();
203205

204206
unsafe {

src/libcore/hash/sip.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! An implementation of SipHash.
22
3-
#![allow(deprecated)]
3+
#![allow(deprecated)] // the types in this module are deprecated
44

55
use marker::PhantomData;
66
use ptr;

src/libcore/lib.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,12 @@
5858
issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/",
5959
test(no_crate_inject, attr(deny(warnings))),
6060
test(attr(allow(dead_code, deprecated, unused_variables, unused_mut))))]
61-
6261
#![no_core]
63-
#![deny(missing_docs)]
64-
#![deny(intra_doc_link_resolution_failure)]
65-
#![deny(missing_debug_implementations)]
62+
63+
#![warn(deprecated_in_future)]
64+
#![warn(missing_docs)]
65+
#![warn(intra_doc_link_resolution_failure)]
66+
#![warn(missing_debug_implementations)]
6667

6768
#![feature(allow_internal_unstable)]
6869
#![feature(arbitrary_self_types)]
@@ -122,6 +123,7 @@
122123
#![feature(structural_match)]
123124
#![feature(abi_unadjusted)]
124125
#![feature(adx_target_feature)]
126+
#![feature(maybe_uninit)]
125127

126128
#[prelude_import]
127129
#[allow(unused)]

src/libcore/macros.rs

+17
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,23 @@ macro_rules! unimplemented {
547547
($($arg:tt)+) => (panic!("not yet implemented: {}", format_args!($($arg)*)));
548548
}
549549

550+
/// A macro to create an array of [`MaybeUninit`]
551+
///
552+
/// This macro constructs and uninitialized array of the type `[MaybeUninit<K>; N]`.
553+
///
554+
/// [`MaybeUninit`]: mem/union.MaybeUninit.html
555+
#[macro_export]
556+
#[unstable(feature = "maybe_uninit", issue = "53491")]
557+
macro_rules! uninitialized_array {
558+
// This `into_inner` is safe because an array of `MaybeUninit` does not
559+
// require initialization.
560+
// FIXME(#49147): Could be replaced by an array initializer, once those can
561+
// be any const expression.
562+
($t:ty; $size:expr) => (unsafe {
563+
MaybeUninit::<[MaybeUninit<$t>; $size]>::uninitialized().into_inner()
564+
});
565+
}
566+
550567
/// Built-in macros to the compiler itself.
551568
///
552569
/// These macros do not have any corresponding definition with a `macro_rules!`

src/libcore/mem.rs

+14
Original file line numberDiff line numberDiff line change
@@ -1148,4 +1148,18 @@ impl<T> MaybeUninit<T> {
11481148
pub fn as_mut_ptr(&mut self) -> *mut T {
11491149
unsafe { &mut *self.value as *mut T }
11501150
}
1151+
1152+
/// Get a pointer to the first element of the array.
1153+
#[unstable(feature = "maybe_uninit", issue = "53491")]
1154+
#[inline(always)]
1155+
pub fn first_ptr(this: &[MaybeUninit<T>]) -> *const T {
1156+
this as *const [MaybeUninit<T>] as *const T
1157+
}
1158+
1159+
/// Get a mutable pointer to the first element of the array.
1160+
#[unstable(feature = "maybe_uninit", issue = "53491")]
1161+
#[inline(always)]
1162+
pub fn first_ptr_mut(this: &mut [MaybeUninit<T>]) -> *mut T {
1163+
this as *mut [MaybeUninit<T>] as *mut T
1164+
}
11511165
}

src/libcore/slice/sort.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,14 @@ fn partition_in_blocks<T, F>(v: &mut [T], pivot: &T, is_less: &mut F) -> usize
216216
let mut block_l = BLOCK;
217217
let mut start_l = ptr::null_mut();
218218
let mut end_l = ptr::null_mut();
219-
let mut offsets_l = MaybeUninit::<[u8; BLOCK]>::uninitialized();
219+
let mut offsets_l: [MaybeUninit<u8>; BLOCK] = uninitialized_array![u8; BLOCK];
220220

221221
// The current block on the right side (from `r.sub(block_r)` to `r`).
222222
let mut r = unsafe { l.add(v.len()) };
223223
let mut block_r = BLOCK;
224224
let mut start_r = ptr::null_mut();
225225
let mut end_r = ptr::null_mut();
226-
let mut offsets_r = MaybeUninit::<[u8; BLOCK]>::uninitialized();
226+
let mut offsets_r: [MaybeUninit<u8>; BLOCK] = uninitialized_array![u8; BLOCK];
227227

228228
// FIXME: When we get VLAs, try creating one array of length `min(v.len(), 2 * BLOCK)` rather
229229
// than two fixed-size arrays of length `BLOCK`. VLAs might be more cache-efficient.
@@ -262,8 +262,8 @@ fn partition_in_blocks<T, F>(v: &mut [T], pivot: &T, is_less: &mut F) -> usize
262262

263263
if start_l == end_l {
264264
// Trace `block_l` elements from the left side.
265-
start_l = offsets_l.as_mut_ptr() as *mut u8;
266-
end_l = offsets_l.as_mut_ptr() as *mut u8;
265+
start_l = MaybeUninit::first_ptr_mut(&mut offsets_l);
266+
end_l = MaybeUninit::first_ptr_mut(&mut offsets_l);
267267
let mut elem = l;
268268

269269
for i in 0..block_l {
@@ -278,8 +278,8 @@ fn partition_in_blocks<T, F>(v: &mut [T], pivot: &T, is_less: &mut F) -> usize
278278

279279
if start_r == end_r {
280280
// Trace `block_r` elements from the right side.
281-
start_r = offsets_r.as_mut_ptr() as *mut u8;
282-
end_r = offsets_r.as_mut_ptr() as *mut u8;
281+
start_r = MaybeUninit::first_ptr_mut(&mut offsets_r);
282+
end_r = MaybeUninit::first_ptr_mut(&mut offsets_r);
283283
let mut elem = r;
284284

285285
for i in 0..block_r {

0 commit comments

Comments
 (0)