Commit Graph

8209 Commits

Author SHA1 Message Date
Guillaume Gomez
3fee6cccde Rollup merge of #140418 - tgross35:std-c-size_t, r=workingjubilee
Reexport types from `c_size_t` in `std`

These are unstably available in `core` and should be in `std` too, but are not currently reexported. Resolve this here.

Tracking issue: https://github.com/rust-lang/rust/issues/88345
2025-06-06 23:53:15 +02:00
Ralf Jung
910a59dfae unsafe keyword docs: emphasize that an unsafe fn in a trait does not get to choose its safety contract 2025-06-06 22:34:10 +02:00
schvv31n
eba3a61067 Stabilised os_string_pathbuf_leak 2025-06-06 12:06:42 -07:00
Ayush Singh
669564d028 std: sys: random: uefi: Provide rdrand based fallback
Some UEFI systems based on American Megatrends Inc. v3.3 do not provide
RNG support [1]. So fallback to rdrand in such cases.

[1]: https://github.com/rust-lang/rust/issues/138252#issuecomment-2891270323

Signed-off-by: Ayush Singh <ayush@beagleboard.org>
2025-06-06 10:14:08 +05:30
WANG Rui
38d69c3f57 Add new Tier-3 targets: loongarch32-unknown-none*
MCP: https://github.com/rust-lang/compiler-team/issues/865
2025-06-06 08:19:38 +08:00
Matthias Krüger
9c895558ec Rollup merge of #142091 - thaliaarchi:aix-getenv, r=workingjubilee
Fix AIX build

Fix rust-lang/rust#141543.

`getenv` was moved out of this file to `sys::env::getenv` in rust-lang/rust#140143. Replace its usage with `std::env::var_os`, the publicly exposed version. This matches the other usages of the same function in this file.
2025-06-06 00:58:48 +02:00
Thalia Archibald
46ce08ef06 Fix AIX build 2025-06-05 10:29:50 -07:00
Tobias Bucher
4b1e28b21f Clarify description of Seek::stream_len
It can only describe the inner workings of the default implementation,
other implementations might not be implemented using seeks at all.
2025-06-05 16:27:28 +02:00
Tobias Bucher
fde8a8d518 Optimize Seek::stream_len impl for File
It uses the file metadata on Unix with a fallback for files incorrectly
reported as zero-sized. It uses `GetFileSizeEx` on Windows.

This reduces the number of syscalls needed for determining the file size
of an open file from 3 to 1.
2025-06-05 16:27:27 +02:00
LorrensP-2158466
00452bd783 change tests to use fixed constants to let them pass with miri 2025-06-05 16:22:13 +02:00
Matthias Krüger
88620b400e Rollup merge of #141467 - cyrgani:const-empty-stringlikes, r=Amanieu
make `OsString::new` and `PathBuf::new` unstably const

Since #129041, `String::into_bytes` is `const`, which allows making `OsString::new` and `PathBuf::new` unstably const now.
Not sure what the exact process for this is; does it need an ACP?
2025-06-04 07:54:33 +02:00
Matthias Krüger
c8a0f69d0e Rollup merge of #141925 - cuviper:vestigial-bootstrap, r=workingjubilee
Remove bootstrap cfgs from library/

These `cfg(bootstrap)` are always false now that rust-lang/rust#119899 has landed, and likewise `cfg(not(bootstrap))` is always true. Therefore, we don't need to wait for the usual stage0 bump to clean these up.
2025-06-03 21:53:37 +02:00
Matthias Krüger
d096ebf8d9 Rollup merge of #141455 - joboet:tls_exhaustion_abort, r=tgross35
std: abort the process on failure to allocate a TLS key

