Skip to content

Commit 27976f4

Browse files
CoAlloc: Big Squash; const_trait TODO and ICE on Dec 4, 2023
1 parent 152a4e9 commit 27976f4

File tree

85 files changed

+2824
-451
lines changed

Some content is hidden

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

85 files changed

+2824
-451
lines changed

compiler/rustc_arena/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@
1111
html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/",
1212
test(no_crate_inject, attr(deny(warnings)))
1313
)]
14+
#![allow(incomplete_features)]
1415
#![cfg_attr(not(bootstrap), doc(rust_logo))]
1516
#![cfg_attr(not(bootstrap), feature(rustdoc_internals))]
1617
#![feature(core_intrinsics)]
1718
#![feature(dropck_eyepatch)]
1819
#![feature(new_uninit)]
1920
#![feature(maybe_uninit_slice)]
21+
//#![feature(min_specialization)]
22+
// TODO CoAlloc specialization!
23+
#![feature(specialization)]
2024
#![feature(decl_macro)]
2125
#![feature(rustc_attrs)]
2226
#![cfg_attr(test, feature(test))]

compiler/rustc_ast/src/ast.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ pub use rustc_type_ir::{Movability, Mutability};
3838
use std::fmt;
3939
use std::mem;
4040
use thin_vec::{thin_vec, ThinVec};
41-
4241
/// A "Label" is an identifier of some point in sources,
4342
/// e.g. in the following code:
4443
///
@@ -3144,30 +3143,31 @@ pub type ForeignItem = Item<ForeignItemKind>;
31443143
mod size_asserts {
31453144
use super::*;
31463145
use rustc_data_structures::static_assert_size;
3146+
use std::alloc::{Allocator, Global};
31473147
// tidy-alphabetical-start
31483148
static_assert_size!(AssocItem, 88);
31493149
static_assert_size!(AssocItemKind, 16);
31503150
static_assert_size!(Attribute, 32);
3151-
static_assert_size!(Block, 32);
3152-
static_assert_size!(Expr, 72);
3153-
static_assert_size!(ExprKind, 40);
3154-
static_assert_size!(Fn, 152);
3151+
static_assert_size!(Block, 32 + mem::size_of::<<Global as Allocator>::CoAllocMeta>());
3152+
static_assert_size!(Expr, 72 + mem::size_of::<<Global as Allocator>::CoAllocMeta>());
3153+
static_assert_size!(ExprKind, 40 + mem::size_of::<<Global as Allocator>::CoAllocMeta>());
3154+
static_assert_size!(Fn, 152 + 2 * mem::size_of::<<Global as Allocator>::CoAllocMeta>());
31553155
static_assert_size!(ForeignItem, 96);
31563156
static_assert_size!(ForeignItemKind, 24);
31573157
static_assert_size!(GenericArg, 24);
3158-
static_assert_size!(GenericBound, 56);
3159-
static_assert_size!(Generics, 40);
3160-
static_assert_size!(Impl, 136);
3161-
static_assert_size!(Item, 136);
3162-
static_assert_size!(ItemKind, 64);
3158+
static_assert_size!(GenericBound, 56 + mem::size_of::<<Global as Allocator>::CoAllocMeta>());
3159+
static_assert_size!(Generics, 40 + 2 * mem::size_of::<<Global as Allocator>::CoAllocMeta>());
3160+
static_assert_size!(Impl, 136 + 3 * mem::size_of::<<Global as Allocator>::CoAllocMeta>());
3161+
static_assert_size!(Item, 136 + 3 * mem::size_of::<<Global as Allocator>::CoAllocMeta>());
3162+
static_assert_size!(ItemKind, 64 + 3 * mem::size_of::<<Global as Allocator>::CoAllocMeta>());
31633163
static_assert_size!(LitKind, 24);
31643164
static_assert_size!(Local, 72);
31653165
static_assert_size!(MetaItemLit, 40);
31663166
static_assert_size!(Param, 40);
3167-
static_assert_size!(Pat, 72);
3167+
static_assert_size!(Pat, 72 + mem::size_of::<<Global as Allocator>::CoAllocMeta>());
31683168
static_assert_size!(Path, 24);
31693169
static_assert_size!(PathSegment, 24);
3170-
static_assert_size!(PatKind, 48);
3170+
static_assert_size!(PatKind, 48 + mem::size_of::<<Global as Allocator>::CoAllocMeta>());
31713171
static_assert_size!(Stmt, 32);
31723172
static_assert_size!(StmtKind, 16);
31733173
static_assert_size!(Ty, 64);

compiler/rustc_ast/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
#![cfg_attr(not(bootstrap), doc(rust_logo))]
1212
#![cfg_attr(not(bootstrap), allow(internal_features))]
1313
#![cfg_attr(not(bootstrap), feature(rustdoc_internals))]
14+
#![feature(allocator_api)]
1415
#![feature(associated_type_bounds)]
1516
#![feature(box_patterns)]
1617
#![feature(const_trait_impl)]
18+
#![feature(global_co_alloc_meta)]
1719
#![feature(if_let_guard)]
1820
#![feature(let_chains)]
1921
#![feature(min_specialization)]

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
390390
return None; // do not suggest code that is already there (#53348)
391391
}
392392

