Report "capacity overflow" for oversized Rc<[T]>/Arc<[T]>
Fixesrust-lang/rust#136797.
Building an Rc<[T]>/Arc<[T]> whose header + payload layout exceeds
isize::MAX unwrapped a LayoutError and panicked with "called
Result::unwrap() on an Err value: LayoutError". Vec and Box report
"capacity overflow" here; this makes Rc/Arc do the same.
Used an inline panic!("capacity overflow") rather than raw_vec's
capacity_overflow(), since that helper is cfg'd out under
no_global_oom_handling while these two layout fns still compile there.
https://github.com/rust-lang/rust/pull/158547 moved `std::io::BufWriter` to
`alloc::io::BufWriter`. That allows it to be used in `no-std` configurations,
and in particular on platforms where unwinding isn't supported.
However one of the doc tests uses `catch_unwind`, which fails on platforms
which cannot unwind. Fix this by copying the magic incantation from a similar
doctest in library/core/src/range.rs
Partially stabilize `box_vec_non_null`
Closesrust-lang/rust#130364
r? libs-api
### What's being stabilized
The following is being stabilized in this PR:
```rust
impl<T: ?Sized> Box<T> {
pub unsafe fn from_non_null(ptr: NonNull<T>) -> Self { .... }
pub fn into_non_null(b: Self) -> NonNull<T> { .... }
}
impl<T> Vec<T> {
pub const unsafe fn from_parts(ptr: NonNull<T>, length: usize, capacity: usize) -> Self { .... }
pub const fn into_parts(self) -> (NonNull<T>, usize, usize) { .... }
}
```
Note that `Vec::from_parts` and `Vec::into_parts` remain const-unstable behind the `const_heap` feature (like `Vec::from_raw_parts` and `Vec::into_raw_parts`).
The following APIs remain gated behind `allocator_api`
```rust
impl<T: ?Sized, A: Allocator> Box<T, A> {
pub unsafe fn from_non_null_in(raw: NonNull<T>, alloc: A) -> Self { .... }
pub fn into_non_null_with_allocator(b: Self) -> (NonNull<T>, A) { .... }
}
impl<T, A: Allocator> Vec<T, A> {
pub const unsafe fn from_parts_in(ptr: NonNull<T>, length: usize, capacity: usize, alloc: A) -> Self { .... }
pub const fn into_parts_with_alloc(self) -> (NonNull<T>, usize, usize, A) { .... }
}
```
The following API is split into a new unstable feature in rust-lang/rust#157843, and remains unstable:
```rust
impl<T, A: Allocator> Vec<T, A> {
pub const fn as_non_null(&mut self) -> NonNull<T> { .... }
}
```
### Implementation History
Most of this feature was ACP'ed in https://github.com/rust-lang/libs-team/issues/418, and implemented in https://github.com/rust-lang/rust/pull/130061. `Vec::as_non_null` was ACP'ed separately in https://github.com/rust-lang/libs-team/issues/440, and implemented in https://github.com/rust-lang/rust/pull/130624.
### Potential issues
* Should `Vec::from_parts` and `Vec::into_parts` be the name used for this? Or should those names be used for functions that take/return `Box<[MaybeUninit<T>]>`? (Raised at https://github.com/rust-lang/rust/issues/130364#issuecomment-2357276951)
* Should these function names be named `non_null` or `nonnull`? (Raised at https://github.com/rust-lang/rust/issues/130364#issuecomment-4525680948. See https://github.com/rust-lang/rust/issues/154237#issuecomment-4120073976.)
* Will the methods on `Vec` collide with anything via autoderef?
Update expect messages in library/alloc/boxed.rs and library/alloc/string.rs to follow the style guide
Related issue: rust-lang/rust#159751
Updates the expect messages in library/alloc/boxed.rs and library/alloc/string.rs to follow the style guide.
Updated expect messages for `CString` struct and method documentation
Completes a task with rust-lang/rust#159751. Updates the expect messages in `library/alloc/src/ffi/c_str.rs`.
document #[global_allocator] constraints
`#[global_allocator]` is opsem magic. For instance, even if we do
```rust
use std::alloc::System;
#[global_allocator]
static ALLOC: System = System;
```
it is still UB to allocate via `std::alloc::alloc()` and deallocate via `System.dealloc()`. This is because we add special magic LLVM attributes in the `#[global_allocator]` machinery that tell LLVM about our global allocation methods so it can optimize accordingly, and mixing allocators is against those rules.
Cc @rust-lang/opsem @rust-lang/lang @nia-e @maxdexh
@nikic is this enough to ensure that adding your intrinsics is sound?
Constructing an Rc<[T]> or Arc<[T]> whose header + payload layout exceeds
isize::MAX unwrapped a LayoutError, panicking with the opaque "called
`Result::unwrap()` on an `Err` value: LayoutError". Match Vec and Box by
reporting "capacity overflow" instead, and add regression tests.
str: add ASCII fast path to word_to_titlecase
The word_to_titlecase function had a FIXME comment since a long time asking to add a fast path for ASCII text.
Before this change, even simple English text like "hello world" was going through slow Unicode lookup tables. That was unnecessary.
This fixes it by using the same trick that to_lowercase already uses — plain English letters are now handled much faster, and only Greek/Chinese/etc still use the Unicode tables.
No behavior changes, just faster for normal English text.
constify `vec![1, 2, 3]` macro
Tracking issues:
`const_heap`: https://github.com/rust-lang/rust/issues/79597
makes all the parts needed for non-empty `vec![]` macros const:
`alloc::boxed::box_assume_init_into_vec_unsafe`
`alloc::boxed::Box::assume_init`
`alloc::boxed::Box::into_raw_with_allocator`
`alloc::boxed::Box::new_uninit`
`alloc::slice::[T]::into_vec`
Note that this does not allow for the use of the `vec![(); 4]` arm of this macro to be used in const-eval, since that uses specialization (spec and const traits don't really like each other so I didn't want to touch any of that in this).
Make `Global` and `System` allocators unstably implement `const Clone + const Default`.
Allocators implementing `const Default` is useful for generically constructing empty collections inside of constants:
```rust
struct Foo<A: Allocator>(Vec<i32, A>);
impl<A: Allocator + const Default> Foo<A> {
pub const EMPTY: Self = Self(Vec::new_in(A::default()));
}
```
`const Clone` is not strictly necessary to permit such constants to exist, but may make more complex code easier to write (e.g. so a const constructor can accept one allocator and clone it).
@rustbot label +A-allocators