The panic machinery uses TLS, so panicking if no TLS keys are left can lead to infinite recursion (see https://github.com/rust-lang/rust/issues/140798#issuecomment-2872307377). Rather than having separate logic for the panic count and the thread name, just always abort the process if a TLS key allocation fails. This also has the benefit of aligning the key-based TLS implementation with the documentation, which does not mention that a panic could also occur because of resource exhaustion.
2025-06-03 21:53:36 +02:00
LorrensP-2158466
5bafe9d8fc Enable Float non-determinism in miri. Update and add tests and change
change tests in std, core and coretests.
2025-06-03 19:46:13 +02:00
Matthias Krüger
55f7571a7e Rollup merge of #140715 - lukaslueg:oncecellsyncdocs, r=tgross35
Clarify &mut-methods' docs on sync::OnceLock

Three small changes to the docs of `sync::OnceLock`:

* The docs for `OnceLock::take()` used to [say](https://doc.rust-lang.org/std/sync/struct.OnceLock.html#method.take) "**Safety** is guaranteed by requiring a mutable reference." (emphasis mine). While technically correct, imho its not necessary to even mention safety - as opposed to unsafety - here: Safety never comes up wrt `OnceLock`, as there is (currently) no way to interact with a `OnceLock` in an unsafe way; there are no unsafe methods on `OnceLock`, so there is "safety" guarantee required anywhere. What we simply meant to say is "**Synchronization** is guaranteed...".
* I've add that phrase to the other methods of `OnceLock` which take a `&mut self`, to highlight the fact that having a `&mut OnceLock` guarantees that synchronization with other threads is not required. This is the same as with [`Mutex::get_mut()`](https://doc.rust-lang.org/std/sync/struct.Mutex.html#method.get_mut), [`Cell::get_mut()`](https://doc.rust-lang.org/std/cell/struct.Cell.html#method.get_mut), and others.
* In that spirit, the half-sentence "or being initialized" was removed from `get_mut()`, as there is no way that the `OnceLock` is being initialized while we are holding `&mut` to it. Probably a copy&paste from `.get()`
2025-06-03 07:03:42 +02:00
Josh Stone
c87b072952 Remove more library bootstrap 2025-06-02 14:46:19 -07:00
Josh Stone
19e02c8211 Remove bootstrap cfgs from library/ 2025-06-02 10:19:58 -07:00
xizheyin
e95751a9ff Explain TOCTOU on the top of std::fs, and ref it in functions
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>

Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
2025-06-02 17:45:00 +08:00
Jacob Pratt
7f306d5729 Rollup merge of #141832 - workingjubilee:explain-what-toctou-races-are, r=thomcc,ChrisDenton
library: explain TOCTOU races in `fs::remove_dir_all`

In the previous description it said there was a TOCTOU race but did not explain exactly what the problem was. I sat down with the CVE, reviewed its text, and created this explanation. This context should hopefully help people understand the actual risk as-such.

Incidentally, it also fixes the capitalization on the name of Redox OS.

Original CVE and advisory:
- CVE: https://www.cve.org/CVERecord?id=CVE-2022-21658
- security advisory: https://groups.google.com/g/rustlang-security-announcements/c/R1fZFDhnJVQ?pli=1
- github cross-post: https://github.com/rust-lang/rust/security/advisories/GHSA-r9cc-f5pr-p3j2
2025-06-01 00:35:54 +02:00
Jacob Pratt
ac49339e03 Rollup merge of #141521 - ruancomelli:const-float-rounding, r=RalfJung
Add `const` support for float rounding methods

# Add `const` support for float rounding methods

This PR makes the following float rounding methods `const`:

- `f64::{floor, ceil, trunc, round, round_ties_even}`
- and the corresponding methods for `f16`, `f32` and `f128`

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

## Procedure

I followed c09ed3e767 as closely as I could in making float methods `const`, and also received great guidance from https://internals.rust-lang.org/t/const-rounding-methods-in-float-types/22957/3?u=ruancomelli.

## Note

This is my first code contribution to the Rust project, so please let me know if I missed anything - I'd be more than happy to revise and learn more. Thank you for taking the time to review it!
2025-06-01 00:35:53 +02:00
Chris Denton
921b6c02aa If HOME is empty, use the fallback instead 2025-05-31 21:55:26 +00:00
Jubilee Young
7f7c415d03 library: explain TOCTOU races in fs::remove_dir_all
In the previous description it said there was a TOCTOU race but did not
explain exactly what the problem was. I sat down with the CVE, reviewed
its text, and created this explanation. This context should hopefully
help people understand the actual risk as-such.

Incidentally, it also fixes the capitalization on the name of Redox OS.
2025-05-31 14:05:29 -07:00
Ruan Comelli
f8e97badb2 Add const support for float rounding methods
Add const support for the float rounding methods floor, ceil, trunc,
fract, round and round_ties_even.
This works by moving the calculation logic from

     src/tools/miri/src/intrinsics/mod.rs

into

     compiler/rustc_const_eval/src/interpret/intrinsics.rs.

All relevant method definitions were adjusted to include the `const`
keyword for all supported float types: f16, f32, f64 and f128.

The constness is hidden behind the feature gate

     feature(const_float_round_methods)

which is tracked in

     https://github.com/rust-lang/rust/issues/141555

This commit is a squash of the following commits:
- test: add tests that we expect to pass when float rounding becomes const
- feat: make float rounding methods `const`
- fix: replace `rustc_allow_const_fn_unstable(core_intrinsics)` attribute with `#[rustc_const_unstable(feature = "f128", issue = "116909")]` in `library/core/src/num/f128.rs`
- revert: undo update to `library/stdarch`
- refactor: replace multiple `float_<mode>_intrinsic` rounding methods with a single, parametrized one
- fix: add `#[cfg(not(bootstrap))]` to new const method tests
- test: add extra sign tests to check `+0.0` and `-0.0`
- revert: undo accidental changes to `round` docs
- fix: gate `const` float round method behind `const_float_round_methods`
- fix: remove unnecessary `#![feature(const_float_methods)]`
- fix: remove unnecessary `#![feature(const_float_methods)]` [2]
- revert: undo changes to `tests/ui/consts/const-eval/float_methods.rs`
- fix: adjust after rebase
- test: fix float tests
- test: add tests for `fract`
- chore: add commented-out `const_float_round_methods` feature gates to `f16` and `f128`
- fix: adjust NaN when rounding floats
- chore: add FIXME comment for de-duplicating float tests
- test: remove unnecessary test file `tests/ui/consts/const-eval/float_methods.rs`
- test: fix tests after upstream simplification of how float tests are run
2025-05-31 15:26:57 -03:00
bors
4d08223c05 Auto merge of #141824 - matthiaskrgr:rollup-7nffwd0, r=matthiaskrgr
Rollup of 8 pull requests

Successful merges:

 - rust-lang/rust#140787 (Note expr being cast when encounter NonScalar cast error)
 - rust-lang/rust#141112 (std: note that `std::str::from_utf8*` functions are aliases to `<str>::from_utf8*` methods)
 - rust-lang/rust#141646 (Document what `distcheck` is intended to exercise)
 - rust-lang/rust#141740 (Hir item kind field order)
 - rust-lang/rust#141793 (`tests/ui`: A New Order [1/N])
 - rust-lang/rust#141805 (Update `compiler-builtins` to 0.1.160)
 - rust-lang/rust#141815 (Enable non-leaf Frame Pointers for mingw-w64 Arm64 Windows)
 - rust-lang/rust#141819 (Fixes for building windows-gnullvm hosts)

r? `@ghost`
`@rustbot` modify labels: rollup
2025-05-31 18:16:35 +00: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
bors
852f15c0f1 Auto merge of #141685 - orlp:inplace-tls-drop, r=joboet
Do not move thread-locals before dropping

Fixes rust-lang/rust#140816. I also (potentially) improved the speed of `get_or_init` a bit by having an explicit hot/cold path.

We still move the value before dropping in the event of a recursive initialization (leading to double-initialization with one value being silently dropped). This is the old behavior, but changing this to panic instead would involve changing tests and also the other OS-specific `thread_local/os.rs` implementation, which is more than I'd like in this PR.
2025-05-31 14:56:33 +00: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
Orson Peters
b374adc9db Address review comments. 2025-05-30 12:14:27 +02:00
Trevor Gross
a5f3b1e5df Make std/src/num mirror core/src/num
The float modules in `std` are currently top-level but for `core`, they
are nested within the `num` directory and referenced by `#[path = ...]`.
For consistency, adjust `std` to use the same structure as `core`.

Also change the `f16` and `f128` gates from outer attributes to inner
attributes like `core` has.
2025-05-29 13:10:32 +00:00
Jacob Pratt
e7ef07a523 Rollup merge of #141690 - Patrick-6:intercept-mutex, r=m-ou-se
Add `rustc_diagnostic_item` to `sys::Mutex` methods

For an ongoing project for adding a concurrency model checker to Miri we need to be able to intercept locking/unlocking operations on standard library mutexes.

This PR adds diagnostic items to the relevant calls `lock`, `try_lock` and `unlock` for the `sys::Mutex` implementation on the targets we care about.
This PR also makes the internals of `pthread::Mutex` less public, to reduce the chance of anyone locking/unlocking a mutex without going through the intercepted methods.

r? ``@RalfJung``
2025-05-29 04:49:47 +02:00
Jacob Pratt
ffdd3b16dc Rollup merge of #141477 - tshepang:patch-1, r=ChrisDenton
Path::with_extension: show that it adds an extension where one did no…

…t exist

I think the times I encountered this, I had to check first if files without extensions were added, since all examples only had files with existing extensions.

Also, this replaced example already has a similar example below.
2025-05-29 04:49:41 +02:00
Jacob Pratt
d9ed86729b Rollup merge of #141104 - PaulDance:fix-win7-test_eq_windows_file_type, r=ChrisDenton
Test(fs): Fix `test_eq_windows_file_type` for Windows 7

Would otherwise fail on:

```
thread 'fs::tests::test_eq_windows_file_type' panicked at library/std/src/test_helpers.rs:53:20:
called `Result::unwrap()` on an `Err` value: Os { code: 5, kind: PermissionDenied, message: "Access is denied." }
```

This came from the read-only attribute set on the test file. In order to fix this, instead of simply disabling the test, the attribute is reset before the test's end so it may still run successfully.

`@rustbot` label T-libs A-filesystem A-testsuite O-windows-7 O-windows-msvc
2025-05-29 04:49:40 +02:00
Lukas Lueg
200d742984 Clarify &mut-methods' docs on sync::OnceLock 2025-05-28 18:31:28 +02:00
Orson Peters
9ffbc62cb6 When replacing an old value we may not drop it in place 2025-05-28 17:53:14 +02:00
Orson Peters
8785f7b122 Add same unsafe bound on get_or_init_slow 2025-05-28 17:10:50 +02:00
Patrick-6
8237107d88 Add comments to diagnostic items 2025-05-28 17:05:45 +02:00
Orson Peters
13bce27e37 Do not panic, maintain old behavior 2025-05-28 16:56:26 +02:00
Trevor Gross
7f5f29b663 Rollup merge of #140697 - Sa4dUs:split-autodiff, r=ZuseZ4
Split `autodiff` into `autodiff_forward` and `autodiff_reverse`

This PR splits `#[autodiff]` macro so `#[autodiff(df, Reverse, args)]` would become `#[autodiff_reverse(df, args)]` and `#[autodiff(df, Forward, args)]` would become `#[autodiff_forwad(df, args)]`.
2025-05-28 10:28:08 -04:00
Trevor Gross
5f17779a03 Rollup merge of #140369 - jplatte:mutex-rwlock-data-ptr, r=Amanieu
Add data_ptr method to Mutex and RwLock

Implementation of https://github.com/rust-lang/rust/issues/140368 / https://github.com/rust-lang/libs-team/issues/531.

I tried to write a useful safety section about when it is safe to read or write through the returned pointers, but couldn't come up with something nice. Hoping this PR is still useful without that. I'm happy to add any doc strings other people come up with if needed before merge, of course.

Unresolved questions:

- Return a `LockResult` or not?
- Return `*mut T` like existing APIs (`Cell::as_ptr` / `MaybeUninit::as[_mut]_ptr` / `Vec::as_ptr` / ...) or be more precise and return `NonNull<T>`?
2025-05-28 10:28:07 -04:00
Patrick-6
149b5b2567 Make pthread Mutex internals less public 2025-05-28 15:13:38 +02:00
Patrick-6
2e99a880e2 Add diagnostic items to sys::Mutex 2025-05-28 15:12:56 +02:00
Orson Peters
f70cf59fc1 Improve safety comment, double-drop is not relevant here 2025-05-28 14:51:52 +02:00
Orson Peters
b0f6b69b81 Do not move thread-locals before dropping 2025-05-28 14:39:51 +02:00
Paul Mabileau
0dd5722d67 Test(fs): Fix test_eq_windows_file_type for Windows 7
Would otherwise fail on:

```
thread 'fs::tests::test_eq_windows_file_type' panicked at library/std/src/test_helpers.rs:53:20:
called `Result::unwrap()` on an `Err` value: Os { code: 5, kind: PermissionDenied, message: "Access is denied." }
```

This came from the read-only attribute set on the test file. In order to
fix this, instead of simply disabling the test, the attribute is reset
before the test's end so it may still run successfully.

Signed-off-by: Paul Mabileau <paul.mabileau@harfanglab.fr>
2025-05-28 12:06:25 +02:00
Matthias Krüger
88b12f3649 Rollup merge of #141312 - cberner:filelock_from, r=joshtriplett
Add From<TryLockError> for io::Error

Adds a `From` impl to make error propagation easier, as discussed in the tracking issue

`TryLockError` is unstable under the "file_lock" feature. The related tracking issue is https://github.com/rust-lang/rust/issues/130994

This PR also cleanups the Windows implementation of `try_lock()` and `try_lock_shared()` as [discussed here](https://github.com/rust-lang/rust/pull/140718#discussion_r2076678485)
2025-05-27 20:57:53 +02:00
Jacob Pratt
a49ae1c40f Rollup merge of #141472 - fluiderson:dev, r=workingjubilee
Attempt to improve the `std::fs::create_dir_all` docs related to atomicity

The original paragraph was added in rust-lang/rust#124520. It doesn't match the actual code logic. It says "function returns an error" if "the parent components" _(which also implies directories)_ "have been created already". The code is as follows:

e88e854634/library/std/src/fs.rs (L3146)
e88e854634/library/std/src/fs.rs (L3160)

These lines suppress all errors if any path component is a directory. I've updated the paragraph to mirror this.
2025-05-26 03:38:18 +02:00
Jacob Pratt
9aae60befc Rollup merge of #140936 - teor2345:wtf-surrogate-docs, r=workingjubilee
Clarify WTF-8 safety docs

This PR is a follow-up to PR #140159, which clarifies ~~two things~~:
- the WTF-8 safety comment [was confusing](https://github.com/rust-lang/rust/pull/140159#discussion_r2082766965), either surrogate condition is actually sufficient for safety, both are not required
- ~~the private `os_str::Slice` type name is easily confused with `std::slice`~~

~~Happy to bikeshed the `OsSlice` name, other alternatives are `OsStrSlice` and `StrSlice`. Now it's got a distinct name from `std::slice`, it's easy to search and replace.~~

cc ``@thaliaarchi`` ``@workingjubilee``
2025-05-26 03:38:17 +02:00
Jacob Pratt
6341f4ef23 Rollup merge of #134696 - ChrisDenton:normalize-lexically, r=workingjubilee
Implement `normalize_lexically`

Implements #134694

This is, I think, the most straightforward implementation I could do, which will hopefully more easily allow experimentation if we decide to change the design here.
2025-05-26 03:38:15 +02:00
Chris Denton
c299e297ee Implement normalize lexically 2025-05-25 08:11:41 +00: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