Commit Graph

26969 Commits

Author SHA1 Message Date
Matthias Krüger
f6cc562026 Rollup merge of #151403 - joboet:clock_nanosleep_time64, r=Mark-Simulacrum
std: use 64-bit `clock_nanosleep` on GNU/Linux if available

glibc 2.31 added support for both 64-bit `clock_gettime` and 64-bit `clock_nanosleep`. Thus, if [`__clock_nanosleep_time64`](https://sourceware.org/git/?p=glibc.git;a=blob;f=include/time.h;h=22b29ca583549488a0e5395cb820f55ec6e38e5f;hb=e14a91e59d35bf2fa649a9726ccce838b8c6e4b7#l322) and the underlying syscall are available, use them for implementing `sleep_until` to avoid having to fall back to `nanosleep` for long-duration sleeps.
2026-01-24 21:04:17 +01:00
Matthias Krüger
275ffd55b5 Rollup merge of #151538 - joboet:sleep_more, r=Mark-Simulacrum
std: `sleep_until` on Motor and VEX

This PR:
* Forwards the public `sleep_until` to the private `sleep_until` on Motor OS
* Adds a `sleep_until` implementation on VEX that yields until the deadline has passed

CC @lasiotus
CC @lewisfm @tropicaaal @Gavin-Niederman @max-niederman
2026-01-24 21:04:16 +01:00
Matthias Krüger
3a69035338 Rollup merge of #151346 - folkertdev:simd-splat, r=workingjubilee
add `simd_splat` intrinsic

Add `simd_splat` which lowers to the LLVM canonical splat sequence.

```llvm
insertelement <N x elem> poison, elem %x, i32 0
shufflevector <N x elem> v0, <N x elem> poison, <N x i32> zeroinitializer
```

Right now we try to fake it using one of

```rust
fn splat(x: u32) -> u32x8 {
    u32x8::from_array([x; 8])
}
```

or (in `stdarch`)

```rust
fn splat(value: $elem_type) -> $name {
    #[derive(Copy, Clone)]
    #[repr(simd)]
    struct JustOne([$elem_type; 1]);
    let one = JustOne([value]);
    // SAFETY: 0 is always in-bounds because we're shuffling
    // a simd type with exactly one element.
    unsafe { simd_shuffle!(one, one, [0; $len]) }
}
```

Both of these can confuse the LLVM optimizer, producing sub-par code. Some examples:

- https://github.com/rust-lang/rust/issues/60637
- https://github.com/rust-lang/rust/issues/137407
- https://github.com/rust-lang/rust/issues/122623
- https://github.com/rust-lang/rust/issues/97804

---

As far as I can tell there is no way to provide a fallback implementation for this intrinsic, because there is no `const` way of evaluating the number of elements (there might be issues beyond that, too). So, I added implementations for all 4 backends.

Both GCC and const-eval appear to have some issues with simd vectors containing pointers. I have a workaround for GCC, but haven't yet been able to make const-eval work. See the comments below.

Currently this just adds the intrinsic, it does not actually use it anywhere yet.
2026-01-24 21:04:15 +01:00
Matthias Krüger
fd5f48f559 Rollup merge of #150905 - PaulDance:patches/unsupport-win7-hostname, r=Mark-Simulacrum
Fix(lib/win/net): Remove hostname support under Win7

Fixes rust-lang/rust#150896. `GetHostNameW` is not available under Windows 7, leading to dynamic linking failures upon program executions. For now, as it is still unstable, this therefore appropriately cfg-gates the feature in order to mark the Win7 as unsupported with regards to this particular feature. Porting the functionality for Windows 7 would require changing the underlying system call and so more work for the immediate need.

@rustbot label C-bug O-windows-7 T-libs A-io
2026-01-24 21:04:14 +01:00
Jonathan 'theJPster' Pallant
7cc102a4ee Revised yield hints
Turns out v7 targets always have v6t2 set, so that line was redundant. Also add a link to the Arm Armv7 A.R.M.
2026-01-24 17:29:25 +00:00
Jonathan 'theJPster' Pallant
96897f016e Add ARMv6 bare-metal targets
Three targets, covering A32 and T32 instructions, and soft-float and
hard-float ABIs. Hard-float not available in Thumb mode. Atomics
in Thumb mode require __sync* functions from compiler-builtins.
2026-01-24 17:29:25 +00:00
Jonathan Brouwer
85430dfc90 Rollup merge of #151555 - nicholasbishop:bishop-fix-just-uefi-test, r=Ayush1325,tgross35
Fix compilation of std/src/sys/pal/uefi/tests.rs

Dropped the `align` test since the `POOL_ALIGNMENT` and `align_size` items it uses do not exist.

The other changes are straightforward fixes for places where the test code drifted from the current API, since the tests are not yet built in CI for the UEFI target.

CC @Ayush1325
2026-01-24 08:18:08 +01:00
Jonathan Brouwer
b6b11f473c Rollup merge of #151551 - ehuss:test-build-script, r=jhpratt
Don't use default build-script fingerprinting in `test`

This changes the `test` build script so that it does not use the default fingerprinting mechanism in cargo which causes a full scan of the package every time it runs. This build script does not depend on any of the files in the package.

This is the recommended approach for writing build scripts.
2026-01-24 08:18:07 +01:00
Jonathan Brouwer
474c9fe9a4 Rollup merge of #151489 - bend-n:constify-all-boolean-methods-under-feature-gate-const-bool, r=jhpratt
constify boolean methods

```rs
// core::bool

impl bool {
    pub const fn then_some<T: [const] Destruct>(self, t: T) -> Option<T>;
    pub const fn then<T, F: [const] FnOnce() -> T + [const] Destruct>(self, f: F) -> Option<T>;
    pub const fn ok_or<E: [const] Destruct>(self, err: E) -> Result<(), E>;
    pub const fn ok_or_else<E, F: [const] FnOnce() -> E + [const] Destruct>;
}
```

will make tracking issue if pr liked
2026-01-24 08:18:07 +01:00
Jonathan Brouwer
13f0399a57 Rollup merge of #151259 - bonega:fix-is-ascii-avx512, r=folkertdev
Fix is_ascii performance regression on AVX-512 CPUs when compiling with -C target-cpu=native

## Summary

This PR fixes a severe performance regression in `slice::is_ascii` on AVX-512 CPUs when compiling with `-C target-cpu=native`.

On affected systems, the current implementation achieves only ~3 GB/s for large inputs, compared to ~60–70 GB/s previously (≈20–24× regression). This PR restores the original performance characteristics.

This change is intended as a **temporary workaround** for upstream LLVM poor codegen. Once the underlying LLVM issue is fixed and Rust is able to consume that fix, this workaround should be reverted.

  ## Problem

  When `is_ascii` is compiled with AVX-512 enabled, LLVM's auto-vectorization generates ~31 `kshiftrd` instructions to extract mask bits one-by-one, instead of using the efficient `pmovmskb`
  instruction. This causes a **~22x performance regression**.

  Because `is_ascii` is marked `#[inline]`, it gets inlined and recompiled with the user's target settings, affecting anyone using `-C target-cpu=native` on AVX-512 CPUs.

## Root cause (upstream)

The underlying issue appears to be an LLVM vectorizer/backend bug affecting certain AVX-512 patterns.

An upstream issue has been filed by @folkertdev  to track the root cause: llvm/llvm-project#176906

Until this is resolved in LLVM and picked up by rustc, this PR avoids triggering the problematic codegen pattern.

  ## Solution

  Replace the counting loop with explicit SSE2 intrinsics (`_mm_movemask_epi8`) that force `pmovmskb` codegen regardless of CPU features.

  ## Godbolt Links (Rust 1.92)

  | Pattern | Target | Link | Result |
  |---------|--------|------|--------|
  | Counting loop (old) | Default SSE2 | https://godbolt.org/z/sE86xz4fY | `pmovmskb` |
  | Counting loop (old) | AVX-512 (znver4) | https://godbolt.org/z/b3jvMhGd3 | 31x `kshiftrd` (broken) |
  | SSE2 intrinsics (fix) | Default SSE2 | https://godbolt.org/z/hMeGfeaPv | `pmovmskb` |
  | SSE2 intrinsics (fix) | AVX-512 (znver4) | https://godbolt.org/z/Tdvdqjohn | `vpmovmskb` (fixed) |

  ## Benchmark Results

  **CPU:** AMD Ryzen 5 7500F (Zen 4 with AVX-512)

  ### Default Target (SSE2) — Mixed

  | Size | Before | After | Change |
  |------|--------|-------|--------|
  | 4 B | 1.8 GB/s | 2.0 GB/s | **+11%** |
  | 8 B | 3.2 GB/s | 5.8 GB/s | **+81%** |
  | 16 B | 5.3 GB/s | 8.5 GB/s | **+60%** |
  | 32 B | 17.7 GB/s | 15.8 GB/s | -11% |
  | 64 B | 28.6 GB/s | 25.1 GB/s | -12% |
  | 256 B | 51.5 GB/s | 48.6 GB/s | ~same |
  | 1 KB | 64.9 GB/s | 60.7 GB/s | ~same |
  | 4 KB+ | ~68-70 GB/s | ~68-72 GB/s | ~same |

  ### Native Target (AVX-512) — Up to 24x Faster

  | Size | Before | After | Speedup |
  |------|--------|-------|---------|
  | 4 B | 1.2 GB/s | 2.0 GB/s | **1.7x** |
  | 8 B | 1.6 GB/s | 5.0 GB/s | **3.3x** |
  | 16 B | ~7 GB/s | ~7 GB/s | ~same |
  | 32 B | 2.9 GB/s | 14.2 GB/s | **4.9x** |
  | 64 B | 2.9 GB/s | 23.2 GB/s | **8x** |
  | 256 B | 2.9 GB/s | 47.2 GB/s | **16x** |
  | 1 KB | 2.8 GB/s | 60.0 GB/s | **21x** |
  | 4 KB+ | 2.9 GB/s | ~68-70 GB/s | **23-24x** |

  ### Summary

  - **SSE2 (default):** Small inputs (4-16 B) 11-81% faster; 32-64 B ~11% slower; large inputs unchanged
  - **AVX-512 (native):** 21-24x faster for inputs ≥1 KB, peak ~70 GB/s (was ~3 GB/s)

  Note: this is the pure ascii path, but the story is similar for the others.
  See linked bench project.

  ## Test Plan

  - [x] Assembly test (`slice-is-ascii-avx512.rs`) verifies no `kshiftrd` with AVX-512
  - [x] Existing codegen test updated to `loongarch64`-only (auto-vectorization still used there)
  - [x] Fuzz testing confirms old/new implementations produce identical results (~53M iterations)
  - [x] Benchmarks confirm performance improvement
  - [x] Tidy checks pass

  ## Reproduction / Test Projects

  Standalone validation tools: https://github.com/bonega/is-ascii-fix-validation

  - `bench/` - Criterion benchmarks for SSE2 vs AVX-512 comparison
  - `fuzz/` - Compares old/new implementations with libfuzzer

  ## Related Issues
  - issue opened by @folkertdev llvm/llvm-project#176906
  - Regression introduced in https://github.com/rust-lang/rust/pull/130733
2026-01-24 08:18:05 +01:00
joboet
dee0e39471 std: implement sleep_until on VEX 2026-01-23 23:15:12 +01:00
Nicholas Bishop
8995ff5ba8 Fix compilation of std/src/sys/pal/uefi/tests.rs
Dropped the `align` test since the `POOL_ALIGNMENT` and `align_size`
items it uses do not exist.

The other changes are straightforward fixes for places where the test
code drifted from the current API, since the tests are not yet built in
CI for the UEFI target.
2026-01-23 17:07:06 -05:00
Eric Huss
e38b55d0b7 Don't use default build-script fingerprinting in test
This changes the `test` build script so that it does not use the default
fingerprinting mechanism in cargo which causes a full scan of the
package every time it runs. This build script does not depend on any of
the files in the package.

This is the recommended approach for writing build scripts.
2026-01-23 11:10:53 -08:00
joboet
bd4211595d std: implement sleep_until on Motor 2026-01-23 13:21:23 +01:00
bors
5944b12bd4 Auto merge of #151533 - JonathanBrouwer:rollup-P3oXrte, r=JonathanBrouwer
Rollup of 5 pull requests

Successful merges:

 - rust-lang/rust#149639 (inline constant localized typeck constraint computation)
 - rust-lang/rust#150780 (Add -Z large-data-threshold)
 - rust-lang/rust#151520 (Rename `HandleCycleError` to `CycleErrorHandling`)
 - rust-lang/rust#151525 (update enzyme, includes an extra patch to fix MacOS builds in CI)
 - rust-lang/rust#151527 (Clean up or resolve cfg-related instances of `FIXME(f16_f128)`)

r? @ghost
2026-01-23 10:48:41 +00:00
Jonathan Brouwer
48b9a6c298 Rollup merge of #151527 - tgross35:f16-fixme-cleanup, r=folkertdev
Clean up or resolve cfg-related instances of `FIXME(f16_f128)`

* Replace target-specific config that has a `FIXME` with `cfg(target_has_reliable_f*)`
* Take care of trivial intrinsic-related FIXMEs
* Split `FIXME(f16_f128)` into `FIXME(f16)`, `FIXME(f128)`, or `FIXME(f16,f128)` to more clearly identify what they block

The individual commit messages have more details.
2026-01-23 11:07:57 +01:00
bendn
207dcbbe7c constify boolean methods 2026-01-23 15:12:47 +07:00
bendn
081c4c6abb reenable tests 2026-01-23 14:26:26 +07:00
bors
9283d592de Auto merge of #151389 - scottmcm:vec-repeat, r=joboet
Use `repeat_packed` when calculating layouts in `RawVec`

Seeing whether this helps the icounts seen in https://github.com/rust-lang/rust/pull/148769#issuecomment-3769921666
2026-01-23 07:24:11 +00:00
Trevor Gross
490b307740 cleanup: Start splitting FIXME(f16_f128) into f16, f128, or f16,f128
Make it easier to identify which FIXMEs are blocking stabilization of
which type.
2026-01-22 23:41:57 -06:00
Trevor Gross
e035c770f2 library: Use fabsf128 in the implementation of abs
This FIXME was introduced in 6e2d934a88 ("Add more `f16` and `f128`
library functions and constants") but I can't actually find my
reasoning. As far as I can tell, LLVM has been lowering `fabs.f128` as
bitwise operations for a while across all targets so there shouldn't be
a problem here.

Thus, apply what is suggested in the FIXME.
2026-01-22 23:41:57 -06:00
Trevor Gross
12a03caf07 library: Resolve outdated FIXMEs related to float bitcasts in const 2026-01-22 23:41:57 -06:00
Trevor Gross
8840409f7a f16,f128: Resolve cfg-releated instances of FIXME(f16_f128)
There are a number of instances of `FIXME(f16_f128)` related to target
configuration; either these could use `target_has_reliable_f128`, or the
FIXME is describing such a cfg and is thus redundant (since any
`cfg(target_has_reliable_f*)` needs to be removed before stabilization
anyway).

Switch to using `target_has_reliable_*` where applicable and remove the
redundant FIXMEs.
2026-01-22 23:41:57 -06:00
Jacob Pratt
0b334b84a0 Rollup merge of #151498 - tshepang:patch-1, r=jhpratt
global.rs: improve readability of re-entrance section
2026-01-22 20:16:51 -05:00
Jacob Pratt
15ce06c931 Rollup merge of #151494 - joboet:sleep_until_ensure_elapsed, r=jhpratt
std: ensure that the deadline has passed in `sleep_until`

The clock source used for `sleep` might not be the same as the one used for `Instant`, so the implementation of `sleep_until` may not assume that the `Instant` has elapsed. This is particularly relevant on Windows, where [`QueryPerformanceCounter`](https://learn.microsoft.com/en-us/windows/win32/api/profileapi/nf-profileapi-queryperformancecounter) is used for `Instant`, but [`SetWaitableTimer`](https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-setwaitabletimer) is (probably[^1]) based on [`QueryUnbiasedInterruptTimePrecise`](https://learn.microsoft.com/en-us/windows/win32/api/realtimeapiset/nf-realtimeapiset-queryunbiasedinterrupttimeprecise). If these clocks drift apart, the `sleep` might return before the deadline has passed.

Thus, this PR makes `sleep_until` recheck the current time after waking up and restart the `sleep` if necessary.

[^1]: The documentation doesn't specify a clock, but `QueryUnbiasedInterruptTimePrecise` uses the same units and doesn't count time during sleep either, so I'd wager it's what is used for the timer.
2026-01-22 20:16:51 -05:00
Jacob Pratt
d2acb11cd0 Rollup merge of #151450 - joboet:sleep_clock_monotonic, r=Amanieu
std: use `clock_nanosleep` for `sleep` where available

`nanosleep` is specified to use `CLOCK_REALTIME` but the documentation (especially the example) for `sleep` imply that it measures time using `Instant`, which uses `CLOCK_MONOTONIC`. Thus, this PR makes `sleep` use a relative `clock_nanosleep` with `CLOCK_MONOTONIC` where available. This doesn't make a difference for Linux (which uses `CLOCK_MONOTONIC` for `nanosleep` anyway) but is relevant for e.g. FreeBSD.

This also restores nanosecond-sleep precision for WASI, since https://github.com/rust-lang/rust/issues/150290 was caused by `nanosleep` internally using `clock_nanosleep` with `CLOCK_REALTIME` which is unsupported on WASIp2.

CC @alexcrichton for the WASI fix
2026-01-22 20:16:50 -05:00
Andreas Liljeqvist
890c0fd4e8 Make is_ascii_sse2 a safe function
Remove the `#[target_feature(enable = "sse2")]` attribute and make the
function safe to call. The SSE2 requirement is already enforced by the
`#[cfg(target_feature = "sse2")]` predicate.

Individual unsafe blocks are used for intrinsic calls with appropriate
SAFETY comments.

Also adds FIXME reference to llvm#176906 for tracking when this
workaround can be removed.
2026-01-22 22:41:57 +01:00
Tshepang Mbambo
33514a8ec6 global.rs: improve readability of re-entrance section 2026-01-22 19:18:27 +02:00
joboet
978c5f567e std: ensure that the deadline has passed in sleep_until 2026-01-22 16:51:40 +01:00
Jonathan Brouwer
4d631df8b4 Rollup merge of #151468 - folkertdev:f16-fixmes, r=tgross35
fix `f16` doctest FIXMEs

tracking issue: https://github.com/rust-lang/rust/issues/116909

Remove a bunch of fixmes, and run docs tests on all targets that (should) support them.

r? tgross35
2026-01-22 13:35:43 +01:00
Jonathan Brouwer
f03c1a2bd3 Rollup merge of #151423 - Voultapher:move-assert-matches, r=Amanieu
Move assert_matches to planned stable path

Another prep PR for https://github.com/rust-lang/rust/pull/137487
2026-01-22 13:35:41 +01:00
Jonathan Brouwer
4fb420afa5 Rollup merge of #151220 - cvengler:map-option, r=tgross35
option: Use Option::map in Option::cloned

This commit removes a repetitive match statement in favor of Option::map for Option::cloned.
2026-01-22 13:35:39 +01:00
Jonathan Brouwer
ddd8965d79 Rollup merge of #151042 - RedDaedalus:fix-select-unpredictable-fallback, r=RalfJung
fix fallback impl for select_unpredictable intrinsic

`intrinsics::select_unpredictable` does not drop the value that is not selected, but the fallback impl did not consider this behavior. This creates an observable difference between Miri and actual execution, and possibly does not play well with the `core::hint` version which has extra logic to drop the value that was not selected.
```rust
#![feature(core_intrinsics)]

fn main() {
    core::intrinsics::select_unpredictable(true, LoudDrop(0), LoudDrop(1));
    // cargo run: "0"; cargo miri run: "1 0"
}

struct LoudDrop(u8);

impl Drop for LoudDrop {
    fn drop(&mut self) {
        print!("{} ", self.0);
    }
}
```

This change let me remove the `T: [const] Destruct` bound as well, since the destructor is no longer relevant.
2026-01-22 13:35:39 +01:00
Folkert de Vries
6c3b9f2946 f16: run more tests on supported platforms
and only require `target_has_reliable_f16_math` for `abs` and
`copysign`.
2026-01-22 09:39:50 +01:00
Jacob Pratt
4a983e0900 Rollup merge of #151439 - Mark-Simulacrum:bootstrap-bump, r=nnethercote
Bump bootstrap compiler to 1.94

https://forge.rust-lang.org/release/process.html#default-branch-bootstrap-update-tuesday
2026-01-22 00:37:43 -05:00
Jacob Pratt
a4cf93bccc Rollup merge of #151118 - BD103:reflect-slices, r=oli-obk
Support slices in type reflection

Tracking issue: rust-lang/rust#146922

This PR adds support for inspecting slices `[T]` through type reflection. It does so by adding the new `Slice` struct + variant, which is very similar to `Array` but without the `len` field:

```rust
pub struct Slice {
    pub element_ty: TypeId,
}
```

Here is an example reflecting a slice:

```rust
match const { Type::of::<[u8]>() }.kind {
    TypeKind::Slice(slice) => assert_eq!(slice.element_ty, TypeId::of::<u8>()),
    _ => unreachable!(),
}
```

In addition to this, I also re-arranged the type info unit tests.

r? @oli-obk
2026-01-22 00:37:42 -05:00
bors
004d710faf Auto merge of #151471 - JonathanBrouwer:rollup-EgxENYU, r=JonathanBrouwer
Rollup of 5 pull requests

Successful merges:

 - rust-lang/rust#151010 (std: use `ByteStr`'s `Display` for `OsStr`)
 - rust-lang/rust#151156 (Add GCC and the GCC codegen backend to build-manifest and rustup)
 - rust-lang/rust#151219 (Fixed ICE when using function pointer as const generic parameter)
 - rust-lang/rust#151343 (Port some crate level attrs to the attribute parser)
 - rust-lang/rust#151463 (add x86_64-unknown-linux-gnuasan to CI)

r? @ghost
2026-01-21 22:38:46 +00:00
Lukas Bergdoll
58be5d6620 Move assert_matches to planned stable path 2026-01-21 23:17:24 +01:00
BD103
a509588fa9 feat: support slices in reflection type info 2026-01-21 16:03:00 -06:00
Jonathan Brouwer
76baf2c803 Rollup merge of #151010 - joboet:osstr-bytestr-display, r=jhpratt
std: use `ByteStr`'s `Display` for `OsStr`

Besides reducing duplication, this also results in formatting parameters like padding, align and fill being respected.
2026-01-21 22:24:01 +01:00
Folkert de Vries
e6fd1bd95a f16: use intrinsics::fabsf16 2026-01-21 20:25:24 +01:00
Folkert de Vries
cd8a64602d f16: fix fixmes 2026-01-21 20:24:05 +01:00
bors
eda76d9d1d Auto merge of #149514 - adamgemmell:dev/adagem01/bootstrap-to-library, r=Kobzol
Move bootstrap configuration to library workspace

This creates a new "dist" profile in the standard library which contains configuration for the distributed std artifacts previously contained in bootstrap, in order for a future build-std implementation to use. bootstrap.toml settings continue to override these defaults, as would any RUSTFLAGS provided. I've left some cargo features driven by bootstrap for a future patch.

Unfortunately, profiles aren't expressive enough to express per-target overrides, so [this risc-v example](c8f22ca269/src/bootstrap/src/core/build_steps/compile.rs (L692)) was not able to be moved across. This could go in its own profile which Cargo would have to know to use, and then the panic-abort rustflags overrides would need duplicating again. Doesn't seem like a sustainable solution as a couple similar overrides would explode the number of lines here.

We could use a cargo config in the library workspace for this, but this then would have to be respected by Cargo's build-std implementation and I'm not yet sure about the tradeoffs there.

This patch also introduces a build probe to deal with the test crate's stability which is obviously not ideal, I'm open to other solutions here or can back that change out for now if anyone prefers.

cc @Mark-Simulacrum https://github.com/rust-lang/rfcs/pull/3874
2026-01-21 19:11:41 +00:00
Jonathan Brouwer
530eeff929 Rollup merge of #151453 - folkertdev:simd-dyn-const, r=RalfJung
make `simd_insert_dyn` and `simd_extract_dyn` const

For use in `stdarch`. We currently use an equivalent of the fallback body here, but on some targets the intrinsic generate better code.

r? RalfJung
2026-01-21 16:39:43 +01:00
Jonathan Brouwer
e937bdde41 Rollup merge of #151432 - folkertdev:intrinsics-require-docs, r=RalfJung
use `deny(missing_docs)` for non-mir intrinsics
2026-01-21 16:39:41 +01:00
Folkert de Vries
b4220ecb68 make simd_insert_dyn and simd_extract_dyn const 2026-01-21 14:57:33 +01:00
Folkert de Vries
9c5bfe821c use deny(missing_docs) for non-mir intrinsics 2026-01-21 13:53:44 +01:00
joboet
443765e394 std: use clock_nanosleep for sleep where available 2026-01-21 12:29:24 +01:00
Scott McMurray
c3f309e32b Use repeat_packed when calculating layouts in RawVec 2026-01-21 01:11:12 -08:00
Jacob Pratt
d37351bbe3 Rollup merge of #148637 - rustc_dyn_incompatible, r=lcnr
Replace `#[rustc_do_not_implement_via_object]` with `#[rustc_dyn_incompatible_trait]`

Background: `#[rustc_do_not_implement_via_object]` on a trait currently still allows `dyn Trait` to exist (if the trait is otherwise dyn-compatible), it just means that `dyn Trait` does not automatically implement `Trait` via the normal object candidate. For some traits, this means that `dyn Trait` does not implement `Trait` at all (e.g. `Unsize` and `Tuple`). For some traits, this means that `dyn Trait` implements `Trait`, but with different associated types (e.g. `Pointee`, `DiscriminantKind`). Both of these cases can can cause issues with codegen , as seen in https://github.com/rust-lang/rust/issues/148089 (and https://github.com/rust-lang/rust/issues/148089#issuecomment-3447803823 ), because codegen assumes that if `dyn Trait` does not implement `Trait` (including if `dyn Trait<Assoc = T>` does not implement `Trait` with `Assoc == T`), then `dyn Trait` cannot be constructed, so vtable accesses on `dyn Trait` are unreachable, but this is not the case if `dyn Trait` has multiple supertraits: one which is `#[rustc_do_not_implement_via_object]`, and one which we are doing the vtable access to call a method from.

This PR replaces `#[rustc_do_not_implement_via_object]` with `#[rustc_dyn_incompatible_trait]`, which makes the marked trait dyn-incompatible, making `dyn Trait` not well-formed, instead of it being well-formed but not implementing `Trait`. This resolves rust-lang/rust#148089 by making it not compile.

May fix rust-lang/rust#148615

The traits that are currently marked `#[rustc_do_not_implement_via_object]` are: `Sized`, `MetaSized`, `PointeeSized`, `TransmuteFrom`, `Unsize`, `BikeshedGuaranteedNoDrop`, `DiscriminantKind`, `Destruct`, `Tuple`, `FnPtr`, `Pointee`. Of these:
* `Sized` and `FnPtr` are already not dyn-compatible (`FnPtr: Copy`, which implies `Sized`)
* `MetaSized`
    * Removed `#[rustc_do_not_implement_via_object]`. Still dyn-compatible after this change. (Has a special-case in the trait solvers to ignore the object candidate for `dyn MetaSized`, since it `dyn MetaSized: MetSized` comes from the sized candidate that all `dyn Trait` get.)
* `PointeeSized`
    * Removed `#[rustc_do_not_implement_via_object]`. It doesn't seem to have been doing anything anyway ([playground](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=a395626c8bef791b87a2d371777b7841)), since `PointeeSized` is removed before trait solving(?).
* `Pointee`, `DiscriminantKind`, `Unsize`, and `Tuple` being dyn-compatible without having `dyn Trait: Trait` (with same assoc tys) can be observed to cause codegen issues (https://github.com/rust-lang/rust/issues/148089) so should be made dyn-incompatible
* `Destruct`, `TransmuteFrom`, and `BikeshedGuaranteedNoDrop` I'm not sure if would be useful as object types, but they can be relaxed to being dyn-compatible later if it is determined they should be.

-----

<details> <summary> resolved </summary>

Questions before merge:

1. `dyn MetaSized: MetaSized` having both `SizedCandidate` and `ObjectCandidate`
    1. I'm not sure if the checks in compiler/rustc_trait_selection/src/traits/project.rs and compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs were "load-bearing" for `MetaSized` (which is the only trait that was previously `#[rustc_do_not_implement_via_object]` that is still dyn-compatible after this change). Is it fine to just remove them? Removing them (as I did in the second commit) doesn't change any UI test results.
    3. IIUC, `dyn MetaSized` could get its `MetaSized` implementation in two ways: the object candidate (the normal `dyn Trait: Trait`) that was supressed by `#[rustc_do_not_implement_via_object]`, and the `SizedCandidate` (that all `dyn Trait` get for `dyn Trait: MetaSized`). Given that `MetaSized` has no associated types or methods, is it fine that these both exist now? Or is it better to only have the `SizedCandidate` and leave these checks in (i.e. drop the second commit, and remove the FIXMEs)?
    4. Resolved: the trait solvers special-case `dyn MetaSized` to ignore the object candidate in preference to the sizedness candidate (technically the check is for any `is_sizedness_trait`, but only `MetaSized` gets this far (`Sized` is inherently dyn-incompatible, and `dyn PointeeSized` is ill-formed for other reasons)
4. Diagnostics improvements?
    1. The diagnostics are kinda bad. If you have a `trait Foo: Pointee {}`, you now get a note that reads like *Foo* "opted out of dyn-compatbility", when really `Pointee` did that.
    2. Resolved: can be improved later

  <details> <summary>diagnostic example</summary>

```rs
#![feature(ptr_metadata)]

trait C: std::ptr::Pointee {}

fn main() {
    let y: &dyn C;
}
```

```rs
error[E0038]: the trait `C` is not dyn compatible
  --> c.rs:6:17
   |
 6 |     let y: &dyn C;
   |                 ^ `C` is not dyn compatible
   |
note: for a trait to be dyn compatible it needs to allow building a vtable
      for more information, visit <https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility>
  --> /home/zachary/opt_mount/zachary/Programming/rust-compiler-2/library/core/src/ptr/metadata.rs:57:1
   |
57 | #[rustc_dyn_incompatible_trait]
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...because it opted out of dyn-compatbility
   |
  ::: c.rs:3:7
   |
 3 | trait C: std::ptr::Pointee {}
   |       - this trait is not dyn compatible...

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0038`.
  ```

  </details> </details>

  Still investigating "3. `compiler/rustc_hir/src/attrs/encode_cross_crate.rs`: Should `DynIncompatibleTrait` attribute be encoded cross crate?"
2026-01-21 02:04:01 -05:00