Commit Graph

9304 Commits

Author SHA1 Message Date
Samuel Tardieu
db7ac64997 Rollup merge of #144867 - scottmcm:more-as-array, r=chenyukang
Use `as_array` in PartialEq for arrays

Now that `as_array` exists we might as well use it here, since it's a bit more convenient than getting the correct type out of `try_into`.
2025-08-05 03:51:38 +02:00
Kivooeo
d2cfe486f0 remove feature gate 2025-08-04 16:41:10 +05:00
Stuart Cook
f6b4e45be7 Rollup merge of #144667 - scottmcm:alignment-is-usize, r=tgross35
`AlignmentEnum` should just be `repr(usize)` now

These used to use specific sizes because they were compiled on all widths.  But now that the types themselves are `#[cfg]`'d, we can save some conversions by having it always be `repr(usize)`.
2025-08-04 11:24:38 +10:00
Stuart Cook
625b180035 Rollup merge of #142205 - paolobarbolini:const_swap_with_slice-impl, r=Mark-Simulacrum
Mark `slice::swap_with_slice` unstably const

Tracking issue rust-lang/rust#142204
2025-08-04 11:24:35 +10:00
Connor Tsui
37922fc24c add poisoning documentation to LazyCell 2025-08-03 22:57:07 +02:00
Scott McMurray
eee28138b8 Use as_array in PartialEq for arrays 2025-08-03 12:47:11 -07:00
Samuel Tardieu
5e1ea9cda4 Rollup merge of #144797 - nilehmann:safety-comment-niche, r=Noratrieb
Update safety comment for new_unchecked in niche_types

Change the safety comment on `new_unchecked` to mention the valid range instead of 0. I noticed this while working on https://github.com/model-checking/verify-rust-std
2025-08-02 11:24:29 +02:00
Samuel Tardieu
d082ff4c04 Rollup merge of #144478 - joshtriplett:doc-code-formatting-prep, r=Amanieu
Improve formatting of doc code blocks

We don't currently apply automatic formatting to doc comment code blocks. As a
result, it has built up various idiosyncracies, which make such automatic
formatting difficult. Some of those idiosyncracies also make things harder for
human readers or other tools.

This PR makes a few improvements to doc code formatting, in the hopes of making
future automatic formatting easier, as well as in many cases providing net
readability improvements.

I would suggest reading each commit separately, as each commit contains one
class of changes.
2025-08-02 11:24:24 +02:00
Nico Lehmann
4c7c9de8ac Fix safety comment for new_unchecked in niche_types 2025-08-01 14:00:56 -07:00
Evgenii Zheltonozhskii
9377e0af52 Constify additional Result functions 2025-08-01 08:55:50 +03:00
Nurzhan Sakén
3ff3a1ee00 Stabilize strict_overflow_ops 2025-07-30 23:39:35 +04:00
Scott McMurray
4220587c22 AlignmentEnum should just be repr(usize) now
Since it's cfg'd instead of type-aliased
2025-07-30 00:09:01 -07:00
bors
c8bb4e8a12 Auto merge of #144658 - jhpratt:rollup-jdzhz27, r=jhpratt
Rollup of 8 pull requests

Successful merges:

 - rust-lang/rust#144034 (tests: Test line number in debuginfo for diverging function calls)
 - rust-lang/rust#144510 (Fix Ord, Eq and Hash implementation of panic::Location)
 - rust-lang/rust#144583 (Enable T-compiler backport nomination)
 - rust-lang/rust#144586 (Update wasi-sdk to 27.0 in CI)
 - rust-lang/rust#144605 (Resolve: cachify `ExternPreludeEntry.binding` through a `Cell`)
 - rust-lang/rust#144632 (Update some tests for LLVM 21)
 - rust-lang/rust#144639 (Update rustc-perf submodule)
 - rust-lang/rust#144640 (Add support for the m68k architecture in 'object_architecture')

r? `@ghost`
`@rustbot` modify labels: rollup
2025-07-29 23:53:04 +00:00
Jacob Pratt
48dfddd39e Rollup merge of #144510 - orlp:fix-location-ord, r=ibraheemdev
Fix Ord, Eq and Hash implementation of panic::Location

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

Now properly compares/hashes the filename rather than the pointer to the string.
2025-07-29 18:55:18 -04:00
bors
ba7e63b638 Auto merge of #144393 - heiher:str-contains-lsx, r=tgross35
LoongArch64 LSX fast-path for `str.contains(&str)`

