Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/rustc_hir/src/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ language_item_table! {

OwnedBox, sym::owned_box, owned_box, Target::Struct, GenericRequirement::Minimum(1);
GlobalAlloc, sym::global_alloc_ty, global_alloc_ty, Target::Struct, GenericRequirement::None;
NoaliasAllocator, sym::noalias_allocator, noalias_allocator_trait, Target::Trait, GenericRequirement::None;

PhantomData, sym::phantom_data, phantom_data, Target::Struct, GenericRequirement::Exact(1);

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1397,6 +1397,7 @@ symbols! {
no_sanitize,
no_stack_check,
no_std,
noalias_allocator,
nomem,
non_ascii_idents,
non_exhaustive,
Expand Down
10 changes: 10 additions & 0 deletions library/alloc/src/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,19 @@ unsafe extern "Rust" {
#[unstable(feature = "allocator_api", issue = "32838")]
#[derive(Copy, Clone, Default, Debug)]
// the compiler needs to know when a Box uses the global allocator vs a custom one
// FIXME(nia-e): change everything to use the noalias_allocator lang item instead
#[lang = "global_alloc_ty"]
pub struct Global;

#[unstable(feature = "allocator_api", issue = "32838")]
unsafe impl core::alloc::AllocatorClone for Global {}

#[unstable(feature = "allocator_api", issue = "32838")]
unsafe impl core::alloc::StaticAllocator for Global {}

#[unstable(feature = "allocator_api", issue = "32838")]
unsafe impl core::alloc::NoaliasAllocator for Global {}

/// Allocates memory with the global allocator.
///
/// This function forwards calls to the [`GlobalAlloc::alloc`] method
Expand Down
34 changes: 26 additions & 8 deletions library/alloc/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,12 @@ use core::task::{Context, Poll};

#[cfg(not(no_global_oom_handling))]
use crate::alloc::handle_alloc_error;
use crate::alloc::{AllocError, Allocator, Global, Layout};
use crate::alloc::{
AllocError, Allocator, AllocatorClone, AllocatorEq, Global, Layout, StaticAllocator,
};
use crate::raw_vec::RawVec;
#[cfg(not(no_global_oom_handling))]
use crate::str::from_boxed_utf8_unchecked;
use crate::str::from_boxed_utf8_unchecked_in;

/// Conversion related impls for `Box<_>` (`From`, `downcast`, etc)
mod convert;
Expand Down Expand Up @@ -710,7 +712,7 @@ impl<T, A: Allocator> Box<T, A> {
#[inline(always)]
pub fn pin_in(x: T, alloc: A) -> Pin<Self>
where
A: 'static + Allocator,
A: StaticAllocator,
{
Self::into_pin(Self::new_in(x, alloc))
}
Expand Down Expand Up @@ -1935,7 +1937,7 @@ impl<T: ?Sized, A: Allocator> Box<T, A> {
#[stable(feature = "box_into_pin", since = "1.63.0")]
pub fn into_pin(boxed: Self) -> Pin<Self>
where
A: 'static,
A: StaticAllocator,
{
// It's not possible to move or replace the insides of a `Pin<Box<T>>`
// when `T: !Unpin`, so it's safe to pin it directly without any
Expand Down Expand Up @@ -2109,11 +2111,10 @@ impl<T: Clone, A: Allocator + Clone> Clone for Box<[T], A> {

#[cfg(not(no_global_oom_handling))]
#[stable(feature = "box_slice_clone", since = "1.3.0")]
impl Clone for Box<str> {
impl<A: Allocator + Clone> Clone for Box<str, A> {
fn clone(&self) -> Self {
// this makes a copy of the data
let buf: Box<[u8]> = self.as_bytes().into();
unsafe { from_boxed_utf8_unchecked(buf) }
let buf = Box::clone_from_ref_in(self.as_bytes(), self.1.clone());
unsafe { from_boxed_utf8_unchecked_in(buf) }
}
}

Expand Down Expand Up @@ -2485,3 +2486,20 @@ unsafe impl<T: ?Sized + Allocator, A: Allocator> Allocator for Box<T, A> {
unsafe { (**self).shrink(ptr, old_layout, new_layout) }
}
}

#[unstable(feature = "allocator_api", issue = "32838")]
unsafe impl<T, A> AllocatorClone for Box<T, A>
where
T: AllocatorClone,
Comment thread
nia-e marked this conversation as resolved.
A: Allocator,
Box<T, A>: Clone,
{
}

#[unstable(feature = "allocator_api", issue = "32838")]
unsafe impl<T, A> AllocatorEq for Box<T, A>
where
T: AllocatorEq + ?Sized,
A: Allocator,
{
}
4 changes: 2 additions & 2 deletions library/alloc/src/boxed/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use core::fmt;
use core::mem;
use core::pin::Pin;

use crate::alloc::Allocator;
use crate::alloc::{Allocator, StaticAllocator};
#[cfg(not(no_global_oom_handling))]
use crate::borrow::Cow;
use crate::boxed::Box;
Expand Down Expand Up @@ -38,7 +38,7 @@ impl<T> From<T> for Box<T> {
#[stable(feature = "pin", since = "1.33.0")]
impl<T: ?Sized, A: Allocator> From<Box<T, A>> for Pin<Box<T, A>>
where
A: 'static,
A: StaticAllocator,
{
/// Converts a `Box<T>` into a `Pin<Box<T>>`. If `T` does not implement [`Unpin`], then
/// `*boxed` will be pinned in memory and unable to be moved.
Expand Down
24 changes: 12 additions & 12 deletions library/alloc/src/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ use core::{borrow, fmt, hint};

#[cfg(not(no_global_oom_handling))]
use crate::alloc::handle_alloc_error;
use crate::alloc::{AllocError, Allocator, Global, Layout};
use crate::alloc::{AllocError, Allocator, AllocatorClone, Global, Layout};
use crate::borrow::{Cow, ToOwned};
use crate::boxed::Box;
#[cfg(not(no_global_oom_handling))]
Expand Down Expand Up @@ -1773,7 +1773,7 @@ impl<T: ?Sized, A: Allocator> Rc<T, A> {
#[stable(feature = "rc_weak", since = "1.4.0")]
pub fn downgrade(this: &Self) -> Weak<T, A>
where
A: Clone,
A: AllocatorClone,
{
this.inner().inc_weak();
// Make sure we do not create a dangling Weak
Expand Down Expand Up @@ -1854,7 +1854,7 @@ impl<T: ?Sized, A: Allocator> Rc<T, A> {
#[unstable(feature = "allocator_api", issue = "32838")]
pub unsafe fn increment_strong_count_in(ptr: *const T, alloc: A)
where
A: Clone,
A: AllocatorClone,
{
// Retain Rc, but don't touch refcount by wrapping in ManuallyDrop
let rc = unsafe { mem::ManuallyDrop::new(Rc::<T, A>::from_raw_in(ptr, alloc)) };
Expand Down Expand Up @@ -2030,7 +2030,7 @@ impl<T: ?Sized, A: Allocator> Rc<T, A> {
}

#[cfg(not(no_global_oom_handling))]
impl<T: ?Sized + CloneToUninit, A: Allocator + Clone> Rc<T, A> {
impl<T: ?Sized + CloneToUninit, A: AllocatorClone> Rc<T, A> {
/// Makes a mutable reference into the given `Rc`.
///
/// If there are other `Rc` pointers to the same allocation, then `make_mut` will
Expand Down Expand Up @@ -2305,7 +2305,7 @@ impl<T: ?Sized, A: Allocator> Rc<T, A> {

// Free the allocation without dropping its contents
let (bptr, alloc) = Box::into_raw_with_allocator(src);
let src = Box::from_raw_in(bptr as *mut mem::ManuallyDrop<T>, alloc.by_ref());
let src = Box::from_raw_in(bptr as *mut mem::ManuallyDrop<T>, &alloc);
drop(src);

Self::from_ptr_in(ptr, alloc)
Expand Down Expand Up @@ -2494,7 +2494,7 @@ unsafe impl<#[may_dangle] T: ?Sized, A: Allocator> Drop for Rc<T, A> {
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized, A: Allocator + Clone> Clone for Rc<T, A> {
impl<T: ?Sized, A: AllocatorClone> Clone for Rc<T, A> {
/// Makes a clone of the `Rc` pointer.
///
/// This creates another pointer to the same allocation, increasing the
Expand All @@ -2519,10 +2519,10 @@ impl<T: ?Sized, A: Allocator + Clone> Clone for Rc<T, A> {
}

#[unstable(feature = "ergonomic_clones", issue = "132290")]
impl<T: ?Sized, A: Allocator + Clone> UseCloned for Rc<T, A> {}
impl<T: ?Sized, A: AllocatorClone> UseCloned for Rc<T, A> {}

#[unstable(feature = "share_trait", issue = "156756")]
impl<T: ?Sized, A: Allocator + Clone> Share for Rc<T, A> {}
impl<T: ?Sized, A: AllocatorClone> Share for Rc<T, A> {}

#[cfg(not(no_global_oom_handling))]
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down Expand Up @@ -3543,7 +3543,7 @@ impl<T: ?Sized, A: Allocator> Weak<T, A> {
#[stable(feature = "rc_weak", since = "1.4.0")]
pub fn upgrade(&self) -> Option<Rc<T, A>>
where
A: Clone,
A: AllocatorClone,
{
let inner = self.inner()?;

Expand Down Expand Up @@ -3688,7 +3688,7 @@ unsafe impl<#[may_dangle] T: ?Sized, A: Allocator> Drop for Weak<T, A> {
}

#[stable(feature = "rc_weak", since = "1.4.0")]
impl<T: ?Sized, A: Allocator + Clone> Clone for Weak<T, A> {
impl<T: ?Sized, A: AllocatorClone> Clone for Weak<T, A> {
/// Makes a clone of the `Weak` pointer that points to the same allocation.
///
/// # Examples
Expand All @@ -3710,7 +3710,7 @@ impl<T: ?Sized, A: Allocator + Clone> Clone for Weak<T, A> {
}

#[unstable(feature = "ergonomic_clones", issue = "132290")]
impl<T: ?Sized, A: Allocator + Clone> UseCloned for Weak<T, A> {}
impl<T: ?Sized, A: AllocatorClone> UseCloned for Weak<T, A> {}

#[stable(feature = "rc_weak", since = "1.4.0")]
impl<T: ?Sized, A: Allocator> fmt::Debug for Weak<T, A> {
Expand Down Expand Up @@ -4410,7 +4410,7 @@ impl<T: ?Sized, A: Allocator> UniqueRc<T, A> {
}
}

impl<T: ?Sized, A: Allocator + Clone> UniqueRc<T, A> {
impl<T: ?Sized, A: AllocatorClone> UniqueRc<T, A> {
/// Creates a new weak reference to the `UniqueRc`.
///
/// Attempting to upgrade this weak reference will fail before the `UniqueRc` has been converted
Expand Down
14 changes: 14 additions & 0 deletions library/alloc/src/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,20 @@ pub unsafe fn from_boxed_utf8_unchecked(v: Box<[u8]>) -> Box<str> {
unsafe { Box::from_raw(Box::into_raw(v) as *mut str) }
}

/// Converts a boxed slice of bytes to a boxed string slice without checking
/// that the string contains valid UTF-8 generically over the box's allocator.
///
/// # Safety
///
/// * The provided bytes must contain a valid UTF-8 sequence.
#[unstable(feature = "allocator_api", issue = "32838")]
pub unsafe fn from_boxed_utf8_unchecked_in<A: crate::alloc::Allocator>(
v: Box<[u8], A>,
) -> Box<str, A> {
let (ptr, alloc) = Box::into_raw_with_allocator(v);
unsafe { Box::from_raw_in(ptr as *mut str, alloc) }
}

/// Converts leading ascii bytes in `s` by calling the `convert` function.
///
/// For better average performance, this happens in chunks of `2*size_of::<usize>()`.
Expand Down
26 changes: 13 additions & 13 deletions library/alloc/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use core::{borrow, fmt, hint};

#[cfg(not(no_global_oom_handling))]
use crate::alloc::handle_alloc_error;
use crate::alloc::{AllocError, Allocator, Global, Layout};
use crate::alloc::{AllocError, Allocator, AllocatorClone, Global, Layout};
use crate::borrow::{Cow, ToOwned};
use crate::boxed::Box;
use crate::rc::is_dangling;
Expand Down Expand Up @@ -1931,7 +1931,7 @@ impl<T: ?Sized, A: Allocator> Arc<T, A> {
#[stable(feature = "arc_weak", since = "1.4.0")]
pub fn downgrade(this: &Self) -> Weak<T, A>
where
A: Clone,
A: AllocatorClone,
{
// This Relaxed is OK because we're checking the value in the CAS
// below.
Expand Down Expand Up @@ -2062,7 +2062,7 @@ impl<T: ?Sized, A: Allocator> Arc<T, A> {
#[unstable(feature = "allocator_api", issue = "32838")]
pub unsafe fn increment_strong_count_in(ptr: *const T, alloc: A)
where
A: Clone,
A: AllocatorClone,
{
// Retain Arc, but don't touch refcount by wrapping in ManuallyDrop
let arc = unsafe { mem::ManuallyDrop::new(Arc::from_raw_in(ptr, alloc)) };
Expand Down Expand Up @@ -2250,7 +2250,7 @@ impl<T: ?Sized, A: Allocator> Arc<T, A> {

// Free the allocation without dropping its contents
let (bptr, alloc) = Box::into_raw_with_allocator(src);
let src = Box::from_raw_in(bptr as *mut mem::ManuallyDrop<T>, alloc.by_ref());
let src = Box::from_raw_in(bptr as *mut mem::ManuallyDrop<T>, &alloc);
drop(src);

Self::from_ptr_in(ptr, alloc)
Expand Down Expand Up @@ -2376,7 +2376,7 @@ impl<T: TrivialClone> ArcFromSlice<T> for Arc<[T]> {
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized, A: Allocator + Clone> Clone for Arc<T, A> {
impl<T: ?Sized, A: AllocatorClone> Clone for Arc<T, A> {
/// Makes a clone of the `Arc` pointer.
///
/// This creates another pointer to the same allocation, increasing the
Expand Down Expand Up @@ -2430,10 +2430,10 @@ impl<T: ?Sized, A: Allocator + Clone> Clone for Arc<T, A> {
}

#[unstable(feature = "ergonomic_clones", issue = "132290")]
impl<T: ?Sized, A: Allocator + Clone> UseCloned for Arc<T, A> {}
impl<T: ?Sized, A: AllocatorClone> UseCloned for Arc<T, A> {}

#[unstable(feature = "share_trait", issue = "156756")]
impl<T: ?Sized, A: Allocator + Clone> Share for Arc<T, A> {}
impl<T: ?Sized, A: AllocatorClone> Share for Arc<T, A> {}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized, A: Allocator> Deref for Arc<T, A> {
Expand All @@ -2455,7 +2455,7 @@ unsafe impl<T: ?Sized, A: Allocator> DerefPure for Arc<T, A> {}
impl<T: ?Sized> LegacyReceiver for Arc<T> {}

#[cfg(not(no_global_oom_handling))]
impl<T: ?Sized + CloneToUninit, A: Allocator + Clone> Arc<T, A> {
impl<T: ?Sized + CloneToUninit, A: AllocatorClone> Arc<T, A> {
/// Makes a mutable reference into the given `Arc`.
///
/// If there are other `Arc` pointers to the same allocation, then `make_mut` will
Expand Down Expand Up @@ -3274,7 +3274,7 @@ impl<T: ?Sized, A: Allocator> Weak<T, A> {
#[stable(feature = "arc_weak", since = "1.4.0")]
pub fn upgrade(&self) -> Option<Arc<T, A>>
where
A: Clone,
A: AllocatorClone,
{
#[inline]
fn checked_increment(n: usize) -> Option<usize> {
Expand Down Expand Up @@ -3409,7 +3409,7 @@ impl<T: ?Sized, A: Allocator> Weak<T, A> {
}

#[stable(feature = "arc_weak", since = "1.4.0")]
impl<T: ?Sized, A: Allocator + Clone> Clone for Weak<T, A> {
impl<T: ?Sized, A: AllocatorClone> Clone for Weak<T, A> {
/// Makes a clone of the `Weak` pointer that points to the same allocation.
///
/// # Examples
Expand Down Expand Up @@ -3441,7 +3441,7 @@ impl<T: ?Sized, A: Allocator + Clone> Clone for Weak<T, A> {
}

#[unstable(feature = "ergonomic_clones", issue = "132290")]
impl<T: ?Sized, A: Allocator + Clone> UseCloned for Weak<T, A> {}
impl<T: ?Sized, A: AllocatorClone> UseCloned for Weak<T, A> {}

#[stable(feature = "downgraded_weak", since = "1.10.0")]
impl<T> Default for Weak<T> {
Expand Down Expand Up @@ -4029,7 +4029,7 @@ impl<T: ?Sized, A: Allocator> From<Box<T, A>> for Arc<T, A> {

#[cfg(not(no_global_oom_handling))]
#[stable(feature = "shared_from_slice", since = "1.21.0")]
impl<T, A: Allocator + Clone> From<Vec<T, A>> for Arc<[T], A> {
impl<T, A: AllocatorClone> From<Vec<T, A>> for Arc<[T], A> {
/// Allocates a reference-counted slice and moves `v`'s items into it.
///
/// # Example
Expand Down Expand Up @@ -4839,7 +4839,7 @@ impl<T: ?Sized, A: Allocator> UniqueArc<T, A> {
}
}

impl<T: ?Sized, A: Allocator + Clone> UniqueArc<T, A> {
impl<T: ?Sized, A: AllocatorClone> UniqueArc<T, A> {
/// Creates a new weak reference to the `UniqueArc`.
///
/// Attempting to upgrade this weak reference will fail before the `UniqueArc` has been converted
Expand Down
7 changes: 0 additions & 7 deletions library/alloctests/tests/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,6 @@ unsafe impl Allocator for ConstAllocator {
}
Ok(new_ptr)
}

fn by_ref(&self) -> &Self
where
Self: Sized,
{
self
}
}

#[allow(unused)]
Expand Down
Loading
Loading