3283 Commits

Author SHA1 Message Date
Michael Goulet
58cda76585
Rollup merge of #142700 - theemathas:remove-weak-comment, r=ibraheemdev
Remove incorrect comments in `Weak`

It is currently possible to create a dangling `Weak` to a DST by calling `Weak::new()` for a sized type, then doing an unsized coercion. Therefore, the comments are wrong.

These comments were added in <https://github.com/rust-lang/rust/pull/73845>. As far as I can tell, the guarantee in the comment was only previously used in the `as_ptr` method. However, the current implementation of `as_ptr` no longer relies on this guarantee.
2025-06-26 20:15:20 -04:00
Guillaume Gomez
30550c048d
Rollup merge of #137268 - bjoernager:c-string-eq-c-str, r=Amanieu
Allow comparisons between `CStr`, `CString`, and `Cow<CStr>`.

Closes: #137265

This PR adds the trait implementations proposed in the [ACP](https://github.com/rust-lang/libs-team/issues/517/) under the `c_string_eq_c_str` feature gate:

```rust
// core::ffi

impl PartialEq<&Self> for CStr;

impl PartialEq<CString> for CStr;

impl PartialEq<Cow<'_, Self>> for CStr;

// alloc::ffi

impl PartialEq<CStr> for CString;

impl PartialEq<&CStr> for CString;

impl PartialEq<Cow<'_, CStr>> for CString;

// alloc::borrow

impl PartialEq<CStr> for Cow<'_, CStr>;

impl PartialEq<&CStr> for Cow<'_, CStr>;

impl PartialEq<CString> for Cow<'_, CStr>;
```

As I understand it, stable traits cannot be unstably implemented for stable types, and we would thereby be forced to skip the FCP and directly stabilise these implementations (as is done in this PR).

(`@joshtriplett` mentioned that Crater may have to be run).
2025-06-24 15:39:37 +02:00
bors
8387d61a6e Auto merge of #142728 - kornelski:string-track, r=tgross35
Let String pass #[track_caller] to its Vec calls

I've added `#[track_caller]` to `String` methods that delegate to `Vec` methods that already have `#[track_caller]`.

I've also added `#[track_caller]` to methods that have `assert!` or `panic!` due to invalid inputs.
2025-06-22 23:30:10 +00:00
Jakub Beránek
74cea635e1
Rollup merge of #142764 - ChaiTRex:ilog_10_to_ilog10, r=workingjubilee
Convert `ilog(10)` to `ilog10()`

Except in tests, convert `integer.ilog(10)` to `integer.ilog10()` for better speed and to provide better examples of code that efficiently counts decimal digits. I couldn't find any instances of `integer.ilog(2)`.
2025-06-20 20:03:24 +02:00
Chai T. Rex
88f1ed4871 Convert ilog(10) to ilog10() 2025-06-20 03:37:07 -04:00
Trevor Gross
52758b7329
Rollup merge of #142668 - hkBst:less-static-mut, r=tgross35
vec_deque/fmt/vec tests: remove static mut

More rust-lang/rust#125035.

r? ```@tgross35```
2025-06-20 02:50:39 -04:00
bors
5b74275f89 Auto merge of #142294 - GuillaumeGomez:specialize-tostring-on-128-integers, r=tgross35
Use a distinct `ToString` implementation for `u128` and `i128`

Part of https://github.com/rust-lang/rust/issues/135543.

Follow-up of rust-lang/rust#136264.

When working on https://github.com/rust-lang/rust/pull/142098, I realized that `i128` and `u128` could also benefit from a distinct `ToString` implementation so here it.

The last commit is just me realizing that I forgot to add the format tests for `usize` and `isize`.

Here is the bench comparison:

| bench name | last nightly | with this PR | diff |
|-|-|-|-|
| bench_i128 | 29.25 ns/iter (+/- 0.66) | 17.52 ns/iter (+/- 0.7) | -40.1% |
| bench_u128 | 34.06 ns/iter (+/- 0.21) | 16.1 ns/iter (+/- 0.6) | -52.7% |

I used this code to test:

```rust
#![feature(test)]

extern crate test;

use test::{Bencher, black_box};

#[inline(always)]
fn convert_to_string<T: ToString>(n: T) -> String {
    n.to_string()
}

macro_rules! decl_benches {
    ($($name:ident: $ty:ident,)+) => {
        $(
	    #[bench]
            fn $name(c: &mut Bencher) {
                c.iter(|| convert_to_string(black_box({ let nb: $ty = 20; nb })));
            }
	)+
    }
}

decl_benches! {
    bench_u128: u128,
    bench_i128: i128,
}
```
2025-06-20 02:55:43 +00:00
Kornel
c109b28ac4 Add #[track_caller] to String methods that assert inputs 2025-06-19 16:55:40 +01:00
Kornel
b26dfa175c Let String pass #[track_caller] to its Vec calls 2025-06-19 16:55:09 +01:00
Tim (Theemathas) Chirananthavat
644469e961 Remove incorrect comments in Weak
It is currently possible to create a dangling `Weak` to a DST by
calling `Weak::new()` for a sized type, then doing an unsized coercion.
Therefore, the comments are wrong.

These comments were added in <https://github.com/rust-lang/rust/pull/73845>.
As far as I can tell, the guarantee in the comment was only previously
used in the `as_ptr` method. However, the current implementation of
`as_ptr` no longer relies on this guarantee.
2025-06-19 12:04:24 +07:00
Marijn Schouten
5bd918fcd8 vec_deque tests: remove static mut 2025-06-18 11:51:40 +00:00
bors
6f935a044d Auto merge of #141061 - dpaoliello:shimasfn, r=bjorn3
Change __rust_no_alloc_shim_is_unstable to be a function

This fixes a long sequence of issues:

1. A customer reported that building for Arm64EC was broken: #138541
2. This was caused by a bug in my original implementation of Arm64EC support, namely that only functions on Arm64EC need to be decorated with `#` but Rust was decorating statics as well.
3. Once I corrected Rust to only decorate functions, I started linking failures where the linker couldn't find statics exported by dylib dependencies. This was caused by the compiler not marking exported statics in the generated DEF file with `DATA`, thus they were being exported as functions not data.
4. Once I corrected the way that the DEF files were being emitted, the linker started failing saying that it couldn't find `__rust_no_alloc_shim_is_unstable`. This is because the MSVC linker requires the declarations of statics imported from other dylibs to be marked with `dllimport` (whereas it will happily link to functions imported from other dylibs whether they are marked `dllimport` or not).
5. I then made a change to ensure that `__rust_no_alloc_shim_is_unstable` was marked as `dllimport`, but the MSVC linker started emitting warnings that `__rust_no_alloc_shim_is_unstable` was marked as `dllimport` but was declared in an obj file. This is a harmless warning which is a performance hint: anything that's marked `dllimport` must be indirected via an `__imp` symbol so I added a linker arg in the target to suppress the warning.
6. A customer then reported a similar warning when using `lld-link` (<https://github.com/rust-lang/rust/pull/140176#issuecomment-2872448443>). I don't think it was an implementation difference between the two linkers but rather that, depending on the obj that the declaration versus uses of `__rust_no_alloc_shim_is_unstable` landed in we would get different warnings, so I suppressed that warning as well: #140954.
7. Another customer reported that they weren't using the Rust compiler to invoke the linker, thus these warnings were breaking their build: <https://github.com/rust-lang/rust/pull/140176#issuecomment-2881867433>. At that point, my original change was reverted (#141024) leaving Arm64EC broken yet again.

Taking a step back, a lot of these linker issues arise from the fact that `__rust_no_alloc_shim_is_unstable` is marked as `extern "Rust"` in the standard library and, therefore, assumed to be a foreign item from a different crate BUT the Rust compiler may choose to generate it either in the current crate, some other crate that will be statically linked in OR some other crate that will by dynamically imported.

Worse yet, it is impossible while building a given crate to know if `__rust_no_alloc_shim_is_unstable` will statically linked or dynamically imported: it might be that one of its dependent crates is the one with an allocator kind set and thus that crate (which is compiled later) will decide depending if it has any dylib dependencies or not to import `__rust_no_alloc_shim_is_unstable` or generate it. Thus, there is no way to know if the declaration of `__rust_no_alloc_shim_is_unstable` should be marked with `dllimport` or not.

There is a simple fix for all this: there is no reason `__rust_no_alloc_shim_is_unstable` must be a static. It needs to be some symbol that must be linked in; thus, it could easily be a function instead. As a function, there is no need to mark it as `dllimport` when dynamically imported which avoids the entire mess above.

There may be a perf hit for changing the `volatile load` to be a `tail call`, so I'm happy to change that part back (although I question what the codegen of a `volatile load` would look like, and if the backend is going to try to use load-acquire semantics).

Build with this change applied BEFORE #140176 was reverted to demonstrate that there are no linking issues with either MSVC or MinGW: <https://github.com/rust-lang/rust/actions/runs/15078657205>

Incidentally, I fixed `tests/run-make/no-alloc-shim` to work with MSVC as I needed it to be able to test locally (FYI for #128602)

r? `@bjorn3`
cc `@jieyouxu`
2025-06-18 09:24:40 +00:00
Jacob Pratt
eb7d246d85
Rollup merge of #142520 - hkBst:less-static-mut, r=tgross35
alloc: less static mut + some cleanup

I'm looking into https://github.com/rust-lang/rust/issues/125035 and would like some feedback on my approach.
2025-06-17 23:19:35 +02:00
Jubilee
346adcef4f
Rollup merge of #138538 - hkBst:patch-4, r=tgross35
Make performance description of String::{insert,insert_str,remove} more precise
2025-06-17 00:28:13 -07:00
Marijn Schouten
2d3a37d1e5 linked_list tests: buff check_links 2025-06-17 07:01:40 +00:00
Marijn Schouten
0348a4a717 Make performance of String::insert_str more precise 2025-06-17 06:04:04 +00:00
Daniel Paoliello
6906b44e1c Change __rust_no_alloc_shim_is_unstable to be a function 2025-06-16 10:54:07 -07:00
Guillaume Gomez
aec32486a8 Specialize ToString implementation on u128 and i128 2025-06-16 11:54:15 +02:00
Marijn Schouten
e8dfd812dd linked_list tests: less static mut 2025-06-16 04:46:06 +00:00
Matthias Krüger
22f91aefd5
Rollup merge of #142484 - dtolnay:bsetextract, r=m-ou-se
Remove unneeded lifetime bound from signature of BTreeSet::extract_if

One way to observe the difference between these signatures, using 0 explicit lifetimes and 0 contrived where-clauses:

```rust
use std::collections::btree_set::{BTreeSet, ExtractIf};
use std::ops::RangeFull;

fn repro(
    set: &mut BTreeSet<i32>,
    predicate: impl Fn(i32) -> bool,
) -> ExtractIf<i32, RangeFull, impl FnMut(&i32) -> bool> {
    set.extract_if(.., move |x| predicate(*x))
}
```

**Before:**

```console
error[E0311]: the parameter type `impl Fn(i32) -> bool` may not live long enough
 --> src/lib.rs:8:5
  |
5 |     set: &mut BTreeSet<i32>,
  |          ------------------ the parameter type `impl Fn(i32) -> bool` must be valid for the anonymous lifetime defined here...
...
8 |     set.extract_if(.., move |x| predicate(*x))
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...so that the type `impl Fn(i32) -> bool` will meet its required lifetime bounds
  |
help: consider adding an explicit lifetime bound
  |
4 ~ fn repro<'a>(
5 ~     set: &'a mut BTreeSet<i32>,
6 ~     predicate: impl Fn(i32) -> bool + 'a,
7 ~ ) -> ExtractIf<'a, i32, RangeFull, impl FnMut(&i32) -> bool> {
  |
```

**After:** compiles success.

- Tracking issue: https://github.com/rust-lang/rust/issues/70530
2025-06-14 11:27:12 +02:00
Jubilee
b38ee5e689
Rollup merge of #142046 - Qelxiros:122742-vec_peek_mut, r=cuviper
add Vec::peek_mut

Tracking issue: rust-lang/rust#122742
2025-06-13 20:59:16 -07:00
David Tolnay
dac9d78647
Remove unneeded lifetimes from signature of BTreeSet::extract_if 2025-06-13 20:33:54 -07:00
Tamir Duberstein
a82062055a
Delegate <CStr as Debug> to ByteStr
This allows UTF-8 characters to be printed without escapes, rather than
just ASCII.
2025-06-12 12:53:14 -04:00
Jeremy Smart
9d19cbe29b
update docs, test 2025-06-11 22:57:57 -04:00
Trevor Gross
fd06f6d658
Rollup merge of #142238 - RalfJung:nonnull_provenance, r=workingjubilee
stabilize nonnull_provenance

Fixes https://github.com/rust-lang/rust/issues/135243
FCP passed in https://github.com/rust-lang/rust/issues/135243
2025-06-09 12:17:55 -05:00
Trevor Gross
ab87ed150b
Rollup merge of #141993 - tgross35:use-in-tree-builtins, r=bjorn3
Use the in-tree `compiler-builtins` for the sysroot

Many of `std`'s dependency have a dependency on the crates.io `compiler-builtins` when used with the feature `rustc-std-workspace-core`. Use a Cargo patch to select the in-tree version instead.

`compiler-builtins` is also added as a dependency of `rustc-std-workspace-core` so these crates can remove their crates.io dependency in the future.

Zulip discussion: [#t-compiler > Using in-tree compiler-builtins](https://rust-lang.zulipchat.com/#narrow/channel/131828-t-compiler/topic/Using.20in-tree.20compiler-builtins/with/522445336)

Once this merges, the following PRs will need to make it to a release for the relevant crates:

- https://github.com/rust-lang/getopts/pull/119 (can merge at any time)
- https://github.com/rust-lang/hashbrown/pull/625 (can merge at any time)
- https://github.com/rust-lang/stdarch/pull/1825
- https://github.com/rust-lang/rustc-demangle/pull/80
- https://github.com/rust-lang/cfg-if/pull/84
- https://github.com/unicode-rs/unicode-width/pull/77

The above should cover all tier 1 targets with no `std` features enabled. The remaining cover the rest:

- https://github.com/alexcrichton/dlmalloc-rs/pull/50 (wasm, xous, sgx)
- https://github.com/gimli-rs/gimli/pull/769
- https://github.com/r-efi/r-efi/pull/89 (efi)
- https://github.com/r-efi/r-efi-alloc/pull/9 (efi)
- https://github.com/fortanix/rust-sgx/pull/770 (sgx)
- https://github.com/hermit-os/hermit-rs/pull/718 (hermit)
- https://github.com/bytecodealliance/wasi-rs/pull/108 (wasi)
- https://github.com/gimli-rs/addr2line/pull/345
- https://github.com/oyvindln/adler2/pull/2
- https://github.com/BurntSushi/memchr/pull/180
- https://github.com/Frommi/miniz_oxide/pull/173
- https://github.com/gimli-rs/object/pull/777

try-job: x86_64-gnu
try-job: test-various
2025-06-09 12:17:53 -05:00
Ralf Jung
3164ca8bb4 stabilize nonnull_provenance 2025-06-09 12:36:24 +02:00
Trevor Gross
cc3e57147e Use the in-tree compiler-builtins
Many of `std`'s dependency have a dependency on the crates.io
`compiler-builtins` when used with the feature
`rustc-std-workspace-core`. Use a Cargo patch to select the in-tree
version instead.

`compiler-builtins` is also added as a dependency of
`rustc-std-workspace-core` so these crates can remove their crates.io
dependency in the future.
2025-06-08 02:36:58 +00:00
Jeremy Smart
cc80429021
fix tests 2025-06-06 19:22:15 -04:00
Tshepang Mbambo
0ffff4657a remove extraneous text
"Basic usage" implies there is an example that shows advanced usage
2025-06-06 06:39:51 +02:00
Jeremy Smart
188c40126d
add Vec::peek_mut 2025-06-04 18:15:12 -04:00
Matthias Krüger
98421b765f
Rollup merge of #141924 - rs-sac:extr-doc, r=jhpratt
Lightly tweak docs for BTree{Map,Set}::extract_if

- Move explanations into comments to match style
- Explain the second examples
- Make variable names match the data structure

Related rust-lang/rust#70530
2025-06-04 16:24:09 +02:00
Josh Stone
c87b072952 Remove more library bootstrap 2025-06-02 14:46:19 -07:00
Sidney Cammeresi
a20cf473e7 Lightly tweak docs for BTree{Map,Set}::extract_if
- Move explanations into comments to match style
- Explain the second examples
- Make variable names match the data structure
2025-06-02 10:10:00 -07:00
Matthias Krüger
04641b14a3
Rollup merge of #141805 - tgross35:update-builtins, r=tgross35
Update `compiler-builtins` to 0.1.160

Includes the following changes:

* Enable `__powitf2` on MSVC [1]
* Update `CmpResult` to use a pointer-sized return type [2]
* Better code reuse between `libm` and `compiler-builtins` [3], [4]
* Stop building C versions of `__netf2` [5] since we have our own implementation

[1]: https://github.com/rust-lang/compiler-builtins/pull/918
[2]: https://github.com/rust-lang/compiler-builtins/pull/920
[3]: https://github.com/rust-lang/compiler-builtins/pull/879
[4]: https://github.com/rust-lang/compiler-builtins/pull/925
[5]: https://github.com/rust-lang/compiler-builtins/pull/828
2025-05-31 18:51:50 +02:00
Trevor Gross
ca1c67ad76 Update compiler-builtins to 0.1.160
Includes the following changes:

* Enable `__powitf2` on MSVC [1]
* Update `CmpResult` to use a pointer-sized return type [2]
* Better code reuse between `libm` and `compiler-builtins` [3], [4]
* Stop building C versions of `__netf2` [5] since we have our own
  implementation

[1]: https://github.com/rust-lang/compiler-builtins/pull/918
[2]: https://github.com/rust-lang/compiler-builtins/pull/920
[3]: https://github.com/rust-lang/compiler-builtins/pull/879
[4]: https://github.com/rust-lang/compiler-builtins/pull/925
[5]: https://github.com/rust-lang/compiler-builtins/pull/828
2025-05-30 16:09:12 +00:00
Sidney Cammeresi
8656d9e619 Unit test for Range parameter of BTreeMap::extract_if 2025-05-27 08:31:56 -07:00
Sidney Cammeresi
38c37eb3cb Update docs for new Range parameter to BTreeMap::extract_if etc. 2025-05-27 08:31:56 -07:00
Sidney Cammeresi
1ae96fcd79 Update tests with Range parameter to BTreeMap::extract_if etc. 2025-05-27 08:31:56 -07:00
Sidney Cammeresi
51d247c2cf Add Range parameter to BTreeMap::extract_if and BTreeSet::extract_if
This change was requested in the btree_extract_if tracking issue:

https://github.com/rust-lang/rust/issues/70530#issuecomment-2486566328
2025-05-27 08:31:40 -07:00
Jacob Pratt
3338ff7dcf
Rollup merge of #141108 - PaulDance:fix-extract_if-docs, r=Mark-Simulacrum
Docs(lib): Fix `extract_if` docs

Various fixes to the documentation comments of the several `extract_if` collection methods available. It originally started with a small typo fix in `Vec`'s spotted when reading the 1.87 release notes, but then by looking at the others' for comparison in order to try determining what was the intended sentence, some inconsistencies were spotted. Therefore, some other changes are also proposed here to reduce these avoidable differences, going more and more nit-picky along the way. See the individual commits for more details about each change.

`@rustbot` label T-libs A-collections A-docs
2025-05-25 04:00:56 +02:00
Matthias Krüger
a31a39833a
Rollup merge of #141370 - WaffleLapkin:hiiii, r=jhpratt
add doc alias `replace_first` for `str::replacen`

`replace_first` is a sensible name for a function, analogous to actually existing `<[_]>::split_first`, for example. (I just saw someone try to search for it)

I think it's reasonable to add such an alias for `replacen`, which replaces the first occurrence of passed a 1.
2025-05-22 07:19:04 +02:00
waffle
cff790c98e
add doc alias replace_first for str::replacen 2025-05-21 23:43:57 +02:00
Matthias Krüger
b5edec2811
Rollup merge of #141230 - xizheyin:issue-141217, r=tgross35
std: fix doctest and explain for `as_slices` and `as_mut_slices` in `VecDeque`

Fixes #141217

r? libs
2025-05-21 22:14:58 +02:00
Matthias Krüger
d4b7915a59
Rollup merge of #140526 - Natr1x:sort-direction-documentation, r=dtolnay
docs: Specify that common sort functions sort in an ascending direction

From [forum discussion](https://users.rust-lang.org/t/is-there-a-way-to-sort-a-slice-in-specifically-ascending-or-descending-order/128998?u=natr1x) it seems like the sorting direction can be expected to always be ascending (in terms of `cmp::Ordering`).

If this is the case then it would be nice to have this stated in the documentation.
2025-05-21 22:14:57 +02:00
xizheyin
46a5c91591 std: fix doctest and explain for as_slices and as_mut_slices in VecDeque
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
2025-05-21 22:13:23 +08:00
Matthias Krüger
c052d263d0
Rollup merge of #141289 - compiler-errors:more-self, r=jhpratt
use `Self` alias in self types rather than manually substituting it

Of the rougly 145 uses of `self: Ty` in the standard library, 5 of them don't use `Self` but instead choose to manually "substitute" the `impl`'s self type into the type.

This leads to weird behavior sometimes (https://github.com/rust-lang/rust/issues/140611#issuecomment-2883761300) -- **to be clear**, none of these usages actually trigger any bugs, but it's possible that they may break in the future (or at least lead to lints), so let's just "fix" them proactively.
2025-05-20 20:57:28 +02:00
Michael Goulet
7b5ea0e7f5 use Self alias in self types rather than manually substituting it 2025-05-20 16:03:04 +02:00
Paul Mabileau
b1d7480acb
Docs(lib/extract_if): Unify example description
Signed-off-by: Paul Mabileau <paul.mabileau@harfanglab.fr>
2025-05-17 02:54:21 +02:00
Paul Mabileau
014434e96f
Docs(lib/extract_if): Unify paragraph about elements mutation
Take the one from `BTreeMap` that seems the best-worded and most
precise among the available variations.

Signed-off-by: Paul Mabileau <paul.mabileau@harfanglab.fr>
2025-05-17 02:45:56 +02:00