Commit Graph

3220 Commits

Author SHA1 Message Date
Guillaume Gomez
aca12a8216
Rollup merge of #140724 - tgross35:update-builtins, r=tgross35
Update `compiler-builtins` to 0.1.158

Includes the following changes:

* Require `target_has_atomic = "ptr"` for runtime feature detection [1]

[1]: https://github.com/rust-lang/compiler-builtins/pull/909
2025-05-07 10:50:50 +02:00
Jacob Pratt
5b165aab89
Rollup merge of #140668 - vkrivopalov:vecdeque-truncate-front, r=jhpratt
Implement `VecDeque::truncate_front()`

Tracking issue: #140667
2025-05-07 00:29:24 +00:00
Jacob Pratt
25631ff093
Rollup merge of #140483 - baumanj:patch-1, r=workingjubilee
Comment on `Rc` abort-guard strategy without naming unrelated fn

`wrapped_add` is used, not `checked_add`, so avoid mentioning specific fn calls that may vary slightly based on "whatever produces the best code" and focus on things that will remain constant into the future.
2025-05-07 00:29:22 +00:00
Trevor Gross
4de822c3d2 Update compiler-builtins to 0.1.158
Includes the following changes:

* Require `target_has_atomic = "ptr"` for runtime feature detection

[1]: https://github.com/rust-lang/compiler-builtins/pull/909
2025-05-06 23:40:50 +00:00
Jon Bauman
6a4af821b0
Update rc.rs docs
Update comment per review feedback
2025-05-06 13:19:42 -07:00
Stuart Cook
d5c09b4aa6
Rollup merge of #139773 - thaliaarchi:vec-into-iter-last, r=workingjubilee
Implement `Iterator::last` for `vec::IntoIter`

Avoid iterating everything when we have random access to the last element.
2025-05-06 16:28:39 +10:00
Stuart Cook
5ca0053c79
Rollup merge of #139764 - dtolnay:extractif, r=Amanieu
Consistent trait bounds for ExtractIf Debug impls

Closes #137654. Refer to that issue for a table of the **4** different impl signatures we previously had in the standard library for Debug impls of various ExtractIf iterator types.

The one we are standardizing on is the one so far only used by `alloc::collections::linked_list::ExtractIf`, which is _no_ `F: Debug` bound, _no_ `F: FnMut` bound, only `T: Debug` bound.

This PR applies the following signature changes:

```diff
/* alloc::collections::btree_map */

    pub struct ExtractIf<'a, K, V, F, A = Global>
    where
-       F: 'a + FnMut(&K, &mut V) -> bool,
        Allocator + Clone,

    impl Debug for ExtractIf<'a, K, V, F,
+       A,
    >
    where
        K: Debug,
        V: Debug,
-       F: FnMut(&K, &mut V) -> bool,
+       A: Allocator + Clone,
```

```diff
/* alloc::collections::btree_set */

    pub struct ExtractIf<'a, T, F, A = Global>
    where
-       T: 'a,
-       F: 'a + FnMut(&T) -> bool,
        Allocator + Clone,

    impl Debug for ExtractIf<'a, T, F, A>
    where
        T: Debug,
-       F: FnMut(&T) -> bool,
        A: Allocator + Clone,
```

```diff
/* alloc::collections::linked_list */

    impl Debug for ExtractIf<'a, T, F,
+       A,
    >
    where
        T: Debug,
+       A: Allocator,
```

```diff
/* alloc::vec */

    impl Debug for ExtractIf<'a, T, F, A>
    where
        T: Debug,
-       F: Debug,
        A: Allocator,
-       A: Debug,
```

```diff
/* std::collections::hash_map */

    pub struct ExtractIf<'a, K, V, F>
    where
-       F: FnMut(&K, &mut V) -> bool,

    impl Debug for ExtractIf<'a, K, V, F>
    where
+       K: Debug,
+       V: Debug,
-       F: FnMut(&K, &mut V) -> bool,
```

