Commit Graph

3960 Commits

Author SHA1 Message Date
Max Dexheimer
8bd0a5aff2 Refactor vec::IntoIter::into_vecdeque to fix unsoundness 2026-09-08 13:35:46 +02:00
Jonathan Brouwer
9ca49d6252 Rollup merge of #162212 - a1phyr:box_random, r=joboet
Implement `Rng` for `Box`

Tracking issue: https://github.com/rust-lang/rust/issues/130703

Like https://github.com/rust-lang/rust/pull/159435, this implementation must be implemented before the stabilization of `Rng`.

r? @joshtriplett
2026-09-04 22:29:12 +02:00
Jonathan Brouwer
8820516fcc Rollup merge of #162289 - nia-e:alloc-safety-comments-part-2, r=Darksonn
alloc: a bunch of safety comments

Following up from rust-lang/rust#160941. Triaging this is what found the errors in rust-lang/rust#162285 & rust-lang/rust#162286. More to come, but I didn't want to make the review effort too high on any single PR.

r? libs
2026-09-04 22:29:03 +02:00
Jonathan Brouwer
b48235859e Rollup merge of #162286 - nia-e:string-splice-nounwind, r=Darksonn
string: don't unwind prematurely

`splice` on a vec can technically unwind (since we still say `handle_alloc_error` *may* unwind in the future, and nightly users could trigger this via setting the alloc error hook) which would expose a string in an invalid state during `replace_range`. This adds an abort guard around it to ensure this is never triggered.

r? libs
2026-09-04 22:29:02 +02:00
Jonathan Brouwer
2e0b41bd13 Rollup merge of #162285 - nia-e:box-map-zst, r=JohnTitor
box: fixup map/try_map deallocate calls

`Box::{map, try_map}` unsoundly called `deallocate` on the previous box's pointer, even if it was a ZST. This was probably okay for `Global` but it's definitely not for arbitrary custom allocators.

r? libs
2026-09-04 22:29:02 +02:00
Nia Deckers
e57809063d typo 2026-09-04 14:24:27 +02:00
Nia Deckers
c66f1501c9 drop unnecessary feature gate 2026-09-04 14:13:47 +02:00
Nia Deckers
1c48bd65b3 review 2026-09-04 14:12:07 +02:00
Nia Deckers
22894e4c30 reserve ahead of time 2026-09-04 13:13:29 +02:00
Nia Deckers
b1a9fcaead alloc: a bunch of safety comments 2026-09-04 12:59:14 +02:00
Nia Deckers
8f1e14781d string: don't unwind prematurely 2026-09-04 11:57:26 +02:00
Nia Deckers
2a2b4c654e box: fixup map/try_map deallocate calls 2026-09-04 11:27:48 +02:00
Jonathan Brouwer
695248d0ed Rollup merge of #161893 - maxdexh:unique-arc-map, r=nia-e
Add custom allocator support to `(try_)map` on `UniqueArc` and `UniqueRc`

Follow-up to rust-lang/rust#161617.

This one required me to add allocator support to a lot more methods.
Probably good for them to have it for future additions ^^

The old, allocator-free methods went unused from this change and I had to remove them because we don't allow warnings (they are trivial to implement on top of the allocator ones). This had the side effect of making the diff a bit weird. Sorry!

r? nia-e
2026-09-03 20:02:52 +02:00
Benoît du Garreau
aea90f17e1 Implement Rng for Box 2026-09-03 20:02:13 +02:00
bors
edc52f87c2 Auto merge of #162128 - clarfonthey:revert-drop-guard, r=clarfonthey
Revert "Use `drop_guard` in some places in {core,alloc,std}"

cc @panstromek @GrigorenkoPV 

Reverts this PR due to perf issues, so that a fix can be verified to not affect perf. Apologies for not initially running perf; kinda tunnel-visioned "this just does the same thing" without considering the changes from non-generic functions to generic closures.
2026-09-02 01:22:53 +00:00
Jonathan Brouwer
51998dbbf8 Rollup merge of #162102 - DanielEScherzer:less-unsafe-in-alloc, r=clarfonthey
`alloc` crate: shrink undocumented `unsafe` blocks