Benchmark results with LLVM 21 on LA664:

```
OLD:
test bench_is_contained_in ... bench:          43.63 ns/iter (+/- 0.04)

NEW:
test bench_is_contained_in ... bench:          12.81 ns/iter (+/- 0.01)
```
2025-07-29 20:44:32 +00:00
Orson Peters
05da623016 Fix Ord, Eq and Hash implementation of panic::Location
Faster equality compare

Add tests

Add missing files for tests
2025-07-29 22:15:44 +02:00
Stuart Cook
7088bf5cc6 Rollup merge of #144634 - lucaswerkmeister:patch-1, r=lqd
Fix typo in `DropGuard` doc

Follows-up rust-lang/rust#144236 (I happened to see the typo yesterday but didn’t think it should delay the PR’s merge so I kept quiet, sorryyyyy).
2025-07-29 23:50:38 +10:00
WANG Rui
1ceacf55a0 LoongArch64 LSX fast-path for str.contains(&str)
Benchmark results with LLVM 21 on LA664:

```
OLD:
test bench_is_contained_in ... bench:          43.63 ns/iter (+/- 0.04)

NEW:
test bench_is_contained_in ... bench:          12.81 ns/iter (+/- 0.01)
```
2025-07-29 21:38:37 +08:00
Lucas Werkmeister
de02a32cf1 Fix typo in DropGuard doc 2025-07-29 13:24:41 +02:00
Stuart Cook
9cba49896c Rollup merge of #144167 - zachs18:rangebounds-not-unsized-reason, r=tgross35
Document why `Range*<&T> as RangeBounds<T>` impls are not `T: ?Sized`, and give an alternative.

`Range*<&T> as RangeBounds<T>` impls have been tried to be relaxed to `T: ?Sized` at least twice:

* https://github.com/rust-lang/rust/pull/61584
* https://github.com/rust-lang/rust/pull/64327

I also was just about to make another PR to do it again until I `./x.py test library/alloc` and rediscovered the type inference regression, then searched around and found the previous PRs. Hence this PR instead so hopefully that doesn't keep happening 😛.

These impls cannot be relaxed for two reasons:

1. Type inference regressions: See ``@SimonSapin's`` explanation from a previous PR: https://github.com/rust-lang/rust/pull/61584#issuecomment-499601046
2. It's a breaking change: `impl RangeBounds<MyUnsizedType> for std::ops::Range<&MyUnsizedType>` is allowed after the coherence rebalance ([playground link](https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=f704a6fe53bfc33e55b2fc246d895ec2)), and relaxing these impls would conflict with that downstream impl.

This PR adds doc-comments explaining that not having `T: ?Sized` is intentional[^1], and gives an explicit alternative: `(Bound<&T>, Bound<&T>)`.

Technically, the impls for the unstable new `std::range` types could be relaxed, as they are still unstable so the change would not be breaking, but having them be different in this regard seems worse (and the non-iterable `RangeTo/RangeToInclusive` range types are shared between the "new" and "old" so cannot be changed anyway), and then the type inference regression would pop up in whatever edition the new range types stabilize in.

The "see \<link\> for discussion of those issues" is intentionally left as a non-doc comment just for whoever may try to relax these impls again in the future, but if it is preferred to have the link in the docs I can add that.

Closes https://github.com/rust-lang/rust/issues/107196 (as wontfix)
CC https://github.com/rust-lang/rust/issues/64027

[^1]: "intentional" is maybe a bit of strong wording, should it instead say something like "was stabilized without it and it would be breaking to change it now"?
2025-07-29 20:19:48 +10:00
gewitternacht
c72bb70ffd clearer wording for unsafe code 2025-07-29 08:39:28 +02:00
Stuart Cook
95781464e0 Rollup merge of #144582 - usamoi:docs, r=RalfJung
fix `Atomic*::as_ptr` wording

r? `````@RalfJung`````

cc rust-lang/rust#144072
2025-07-29 16:16:45 +10:00
Stuart Cook
f9c114ef90 Rollup merge of #144575 - xonx4l:patch-6, r=scottmcm
fixed typo chunks->as_chunks

Fixes rust-lang/rust#144555

info-:
fix typo chunks -> as_chunks

This now take us to as_chunks page when clicking on as_chunks link and not to chunks .