393-
let method_call_list = [sym::to_vec, sym::to_string];
393+
let method_call_list = [sym::to_vec, sym::to_vec_co, sym::to_string];
394394
let mut sugg = if let ExprKind::MethodCall(receiver_method, ..) = expr.kind
395395
&& receiver_method.ident.name == sym::clone
396396
&& method_call_list.contains(&conversion_method.name)

compiler/rustc_middle/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#![cfg_attr(bootstrap, feature(generators))]
3636
#![cfg_attr(not(bootstrap), feature(coroutines))]
3737
#![feature(get_mut_unchecked)]
38+
#![feature(global_co_alloc_meta)]
3839
#![feature(if_let_guard)]
3940
#![feature(inline_const)]
4041
#![feature(iter_from_coroutine)]

compiler/rustc_middle/src/mir/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1629,7 +1629,10 @@ mod size_asserts {
16291629
use super::*;
16301630
use rustc_data_structures::static_assert_size;
16311631
// tidy-alphabetical-start
1632-
static_assert_size!(BasicBlockData<'_>, 136);
1632+
static_assert_size!(
1633+
BasicBlockData<'_>,
1634+
136 + mem::size_of::<<std::alloc::Global as std::alloc::Allocator>::CoAllocMeta>()
1635+
);
16331636
static_assert_size!(LocalDecl<'_>, 40);
16341637
static_assert_size!(SourceScopeData<'_>, 72);
16351638
static_assert_size!(Statement<'_>, 32);

compiler/rustc_middle/src/mir/syntax.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1442,6 +1442,9 @@ mod size_asserts {
14421442
static_assert_size!(Operand<'_>, 24);
14431443
static_assert_size!(Place<'_>, 16);
14441444
static_assert_size!(PlaceElem<'_>, 24);
1445-
static_assert_size!(Rvalue<'_>, 40);
1445+
static_assert_size!(
1446+
Rvalue<'_>,
1447+
40 + std::mem::size_of::<<std::alloc::Global as std::alloc::Allocator>::CoAllocMeta>()
1448+
);
14461449
// tidy-alphabetical-end
14471450
}

compiler/rustc_parse/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
//! The main parser interface.
22
3+
#![feature(allocator_api)]
34
#![feature(array_windows)]
45
#![feature(box_patterns)]
6+
#![feature(global_co_alloc_meta)]
57
#![feature(if_let_guard)]
68
#![feature(iter_intersperse)]
79
#![feature(let_chains)]

compiler/rustc_parse/src/parser/attr_wrapper.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -458,8 +458,12 @@ fn make_token_stream(
458458
mod size_asserts {
459459
use super::*;
460460
use rustc_data_structures::static_assert_size;
461+
use std::alloc::{Allocator, Global};
461462
// tidy-alphabetical-start
462463
static_assert_size!(AttrWrapper, 16);
463-
static_assert_size!(LazyAttrTokenStreamImpl, 104);
464+
static_assert_size!(
465+
LazyAttrTokenStreamImpl,
466+
104 + std::mem::size_of::<<Global as Allocator>::CoAllocMeta>()
467+
);
464468
// tidy-alphabetical-end
465469
}

compiler/rustc_parse/src/parser/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,10 @@ pub struct Parser<'a> {
179179
// This type is used a lot, e.g. it's cloned when matching many declarative macro rules with nonterminals. Make sure
180180
// it doesn't unintentionally get bigger.
181181
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
182-
rustc_data_structures::static_assert_size!(Parser<'_>, 264);
182+
rustc_data_structures::static_assert_size!(
183+
Parser<'_>,
184+
264 + 4 * mem::size_of::<<std::alloc::Global as std::alloc::Allocator>::CoAllocMeta>()
185+
);
183186

184187
/// Stores span information about a closure.
185188
#[derive(Clone)]

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1628,6 +1628,7 @@ symbols! {
16281628
to_string,
16291629
to_string_method,
16301630
to_vec,
1631+
to_vec_co,
16311632
todo_macro,
16321633
tool_attributes,
16331634
tool_lints,

compiler/rustc_trait_selection/src/lib.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@
1414
#![cfg_attr(not(bootstrap), doc(rust_logo))]
1515
#![cfg_attr(not(bootstrap), feature(rustdoc_internals))]
1616
#![cfg_attr(not(bootstrap), allow(internal_features))]
17+
#![feature(allocator_api)]
1718
#![feature(associated_type_bounds)]
1819
#![feature(box_patterns)]
1920
#![feature(control_flow_enum)]
20-
#![feature(extract_if)]
21+
// TODO CoAlloc #![feature(drain_filter)]
22+
// TODO CoAlloc #![feature(extract_if)]
23+
#![feature(global_co_alloc_meta)]
24+
// TODO CoAlloc #![feature(hash_drain_filter)]
2125
#![feature(let_chains)]
2226
#![feature(if_let_guard)]
2327
#![feature(never_type)]

compiler/rustc_trait_selection/src/traits/fulfill.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ pub struct PendingPredicateObligation<'tcx> {
7474

7575
// `PendingPredicateObligation` is used a lot. Make sure it doesn't unintentionally get bigger.
7676
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
77-
static_assert_size!(PendingPredicateObligation<'_>, 72);
77+
static_assert_size!(
78+
PendingPredicateObligation<'_>,
79+
72 + std::mem::size_of::<<std::alloc::Global as std::alloc::Allocator>::CoAllocMeta>()
80+
);
7881

7982
impl<'tcx> FulfillmentContext<'tcx> {
8083
/// Creates a new fulfillment context.

library/alloc/src/boxed.rs

+58-14
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@
146146
147147
#![stable(feature = "rust1", since = "1.0.0")]
148148

149+
#[cfg(not(no_global_oom_handling))]
150+
use crate::co_alloc::CoAllocPref;
149151
use core::any::Any;
150152
use core::async_iter::AsyncIterator;
151153
use core::borrow;
@@ -632,7 +634,10 @@ impl<T> Box<[T]> {
632634
#[unstable(feature = "new_uninit", issue = "63291")]
633635
#[must_use]
634636
pub fn new_uninit_slice(len: usize) -> Box<[mem::MaybeUninit<T>]> {
635-
unsafe { RawVec::with_capacity(len).into_box(len) }
637+
// false = no need for co-alloc metadata, since it would get lost once converted to Box.
638+
unsafe {
639+
RawVec::<T, Global, { CO_ALLOC_PREF_META_NO!() }>::with_capacity(len).into_box(len)
640+
}
636641
}
637642

638643
/// Constructs a new boxed slice with uninitialized contents, with the memory
@@ -657,7 +662,11 @@ impl<T> Box<[T]> {
657662
#[unstable(feature = "new_uninit", issue = "63291")]
658663
#[must_use]
659664
pub fn new_zeroed_slice(len: usize) -> Box<[mem::MaybeUninit<T>]> {
660-
unsafe { RawVec::with_capacity_zeroed(len).into_box(len) }
665+
// false = no need for co-alloc metadata, since it would get lost once converted to Box.
666+
unsafe {
667+
RawVec::<T, Global, { CO_ALLOC_PREF_META_NO!() }>::with_capacity_zeroed(len)
668+
.into_box(len)
669+
}
661670
}
662671

663672
/// Constructs a new boxed slice with uninitialized contents. Returns an error if
@@ -692,7 +701,14 @@ impl<T> Box<[T]> {
692701
};
693702
Global.allocate(layout)?.cast()
694703
};
695-
unsafe { Ok(RawVec::from_raw_parts_in(ptr.as_ptr(), len, Global).into_box(len)) }
704+
unsafe {
705+
Ok(RawVec::<T, Global, { CO_ALLOC_PREF_META_NO!() }>::from_raw_parts_in(
706+
ptr.as_ptr(),
707+
len,
708+
Global,
709+
)
710+
.into_box(len))
711+
}
696712
}
697713

698714
/// Constructs a new boxed slice with uninitialized contents, with the memory
@@ -726,11 +742,22 @@ impl<T> Box<[T]> {
726742
};
727743
Global.allocate_zeroed(layout)?.cast()
728744
};
729-
unsafe { Ok(RawVec::from_raw_parts_in(ptr.as_ptr(), len, Global).into_box(len)) }
745+
unsafe {
746+
Ok(RawVec::<T, Global, { CO_ALLOC_PREF_META_NO!() }>::from_raw_parts_in(
747+
ptr.as_ptr(),
748+
len,
749+
Global,
750+
)
751+
.into_box(len))
752+
}
730753
}
731754
}
732755

733-
impl<T, A: Allocator> Box<[T], A> {
756+
#[allow(unused_braces)]
757+
impl<T, A: Allocator> Box<[T], A>
758+
where
759+
[(); { crate::meta_num_slots!(A, crate::CO_ALLOC_PREF_META_NO!()) }]:,
760+
{
734761
/// Constructs a new boxed slice with uninitialized contents in the provided allocator.
735762
///
736763
/// # Examples
@@ -757,8 +784,11 @@ impl<T, A: Allocator> Box<[T], A> {
757784
#[unstable(feature = "allocator_api", issue = "32838")]
758785
// #[unstable(feature = "new_uninit", issue = "63291")]
759786
#[must_use]
787+
#[allow(unused_braces)]
760788
pub fn new_uninit_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A> {
761-
unsafe { RawVec::with_capacity_in(len, alloc).into_box(len) }
789+
unsafe {
790+
RawVec::<T, A, { CO_ALLOC_PREF_META_NO!() }>::with_capacity_in(len, alloc).into_box(len)
791+
}
762792
}
763793

764794
/// Constructs a new boxed slice with uninitialized contents in the provided allocator,
@@ -785,8 +815,12 @@ impl<T, A: Allocator> Box<[T], A> {
785815
#[unstable(feature = "allocator_api", issue = "32838")]
786816
// #[unstable(feature = "new_uninit", issue = "63291")]
787817
#[must_use]
818+
#[allow(unused_braces)]
788819
pub fn new_zeroed_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A> {
789-
unsafe { RawVec::with_capacity_zeroed_in(len, alloc).into_box(len) }
820+
unsafe {
821+
RawVec::<T, A, { CO_ALLOC_PREF_META_NO!() }>::with_capacity_zeroed_in(len, alloc)
822+
.into_box(len)
823+
}
790824
}
791825
}
792826

@@ -1487,7 +1521,7 @@ trait BoxFromSlice<T> {
14871521
impl<T: Clone> BoxFromSlice<T> for Box<[T]> {
14881522
#[inline]
14891523
default fn from_slice(slice: &[T]) -> Self {
1490-
slice.to_vec().into_boxed_slice()
1524+
slice.to_vec_co::<{ CO_ALLOC_PREF_META_NO!() }>().into_boxed_slice()
14911525
}
14921526
}
14931527

@@ -1496,7 +1530,7 @@ impl<T: Copy> BoxFromSlice<T> for Box<[T]> {
14961530
#[inline]
14971531
fn from_slice(slice: &[T]) -> Self {
14981532
let len = slice.len();
1499-
let buf = RawVec::with_capacity(len);
1533+
let buf = RawVec::<T, Global, { CO_ALLOC_PREF_META_NO!() }>::with_capacity(len);
15001534
unsafe {
15011535
ptr::copy_nonoverlapping(slice.as_ptr(), buf.ptr(), len);
15021536
buf.into_box(slice.len()).assume_init()
@@ -1682,8 +1716,13 @@ impl<T, const N: usize> TryFrom<Box<[T]>> for Box<[T; N]> {
16821716

16831717
#[cfg(not(no_global_oom_handling))]
16841718
#[stable(feature = "boxed_array_try_from_vec", since = "1.66.0")]
1685-
impl<T, const N: usize> TryFrom<Vec<T>> for Box<[T; N]> {
1686-
type Error = Vec<T>;
1719+
#[allow(unused_braces)]
1720+
impl<T, const N: usize, const CO_ALLOC_PREF: CoAllocPref> TryFrom<Vec<T, Global, CO_ALLOC_PREF>>
1721+
for Box<[T; N]>
1722+
where
1723+
[(); { meta_num_slots_global!(CO_ALLOC_PREF) }]:,
1724+
{
1725+
type Error = Vec<T, Global, CO_ALLOC_PREF>;
16871726

16881727
/// Attempts to convert a `Vec<T>` into a `Box<[T; N]>`.
16891728
///
@@ -1703,7 +1742,7 @@ impl<T, const N: usize> TryFrom<Vec<T>> for Box<[T; N]> {
17031742
/// let state: Box<[f32; 100]> = vec![1.0; 100].try_into().unwrap();
17041743
/// assert_eq!(state.len(), 100);
17051744
/// ```
1706-
fn try_from(vec: Vec<T>) -> Result<Self, Self::Error> {
1745+
fn try_from(vec: Vec<T, Global, CO_ALLOC_PREF>) -> Result<Self, Self::Error> {
17071746
if vec.len() == N {
17081747
let boxed_slice = vec.into_boxed_slice();
17091748
Ok(unsafe { boxed_slice_as_array_unchecked(boxed_slice) })
@@ -2038,10 +2077,15 @@ impl<I> FromIterator<I> for Box<[I]> {
20382077

20392078
#[cfg(not(no_global_oom_handling))]
20402079
#[stable(feature = "box_slice_clone", since = "1.3.0")]
2041-
impl<T: Clone, A: Allocator + Clone> Clone for Box<[T], A> {
2080+
#[allow(unused_braces)]
2081+
impl<T: Clone, A: Allocator + Clone> Clone for Box<[T], A>
2082+
where
2083+
[(); { crate::meta_num_slots!(A, crate::CO_ALLOC_PREF_META_NO!()) }]:,
2084+
{
20422085
fn clone(&self) -> Self {
20432086
let alloc = Box::allocator(self).clone();
2044-
self.to_vec_in(alloc).into_boxed_slice()
2087+
// false = no need for co-alloc metadata, since it would get lost once converted to the boxed slice.
2088+
self.to_vec_in_co::<A, { CO_ALLOC_PREF_META_NO!() }>(alloc).into_boxed_slice()
20452089
}
20462090

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

0 commit comments

Comments
 (0)