Reduce the scope of `unsafe` blocks that are not yet documented by moving safe operations out of the blocks, making it easier to add the missing documentation in the future.
2026-09-01 21:26:19 +02:00
Jonathan Brouwer
e2897d777d Rollup merge of #162073 - camsteffen:lib-eat-never, r=clarfonthey
Change some `Infallible` to `!` in std

I believe this desirable since we recommend `!` over `Infallible`, so std follows its own advice. Most changes are to unstable features, but not all. Similar changes in compiler have to wait for a bootstrap update.
2026-09-01 21:26:17 +02:00
Max Dexheimer
7791246b91 Add custom allocator support to (try_)map on UniqueArc and UniqueRc 2026-09-01 19:50:35 +02:00
Cameron Steffen
dd132214ed Dogfood never type in std 2026-09-01 08:57:20 -05:00
ltdk
7755d45ec9 Revert "Use drop_guard in some places in {core,alloc,std}"
This reverts commit 4f404c1f7e.
2026-09-01 09:37:27 -04:00
Jonathan Brouwer
dcb658289c Rollup merge of #160534 - Qelxiros:stabilize-smart-pointer-map, r=nia-e
stabilize smart pointer map functions

Tracking issue: rust-lang/rust#144419
@rustbot modify labels: +T-libs-api
2026-09-01 13:27:36 +02:00
Daniel Scherzer
b5e834472f alloc crate: shrink undocumented unsafe blocks
Reduce the scope of `unsafe` blocks that are not yet documented by moving safe
operations out of the blocks, making it easier to add the missing documentation
in the future.
2026-08-31 21:24:01 -07:00
Jacob Pratt
99e83a5e18 Rollup merge of #161379 - steffahn:extend_trait_param_name, r=JohnTitor
Use better generic type parameter names for `Extend` and `FromIterator`

Change
```rs
pub trait Extend<A> {
    fn extend<T: IntoIterator<Item = A>>(&mut self, iter: T);
    …
}
```
to
```rs
pub trait Extend<T> {
    fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I);
    …
}
```

to be more consistent with common use of `T` as the item type in containers, and their `Extend` impls; and use `I` as the name for the `IntoIterator` type.

also do the basically the same thing for `FromIterator<A>`→`FromIterator<T>` as well

cases of these parameters in trait impls for the abovementioned traits.

