Skip to content

Commit 044ef55

Browse files
CoAlloc: Vec, VecDeque, RawVec; related str, slice, Box, BinaryHeap, proc_macro...
1 parent 187a2cc commit 044ef55

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1299
-373
lines changed

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
//!
33
//! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/mir/index.html
44
5-
use core::alloc::GlobalCoAllocMeta;
65
use crate::mir::interpret::{
76
AllocRange, ConstAllocation, ConstValue, ErrorHandled, GlobalAlloc, LitToConstInput, Scalar,
87
};
@@ -14,6 +13,7 @@ use crate::ty::visit::{TypeVisitable, TypeVisitor};
1413
use crate::ty::{self, ir, DefIdTree, List, Ty, TyCtxt};
1514
use crate::ty::{AdtDef, InstanceDef, ScalarInt, UserTypeAnnotationIndex};
1615
use crate::ty::{GenericArg, InternalSubsts, SubstsRef};
16+
use core::alloc::GlobalCoAllocMeta;
1717

1818
use rustc_data_structures::captures::Captures;
1919
use rustc_errors::ErrorGuaranteed;

compiler/rustc_middle/src/mir/syntax.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
//! This is in a dedicated file so that changes to this file can be reviewed more carefully.
44
//! The intention is that this file only contains datatype declarations, no code.
55
6+
use super::{BasicBlock, Constant, Field, Local, SwitchTargets, UserTypeProjection};
67
use core::alloc::GlobalCoAllocMeta;
78
use core::mem;
8-
use super::{BasicBlock, Constant, Field, Local, SwitchTargets, UserTypeProjection};
99

1010
use crate::mir::coverage::{CodeRegion, CoverageKind};
1111
use crate::traits::Reveal;

compiler/rustc_parse/src/parser/attr_wrapper.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
use super::{Capturing, FlatToken, ForceCollect, Parser, ReplaceRange, TokenCursor, TrailingToken};
12
use core::alloc::GlobalCoAllocMeta;
23
use core::mem;
3-
use super::{Capturing, FlatToken, ForceCollect, Parser, ReplaceRange, TokenCursor, TrailingToken};
44
use rustc_ast::token::{self, Delimiter, Token, TokenKind};
55
use rustc_ast::tokenstream::{AttrTokenStream, AttributesData, ToAttrTokenStream};
66
use rustc_ast::tokenstream::{AttrTokenTree, DelimSpan, LazyAttrTokenStream, Spacing};

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ mod stmt;
1111
mod ty;
1212

1313
use crate::lexer::UnmatchedBrace;
14-
use core::alloc::GlobalCoAllocMeta;
1514
pub use attr_wrapper::AttrWrapper;
15+
use core::alloc::GlobalCoAllocMeta;
1616
pub use diagnostics::AttemptLocalParseRecovery;
1717
pub(crate) use item::FnParseMode;
1818
pub use pat::{CommaRecoveryMode, RecoverColon, RecoverComma};

library/alloc/src/boxed.rs

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,8 @@ impl<T> Box<[T]> {
641641
#[unstable(feature = "new_uninit", issue = "63291")]
642642
#[must_use]
643643
pub fn new_uninit_slice(len: usize) -> Box<[mem::MaybeUninit<T>]> {
644-
unsafe { RawVec::with_capacity(len).into_box(len) }
644+
// false = no need for co-alloc metadata, since it would get lost once converted to Box.
645+
unsafe { RawVec::<T, Global, false>::with_capacity(len).into_box(len) }
645646
}
646647

647648
/// Constructs a new boxed slice with uninitialized contents, with the memory
@@ -666,7 +667,8 @@ impl<T> Box<[T]> {
666667
#[unstable(feature = "new_uninit", issue = "63291")]
667668
#[must_use]
668669
pub fn new_zeroed_slice(len: usize) -> Box<[mem::MaybeUninit<T>]> {
669-
unsafe { RawVec::with_capacity_zeroed(len).into_box(len) }
670+
// false = no need for co-alloc metadata, since it would get lost once converted to Box.
671+
unsafe { RawVec::<T, Global, false>::with_capacity_zeroed(len).into_box(len) }
670672
}
671673

672674
/// Constructs a new boxed slice with uninitialized contents. Returns an error if
@@ -698,7 +700,12 @@ impl<T> Box<[T]> {
698700
Err(_) => return Err(AllocError),
699701
};
700702
let ptr = Global.allocate(layout)?;
701-
Ok(RawVec::from_raw_parts_in(ptr.as_mut_ptr() as *mut _, len, Global).into_box(len))
703+
Ok(RawVec::<T, Global, false>::from_raw_parts_in(
704+
ptr.as_mut_ptr() as *mut _,
705+
len,
706+
Global,
707+
)
708+
.into_box(len))
702709
}
703710
}
704711