Thanks .
2025-07-29 16:16:44 +10:00
Stuart Cook
e146ab281a Rollup merge of #144539 - RalfJung:const_with_exposed_provenance, r=oli-obk
constify with_exposed_provenance

We allow `int as ptr` in const, so it only makes sense to also allow this function. Otherwise, `const fn` can't be ported to use the more explicit exposed provenance APIs.

Note that as of today, `with_exposed_provenance` in const is equivalent to `without_provenance`. However, we probably don't want to promise that: if someone does `with_exposed_provenance(MMIO_ADDR)` in const and then uses that pointer at runtime, that is something we should ensure keeps working; if someone does the same with `without_provenance` then I would consider that UB.

Tracking: https://github.com/rust-lang/rust/issues/144538
Cc `````@rust-lang/wg-const-eval````` `````@rust-lang/opsem`````
2025-07-29 16:16:42 +10:00
Stuart Cook
ed7d6a941d Rollup merge of #144236 - yoshuawuyts:drop-guard, r=Mark-Simulacrum
Add `core::mem::DropGuard`

## 1.0 Summary

This PR introduces a new type `core::mem::DropGuard` which wraps a value and runs a closure when the value is dropped.

```rust
use core::mem::DropGuard;

// Create a new guard around a string that will
// print its value when dropped.
let s = String::from("Chashu likes tuna");
let mut s = DropGuard::new(s, |s| println!("{s}"));

// Modify the string contained in the guard.
s.push_str("!!!");

// The guard will be dropped here, printing:
// "Chashu likes tuna!!!"
```

## 2.0 Motivation

