about summary refs log tree commit diff
path: root/library/core/src
AgeCommit message (Collapse)AuthorLines
2025-04-09Speed up `String::push` and `String::insert`lincot-30/+62
Improve performance of `String` methods by avoiding unnecessary memcpy for the character bytes, with added codegen check to ensure compliance.
2025-04-08Rollup merge of #139526 - smanilov:issue-139505, r=RalfJungMatthias Krüger-3/+3
Fix deprecation note for std::intrinsics Also checked the rest of the mentions of std::mem in the changed file and they look good to me. Fixes #139505
2025-04-08Fix unreachable expression warningCelina G. Val-7/+19
Invert the order that we pass the arguments to the `contract_check_ensures` function to avoid the warning when the tail of the function is unreachable. Note that the call itself is also unreachable, but we have already handled that case by ignoring unreachable call for contract calls.
2025-04-08Update 'u8'-to-and-from-'i8' suggestions;Gabriel Bjørnager Jensen-2/+2
2025-04-08Fix deprecation note for std::intrinsicsStan Manilov-3/+3
Also checked the rest of the mentions of std::mem in the changed file and they look good to me.
2025-04-07Enable contracts for const functionsCelina G. Val-14/+47
Use `const_eval_select!()` macro to enable contract checking only at runtime. The existing contract logic relies on closures, which are not supported in constant functions. This commit also removes one level of indirection for ensures clauses, however, it currently has a spurious warning message when the bottom of the function is unreachable.
2025-04-07update intrinsics/mod.rs comment about const intrinsicsRalf Jung-8/+4
2025-04-07f*::NAN: guarantee that this is a quiet NaNRalf Jung-32/+40
2025-04-07Rollup merge of #139455 - Skgland:remove_rust-intrinsic_ABI, r=oli-obkStuart Cook-0/+1
Remove support for `extern "rust-intrinsic"` blocks Part of rust-lang/rust#132735 Looked manageable and there didn't appear to have been progress in the last two weeks, so decided to give it a try.
2025-04-06Auto merge of #138951 - jwnrt:alloc-raw-vec-strict-prov, r=Noratriebbors-1/+15
Replace last `usize` -> `ptr` transmute in `alloc` with strict provenance API This replaces the `usize -> ptr` transmute in `RawVecInner::new_in` with a strict provenance API (`NonNull::without_provenance`). The API is changed to take an `Alignment` which encodes the non-null constraint needed for `Unique` and allows us to do the construction safely. Two internal-only APIs were added to let us avoid UB-checking in this hot code: `Layout::alignment` to get the `Alignment` type directly rather than as a `usize`, and `Unique::from_non_null` to create `Unique` in const context without a transmute.
2025-04-06update docsBennet Bleßmann-0/+1
- src\doc\nomicon\src\ffi.md should also have its ABI list updated
2025-04-06Rollup merge of #139072 - nickkuk:align_to_uninit_mut, r=Mark-SimulacrumGuillaume Gomez-2/+51
Add `slice::align_to_uninit_mut` Add new `slice::align_to_uninit_mut` method. Tracking issue: https://github.com/rust-lang/rust/issues/139062 ACP: https://github.com/rust-lang/libs-team/issues/564
2025-04-06Rollup merge of #138562 - kornelski:nth-panic, r=NoratriebGuillaume Gomez-25/+25
Optimize slice {Chunks,Windows}::nth I've noticed that the `nth` functions on slice iters had non-optimized-out bounds checks. The new implementation even generates branchless code.
2025-04-06doc changesizarma-6/+6
2025-04-06Rollup merge of #139123 - thaliaarchi:core-alloc-test-paths, r=bjorn3Stuart Cook-2/+2
tidy: Fix paths to `coretests` and `alloctests` Following `#135937` and `#136642`, tests for core and alloc are in coretests and alloctests. Fix tidy to lint for the new paths. Also, update comments referring to the old locations. Some context for changes which don't match that pattern: - `library/std/src/thread/local/dynamic_tests.rs` and `library/std/src/sync/mpsc/sync_tests.rs` were moved under `library/std/tests/` in 332fb7e6f1d (Move std::thread_local unit tests to integration tests, 2025-01-17) and b8ae372e483 (Move std::sync unit tests to integration tests, 2025-01-17), respectively, so are no longer special cases. - There never was a `library/core/tests/fmt.rs` file. That comment previously referred to `src/test/ui/ifmt.rs`, which was folded into `library/alloc/tests/fmt.rs` in 949c96660c3 (move format! interface tests, 2020-09-08). Now, the only matches for `(alloc|core)/tests` are in `compiler/rustc_codegen_{cranelift,gcc}/patches`. I don't know why CI hasn't broken because those patches can't apply. Or maybe they somehow still can apply? r? `@bjorn3`
2025-04-05Polymorphize `array::IntoIter`'s iterator implScott McMurray-141/+420
2025-04-05tidy: Fix paths to coretests and alloctestsThalia Archibald-2/+2
Following `#135937` and `#136642`, tests for core and alloc are in coretests and alloctests. Fix tidy to lint for the new paths. Also, update comments referring to the old locations. Some context for changes which don't match that pattern: * library/std/src/thread/local/dynamic_tests.rs and library/std/src/sync/mpsc/sync_tests.rs were moved under library/std/tests/ in 332fb7e6f1d (Move std::thread_local unit tests to integration tests, 2025-01-17) and b8ae372e483 (Move std::sync unit tests to integration tests, 2025-01-17), respectively, so are no longer special cases. * There never was a library/core/tests/fmt.rs file. That comment previously referred to src/test/ui/ifmt.rs, which was folded into library/alloc/tests/fmt.rs in 949c96660c3 (move format! interface tests, 2020-09-08).
2025-04-05Rollup merge of #136877 - Sky9x:const-inherent-ptr-replace, r=jhprattMatthias Krüger-2/+4
Fix missing const for inherent pointer `replace` methods `ptr::replace` (the free fn) is already const stable. However, there are inherent convenience methods on `*mut T` and `NonNull<T>`, allowing you to write eg. `unsafe { foo.replace(bar) }` where `foo` is `*mut T` or `NonNull<T>`. It seems const was never added to the inherent method (likely oversight), so this PR adds it. I don't believe this needs another[^1] FCP as the inherent methods are already stable and `ptr::replace` is already const stable, so this adds no new API. Original tracking issue: #83164 `ptr::replace` constified in #83091 `ptr::replace` const stabilized in #130954 [^1]: `const_replace` FCP completed: https://github.com/rust-lang/rust/issues/83164#issuecomment-2385670050
2025-04-05Rollup merge of #138381 - thaliaarchi:bstr-sliceindex, r=joshtriplettMatthias Krüger-273/+291
Implement `SliceIndex` for `ByteStr` Implement `Index` and `IndexMut` for `ByteStr` in terms of `SliceIndex`. Implement it for the same types that `&[u8]` supports (a superset of those supported for `&str`, which does not have `usize` and `ops::IndexRange`). At the same time, move compare and index traits to a separate file in the `bstr` module, to give it more space to grow as more functionality is added (e.g., iterators and string-like ops). Order the items in `bstr/traits.rs` similarly to `str/traits.rs`. cc `@joshtriplett` `ByteStr`/`ByteString` tracking issue: https://github.com/rust-lang/rust/issues/134915
2025-04-05Rollup merge of #139389 - mejrs:hidden, r=workingjubileeStuart Cook-0/+1
make `Arguments::as_statically_known_str` doc(hidden) Fixes `as_statically_known_str` being [visible](https://doc.rust-lang.org/nightly/std/fmt/struct.Arguments.html#method.as_statically_known_str) ([Rendered](https://github.com/user-attachments/assets/45482d9f-2ec5-4610-be9c-b231bd2850c6)) This snuck in with https://github.com/rust-lang/rust/pull/138650, cc `@thaliaarchi` This is also visible in the beta docs. `@rustbot` label +beta-nominated
2025-04-05Rollup merge of #138024 - reitermarkus:unicode-panic-optimization, r=ibraheemdevStuart Cook-83/+234
Allow optimizing out `panic_bounds_check` in Unicode checks. Allow optimizing out `panic_bounds_check` in Unicode checks. For context, see https://github.com/japaric/ufmt/issues/52#issuecomment-2699207241.
2025-04-05Rollup merge of #136457 - calder:master, r=tgross35Stuart Cook-5/+250
Expose algebraic floating point intrinsics # Problem A stable Rust implementation of a simple dot product is 8x slower than C++ on modern x86-64 CPUs. The root cause is an inability to let the compiler reorder floating point operations for better vectorization. See https://github.com/calder/dot-bench for benchmarks. Measurements below were performed on a i7-10875H. ### C++: 10us ✅ With Clang 18.1.3 and `-O2 -march=haswell`: <table> <tr> <th>C++</th> <th>Assembly</th> </tr> <tr> <td> <pre lang="cc"> float dot(float *a, float *b, size_t len) { #pragma clang fp reassociate(on) float sum = 0.0; for (size_t i = 0; i < len; ++i) { sum += a[i] * b[i]; } return sum; } </pre> </td> <td> <img src="https://github.com/user-attachments/assets/739573c0-380a-4d84-9fd9-141343ce7e68" /> </td> </tr> </table> ### Nightly Rust: 10us ✅ With rustc 1.86.0-nightly (8239a37f9) and `-C opt-level=3 -C target-feature=+avx2,+fma`: <table> <tr> <th>Rust</th> <th>Assembly</th> </tr> <tr> <td> <pre lang="rust"> fn dot(a: &[f32], b: &[f32]) -> f32 { let mut sum = 0.0; for i in 0..a.len() { sum = fadd_algebraic(sum, fmul_algebraic(a[i], b[i])); } sum } </pre> </td> <td> <img src="https://github.com/user-attachments/assets/9dcf953a-2cd7-42f3-bc34-7117de4c5fb9" /> </td> </tr> </table> ### Stable Rust: 84us ❌ With rustc 1.84.1 (e71f9a9a9) and `-C opt-level=3 -C target-feature=+avx2,+fma`: <table> <tr> <th>Rust</th> <th>Assembly</th> </tr> <tr> <td> <pre lang="rust"> fn dot(a: &[f32], b: &[f32]) -> f32 { let mut sum = 0.0; for i in 0..a.len() { sum += a[i] * b[i]; } sum } </pre> </td> <td> <img src="https://github.com/user-attachments/assets/936a1f7e-33e4-4ff8-a732-c3cdfe068dca" /> </td> </tr> </table> # Proposed Change Add `core::intrinsics::f*_algebraic` wrappers to `f16`, `f32`, `f64`, and `f128` gated on a new `float_algebraic` feature. # Alternatives Considered https://github.com/rust-lang/rust/issues/21690 has a lot of good discussion of various options for supporting fast math in Rust, but is still open a decade later because any choice that opts in more than individual operations is ultimately contrary to Rust's design principles. In the mean time, processors have evolved and we're leaving major performance on the table by not supporting vectorization. We shouldn't make users choose between an unstable compiler and an 8x performance hit. # References * https://github.com/rust-lang/rust/issues/21690 * https://github.com/rust-lang/libs-team/issues/532 * https://github.com/rust-lang/rust/issues/136469 * https://github.com/calder/dot-bench * https://www.felixcloutier.com/x86/vfmadd132ps:vfmadd213ps:vfmadd231ps try-job: x86_64-gnu-nopt try-job: x86_64-gnu-aux
2025-04-04Expose algebraic floating point intrinsicsCalder Coalson-5/+250
2025-04-04make `Arguments::as_statically_known_str` doc(hidden)mejrs-0/+1
2025-04-04Optimize setting the slice to empty in slice IterKornel-9/+9
This avoids generating extra instructions that needlessly modify the slice's pointer
2025-04-04Optimize slice Windows::nthKornel-7/+9
Generates branchless code
2025-04-04Optimize slice Iter::nthKornel-10/+8
2025-04-03Stabilize the `cell_update` featureTrevor Gross-3/+1
Included API: impl<T: Copy> Cell<T> { pub fn update(&self, f: impl FnOnce(T) -> T); } Closes: https://github.com/rust-lang/rust/issues/50186
2025-04-03Auto merge of #139301 - matthiaskrgr:rollup-sa6ali8, r=matthiaskrgrbors-71/+38
Rollup of 7 pull requests Successful merges: - #139080 (Experimental feature gate for `super let`) - #139145 (slice: Remove some uses of unsafe in first/last chunk methods) - #139149 (unstable book: document import_trait_associated_functions) - #139273 (Apply requested API changes to `cell_update`) - #139282 (rustdoc: make settings checkboxes always square) - #139283 (Rustc dev guide subtree update) - #139294 (Fix the `f16`/`f128` feature gates on integer literals) r? `@ghost` `@rustbot` modify labels: rollup
2025-04-03std: clarify RefCell::get_mut more clearlyxizheyin-1/+5
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
2025-04-03Auto merge of #137738 - Daniel-Aaron-Bloom:const_slice_make_iter, r=dtolnaybors-36/+57
Make slice iterator constructors unstably const See [tracking issue](https://github.com/rust-lang/rust/issues/137737) for justification. try-job: aarch64-apple try-job: x86_64-gnu
2025-04-03Rollup merge of #139273 - tgross35:cell-update-changes, r=jhprattMatthias Krüger-11/+4
Apply requested API changes to `cell_update` Do the following: * Switch to `impl FnOnce` rather than a generic `F`. * Change `update` to return nothing. This was discussed at a libs-api meeting [1]. Tracking issue: https://github.com/rust-lang/rust/issues/50186 [1]: https://github.com/rust-lang/rust/pull/134446#issuecomment-2770842949
2025-04-03Rollup merge of #139145 - okaneco:safe_splits, r=AmanieuMatthias Krüger-60/+34
slice: Remove some uses of unsafe in first/last chunk methods Remove unsafe `split_at_unchecked` and `split_at_mut_unchecked` in some slice `split_first_chunk`/`split_last_chunk` methods. Replace those calls with the safe `split_at` and `split_at_checked` where applicable. Add codegen tests to check for no panics when calculating the last chunk index using `checked_sub` and `split_at`. Better viewed with whitespace disabled in diff view --- The unchecked calls are mostly manual implementations of the safe methods, but with the safety condition negated from `mid <= len` to `len < mid`. ```rust if self.len() < N { None } else { // SAFETY: We manually verified the bounds of the split. let (first, tail) = unsafe { self.split_at_unchecked(N) }; // Or for the last_chunk methods let (init, last) = unsafe { self.split_at_unchecked(self.len() - N) }; ``` Unsafe is still needed for the pointer array casts. Their safety comments are unmodified.
2025-04-02Apply requested API changes to `cell_update`Trevor Gross-11/+4
Do the following: * Switch to `impl FnOnce` rather than a generic `F`. * Change `update` to return nothing. This was discussed at a libs-api meeting [1]. Tracking issue: https://github.com/rust-lang/rust/issues/50186 [1]: https://github.com/rust-lang/rust/pull/134446#issuecomment-2770842949
2025-04-02Make slice iterator constructors unstably constDaniel Bloom-36/+57
2025-03-31Refactor `diy_float`Tobias Decking-47/+15
2025-03-31Rollup merge of #139157 - mejrs:never, r=NoratriebMatthias Krüger-5/+3
Remove mention of `exhaustive_patterns` from `never` docs The example shows an exhaustive match: ```rust #![feature(exhaustive_patterns)] use std::str::FromStr; let Ok(s) = String::from_str("hello"); ``` But https://github.com/rust-lang/rust/issues/119612 moved this functionality to `#![feature(min_exhaustive_patterns)` and then stabilized it.
2025-03-31Auto merge of #139154 - jhpratt:rollup-rv8f915, r=jhprattbors-2/+2
Rollup of 5 pull requests Successful merges: - #139044 (bootstrap: Avoid cloning `change-id` list) - #139111 (Properly document FakeReads) - #139122 (Remove attribute `#[rustc_error]`) - #139132 (Improve hir_pretty for struct expressions.) - #139141 (Switch some rustc_on_unimplemented uses to diagnostic::on_unimplemented) r? `@ghost` `@rustbot` modify labels: rollup
2025-03-31Remove mention of `exhaustive_patterns` from `never` docsmejrs-5/+3
2025-03-30Rollup merge of #139141 - mejrs:on_unimpl, r=NoratriebJacob Pratt-2/+2
Switch some rustc_on_unimplemented uses to diagnostic::on_unimplemented The use on the SliceIndex impl appears unreachable, there is no mention of "vector indices" in any test output and I could not get it to show up in error messages.
2025-03-30Auto merge of #139131 - m-ou-se:format-args-struct-expr, r=Mark-Simulacrumbors-7/+1
Simplify expansion for format_args!(). Instead of calling `Placeholder::new()`, we can just use a struct expression directly. Before: ```rust Placeholder::new(…, …, …, …) ``` After: ```rust Placeholder { position: …, flags: …, width: …, precision: …, } ``` (I originally avoided the struct expression, because `Placeholder` had a lot of fields. But now that https://github.com/rust-lang/rust/pull/136974 is merged, it only has four fields left.) This will make the `fmt` argument to `fmt::Arguments::new_v1_formatted()` a candidate for const promotion, which is important if we ever hope to tackle https://github.com/rust-lang/rust/issues/92698 (It doesn't change anything yet though, because the `args` argument to `fmt::Arguments::new_v1_formatted()` is not const-promotable.)
2025-03-30slice: Remove some uses of unsafe in first/last chunk methodsokaneco-60/+34
Remove unsafe `split_at_unchecked` and `split_at_mut_unchecked` in some slice `split_first_chunk`/`split_last_chunk` methods. Replace those calls with the safe `split_at` and `split_at_checked` where applicable. Add codegen tests to check for no panics when calculating the last chunk index using `checked_sub` and `split_at`
2025-03-30use `diagnostic::on_unimplemented` insteadmejrs-2/+2
2025-03-30Simplify expansion for format_args!().Mara Bos-7/+1
Instead of calling new(), we can just use a struct expression directly. Before: Placeholder::new(…, …, …, …) After: Placeholder { position: …, flags: …, width: …, precision: …, }
2025-03-29Rollup merge of #137928 - RalfJung:const_cell, r=m-ou-seMatthias Krüger-5/+5
stabilize const_cell ``@rust-lang/libs-api`` ``@rust-lang/wg-const-eval`` I see no reason to wait any longer, so I propose we stabilize the use of `Cell` in `const fn` -- specifically the APIs listed here: ```rust // core::cell impl<T> Cell<T> { pub const fn replace(&self, val: T) -> T; } impl<T: Copy> Cell<T> { pub const fn get(&self) -> T; } impl<T: ?Sized> Cell<T> { pub const fn get_mut(&mut self) -> &mut T; pub const fn from_mut(t: &mut T) -> &Cell<T>; } impl<T> Cell<[T]> { pub const fn as_slice_of_cells(&self) -> &[Cell<T>]; } ``` Unfortunately, `set` cannot be made `const fn` yet as it drops the old contents. Fixes https://github.com/rust-lang/rust/issues/131283
2025-03-29Promise `array::from_fn` in generated in order of increasing indicesScott McMurray-6/+27
2025-03-28fix docs for `Peekable::next_if{_eq}`Yotam Ofek-3/+3
2025-03-28Rollup merge of #138976 - xizheyin:issue-138969, r=RalfJungMatthias Krüger-1/+2
Explain one-past-the-end pointer in std library Closing #138969 r? libs
2025-03-28Add `slice::align_to_uninit_mut`Nikolai Kuklin-2/+51
2025-03-28std: Explain range follows standard half-open range in `offset`xizheyin-1/+2
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>