Skip to content

Commit e18b563

Browse files
committed
Auto merge of rust-lang#75033 - Manishearth:rollup-d8afil1, r=Manishearth
Rollup of 5 pull requests Successful merges: - rust-lang#74602 (Clarify the doc for MaybeUninit::zeroed on incorrect use) - rust-lang#74720 (Clean up E0728 explanation) - rust-lang#74992 (fix rustdoc generic param order) - rust-lang#75015 (Add Vec::spare_capacity_mut) - rust-lang#75022 (Use a slice pattern instead of rchunks_exact(_).next()) Failed merges: r? @ghost
2 parents 5ef872f + 8214788 commit e18b563

File tree

8 files changed

+63
-37
lines changed

8 files changed

+63
-37
lines changed

library/alloc/src/vec.rs

+42-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ use core::hash::{Hash, Hasher};
6565
use core::intrinsics::{arith_offset, assume};
6666
use core::iter::{FromIterator, FusedIterator, TrustedLen};
6767
use core::marker::PhantomData;
68-
use core::mem::{self, ManuallyDrop};
68+
use core::mem::{self, ManuallyDrop, MaybeUninit};
6969
use core::ops::Bound::{Excluded, Included, Unbounded};
7070
use core::ops::{self, Index, IndexMut, RangeBounds};
7171
use core::ptr::{self, NonNull};
@@ -1523,6 +1523,47 @@ impl<T> Vec<T> {
15231523
{
15241524
Box::leak(self.into_boxed_slice())
15251525
}
1526+
1527+
/// Returns the remaining spare capacity of the vector as a slice of
1528+
/// `MaybeUninit<T>`.
1529+
///
1530+
/// The returned slice can be used to fill the vector with data (e.g. by
1531+
/// reading from a file) before marking the data as initialized using the
1532+
/// [`set_len`] method.
1533+
///
1534+
/// [`set_len`]: #method.set_len
1535+
///
1536+
/// # Examples
1537+
///
1538+
/// ```
1539+
/// #![feature(vec_spare_capacity, maybe_uninit_extra)]
1540+
///
1541+
/// // Allocate vector big enough for 10 elements.
1542+
/// let mut v = Vec::with_capacity(10);
1543+
///
1544+
/// // Fill in the first 3 elements.
1545+
/// let uninit = v.spare_capacity_mut();
1546+
/// uninit[0].write(0);
1547+
/// uninit[1].write(1);
1548+
/// uninit[2].write(2);
1549+
///
1550+
/// // Mark the first 3 elements of the vector as being initialized.
1551+
/// unsafe {
1552+
/// v.set_len(3);
1553+
/// }
1554+
///
1555+
/// assert_eq!(&v, &[0, 1, 2]);
1556+
/// ```
1557+
#[unstable(feature = "vec_spare_capacity", issue = "75017")]
1558+
#[inline]
1559+
pub fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit<T>] {
1560+
unsafe {
1561+
slice::from_raw_parts_mut(
1562+
self.as_mut_ptr().add(self.len) as *mut MaybeUninit<T>,
1563+
self.buf.capacity() - self.len,
1564+
)
1565+
}
1566+
}
15261567
}
15271568