```diff
/* std::collections::hash_set */

    pub struct ExtractIf<'a, T, F>
    where
-       F: FnMut(&T) -> bool,

    impl Debug for ExtractIf<'a, T, F>
    where
+       T: Debug,
-       F: FnMut(&T) -> bool,
```

I have made the following changes to bring these types into better alignment with one another.

- Delete `F: Debug` bounds. These are especially problematic because Rust closures do not come with a Debug impl, rendering the impl useless.

- Delete `A: Debug` bounds. Allocator parameters are unstable for now, but in the future this would become an API commitment that we do not debug-print a representation of the allocator when printing an iterator.

- Delete `F: FnMut` bounds. Requires `hashbrown` PR: https://github.com/rust-lang/hashbrown/pull/616. **API commitment:** we commit to not doing RefCell voodoo inside ExtractIf to have some way for its Debug impl (which takes &amp;self) to call a FnMut closure, if this is even possible.

- Add `T: Debug` bounds (or `K`/`V`), even on Debug impls that do not currently make use of them, but might in the future. **Breaking change.** Must backport into Rust 1.87 (current beta) or do a de-stabilization PR in beta to delay those types by one release.

- Render using `debug_struct` + `finish_non_exhaustive`, instead of `debug_tuple`.

- Do not render the _entire_ underlying collection.

- Show a "peek" field indicating the current position of the iterator.
2025-05-06 16:28:38 +10:00
David Tolnay
c35914383a
Consistent trait bounds for ExtractIf Debug impls 2025-05-05 19:46:46 -07:00
Vladimir Krivopalov
cdf4143eb8 Implement VecDeque::truncate_front()
Tracking issue: #140667

Signed-off-by: Vladimir Krivopalov <vladimir.krivopalov@gmail.com>
2025-05-05 11:13:26 -04:00
Trevor Gross
512dab0bee
Rollup merge of #140648 - tgross35:update-builtins, r=tgross35
Update `compiler-builtins` to 0.1.157

Includes the following changes:

* Use runtime feature detection for fma routines on x86 [1]

Fixes: https://github.com/rust-lang/rust/issues/140452

[1]: https://github.com/rust-lang/compiler-builtins/pull/896
2025-05-05 00:20:58 -04:00
Trevor Gross
c44e1d65f3
Rollup merge of #135734 - nk9:extract_if-doc-equivalent, r=tgross35
Correct `extract_if` sample equivalent.

Tracking issue: https://github.com/rust-lang/rust/issues/43244

Original PR: #133265

The sample code marked as equivalent in the doc comment isn't currently equivalent. Given the same predicate and range, if your vector were `[1, 2, 3, 3, 3, 3, 3, 3, 4, 5, 6]`, then all of the 3s would be removed. `i` is only incremented when an element is dropped, but `range.end` is unchanged, so the items shift down. I got very confused when reading the docs and trying to square this sample code with the explanation of how the function works.

Fortunately, the real `extract_if()` does not have this problem. I've added an `end` variable to align the behavior. I've also taken the opportunity to simplify the predicate, which now just matches odd numbers, and to pad out the vec of numbers to line up the zero-indexed range with the integers in the vec.

r? the8472
2025-05-05 00:20:57 -04:00
Trevor Gross
435fc7d685 Update compiler-builtins to 0.1.157
Includes the following changes:

* Use runtime feature detection for fma routines on x86 [1]

Fixes: https://github.com/rust-lang/rust/issues/140452

[1]: https://github.com/rust-lang/compiler-builtins/pull/896
2025-05-04 22:54:55 +00:00
Nick Kocharhook
52d806a7c0 extract_if's sample equivalent now really equivalent.
Simpler predicate.
Compare sample code output to that of the library function.
2025-05-04 23:38:54 +01:00
Paolo Barbolini
5cbb27ff90 Suggest retain_mut over retain as Vec::extract_if alternative 2025-05-03 16:39:47 +00:00
Thalia Archibald
cbdd7134ff Implement Iterator::last for vec::IntoIter 2025-05-02 20:08:28 -07:00
bors
427288b3ce Auto merge of #140188 - nnethercote:streamline-format-macro, r=cuviper
Streamline the `format` macro.

Removing the unnecessary local variable speeds up compilation a little.

