about summary refs log tree commit diff
path: root/library/alloc/src
AgeCommit message (Collapse)AuthorLines
2024-03-20step cfgsMark Rousskov-3/+2
2024-03-19fix OOB pointer formed in Vec::indexJoshua Wong-4/+3
Move the length check to before using `index` with `ptr::add` to prevent an out of bounds pointer from being formed. Fixes #122760
2024-03-19Only split by-ref/by-move futures for async closuresMichael Goulet-5/+3
2024-03-19SeqCst->Relaxed in doc examples.Mara Bos-1/+1
SeqCst is unnecessary here.
2024-03-19Auto merge of #122055 - compiler-errors:stabilize-atb, r=oli-obkbors-1/+1
Stabilize associated type bounds (RFC 2289) This PR stabilizes associated type bounds, which were laid out in [RFC 2289]. This gives us a shorthand to express nested type bounds that would otherwise need to be expressed with nested `impl Trait` or broken into several `where` clauses. ### What are we stabilizing? We're stabilizing the associated item bounds syntax, which allows us to put bounds in associated type position within other bounds, i.e. `T: Trait<Assoc: Bounds...>`. See [RFC 2289] for motivation. In all position, the associated type bound syntax expands into a set of two (or more) bounds, and never anything else (see "How does this differ[...]" section for more info). Associated type bounds are stabilized in four positions: * **`where` clauses (and APIT)** - This is equivalent to breaking up the bound into two (or more) `where` clauses. For example, `where T: Trait<Assoc: Bound>` is equivalent to `where T: Trait, <T as Trait>::Assoc: Bound`. * **Supertraits** - Similar to above, `trait CopyIterator: Iterator<Item: Copy> {}`. This is almost equivalent to breaking up the bound into two (or more) `where` clauses; however, the bound on the associated item is implied whenever the trait is used. See #112573/#112629. * **Associated type item bounds** - This allows constraining the *nested* rigid projections that are associated with a trait's associated types. e.g. `trait Trait { type Assoc: Trait2<Assoc2: Copy>; }`. * **opaque item bounds (RPIT, TAIT)** - This allows constraining associated types that are associated with the opaque without having to *name* the opaque. For example, `impl Iterator<Item: Copy>` defines an iterator whose item is `Copy` without having to actually name that item bound. The latter three are not expressible in surface Rust (though for associated type item bounds, this will change in #120752, which I don't believe should block this PR), so this does represent a slight expansion of what can be expressed in trait bounds. ### How does this differ from the RFC? Compared to the RFC, the current implementation *always* desugars associated type bounds to sets of `ty::Clause`s internally. Specifically, it does *not* introduce a position-dependent desugaring as laid out in [RFC 2289], and in particular: * It does *not* desugar to anonymous associated items in associated type item bounds. * It does *not* desugar to nested RPITs in RPIT bounds, nor nested TAITs in TAIT bounds. This position-dependent desugaring laid out in the RFC existed simply to side-step limitations of the trait solver, which have mostly been fixed in #120584. The desugaring laid out in the RFC also added unnecessary complication to the design of the feature, and introduces its own limitations to, for example: * Conditionally lowering to nested `impl Trait` in certain positions such as RPIT and TAIT means that we inherit the limitations of RPIT/TAIT, namely lack of support for higher-ranked opaque inference. See this code example: https://github.com/rust-lang/rust/pull/120752#issuecomment-1979412531. * Introducing anonymous associated types makes traits no longer object safe, since anonymous associated types are not nameable, and all associated types must be named in `dyn` types. This last point motivates why this PR is *not* stabilizing support for associated type bounds in `dyn` types, e.g, `dyn Assoc<Item: Bound>`. Why? Because `dyn` types need to have *concrete* types for all associated items, this would necessitate a distinct lowering for associated type bounds, which seems both complicated and unnecessary compared to just requiring the user to write `impl Trait` themselves. See #120719. ### Implementation history: Limited to the significant behavioral changes and fixes and relevant PRs, ping me if I left something out-- * #57428 * #108063 * #110512 * #112629 * #120719 * #120584 Closes #52662 [RFC 2289]: https://rust-lang.github.io/rfcs/2289-associated-type-bounds.html
2024-03-18Rollup merge of #122642 - pallix:improve-wording-for-vec-swap_remove, r=AmanieuMatthias Krüger-1/+1
Improve wording of `Vec::swap_remove` This improve the wording for `Vec::swap_remove`.
2024-03-18Fix a typo in the alloc::string::String docsReed-1/+1
2024-03-18add_retag: ensure box-to-raw-ptr casts are preserved for MiriRalf Jung-11/+6
2024-03-17Improve wording of `Vec::swap_remove`Pierre Allix-1/+1
2024-03-14Stabilize `unchecked_{add,sub,mul}`Scott McMurray-1/+0
2024-03-12Remove unused fields in some structuresArthur Carcano-8/+4
The dead_code lint was previously eroneously missing those. Since this lint bug has been fixed, the unused fields need to be removed.
2024-03-11Rollup merge of #122298 - RalfJung:raw-vec-into-box, r=cuviperJacob Pratt-2/+1
RawVec::into_box: avoid unnecessary intermediate reference Fixes the problem described [here](https://github.com/rust-lang/miri/issues/3341#issuecomment-1987207195).
2024-03-11Rollup merge of #121148 - clarfonthey:try-range, r=dtolnayJacob Pratt-2/+2
Add slice::try_range This adds a fallible version of the unstable `slice::range` (tracking: #76393) which is highly requested in the tracking issue. Hoping this can slide by without an ACP (since the feature is already being tracked), but let me know otherwise.
2024-03-10RawVec::into_box: avoid unnecessary intermediate referenceRalf Jung-2/+1
2024-03-10Rollup merge of #112136 - clarfonthey:ffi-c_str, r=cuviperMatthias Krüger-3/+9
Add std::ffi::c_str module ACP: rust-lang/libs-team#134 `std::ffi` docs before change: ![Structs: VaList, VaListImpl, CStr, CString, FromBytesWithNulError, FromVecWithNulError, IntoStringError, NulError, OsStr, OsString](https://github.com/rust-lang/rust/assets/15850505/b2cf3534-30f9-4ef0-a655-bacdc3a19e17) `std::ffi` docs after change: ![Re-exports: self::c_str::{FromBytesWithNulError, FromBytesUntilNulError, FromVecWithNulError, NulError, IntoStringError} ; Modules: c_str ; Structs: VaList, VaListImpl, CStr, CString, OsStr, OsString](https://github.com/rust-lang/rust/assets/15850505/23aa6964-da7a-4942-bbf7-42bde2146f9e) (note: I'm omitting the `c_int`, etc. stuff from the screenshots since it's the same in both. this doesn't just delete those types)
2024-03-09Rollup merge of #120504 - kornelski:try_with_capacity, r=AmanieuGuillaume Boisseau-26/+114
Vec::try_with_capacity Related to #91913 Implements try_with_capacity for `Vec`, `VecDeque`, and `String`. I can follow it up with more collections if desired. `Vec::try_with_capacity()` is functionally equivalent to the current stable: ```rust let mut v = Vec::new(); v.try_reserve_exact(n)? ``` However, `try_reserve` calls non-inlined `finish_grow`, which requires old and new `Layout`, and is designed to reallocate memory. There is benefit to using `try_with_capacity`, besides syntax convenience, because it generates much smaller code at the call site with a direct call to the allocator. There's codegen test included. It's also a very desirable functionality for users of `no_global_oom_handling` (Rust-for-Linux), since it makes a very commonly used function available in that environment (`with_capacity` is used much more frequently than all `(try_)reserve(_exact)`).
2024-03-09miri: do not apply aliasing restrictions to Box with custom allocatorRalf Jung-10/+14
2024-03-08Stabilize associated type boundsMichael Goulet-1/+1
2024-03-08Document overrides of `clone_from()`Noa-18/+80
Specifically, when an override doesn't just forward to an inner type, document the behavior and that it's preferred over simply assigning a clone of source. Also, change instances where the second parameter is "other" to "source".
2024-03-08Rollup merge of #122099 - Urgau:btreemap-inline-new, r=AmanieuMatthias Krüger-0/+1
Add `#[inline]` to `BTreeMap::new` constructor This PR add the `#[inline]` attribute to `BTreeMap::new` constructor as to make it eligible for inlining. <details> For some context: I was profiling `rustc --check-cfg` with callgrind and due to the way we currently setup all the targets and we end-up calling `BTreeMap::new` multiple times for (nearly) all the targets. Adding the `#[inline]` attribute reduced the number of instructions needed. </details>
2024-03-07Rust is a proper name: rust → RustRalf Jung-1/+1
2024-03-06Add #[inline] to BTreeMap::new constructorUrgau-0/+1
2024-03-05Rollup merge of #122018 - RalfJung:box-custom-alloc, r=oli-obkMatthias Krüger-0/+5
only set noalias on Box with the global allocator As discovered in https://github.com/rust-lang/miri/issues/3341, `noalias` and custom allocators don't go well together. rustc can now check whether a Box uses the global allocator. This replaces the previous ad-hoc and rather unprincipled check for a zero-sized allocator. This is the rustc part of fixing that; Miri will also need a patch.
2024-03-05Rollup merge of #121894 - RalfJung:const_eval_select, r=oli-obkMatthias Krüger-0/+1
const_eval_select: make it safe but be careful with what we expose on stable for now As this is all still nightly-only I think `````@rust-lang/wg-const-eval````` can do that without involving t-lang. r? `````@oli-obk````` Cc `````@Nilstrieb````` -- the updated version of your RFC would basically say that we can remove these comments about not making behavior differences visible in stable `const fn`
2024-03-05only set noalias on Box with the global allocatorRalf Jung-0/+5
2024-03-05Rollup merge of #121287 - zachs18:rc-into-raw-must-use, r=cuviperMatthias Krüger-2/+3
Clarify/add `must_use` message for Rc/Arc/Weak::into_raw. The current `#[must_use]` messages for `{sync,rc}::Weak::into_raw` ("`self` will be dropped if the result is not used") are misleading, as `self` is consumed and will *not* be dropped. This PR changes their `#[must_use]` message to the same as `Arc::into_raw`'s[ current `#[must_use]` message](https://github.com/rust-lang/rust/blob/d5735645753e990a72446094f703df9b5e421555/library/alloc/src/sync.rs#L1482) ("losing the pointer will leak memory"), and also adds it to `Rc::into_raw`, which is not currently `#[must_use]`.
2024-03-05Rollup merge of #121262 - 20jasper:add-vector-time-complexity, r=cuviperMatthias Krüger-0/+21
Add vector time complexity Added time complexity for `Vec` methods `push`, `push_within_capacity`, `pop`, and `insert`. <details> <summary> Reference images </summary> ![`Vec::push` documentation](https://github.com/rust-lang/rust/assets/78604367/dc966bbd-e92e-45a6-af82-35afabfa79a9) ![`Vec::push_within_capacity` documentation](https://github.com/rust-lang/rust/assets/78604367/9aadaf48-46ed-4fad-bdd5-74b98a61f4bb) ![`Vec::pop` documentation](https://github.com/rust-lang/rust/assets/78604367/88ec0389-a346-4ea5-a3b7-17caf514dd8b) ![`Vec::insert` documentation](https://github.com/rust-lang/rust/assets/78604367/960c15c3-ef8e-4aa7-badc-35ce80f6f221) </details> I followed a convention to use `#Time complexity` that I found in [the `BinaryHeap` documentation](https://doc.rust-lang.org/std/collections/struct.BinaryHeap.html#time-complexity-1). Looking through the rest of standard library collections, there is not a consistent way to handle this. [`Vec::swap_remove`](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.swap_remove) does not have a dedicated section for time complexity but does list it. [`VecDeque::rotate_left`](https://doc.rust-lang.org/std/collections/struct.VecDeque.html#complexity) uses a `#complexity` heading.
2024-03-05Rollup merge of #121213 - Takashiidobe:takashi/example-for-rc-into-inner, ↵Matthias Krüger-0/+15
r=cuviper Add an example to demonstrate how Rc::into_inner works This PR adds an example to Rc::into_inner, since it didn't have one previously.
2024-03-02Rollup merge of #121622 - dtolnay:wake, r=cuviperGuillaume Boisseau-0/+13
Preserve same vtable pointer when cloning raw waker, to fix Waker::will_wake Fixes #121600. As `@jkarneges` identified in https://github.com/rust-lang/rust/issues/121600#issuecomment-1963041051, the issue is two different const promotions produce two statics at different addresses, which may or may not later be deduplicated by the linker (in this case not). Prior to #119863, the content of the statics was compared, and they were equal. After, the address of the statics are compared and they are not equal. It is documented that `will_wake` _"works on a best-effort basis, and may return false even when the Wakers would awaken the same task"_ so this PR fixes a quality-of-implementation issue, not a correctness issue.
2024-03-02const_eval_select: make it safe but be careful with what we expose on stable ↵Ralf Jung-0/+1
for now
2024-03-01Move capacity_overflow function to make ui tests change lessKornel-9/+9
Code changes in raw_vec require blessing UI tests every time
2024-03-01try_with_capacity for Vec, VecDeque, StringKornel-0/+81
#91913
2024-03-01try_with_capacity for RawVecKornel-18/+25
2024-03-01remove hidden use of GlobalAlexander-1/+1
2024-02-29Rollup merge of #120291 - pitaj:string-sliceindex, r=AmanieuJacob Pratt-88/+15
Have `String` use `SliceIndex` impls from `str` This PR simplifies the implementation of `Index` and `IndexMut` on `String`, and in the process enables indexing `String` by any user types that implement `SliceIndex<str>`. Similar to #47832 r? libs Not sure if this warrants a crater run.
2024-02-27have `String` use `SliceIndex` impls from `str`Peter Jaszkowiak-88/+15
2024-02-26Generate original vtable and clone's vtable in the same CGUDavid Tolnay-0/+13
2024-02-26Document args returned from `String::into_raw_parts`许杰友 Jieyou Xu (Joe)-1/+1
2024-02-26Document args returned from `Vec::into_raw_parts{,_with_alloc}`许杰友 Jieyou Xu (Joe)-2/+2
2024-02-26Rearrange `String::from_raw_parts` doc argument order to match code argument ↵许杰友 Jieyou Xu (Joe)-1/+1
order
2024-02-26Rearrange `Vec::from_raw_parts{,_in}` doc argument order to match code ↵许杰友 Jieyou Xu (Joe)-2/+2
argument order
2024-02-25Make push docs more vagueJacob Asper-4/+3
2024-02-24Rollup merge of #121556 - GrigorenkoPV:addr_of, r=NilstriebMatthias Krüger-5/+5
Use `addr_of!` As per https://github.com/rust-lang/rust/pull/121303#discussion_r1500954662
2024-02-24Forbid use of `extern "C-unwind"` inside standard libraryGary Guo-0/+1
Those libraries are build with `-C panic=unwind` and is expected to be linkable to `-C panic=abort` library. To ensure unsoundness compiler needs to prevent a `C-unwind` call to exist, as doing so may leak foreign exceptions into `-C panic=abort`.
2024-02-24library: use `addr_of!`Pavel Grigorenko-5/+5
2024-02-23Auto merge of #121514 - matthiaskrgr:rollup-5f0vhv7, r=matthiaskrgrbors-6/+6
Rollup of 6 pull requests Successful merges: - #120742 (mark `min_exhaustive_patterns` as complete) - #121470 (Don't ICE on anonymous struct in enum variant) - #121492 (coverage: Rename `is_closure` to `is_hole`) - #121495 (remove repetitive words) - #121498 (Make QNX/NTO specific "timespec capping" public to crate::sys) - #121510 (lint-overflowing-ops: unify cases and remove redundancy) r? `@ghost` `@rustbot` modify labels: rollup
2024-02-23Auto merge of #121454 - reitermarkus:generic-nonzero-library, r=dtolnaybors-41/+28
Use generic `NonZero` everywhere in `library`. Tracking issue: https://github.com/rust-lang/rust/issues/120257 Use generic `NonZero` everywhere (except stable examples). r? `@dtolnay`
2024-02-23remove repetitive wordscui fliter-6/+6
Signed-off-by: cui fliter <imcusg@gmail.com>
2024-02-22On type error of method call arguments, look at confusables for suggestionEsteban Küber-0/+1
2024-02-22Add `rustc_confusables` annotations to some stdlib APIsEsteban Küber-0/+34
Help with common API confusion, like asking for `push` when the data structure really has `append`. ``` error[E0599]: no method named `size` found for struct `Vec<{integer}>` in the current scope --> $DIR/rustc_confusables_std_cases.rs:17:7 | LL | x.size(); | ^^^^ | help: you might have meant to use `len` | LL | x.len(); | ~~~ help: there is a method with a similar name | LL | x.resize(); | ~~~~~~ ``` #59450