@@ -730,12 +737,20 @@ impl<T> Box<[T]> {
730737
Err(_) => return Err(AllocError),
731738
};
732739
let ptr = Global.allocate_zeroed(layout)?;
733-
Ok(RawVec::from_raw_parts_in(ptr.as_mut_ptr() as *mut _, len, Global).into_box(len))
740+
Ok(RawVec::<T, Global, false>::from_raw_parts_in(
741+
ptr.as_mut_ptr() as *mut _,
742+
len,
743+
Global,
744+
)
745+
.into_box(len))
734746
}
735747
}
736748
}
737749

738-
impl<T, A: Allocator> Box<[T], A> {
750+
impl<T, A: Allocator> Box<[T], A>
751+
where
752+
[(); core::alloc::co_alloc_metadata_num_slots::<A>()]:,
753+
{
739754
/// Constructs a new boxed slice with uninitialized contents in the provided allocator.
740755
///
741756
/// # Examples
@@ -762,8 +777,13 @@ impl<T, A: Allocator> Box<[T], A> {
762777
#[unstable(feature = "allocator_api", issue = "32838")]
763778
// #[unstable(feature = "new_uninit", issue = "63291")]
764779
#[must_use]
765-
pub fn new_uninit_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A> {
766-
unsafe { RawVec::with_capacity_in(len, alloc).into_box(len) }
780+
#[allow(unused_braces)]
781+
pub fn new_uninit_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A>
782+
where
783+
// false = no need for co-alloc metadata, since it would get lost once converted to Box.
784+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(false)]:,
785+
{
786+
unsafe { RawVec::<T, A, false>::with_capacity_in(len, alloc).into_box(len) }
767787
}
768788

769789
/// Constructs a new boxed slice with uninitialized contents in the provided allocator,
@@ -790,8 +810,13 @@ impl<T, A: Allocator> Box<[T], A> {
790810
#[unstable(feature = "allocator_api", issue = "32838")]
791811
// #[unstable(feature = "new_uninit", issue = "63291")]
792812
#[must_use]
793-
pub fn new_zeroed_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A> {
794-
unsafe { RawVec::with_capacity_zeroed_in(len, alloc).into_box(len) }
813+
#[allow(unused_braces)]
814+
pub fn new_zeroed_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A>
815+
where
816+
// false = no need for co-alloc metadata, since it would get lost once converted to Box.
817+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(false)]:,
818+
{
819+
unsafe { RawVec::<T, A, false>::with_capacity_zeroed_in(len, alloc).into_box(len) }
795820
}
796821
}
797822

@@ -1496,7 +1521,8 @@ impl<T: Copy> From<&[T]> for Box<[T]> {
14961521
/// ```
14971522
fn from(slice: &[T]) -> Box<[T]> {
14981523
let len = slice.len();
1499-
let buf = RawVec::with_capacity(len);
1524+
// false = no need for co-alloc metadata, since it would get lost once converted to Box.
1525+
let buf = RawVec::<T, Global, false>::with_capacity(len);
15001526
unsafe {
15011527
ptr::copy_nonoverlapping(slice.as_ptr(), buf.ptr(), len);
15021528
buf.into_box(slice.len()).assume_init()
@@ -1661,8 +1687,12 @@ impl<T, const N: usize> TryFrom<Box<[T]>> for Box<[T; N]> {
16611687

16621688
#[cfg(not(no_global_oom_handling))]
16631689
#[stable(feature = "boxed_array_try_from_vec", since = "1.66.0")]
1664-
impl<T, const N: usize> TryFrom<Vec<T>> for Box<[T; N]> {
1665-
type Error = Vec<T>;
1690+
impl<T, const N: usize, const COOP_PREFERRED: bool> TryFrom<Vec<T, Global, COOP_PREFERRED>>
1691+
for Box<[T; N]>
1692+
where
1693+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
1694+
{
1695+
type Error = Vec<T, Global, COOP_PREFERRED>;
16661696

16671697
/// Attempts to convert a `Vec<T>` into a `Box<[T; N]>`.
16681698
///
@@ -1682,7 +1712,7 @@ impl<T, const N: usize> TryFrom<Vec<T>> for Box<[T; N]> {
16821712
/// let state: Box<[f32; 100]> = vec![1.0; 100].try_into().unwrap();
16831713
/// assert_eq!(state.len(), 100);
16841714
/// ```
1685-
fn try_from(vec: Vec<T>) -> Result<Self, Self::Error> {
1715+
fn try_from(vec: Vec<T, Global, COOP_PREFERRED>) -> Result<Self, Self::Error> {
16861716
if vec.len() == N {
16871717
let boxed_slice = vec.into_boxed_slice();
16881718
Ok(unsafe { boxed_slice_as_array_unchecked(boxed_slice) })
@@ -2019,10 +2049,14 @@ impl<I> FromIterator<I> for Box<[I]> {
20192049

20202050
#[cfg(not(no_global_oom_handling))]
20212051
#[stable(feature = "box_slice_clone", since = "1.3.0")]
2022-
impl<T: Clone, A: Allocator + Clone> Clone for Box<[T], A> {
2052+
impl<T: Clone, A: Allocator + Clone> Clone for Box<[T], A>
2053+
where
2054+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(false)]:,
2055+
{
20232056
fn clone(&self) -> Self {
20242057
let alloc = Box::allocator(self).clone();
2025-
self.to_vec_in(alloc).into_boxed_slice()
2058+
// false = no need for co-alloc metadata, since it would get lost once converted to the boxed slice.
2059+
self.to_vec_in::<A, false>(alloc).into_boxed_slice()
20262060
}
20272061

20282062
fn clone_from(&mut self, other: &Self) {

library/alloc/src/collections/binary_heap/mod.rs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,12 @@ use core::num::NonZeroUsize;
150150
use core::ops::{Deref, DerefMut};
151151
use core::ptr;
152152

153+
use crate::alloc::Global;
154+
153155
use crate::collections::TryReserveError;
154156
use crate::slice;
155157
use crate::vec::{self, AsVecIntoIter, Vec};
158+
use crate::DEFAULT_COOP_PREFERRED;
156159

157160
use super::SpecExtend;
158161

@@ -1241,7 +1244,8 @@ impl<T> BinaryHeap<T> {
12411244
/// ```
12421245
#[inline]
12431246
#[stable(feature = "drain", since = "1.6.0")]
1244-
pub fn drain(&mut self) -> Drain<'_, T> {
1247+
#[allow(unused_braces)]
1248+
pub fn drain(&mut self) -> Drain<'_, T, { SHORT_TERM_VEC_PREFERS_COOP!() }> {
12451249
Drain { iter: self.data.drain(..) }
12461250
}
12471251

@@ -1521,12 +1525,18 @@ unsafe impl<T: Ord> TrustedLen for IntoIterSorted<T> {}
15211525
/// [`drain`]: BinaryHeap::drain
15221526
#[stable(feature = "drain", since = "1.6.0")]
15231527
#[derive(Debug)]
1524-
pub struct Drain<'a, T: 'a> {
1525-
iter: vec::Drain<'a, T>,
1528+
pub struct Drain<'a, T: 'a, const COOP_PREFERRED: bool>
1529+
where
1530+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
1531+
{
1532+
iter: vec::Drain<'a, T, Global, COOP_PREFERRED>,
15261533
}
15271534

15281535
#[stable(feature = "drain", since = "1.6.0")]
1529-
impl<T> Iterator for Drain<'_, T> {
1536+
impl<T, const COOP_PREFERRED: bool> Iterator for Drain<'_, T, COOP_PREFERRED>
1537+
where
1538+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
1539+
{
15301540
type Item = T;
15311541

15321542
#[inline]
@@ -1541,22 +1551,31 @@ impl<T> Iterator for Drain<'_, T> {
15411551
}
15421552

15431553
#[stable(feature = "drain", since = "1.6.0")]
1544-
impl<T> DoubleEndedIterator for Drain<'_, T> {
1554+
impl<T, const COOP_PREFERRED: bool> DoubleEndedIterator for Drain<'_, T, COOP_PREFERRED>
1555+
where
1556+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
1557+
{
15451558
#[inline]
15461559
fn next_back(&mut self) -> Option<T> {
15471560
self.iter.next_back()
15481561
}
15491562
}
15501563

15511564
#[stable(feature = "drain", since = "1.6.0")]
1552-
impl<T> ExactSizeIterator for Drain<'_, T> {
1565+
impl<T, const COOP_PREFERRED: bool> ExactSizeIterator for Drain<'_, T, COOP_PREFERRED>
1566+
where
1567+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
1568+
{
15531569
fn is_empty(&self) -> bool {
15541570
self.iter.is_empty()
15551571
}
15561572
}
15571573

15581574
#[stable(feature = "fused", since = "1.26.0")]
1559-
impl<T> FusedIterator for Drain<'_, T> {}
1575+
impl<T, const COOP_PREFERRED: bool> FusedIterator for Drain<'_, T, COOP_PREFERRED> where
1576+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:
1577+
{
1578+
}
15601579

15611580
/// A draining iterator over the elements of a `BinaryHeap`.
15621581
///
@@ -1644,7 +1663,8 @@ impl<T: Ord, const N: usize> From<[T; N]> for BinaryHeap<T> {
16441663
}
16451664

16461665
#[stable(feature = "binary_heap_extras_15", since = "1.5.0")]
1647-
impl<T> From<BinaryHeap<T>> for Vec<T> {
1666+
#[allow(unused_braces)]
1667+
impl<T> From<BinaryHeap<T>> for Vec<T, Global, { DEFAULT_COOP_PREFERRED!() }> {
16481668
/// Converts a `BinaryHeap<T>` into a `Vec<T>`.
16491669
///
16501670
/// This conversion requires no data movement or allocation, and has

0 commit comments

Comments
 (0)