r? `@cuviper`
2025-04-30 04:04:21 +00:00
Jon Bauman
dd20225681
Update rc.rs docs
`wrapped_add` is used, not `checked_add`
2025-04-29 11:24:31 -07:00
DaniPopes
f07cc409d3
Rename sub_ptr to offset_from_unsigned in docs 2025-04-28 13:58:27 +02:00
bors
a932eb36f8 Auto merge of #123239 - Urgau:dangerous_implicit_autorefs, r=jdonszelmann,traviscross
Implement a lint for implicit autoref of raw pointer dereference - take 2

*[t-lang nomination comment](https://github.com/rust-lang/rust/pull/123239#issuecomment-2727551097)*

This PR aims at implementing a lint for implicit autoref of raw pointer dereference, it is based on #103735 with suggestion and improvements from https://github.com/rust-lang/rust/pull/103735#issuecomment-1370420305.

The goal is to catch cases like this, where the user probably doesn't realise it just created a reference.

```rust
pub struct Test {
    data: [u8],
}

pub fn test_len(t: *const Test) -> usize {
    unsafe { (*t).data.len() }  // this calls <[T]>::len(&self)
}
```

Since #103735 already went 2 times through T-lang, where they T-lang ended-up asking for a more restricted version (which is what this PR does), I would prefer this PR to be reviewed first before re-nominating it for T-lang.

----

Compared to the PR it is as based on, this PR adds 3 restrictions on the outer most expression, which must either be:
   1. A deref followed by any non-deref place projection (that intermediate deref will typically be auto-inserted)
   2. A method call annotated with `#[rustc_no_implicit_refs]`.
   3. A deref followed by a `addr_of!` or `addr_of_mut!`. See bottom of post for details.

There are several points that are not 100% clear to me when implementing the modifications:
 - ~~"4. Any number of automatically inserted deref/derefmut calls." I as never able to trigger this. Am I missing something?~~ Fixed
 - Are "index" and "field" enough?

----

cc `@JakobDegen` `@WaffleLapkin`
r? `@RalfJung`

try-job: dist-various-1
try-job: dist-various-2
2025-04-28 08:25:23 +00:00
bors
0134651fb8 Auto merge of #136316 - GrigorenkoPV:generic_atomic, r=Mark-Simulacrum
Create `Atomic<T>` type alias (rebase)

Rebase of #130543.

Additional changes:
- Switch from `allow` to `expect` for `private_bounds` on `AtomicPrimitive`
- Unhide `AtomicPrimitive::AtomicInner` from docs, because rustdoc shows the definition `pub type Atomic<T> = <T as AtomicPrimitive>::AtomicInner;` and generated links for it.
  - `NonZero` did not have this issue, because they kept the new alias private before the direction was changed.
- Use `Atomic<_>` in more places, including inside `Once`'s `Futex`. This is possible thanks to https://github.com/rust-lang/rust-clippy/pull/14125

The rest will either get moved back to #130543 or #130543 will be closed in favor of this instead.

---

* ACP: https://github.com/rust-lang/libs-team/issues/443#event-14293381061
* Tracking issue: #130539
2025-04-28 05:12:59 +00:00
Chris Denton
55f93265f0
Rollup merge of #138939 - SabrinaJewson:arc-is-unique, r=tgross35
Add `Arc::is_unique`

Adds

```rs
impl<T> Arc<T> {
    pub fn is_unique(this: &Self) -> bool;
}
```

Tracking issue: #138938
ACP: https://github.com/rust-lang/libs-team/issues/560
2025-04-28 01:58:48 +00:00
Nicholas Nethercote
bc8df506f6 Streamline the format macro.
Removing the unnecessary local variable speeds up compilation a little.
2025-04-28 06:56:13 +10:00
SabrinaJewson
2ab403441f
Add Arc::is_unique 2025-04-27 14:55:46 +01:00
Matthias Krüger
bd3af53489
Rollup merge of #137714 - DiuDiu777:doc-fix, r=tgross35
Update safety documentation for `CString::from_ptr` and `str::from_boxed_utf8_unchecked`

## PR Description​
This PR addresses missing safety documentation for two APIs:

​**1. alloc::ffi::CStr::from_raw**​

- ​`Alias`: The pointer ​must not be aliased​ (accessed via other pointers) during the reconstructed CString's lifetime.
- `Owning`: Calling this function twice on the same pointer and creating two objects with overlapping lifetimes, introduces two alive owners of the same memory. This may result in a double-free.
- `Dangling`: The prior documentation required the pointer to originate from CString::into_raw, but this constraint is incomplete. A validly sourced pointer can also cause undefined behavior (UB) if it becomes dangling. A simple Poc for this situation:
```
use std::ffi::CString;
use std::os::raw::c_char;

fn create_dangling() -> *mut c_char {
    let local_ptr: *mut c_char = {
        let valid_data = CString::new("valid").unwrap();
        valid_data.into_raw()
    };

    unsafe {
        let _x = CString::from_raw(local_ptr);
    }
    local_ptr
}

fn main() {
    let dangling = create_dangling();
    unsafe {let _y = CString::from_raw(dangling);} // Cause UB!
}
```

​**2. alloc::str::from_boxed_utf8_unchecked**​

- `ValidStr`: Bytes must contain a ​valid UTF-8 sequence.
2025-04-27 11:54:57 +02:00
Matthias Krüger
9630242e5e
Rollup merge of #137439 - clarfonthey:c-str-module, r=tgross35
Stabilise `std::ffi::c_str`

This finished FCP in #112134 but never actually got a stabilisation PR. Since the FCP in #120048 recently passed to add the `os_str` module, it would be nice to also merge this too, to ensure that both get added in the next version.

Note: The added stability attributes which *somehow* were able to be omitted before (rustc bug?) were added based on the fact that they were added in 302551388b, which ended up in 1.85.0.

Closes: https://github.com/rust-lang/rust/issues/112134

r? libs-api
2025-04-27 11:54:56 +02:00
LemonJ
bfdd947bbd fix missing doc in CString::from_raw and str::from_boxed_utf8_unchecked 2025-04-27 12:49:37 +08:00
Christopher Durham
4d93f60568 use generic Atomic type where possible
in core/alloc/std only for now, and ignoring test files

Co-authored-by: Pavel Grigorenko <GrigorenkoPV@ya.ru>
2025-04-27 02:18:08 +03:00
Matthias Krüger
986750ded4
Rollup merge of #140232 - nnethercote:rm-unnecessary-clones, r=SparrowLii
Remove unnecessary clones

r? `@SparrowLii`
2025-04-24 08:13:01 +02:00
Nicholas Nethercote
055a27da2a Remove some unnecessary clones.
I found these by grepping for `&[a-z_\.]*\.clone()`, i.e. expressions
like `&a.b.clone()`, which are sometimes unnecessary clones, and also
looking at clones nearby to cases like that.
2025-04-24 11:12:34 +10:00
Trevor Gross
e800bf7df4 Update compiler_builtins to 0.1.156
Includes the following changes:

* Provide `abort` on AVR [1]

[1]: https://github.com/rust-lang/compiler-builtins/pull/830
2025-04-22 05:46:51 +00:00
Chris Denton
1cb9a0d450
Rollup merge of #140118 - tamird:cstr-cleanup, r=joboet
{B,C}Str: minor cleanup

(hopefully) uncontroversial bits extracted from #139994.
2025-04-21 18:53:21 +00:00
Urgau
40ba47d3b0 Implement lint against dangerous implicit autorefs 2025-04-20 11:36:28 +02:00
Urgau
1632f624fb Add #[rustc_no_implicit_autorefs] and apply it to std methods 2025-04-20 11:36:22 +02:00
Chris Denton
3c5aef3625
Rollup merge of #139922 - jnqnfe:cstr_doc_fix, r=jhpratt
fix incorrect type in cstr `to_string_lossy()` docs

Restoring what it said prior to commit 67065fe in which it was changed incorrectly with no supporting explanation.

Closes #139835.
2025-04-19 14:01:39 +00:00
Tamir Duberstein
f727b3622b
Invert <CString as Deref>::deref and CString::as_c_str
This is consistent with the style of `ByteString`.
2025-04-18 10:31:12 -04:00
Trevor Gross
7d6f41b08e Update compiler-builtins to 0.1.155
Includes the following changes:

* Replace `#[naked]` with `#[unsafe(naked)]` [1] [2]
* Replace `bl!` with `asm_sym` [3]

[1]: https://github.com/rust-lang/compiler-builtins/pull/817
[2]: https://github.com/rust-lang/compiler-builtins/pull/821
[3]: https://github.com/rust-lang/compiler-builtins/pull/820
2025-04-17 19:08:27 +00:00
Lyndon Brown
2f0ba67919 fix incorrect type in cstr to_string_lossy() docs
Restoring what it said prior to commit 67065fe in which it was changed
incorrectly with no supporting explanation.

Closes #139835.
2025-04-16 14:42:11 +01:00
GenYuLi
1baa62e884 Fix typo in documentation
Correct the misspelling of "indentifier" to "identifier" in `library/alloc/src/fmt.rs`.
2025-04-12 22:26:38 +08:00
Stuart Cook
c8acc23d1d
Rollup merge of #139600 - tgross35:update-builtins, r=tgross35
Update `compiler-builtins` to 0.1.153

Includes the following changes:

* Avoid OOB access in `memcpy` and `memmove` [1]
* Enable intrinsics on AVR [2]
* `libm` updates to avoid using `core::arch` vector intrinsics [3]
* Re-enable `f16` on aarch64 without Neon [4]

[1]: https://github.com/rust-lang/compiler-builtins/pull/799
[2]: https://github.com/rust-lang/compiler-builtins/pull/791
[3]: https://github.com/rust-lang/compiler-builtins/pull/814
[4]: https://github.com/rust-lang/compiler-builtins/pull/809
2025-04-11 13:31:49 +10:00
Trevor Gross
b435def33c Update compiler-builtins to 0.1.153
Includes the following changes:

* Avoid OOB access in `memcpy` and `memmove` [1]
* Enable intrinsics on AVR [2]
* `libm` updates to avoid using `core::arch` vector intrinsics [3]
* Re-enable `f16` on aarch64 without Neon [4]

[1]: https://github.com/rust-lang/compiler-builtins/pull/799
[2]: https://github.com/rust-lang/compiler-builtins/pull/791
[3]: https://github.com/rust-lang/compiler-builtins/pull/814
[4]: https://github.com/rust-lang/compiler-builtins/pull/809
2025-04-10 17:40:15 +00:00
bors
6813f955a6 Auto merge of #139279 - BoxyUwU:bump-boostrap, r=jieyouxu
Bump boostrap compiler to new beta

try-job: `*msvc*`
2025-04-10 00:43:25 +00:00
Boxy
acf678bd4c intra-doc link 2025-04-09 12:29:59 +01:00
Boxy
c93005ee65 update cfgs 2025-04-09 12:29:59 +01:00
Boxy
a6c2ec04b4 replace version placeholder 2025-04-09 12:29:59 +01:00
lincot
09d5bcf1ad
Speed up String::push and String::insert
Improve performance of `String` methods by avoiding unnecessary memcpy
for the character bytes, with added codegen check to ensure compliance.
2025-04-09 13:06:10 +03:00
Jonathan Gruner
4aab8e88e4 document panic behavior of Vec::resize and Vec::resize_with 2025-04-08 00:00:38 +02:00
bors
25a615bf82 Auto merge of #138951 - jwnrt:alloc-raw-vec-strict-prov, r=Noratrieb
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-06 23:07:48 +00:00
bjorn3
fde54c2c03 Fix testing with randomized layouts enabled 2025-04-03 15:30:01 +00:00
Jacob Pratt
a99b5339f4
Rollup merge of #139141 - mejrs:on_unimpl, r=Noratrieb
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-30 17:59:29 -04:00
mejrs
b1bc7255bb Delete unreacheable #[rustc_on_unimplemented] 2025-03-30 15:25:27 +02:00