summary refs log tree commit diff
path: root/library/alloc
AgeCommit message (Collapse)AuthorLines
2024-02-03Bump version placeholdersMark Rousskov-2/+2
2024-01-31Rollup merge of #120355 - the8472:doc-vec-fromiter, r=cuviperNadrieril-0/+44
document `FromIterator for Vec` allocation behaviors [t-libs discussion](https://rust-lang.zulipchat.com/#narrow/stream/259402-t-libs.2Fmeetings/topic/Meeting.202024-01-24/near/417686526) about #120091 didn't reach a strong consensus, but it was agreed that if we keep the current behavior it should at least be documented even though it is an implementation detail. The language is intentionally non-committal. The previous (non-existent) documentation permits a lot of implementation leeway and we want retain that. In some cases we even must retain it to be able to rip out some code paths that rely on unstable features.
2024-01-30Apply suggestions from code reviewthe8472-12/+11
Co-authored-by: Josh Stone <cuviper@gmail.com>
2024-01-30document `FromIterator for Vec` allocation behaviorsThe 8472-0/+45
2024-01-30Rollup merge of #120445 - Nemo157:arc-plug, r=Mark-SimulacrumGuillaume Gomez-29/+80
Fix some `Arc` allocator leaks This doesn't matter for the stable `Global` allocator as it is a ZST singleton, but other allocators may rely on all instances being dropped.
2024-01-29Rollup merge of #120266 - steffahn:a_rc_into_inner_docs, r=Mark-SimulacrumDylan DPC-5/+12
Improve documentation for [A]Rc::into_inner General improvements, and also aims to better encourage the reader to actually check out Arc::try_unwrap. This addresses concerns from https://github.com/rust-lang/rust/issues/106894#issuecomment-1905627234. Rendered: ![Screenshot_20240123_114436](https://github.com/rust-lang/rust/assets/3986214/68896d62-13e0-4f3a-8073-91d8e77c5554) ![Screenshot_20240123_114455](https://github.com/rust-lang/rust/assets/3986214/dc58e4bd-dd7f-40b1-bc50-fd6200dde593)
2024-01-28Fix some `Arc` allocator leaksWim Looman-29/+80
This doesn't matter for the stable `Global` allocator as it is a ZST singleton, but other allocators may rely on all instances being dropped.
2024-01-26Rollup merge of #113489 - tguichaoua:cow_from_array, r=dtolnayMatthias Krüger-0/+13
impl `From<&[T; N]>` for `Cow<[T]>` Implement `From<&[T; N]>` for `Cow<[T]>` to simplify its usage in the following example. ```rust fn foo(data: impl Into<Cow<'static, [&'static str]>>) { /* ... */ } fn main() { foo(vec!["hello", "world"]); foo(&["hello", "world"]); // Error: the trait `From<&[&str; 2]>` is not implemented for `Cow<'static, [&'static str]>` foo(&["hello", "world"] as &[_]); // Explicit convertion into a slice is required } ```
2024-01-26Rollup merge of #103522 - Dylan-DPC:76118/array-methods-stab, r=dtolnayMatthias Krüger-1/+0
stabilise array methods Closes #76118 Stabilises the remaining array methods FCP is yet to be carried out for this There wasn't a clear consensus on the naming, but all the other alternatives had some flaws as discussed in the tracking issue and there was a silence on this issue for a year
2024-01-26Rollup merge of #120372 - bjorn3:fix_outdated_comment, r=NilstriebMatthias Krüger-2/+1
Fix outdated comment on Box Caught by `@vi` in https://github.com/rust-lang/rust/pull/113960#discussion_r1454278520
2024-01-26Rollup merge of #119917 - Zalathar:split-off, r=cuviperMatthias Krüger-14/+18
Remove special-case handling of `vec.split_off(0)` #76682 added special handling to `Vec::split_off` for the case where `at == 0`. Instead of copying the vector's contents into a freshly-allocated vector and returning it, the special-case code steals the old vector's allocation, and replaces it with a new (empty) buffer with the same capacity. That eliminates the need to copy the existing elements, but comes at a surprising cost, as seen in #119913. The returned vector's capacity is no longer determined by the size of its contents (as would be expected for a freshly-allocated vector), and instead uses the full capacity of the old vector. In cases where the capacity is large but the size is small, that results in a much larger capacity than would be expected from reading the documentation of `split_off`. This is especially bad when `split_off` is called in a loop (to recycle a buffer), and the returned vectors have a wide variety of lengths. I believe it's better to remove the special-case code, and treat `at == 0` just like any other value: - The current documentation states that `split_off` returns a “newly allocated vector”, which is not actually true in the current implementation when `at == 0`. - If the value of `at` could be non-zero at runtime, then the caller has already agreed to the cost of a full memcpy of the taken elements in the general case. Avoiding that copy would be nice if it were close to free, but the different handling of capacity means that it is not. - If the caller specifically wants to avoid copying in the case where `at == 0`, they can easily implement that behaviour themselves using `mem::replace`. Fixes #119913.
2024-01-26Rollup merge of #117678 - niklasf:stabilize-slice_group_by, r=dtolnayMatthias Krüger-12/+10
Stabilize `slice_group_by` Renamed "group by" to "chunk by" a per #80552. Newly stable items: * `core::slice::ChunkBy` * `core::slice::ChunkByMut` * `[T]::chunk` * `[T]::chunk_by` Closes #80552.
2024-01-26Fix outdated comment on Boxbjorn3-2/+1
2024-01-26Rollup merge of #119466 - Sky9x:str_from_raw_parts, r=dtolnayMatthias Krüger-0/+2
Initial implementation of `str::from_raw_parts[_mut]` ACP (accepted): rust-lang/libs-team#167 Tracking issue: #119206 Thanks to ``@Kixiron`` for previous work on this (#107207) ``@rustbot`` label +T-libs-api -T-libs r? ``@thomcc`` Closes #107207.
2024-01-25Export core::str::from_raw_parts{,_mut} into alloc::str and std::strDavid Tolnay-0/+2
2024-01-25Rollup merge of #118208 - Amanieu:btree_cursor2, r=dtolnayMatthias Krüger-392/+549
Rewrite the BTreeMap cursor API using gaps Tracking issue: #107540 Currently, a `Cursor` points to a single element in the tree, and allows moving to the next or previous element while mutating the tree. However this was found to be confusing and hard to use. This PR completely refactors cursors to instead point to a gap between two elements in the tree. This eliminates the need for a "ghost" element that exists after the last element and before the first one. Additionally, `upper_bound` and `lower_bound` now have a much clearer meaning. The ability to mutate keys is also factored out into a separate `CursorMutKey` type which is unsafe to create. This makes the API easier to use since it avoids duplicated versions of each method with and without key mutation. API summary: ```rust impl<K, V> BTreeMap<K, V> { fn lower_bound<Q>(&self, bound: Bound<&Q>) -> Cursor<'_, K, V> where K: Borrow<Q> + Ord, Q: Ord; fn lower_bound_mut<Q>(&mut self, bound: Bound<&Q>) -> CursorMut<'_, K, V> where K: Borrow<Q> + Ord, Q: Ord; fn upper_bound<Q>(&self, bound: Bound<&Q>) -> Cursor<'_, K, V> where K: Borrow<Q> + Ord, Q: Ord; fn upper_bound_mut<Q>(&mut self, bound: Bound<&Q>) -> CursorMut<'_, K, V> where K: Borrow<Q> + Ord, Q: Ord; } struct Cursor<'a, K: 'a, V: 'a>; impl<'a, K, V> Cursor<'a, K, V> { fn next(&mut self) -> Option<(&'a K, &'a V)>; fn prev(&mut self) -> Option<(&'a K, &'a V)>; fn peek_next(&self) -> Option<(&'a K, &'a V)>; fn peek_prev(&self) -> Option<(&'a K, &'a V)>; } struct CursorMut<'a, K: 'a, V: 'a>; impl<'a, K, V> CursorMut<'a, K, V> { fn next(&mut self) -> Option<(&K, &mut V)>; fn prev(&mut self) -> Option<(&K, &mut V)>; fn peek_next(&mut self) -> Option<(&K, &mut V)>; fn peek_prev(&mut self) -> Option<(&K, &mut V)>; unsafe fn insert_after_unchecked(&mut self, key: K, value: V); unsafe fn insert_before_unchecked(&mut self, key: K, value: V); fn insert_after(&mut self, key: K, value: V) -> Result<(), UnorderedKeyError>; fn insert_before(&mut self, key: K, value: V) -> Result<(), UnorderedKeyError>; fn remove_next(&mut self) -> Option<(K, V)>; fn remove_prev(&mut self) -> Option<(K, V)>; fn as_cursor(&self) -> Cursor<'_, K, V>; unsafe fn with_mutable_key(self) -> CursorMutKey<'a, K, V, A>; } struct CursorMutKey<'a, K: 'a, V: 'a>; impl<'a, K, V> CursorMut<'a, K, V> { fn next(&mut self) -> Option<(&mut K, &mut V)>; fn prev(&mut self) -> Option<(&mut K, &mut V)>; fn peek_next(&mut self) -> Option<(&mut K, &mut V)>; fn peek_prev(&mut self) -> Option<(&mut K, &mut V)>; unsafe fn insert_after_unchecked(&mut self, key: K, value: V); unsafe fn insert_before_unchecked(&mut self, key: K, value: V); fn insert_after(&mut self, key: K, value: V) -> Result<(), UnorderedKeyError>; fn insert_before(&mut self, key: K, value: V) -> Result<(), UnorderedKeyError>; fn remove_next(&mut self) -> Option<(K, V)>; fn remove_prev(&mut self) -> Option<(K, V)>; fn as_cursor(&self) -> Cursor<'_, K, V>; unsafe fn with_mutable_key(self) -> CursorMutKey<'a, K, V, A>; } struct UnorderedKeyError; ```
2024-01-23Auto merge of #119433 - taiki-e:rc-uninit-ref, r=Nilstriebbors-20/+25
rc,sync: Do not create references to uninitialized values Closes #119241 r? `@RalfJung`
2024-01-23Improve documentation for [A]Rc::into_innerFrank Steffahn-5/+12
General improvements, and also aims to better encourage the reader to actually check out Arc::try_unwrap.
2024-01-23Auto merge of #119892 - joboet:libs_use_assert_unchecked, r=Nilstrieb,cuviperbors-10/+12
Use `assert_unchecked` instead of `assume` intrinsic in the standard library Now that a public wrapper for the `assume` intrinsic exists, we can use it in the standard library. CC #119131
2024-01-22Rollup merge of #119801 - zachs18:zachs18-patch-1, r=steffahn,NilstriebMatthias Krüger-2/+2
Fix deallocation with wrong allocator in (A)Rc::from_box_in Deallocate the `Box` with the original allocator (via `&A`), not `Global`. Fixes #119749 <details> <summary>Example code with error and Miri output</summary> (Note that this UB is not observable on stable, because the only usable allocator on stable is `Global` anyway.) Code ([playground link](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=96193c2c6a1912d7f669fbbe39174b09)): ```rs #![feature(allocator_api)] use std::alloc::System; // uncomment one of these use std::rc::Rc; //use std::sync::Arc as Rc; fn main() { let x: Box<[u32], System> = Box::new_in([1,2,3], System); let _: Rc<[u32], System> = Rc::from(x); } ``` Miri output: ```rs error: Undefined Behavior: deallocating alloc904, which is C heap memory, using Rust heap deallocation operation --> /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/alloc.rs:117:14 | 117 | unsafe { __rust_dealloc(ptr, layout.size(), layout.align()) } | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ deallocating alloc904, which is C heap memory, using Rust heap deallocation operation | = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information = note: BACKTRACE: = note: inside `std::alloc::dealloc` at /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/alloc.rs:117:14: 117:64 = note: inside `<std::alloc::Global as std::alloc::Allocator>::deallocate` at /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/alloc.rs:254:22: 254:51 = note: inside `<std::boxed::Box<std::mem::ManuallyDrop<[u32]>> as std::ops::Drop>::drop` at /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/boxed.rs:1244:17: 1244:66 = note: inside `std::ptr::drop_in_place::<std::boxed::Box<std::mem::ManuallyDrop<[u32]>>> - shim(Some(std::boxed::Box<std::mem::ManuallyDrop<[u32]>>))` at /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ptr/mod.rs:507:1: 507:56 = note: inside `std::mem::drop::<std::boxed::Box<std::mem::ManuallyDrop<[u32]>>>` at /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/mem/mod.rs:992:24: 992:25 = note: inside `std::rc::Rc::<[u32], std::alloc::System>::from_box_in` at /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/rc.rs:1928:13: 1928:22 = note: inside `<std::rc::Rc<[u32], std::alloc::System> as std::convert::From<std::boxed::Box<[u32], std::alloc::System>>>::from` at /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/rc.rs:2504:9: 2504:27 note: inside `main` --> src/main.rs:10:32 | 10 | let _: Rc<[u32], System> = Rc::from(x); | ^^^^^^^^^^^ note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace error: aborting due to 1 previous error ``` </details>
2024-01-22Auto merge of #120196 - matthiaskrgr:rollup-id2zocf, r=matthiaskrgrbors-13/+31
Rollup of 8 pull requests Successful merges: - #120005 (Update Readme) - #120045 (Un-hide `iter::repeat_n`) - #120128 (Make stable_mir::with_tables sound) - #120145 (fix: Drop guard was deallocating with the incorrect size) - #120158 (`rustc_mir_dataflow`: Restore removed exports) - #120167 (Capture the rationale for `-Zallow-features=` in bootstrap.py) - #120174 (Warn users about limited review for tier 2 and 3 code) - #120180 (Document some alternatives to `Vec::split_off`) Failed merges: - #120171 (Fix assume and assert in jump threading) r? `@ghost` `@rustbot` modify labels: rollup
2024-01-21Rollup merge of #120180 - Zalathar:vec-split-off-alternatives, r=dtolnayMatthias Krüger-0/+6
Document some alternatives to `Vec::split_off` One of the discussion points that came up in #119917 is that some people use `Vec::split_off` in cases where they probably shouldn't, because the alternatives (like `mem::take`) are hard to discover. This PR adds some suggestions to the documentation of `split_off` that should point people towards alternatives that might be more appropriate for their use-case. I've deliberately tried to keep these changes as simple and uncontroversial as possible, so that they don't depend on how the team decides to handle the concerns raised in #119917. That's why I haven't touched the existing documentation for `split_off`, and haven't added links to `split_off` to the documentation of other methods.
2024-01-21Rollup merge of #120145 - the8472:fix-inplace-dest-drop, r=cuviperMatthias Krüger-13/+25
fix: Drop guard was deallocating with the incorrect size InPlaceDstBufDrop holds onto the allocation before the shrinking happens which means it must deallocate the destination elements but the source allocation. Thanks `@cuviper` for spotting this.
2024-01-21Auto merge of #85528 - the8472:iter-markers, r=dtolnaybors-2/+6
Implement iterator specialization traits on more adapters This adds * `TrustedLen` to `Skip` and `StepBy` * `TrustedRandomAccess` to `Skip` * `InPlaceIterable` and `SourceIter` to `Copied` and `Cloned` The first two might improve performance in the compiler itself since `skip` is used in several places. Constellations that would exercise the last point are probably rare since it would require an owning iterator that has references as Items somewhere in its iterator pipeline. Improvements for `Skip`: ``` # old test iter::bench_skip_trusted_random_access ... bench: 8,335 ns/iter (+/- 90) # new test iter::bench_skip_trusted_random_access ... bench: 2,753 ns/iter (+/- 27) ```
2024-01-21Document some alternatives to `Vec::split_off`Zalathar-0/+6
2024-01-20Rollup merge of #120116 - the8472:only-same-alignments, r=cuviperGuillaume Gomez-12/+15
Remove alignment-changing in-place collect This removes the alignment-changing in-place collect optimization introduced in #110353 Currently stable users can't benefit from the optimization because GlobaAlloc doesn't support alignment-changing realloc and neither do most posix allocators. So in practice it has a negative impact on performance. Explanation from https://github.com/rust-lang/rust/issues/120091#issuecomment-1899071681: > > You mention that in case of alignment mismatch -- when the new alignment is less than the old -- the implementation calls `mremap`. > > I was trying to note that this isn't really the case in practice, due to the semantics of Rust's allocator APIs. The only use of the allocator within the `in_place_collect` implementation itself is [a call to `Allocator::shrink()`](https://github.com/rust-lang/rust/blob/db7125f008cfd72e8951c9a863178956e2cbacc3/library/alloc/src/vec/in_place_collect.rs#L299-L303), which per its documentation [allows decreasing the required alignment](https://doc.rust-lang.org/1.75.0/core/alloc/trait.Allocator.html). However, in stable Rust, the only available `Allocator` is [`Global`](https://doc.rust-lang.org/1.75.0/alloc/alloc/struct.Global.html), which delegates to the registered `GlobalAlloc`. Since `GlobalAlloc::realloc()` [cannot change the required alignment](https://doc.rust-lang.org/1.75.0/core/alloc/trait.GlobalAlloc.html#method.realloc), the implementation of [`<Global as Allocator>::shrink()`](https://github.com/rust-lang/rust/blob/db7125f008cfd72e8951c9a863178956e2cbacc3/library/alloc/src/alloc.rs#L280-L321) must fall back to creating a brand-new allocation, `memcpy`ing the data into it, and freeing the old allocation, whenever the alignment doesn't remain exactly the same. > > Therefore, the underlying allocator, provided by libc or some other source, has no opportunity to internally `mremap()` the data when the alignment is changed, since it has no way of knowing that the allocation is the same.
2024-01-19fix: Drop guard was deallocating with the incorrect sizeThe 8472-13/+25
InPlaceDstBufDrop holds onto the allocation before the shrinking happens which means it must deallocate the destination elements but the source allocation.
2024-01-19Rollup merge of #120110 - invpt:patch-1, r=the8472Matthias Krüger-7/+12
Update documentation for Vec::into_boxed_slice to be more clear about excess capacity Currently, the documentation for Vec::into_boxed_slice says that "if the vector has excess capacity, its items will be moved into a newly-allocated buffer with exactly the right capacity." This is misleading, as copies do not necessarily occur, depending on if the allocator supports in-place shrinking. I copied some of the wording from shrink_to_fit, though it could potentially still be worded better than this.
2024-01-18Clarify docs for Vec::into_boxed_slice, Vec::shrink_to_fitinvpt-7/+12
2024-01-18remove alignment-changing in-place collectThe 8472-5/+8
Currently stable users can't benefit from this because GlobaAlloc doesn't support alignment-changing realloc and neither do most posix allocators. So in practice it always results in an extra memcpy.
2024-01-18update internal ASCII art comment for vec specializationsThe 8472-7/+7
2024-01-18Remove no-longer-needed `allow(dead_code)` from the standard libraryJake Goulding-1/+0
`repr(transparent)` now silences the lint.
2024-01-16Fix typo in comments (in_place_collect)Robert Grosse-1/+1
2024-01-13libs: use `assert_unchecked` instead of intrinsicjoboet-10/+12
2024-01-13Remove special-case handling of `vec.split_off(0)`Zalathar-14/+18
2024-01-11Rollup merge of #119853 - klensy:rustfmt-ignore, r=cuviperMatthias Krüger-13/+4
rustfmt.toml: don't ignore just any tests path, only root one Previously ignored any `tests` path, now only /tests at repo root. For reference, https://git-scm.com/docs/gitignore#_pattern_format
2024-01-11apply fmtklensy-13/+4
2024-01-11chore: remove unnecessary blank linehi-rustin-1/+0
Signed-off-by: hi-rustin <rustin.liu@gmail.com>
2024-01-10Implement in-place iteratation markers for iter::{Copied, Cloned}The8472-2/+6
2024-01-10Fix deallocation with wrong allocator in (A)Rc::from_box_inzachs18-2/+2
2024-01-08rc,sync: Do not create references to uninitialized valuesTaiki Endo-20/+25
2024-01-07mark vec::IntoIter pointers as `!nonnull`The 8472-49/+75
2024-01-07typo fixThe 8472-1/+1
2024-01-06Rollup merge of #118781 - RalfJung:core-panic-feature, r=the8472Matthias Krüger-1/+1
merge core_panic feature into panic_internals I don't know why those are two separate features, but it does not seem intentional. This merge is useful because with https://github.com/rust-lang/rust/pull/118123, panic_internals is recognized as an internal feature, but core_panic is not -- but core_panic definitely should be internal.
2024-01-05Auto merge of #118297 - shepmaster:warn-dead-tuple-fields, r=WaffleLapkinbors-7/+8
Merge `unused_tuple_struct_fields` into `dead_code` This implicitly upgrades the lint from `allow` to `warn` and places it into the `unused` lint group. [Discussion on Zulip](https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/Moving.20.60unused_tuple_struct_fields.60.20from.20allow.20to.20warn)
2024-01-03Rollup merge of #119434 - taiki-e:rc-is-dangling, r=Mark-SimulacrumLeón Orell Valerian Liehr-3/+3
rc: Take *const T in is_dangling It is not important which one is used since `is_dangling` does not access memory, but `*const` removes the needs of `*const T` -> `*mut T` casts in `from_raw_in`.
2024-01-02Adjust library tests for unused_tuple_struct_fields -> dead_codeJake Goulding-7/+8
2023-12-30Rollup merge of #119158 - JohnTheCoolingFan:arc-weak-clone-pretty, r=cuviperMatthias Krüger-14/+11
Clean up alloc::sync::Weak Clone implementation Since both return points (tail and early return) return the same expression and the only difference is whether inner is available, the code that does the atomic operations and checks on inner was moved into the if body and the only return is at the tail. Original comments preserved.
2023-12-30rc: Take *const T in is_danglingTaiki Endo-3/+3
It is not important which one is used since `is_dangling` does not access memory, but `*const` removes the needs of `*const T` -> `*mut T` casts in `from_raw_in`.
2023-12-29Italicise "bytes" in the docs of some `Vec` methodsGurinder Singh-8/+8
because on a cursory read it's easy to miss that the limit is in terms of bytes not no. of elements. The italics should help with that.