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
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
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
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
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.
`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.
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.
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.
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>`
Abort instead of unwinding out of an inconsistent BTreeMap::split_off
Fixesrust-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
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.
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.
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!
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
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