15281569
impl<T: Clone> Vec<T> {

library/core/src/mem/maybe_uninit.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,8 @@ impl<T> MaybeUninit<T> {
336336
/// assert_eq!(x, (0, false));
337337
/// ```
338338
///
339-
/// *Incorrect* usage of this function: initializing a struct with zero, where some fields
340-
/// cannot hold 0 as a valid value.
339+
/// *Incorrect* usage of this function: calling `x.zeroed().assume_init()`
340+
/// when `0` is not a valid bit-pattern for the type:
341341
///
342342
/// ```rust,no_run
343343
/// use std::mem::MaybeUninit;

src/librustc_error_codes/error_codes/E0728.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
[`await`] has been used outside [`async`] function or block.
1+
[`await`] has been used outside [`async`] function or [`async`] block.
22

3-
Erroneous code examples:
3+
Erroneous code example:
44

55
```edition2018,compile_fail,E0728
66
# use std::pin::Pin;
@@ -33,7 +33,7 @@ fn foo() {
3333

3434
[`await`] is used to suspend the current computation until the given
3535
future is ready to produce a value. So it is legal only within
36-
an [`async`] context, like an `async fn` or an `async` block.
36+
an [`async`] context, like an `async` function or an `async` block.
3737

3838
```edition2018
3939
# use std::pin::Pin;

src/librustc_privacy/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ impl EmbargoVisitor<'tcx> {
637637
&mut self,
638638
segments: &[hir::PathSegment<'_>],
639639
) {
640-
if let Some([module, segment]) = segments.rchunks_exact(2).next() {
640+
if let [.., module, segment] = segments {
641641
if let Some(item) = module
642642
.res
643643
.and_then(|res| res.mod_def_id())

src/librustdoc/clean/auto_trait.rs

+5
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,11 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
480480
.clean(self.cx)
481481
.params;
482482

483+
debug!(
484+
"param_env_to_generics({:?}): generic_params={:?}",
485+
param_env_def_id, generic_params
486+
);
487+
483488
let mut has_sized = FxHashSet::default();
484489
let mut ty_to_bounds: FxHashMap<_, FxHashSet<_>> = Default::default();
485490
let mut lifetime_to_bounds: FxHashMap<_, FxHashSet<_>> = Default::default();

src/librustdoc/clean/mod.rs

+8-15
Original file line numberDiff line numberDiff line change
@@ -716,11 +716,11 @@ impl<'a, 'tcx> Clean<Generics> for (&'a ty::Generics, ty::GenericPredicates<'tcx
716716
// Bounds in the type_params and lifetimes fields are repeated in the
717717
// predicates field (see rustc_typeck::collect::ty_generics), so remove
718718
// them.
719-
let stripped_typarams = gens
719+
let stripped_params = gens
720720
.params
721721
.iter()
722722
.filter_map(|param| match param.kind {
723-
ty::GenericParamDefKind::Lifetime => None,
723+
ty::GenericParamDefKind::Lifetime => Some(param.clean(cx)),
724724
ty::GenericParamDefKind::Type { synthetic, .. } => {
725725
if param.name == kw::SelfUpper {
726726
assert_eq!(param.index, 0);
@@ -732,7 +732,7 @@ impl<'a, 'tcx> Clean<Generics> for (&'a ty::Generics, ty::GenericPredicates<'tcx
732732
}
733733
Some(param.clean(cx))
734734
}
735-
ty::GenericParamDefKind::Const { .. } => None,
735+
ty::GenericParamDefKind::Const { .. } => Some(param.clean(cx)),
736736
})
737737
.collect::<Vec<GenericParamDef>>();
738738

@@ -844,8 +844,10 @@ impl<'a, 'tcx> Clean<Generics> for (&'a ty::Generics, ty::GenericPredicates<'tcx
844844

845845
// Run through the type parameters again and insert a ?Sized
846846
// unbound for any we didn't find to be Sized.
847-
for tp in &stripped_typarams {
848-
if !sized_params.contains(&tp.name) {
847+
for tp in &stripped_params {
848+
if matches!(tp.kind, types::GenericParamDefKind::Type { .. })
849+
&& !sized_params.contains(&tp.name)
850+
{
849851
where_predicates.push(WP::BoundPredicate {
850852
ty: Type::Generic(tp.name.clone()),
851853
bounds: vec![GenericBound::maybe_sized(cx)],
@@ -858,16 +860,7 @@ impl<'a, 'tcx> Clean<Generics> for (&'a ty::Generics, ty::GenericPredicates<'tcx
858860
// and instead see `where T: Foo + Bar + Sized + 'a`
859861

860862
Generics {
861-
params: gens
862-
.params
863-
.iter()
864-
.flat_map(|param| match param.kind {
865-
ty::GenericParamDefKind::Lifetime => Some(param.clean(cx)),
866-
ty::GenericParamDefKind::Type { .. } => None,
867-
ty::GenericParamDefKind::Const { .. } => Some(param.clean(cx)),
868-
})
869-
.chain(simplify::ty_params(stripped_typarams).into_iter())
870-
.collect(),
863+
params: stripped_params,
871864
where_predicates: simplify::where_clauses(cx, where_predicates),
872865
}
873866
}

src/librustdoc/clean/simplify.rs

-13
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
//! bounds by special casing scenarios such as these. Fun!
1313
1414
use std::collections::BTreeMap;
15-
use std::mem;
1615

1716
use rustc_hir::def_id::DefId;
1817
use rustc_middle::ty;
@@ -118,18 +117,6 @@ pub fn merge_bounds(
118117
})
119118
}
120119

121-
pub fn ty_params(mut params: Vec<clean::GenericParamDef>) -> Vec<clean::GenericParamDef> {
122-
for param in &mut params {
123-
match param.kind {
124-
clean::GenericParamDefKind::Type { ref mut bounds, .. } => {
125-
*bounds = mem::take(bounds);
126-
}
127-
_ => panic!("expected only type parameters"),
128-
}
129-
}
130-
params
131-
}
132-
133120
fn trait_is_same_or_supertrait(cx: &DocContext<'_>, child: DefId, trait_: DefId) -> bool {
134121
if child == trait_ {
135122
return true;

src/test/rustdoc/const-generics/const-impl.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ pub enum Order {
1111
}
1212

1313
// @has foo/struct.VSet.html '//pre[@class="rust struct"]' 'pub struct VSet<T, const ORDER: Order>'
14-
// @has foo/struct.VSet.html '//h3[@id="impl-Send"]/code' 'impl<const ORDER: Order, T> Send for VSet<T, ORDER>'
15-
// @has foo/struct.VSet.html '//h3[@id="impl-Sync"]/code' 'impl<const ORDER: Order, T> Sync for VSet<T, ORDER>'
14+
// @has foo/struct.VSet.html '//h3[@id="impl-Send"]/code' 'impl<T, const ORDER: Order> Send for VSet<T, ORDER>'
15+
// @has foo/struct.VSet.html '//h3[@id="impl-Sync"]/code' 'impl<T, const ORDER: Order> Sync for VSet<T, ORDER>'
1616
pub struct VSet<T, const ORDER: Order> {
1717
inner: Vec<T>,
1818
}

0 commit comments

Comments
 (0)