These changes obviously don't change/break behavior of the traits and its users, but the improve the way the documentation of the traits renders (the improvement is the more sensible and more consistent names for the parameters). The only downside I'm aware of (besides that this is touching quite a few files..) is that this does affect some link anchor names in *some* cases. E.g.
```diff
- impl<A, V: FromIterator<A>> FromIterator<Option<A>> for Option<V> {
+ impl<T, V: FromIterator<T>> FromIterator<Option<T>> for Option<V> {
```
would mean that [doc.rust-lang.org/core/iter/trait.FromIterator.html#impl-FromIterator<Option\<A>>-for-Option\<V>](https://doc.rust-lang.org/core/iter/trait.FromIterator.html#impl-FromIterator%3COption%3CA%3E%3E-for-Option%3CV%3E) no longer works, as it changes `…#impl-FromIterator<Option<A>>-for-Option<V>` to `…#impl-FromIterator<Option<T>>-for-Option<V>`
2026-08-31 02:24:56 -04:00
Frank Steffahn
9bca736808 More consistent use of variable names for trait implementations 2026-08-31 01:11:58 +02:00
Pavel Grigorenko
4f404c1f7e Use drop_guard in some places in {core,alloc,std} 2026-08-30 17:08:08 +03:00
Jeremy Smart
82ee18a644 stabilize map functions 2026-08-29 17:34:04 -04:00
Guillaume Gomez
a083cee009 Rollup merge of #161975 - matthew-demidoff:btree-split-off-panic-safety, r=nia-e
Abort instead of unwinding out of an inconsistent BTreeMap::split_off

Fixes rust-lang/rust#158165. Supersedes rust-lang/rust#161784, which I had to abandon after a bad force-push from a shallow clone left its head detached.

`BTreeMap::split_off` runs the caller's `Ord`/`Borrow` impl via `search_node` inside `Root::split_off`'s descent. After the first `move_suffix`, the two roots alias the same values through structurally invalid trees until the borders are fixed; a comparator panic there unwinds with a stale length and double-frees on later iteration or drop. It is reachable from `#![forbid(unsafe_code)]` on stable (reproducer on the issue).

Guard the descent loop with an abort-on-panic `PanicGuard`, as `btree::mem::replace` already does. `catch_unwind` isnt available in `alloc` (no_std), and I kept an inline guard rather than the unstable `DropGuard`. It is armed once before the loop; the first `search_node` stays outside it, since a panic there can still unwind safely (nothing has moved yet).

Verified with Miri: the reproducer goes from a double-free to a clean abort, and normal multi-level splits are unaffected. Adds a happy-path regression test.

Credit to @ostrowr for the report and to rust-lang/rust#158710 for the original approach.

r? @nia-e
2026-08-29 22:46:00 +02:00
Matthew Demidoff
303dafdc11 Abort instead of unwinding out of an inconsistent BTreeMap::split_off
After the first move_suffix in Root::split_off, the source and result
trees share values through two temporarily invalid structures. If a
later key comparison panics, unwinding leaves the map with a stale
length over a partially detached tree, and consuming iteration then
double-frees the shared values, reachable from safe code.

Guard the descent loop with a mem::DropGuard that aborts on unwind,
armed before the first move_suffix and dismissed once the borders are
fixed.
2026-08-29 19:19:26 +02:00
Nia Deckers
483873e1e8 update some comments 2026-08-28 23:56:17 +02:00
Nia Deckers
f1f423d070 tidy: enforce documented unsafe in alloc
safety comments in alloc

this was pain and it's not even half i think

oepsje woepsje
2026-08-28 23:56:11 +02:00
Jacob Pratt
97a7674c40 Rollup merge of #161034 - ywxt:retian-sve, r=Amanieu
Add SVE-accelerated Vec::retain_mut for aarch64

The PR adds SVE support for specified width types(8, 16, 32 and 64 bits) in `Vec::retain_mut`. Due to [pointer provenance being stripped by intrinsics](https://rust-lang.zulipchat.com/#narrow/channel/208962-t-libs.2Fstdarch/topic/MaybeUninit.20lane.20variants.20for.20vector.20data-movement.20intrinsic/with/615526760)) here it has to use inline asm instead of sve intrinsics.

## 1. retain half (ns/iter)

| Elements | u32 SVE | u32 scalar | Change | u64 SVE | u64 scalar | Change |
|---|---|---|---|---|---|---|
| 4 | 10.32 | 12.36 | / | 10.25 | 11.83 | / |
| 8 | 12.93 | 15.08 | / | 13.40 | 14.74 | / |
| 16 | 18.66 | 19.46 | / | 19.08 | 18.89 | / |
| 32 | 31.84 | 31.12 |/ | 31.78 | 31.26 | / |
| 64 | 32.99 | 59.17 | **-44.2%** | 59.86 | 59.44 | / |
| 1,000 | 471.51 | 811.71 | **-41.9%** | 820.98 | 827.16 | / |
| 10,000 | 4,660 | 7,990 | **-41.7%** | 5,836 | 8,242 | **-29.2%** |
| 100,000 | 46,414 | 79,608 | **-41.7%** | 57,561 | 82,861 | **-30.5%** |

## 2. retain whole

| Elements | u32 SVE | u32 scalar | Change | u64 SVE | u64 scalar | Change |
|---|---|---|---|---|---|---|
| 4 | 3.46 | 3.11 | / | 3.45 | 3.45 | / |
| 8 | 4.90 | 4.49 | / | 4.83 | 6.22 | / |
| 16 | 8.44 | 8.14 | / | 8.44 | 11.74 | / |
| 32 | 15.83 | 15.51 | / | 15.83 | 22.79 | / |
| 64 | 21.57 | 30.66 | **-29.6%** | 30.72 | 44.89 | / |
| 1,000 | 358.53 | 483.33 | **-25.8%** | 469.18 | 696.43 | / |
| 10,000 | 3,127 | 4,745 | **-34.1%** | 5,082 | 6,912 | **-26.5%** |
| 100,000 | 32,509 | 49,501 | **-34.3%** | 49,484 | 79,749 | **-38.0%** |

r? @Amanieu
2026-08-27 00:40:20 -04:00
Jonathan Brouwer
35df304c76 Rollup merge of #150067 - fereidani:string_retain, r=joboet
Alloc `String::retain` optimization

Hi again!
This uses the exact same algorithm as my other PR: https://github.com/rust-lang/rust/pull/149784

Technically it should improve performance for `String::retain` too, But let's see what bors thinks.
2026-08-26 17:23:18 +02:00
Jonathan Brouwer
f6d0a0ada9 Rollup merge of #161684 - maxdexh:smoke-trees, r=nia-e
Replace `Allocator + Clone` with `AllocatorClone` in btree

powered by ripgrep

r? nia-e
2026-08-25 23:01:09 +02:00
Jonathan Brouwer
f29a880d9e Rollup merge of #161328 - ChrisDenton:clippy3, r=nia-e
Enforce even more library clippy lints in CI

Once again, this is best reviewed commit-by-commit since each commit fixes a lint.

I've bundled together a large(ish) number of lints because I feel they shouldn't be controversial and don't touch too much code. A large chunk of them is just telling clippy that, for example, implementing `is_digit` by calling `is_digit` just isn't going to work. I have reviewed each and every fix myself but please do double or triple check my working!
2026-08-25 23:01:08 +02:00
Chris Denton
f591acd10f clippy::assign_op_pattern 2026-08-25 18:15:15 +00:00
Chris Denton
836a20fc89 clippy::mem_replace_option_with_none 2026-08-25 18:15:12 +00:00
Chris Denton
4b68669089 clippy::double_must_use 2026-08-25 18:15:10 +00:00
Chris Denton
5051bc382a clippy::redundant_slicing 2026-08-25 18:15:08 +00:00
Chris Denton
64393ebfcf clippy::manual_clear 2026-08-25 18:15:07 +00:00
Chris Denton
097f843c2d clippy::derivable_impls 2026-08-25 18:15:06 +00:00
Chris Denton
a7f7b19ad6 clippy::redundant_closure 2026-08-25 18:15:03 +00:00
Chris Denton
28f78d48ee clippy::default_constructed_unit_structs 2026-08-25 18:09:27 +00:00
Chris Denton
abce09f22b clippy::partialeq_ne_impl 2026-08-25 18:09:24 +00:00
Jonathan Brouwer
a725c3e72b Rollup merge of #161617 - maxdexh:can-i-has-map-allocator, r=nia-e
Add custom allocators to `(try_)map` on `Box`, `Rc`, `Arc`

Adds custom allocators to `(try_)map` on `Box`, `Rc`, `Arc`.

Not on `UniqueArc`/`UniqueRc` because too much allocator-related API is missing atm and I didn't want to end up writing unsound garbage. The `else` branch in those needs to drop a weakref, I'll get to that later.

cc rust-lang/rust#160534

r? nia-e
2026-08-25 18:00:02 +02:00
ywxt
78b1808129 Add SVE-accelerated Vec::retain_mut for aarch64 2026-08-25 16:33:39 +08:00
Khashayar Fereidani
6a24a6e47c Improve and optimize string::retain implementation 2026-08-24 22:30:28 +03:30
Max Dexheimer
bd72f7ac4c Replace Allocator + Clone with AllocatorClone in btree 2026-08-24 19:21:59 +02:00
Waffle Lapkin
3de37a208a remove #![feature(never_type)] from library 2026-08-24 19:04:00 +02:00
Max Dexheimer
c158e17996 Add custom allocators to (try_)map on Box, Rc, Arc 2026-08-23 21:04:46 +02:00
Jacob Pratt
7e191f1fbe Rollup merge of #156160 - kx0101:cow-vec-symmetric-partialeq, r=Mark-Simulacrum
feat: add symmetric PartialEq impls for Vec, &[T], &mut [T] versus Cow<'_, [T]>

add the missing reverse `PartialEq<Cow<'_, [U]>>` impls for `Vec<T, A>`, `&[T]`, and `&mut [T]`, essentially mirroring the existing forwards in `library/alloc/src/vec/partial_eq.rs`

partially addresses rust-lang/rust#152830. The `VecDeque` half of that issue is being handled separately by rust-lang/rust#152972, so there is no overlap with this PR

also fyi: verified locally with `./x test library/alloctests --stage 1` and the new `test_partial_eq_cow_symmetric` test passes alongside the existing alloc test suite
2026-08-22 19:37:12 -04:00