A number of programming languages include constructs like `try..finally` or `defer` to run code as the last piece of a particular sequence, regardless of whether an error occurred. This is typically used to clean up resources, like closing files, freeing memory, or unlocking resources. In Rust we use the `Drop` trait instead, allowing us to [never having to manually close sockets](https://blog.skylight.io/rust-means-never-having-to-close-a-socket/).

While `Drop` (and RAII in general) has been working incredibly well for Rust in general, sometimes it can be a little verbose to setup. In particular when upholding invariants are local to functions, having a quick inline way to setup an `impl Drop` can be incredibly convenient. We can see this in use in the Rust stdlib, which has a number of private `DropGuard` impls used internally:

- [library/alloc/src/vec/drain.rs](9982d6462b/library/alloc/src/vec/drain.rs (L177))
- [library/alloc/src/boxed/thin.rs](9982d6462b/library/alloc/src/boxed/thin.rs (L362))
- [library/alloc/src/slice.rs](9982d6462b/library/alloc/src/slice.rs (L413))
- [library/alloc/src/collections/linked_list.rs](9982d6462b/library/alloc/src/collections/linked_list.rs (L1135))
- [library/alloc/src/collections/binary_heap/mod.rs](9982d6462b/library/alloc/src/collections/binary_heap/mod.rs (L1816))
- [library/alloc/src/collections/btree/map.rs](9982d6462b/library/alloc/src/collections/btree/map.rs (L1715))
- [library/alloc/src/collections/vec_deque/drain.rs](9982d6462b/library/alloc/src/collections/vec_deque/drain.rs (L95))
- [library/alloc/src/vec/into_iter.rs](9982d6462b/library/alloc/src/vec/into_iter.rs (L488))
- [library/std/src/os/windows/process.rs](9982d6462b/library/std/src/os/windows/process.rs (L320))
- [tests/ui/process/win-proc-thread-attributes.rs](9982d6462b/tests/ui/process/win-proc-thread-attributes.rs (L17))

## 3.0 Design

This PR implements what can be considered about the simplest possible design:

1. A single type `DropGuard` which takes both a generic type `T` and a closure `F`.
2. `Deref` + `DerefMut` impls to make it easy to work with the `T` in the guard.
3. An `impl Drop` on the guard which calls the closure `F` on drop.
4. An inherent `fn into_inner` which takes the type `T` out of the guard without calling the closure `F`.

Notably this design does not allow divergent behavior based on the type of drop that has occurred. The [`scopeguard` crate](https://docs.rs/scopeguard/latest/scopeguard/index.html) includes additional `on_success` and `on_onwind` variants which can be used to branch on unwind behavior instead. However [in a lot of cases](https://github.com/rust-lang/rust/issues/143612#issuecomment-3053928328) this doesn’t seem necessary, and using the arm/disarm pattern seems to provide much the same functionality:

```rust
let guard = DropGuard::new((), |s| ...);  // 1. Arm the guard
other_function();                         // 2. Perform operations
guard.into_inner();                       // 3. Disarm the guard
```

`DropGuard` combined with this pattern seems like it should cover the vast majority of use cases for quick, inline destructors. It certainly seems like it should cover all existing uses in the stdlib, as well as all existing uses in crates like [hashbrown](https://github.com/search?q=repo%3Arust-lang%2Fhashbrown%20guard&type=code).

## 4.0 Acknowledgements

This implementation is based on the [mini-scopeguard crate](https://github.com/yoshuawuyts/mini-scopeguard) which in turn is based on the [scopeguard  crate](https://docs.rs/scopeguard). The implementations only differ superficially; because of the nature of the problem there is only really one obvious way to structure the solution. And the scopeguard crate got that right!

## 5.0 Conclusion

This PR adds a new type `core::mem::DropGuard` to the stdlib which adds a small convenience helper to create inline destructors with. This would bring the majority of the functionality of the `scopeguard` crate into the stdlib, which is the [49th most downloaded crate](https://crates.io/crates?sort=downloads) on crates.io (387 million downloads).

Given the actual implementation of `DropGuard` is only around 60 lines, it seems to hit that sweet spot of low-complexity / high-impact that makes for a particularly efficient stdlib addition. Which is why I’m putting this forward for consideration; thanks!
2025-07-29 16:16:41 +10:00
bors
cb6785f73d Auto merge of #143289 - scottmcm:remove-array-chunks, r=jhpratt
Remove `[T]::array_chunks(_mut)`

Since libs-api is proposing as much in https://github.com/rust-lang/rust/issues/74985#issuecomment-3024465102

Closes rust-lang/rust#74985
Closes rust-lang/rust#76354

try-job: dist-various-1
try-job: dist-various-2
2025-07-29 02:27:52 +00:00
usamoi
71920e265c fix Atomic*::as_ptr wording 2025-07-28 20:41:24 +08:00
Yosh
68f08c5dd9 Add core::mem::DropGuard
Fix CI for drop_guard

fix CI

fix all tidy lints

fix tidy link

add first batch of feedback from review

Add second batch of feedback from review

add third batch of feedback from review

fix failing test

Update library/core/src/mem/drop_guard.rs

Co-authored-by: Ruby Lazuli <general@patchmixolydic.com>

fix doctests

Implement changes from T-Libs-API review

And start tracking based on the tracking issue.

fix tidy lint
2025-07-28 12:12:40 +02:00
xonx
c340233769 fixed typo chunks->as_chunks 2025-07-28 15:05:36 +05:30
Matthias Krüger
e36b844b4e Rollup merge of #144472 - okaneco:char_bound, r=Mark-Simulacrum
str: Mark unstable `round_char_boundary` feature functions as const

Mark `floor_char_boundary`, `ceil_char_boundary` const
Simplify the implementations, reducing the number of arithmetic operations

It seems unnecessary to do the lower/upper bounds calculations and extra slicing when we can jump straight to inspecting the bytes, assuming the underlying data is valid UTF-8.

Tracking issue https://github.com/rust-lang/rust/issues/93743
2025-07-28 08:36:53 +02:00
Matthias Krüger
a9f58bff03 Rollup merge of #144072 - usamoi:docs, r=Mark-Simulacrum
update `Atomic*::from_ptr` and `Atomic*::as_ptr` docs

Since https://github.com/rust-lang/rust/pull/128778, it's allowed to perform atomic read and non-atomic read on the same atomic at the same time. Update the `Atomic*::from_ptr` and `Atomic*::as_ptr` documentation to remove expressions such as `not allowed to mix atomic and non-atomic accesses`.

see also [std::sync::atomic](https://doc.rust-lang.org/std/sync/atomic/index.html#memory-model-for-atomic-accesses)
2025-07-28 08:36:51 +02:00
Scott McMurray
173926da2b Remove [T]::array_chunks(_mut) 2025-07-27 23:03:07 -07:00
Ralf Jung
d12ecde365 constify with_exposed_provenance 2025-07-27 17:00:31 +02:00
Scott McMurray
51b0416c19 Use cast_array in core 2025-07-26 17:06:02 -07:00
Scott McMurray
7ead5764a8 Implement ptr_cast_array 2025-07-26 13:22:24 -07:00
Matthias Krüger
c9541a2bf8 Rollup merge of #144331 - jplatte:matches-allow-non_exhaustive_omitted_patterns, r=Nadrieril
Disable non_exhaustive_omitted_patterns within matches! macro

Closes rust-lang/rust#117304.

I believe I can skip all of the bootstrap stuff mentioned in https://github.com/rust-lang/rust/issues/117304#issuecomment-1784414453 due to https://blog.rust-lang.org/inside-rust/2025/05/29/redesigning-the-initial-bootstrap-sequence/, right?

cc `@Jules-Bertholet`
2025-07-26 15:27:58 +02:00
Josh Triplett
56c5b1df94 Add parentheses around expression arguments to ..
This makes it easier for humans to parse, and improves the result of
potential future automatic formatting.
2025-07-25 22:02:09 -07:00
Josh Triplett
b6c54a97b3 Avoid placing // FIXME comments inside doc code blocks
This leads tools like rustfmt to get confused, because the doc code
block effectively spans two doc comments. As a result, the tools think
the first code block is unclosed, and the subsequent terminator opens a
new block.

Move the FIXME comments outside the doc code blocks, instead.
2025-07-25 22:02:09 -07:00
Josh Triplett
715088094c Improve and regularize comment placement in doc code
Because doc code does not get automatically formatted, some doc code has
creative placements of comments that automatic formatting can't handle.
Reformat those comments to make the resulting code support standard Rust
formatting without breaking; this is generally an improvement to
readability as well.

Some comments are not indented to the prevailing indent, and are instead
aligned under some bit of code. Indent them to the prevailing indent,
and put spaces *inside* the comments to align them with code.

Some comments span several lines of code (which aren't the line the
comment is about) and expect alignment. Reformat them into one comment
not broken up by unrelated intervening code.

Some comments are placed on the same line as an opening brace, placing
them effectively inside the subsequent block, such that formatting would
typically format them like a line of that block. Move those comments to
attach them to what they apply to.

Some comments are placed on the same line as a one-line braced block,
effectively attaching them to the closing brace, even though they're
about the code inside the block. Reformat to make sure the comment will
stay on the same line as the code it's commenting.
2025-07-25 22:02:09 -07:00
okaneco
7f7d343400 str: Mark unstable round_char_boundary feature functions as const
Mark `floor_char_boundary`, `ceil_char_boundary` const
Simplify the implementations, reducing the number of arithmetic operations
2025-07-25 22:09:42 -04:00
Matthias Krüger
a2681f943c Rollup merge of #144314 - kornelski:pivot-safely, r=jhpratt
Hint that choose_pivot returns index in bounds

Instead of using `unsafe` in multiple places, one `hint::assert_unchecked` allows use of safe code instead.

Part of #rust-lang/rust#144326
2025-07-25 11:16:37 +02:00
Matthias Krüger
e3b07352a6 Rollup merge of #143970 - SunkenPotato:update_mem_copy_docs, r=scottmcm
Update core::mem::copy documentation

Update the documentation of `core::mem::copy` to include a `const` on the definition of the function.
2025-07-25 11:16:34 +02:00
Matthias Krüger
dd159ee24e Rollup merge of #143424 - hkBst:auto-deref, r=jhpratt
clippy fix: rely on autoderef

Changes instances of `&**self` to `self`.
2025-07-25 11:16:34 +02:00
León Orell Valerian Liehr
488fc49e05 Rollup merge of #144278 - Qelxiros:rev-into-inner, r=tgross35
add Rev::into_inner

Tracking issue: rust-lang/rust#144277
2025-07-24 15:08:25 +02:00
Jeremy Smart
6f1b56c499 add Rev::into_inner 2025-07-23 22:03:03 -04:00
Zachary S
477814a3e4 Document (internally) that Range*<&T> as RangeBounds<T> impls are intentionally not T: ?Sized, and document (publically) an alternative. 2025-07-23 15:39:03 -05:00
Jonas Platte
8a1b20ed60 Disable non_exhaustive_omitted_patterns within matches! macro 2025-07-23 14:26:06 +02:00
Kornel
01fdafc9aa Hint that choose_pivot returns index in bounds 2025-07-23 12:01:44 +01:00
Ralf Jung
de1b999ff6 atomicrmw on pointers: move integer-pointer cast hacks into backend 2025-07-23 08:32:55 +02:00
gewitternacht
623317eb5e add links to collections 2025-07-23 01:44:18 +02:00