rust/library/coretests/tests
bors 99ba556567 Auto merge of #144081 - RalfJung:const-ptr-fragments, r=oli-obk
const-eval: full support for pointer fragments

This fixes https://github.com/rust-lang/const-eval/issues/72 and makes `swap_nonoverlapping` fully work in const-eval by enhancing per-byte provenance tracking with tracking of *which* of the bytes of the pointer this one is. Later, if we see all the same bytes in the exact same order, we can treat it like a whole pointer again without ever risking a leak of the data bytes (that encode the offset into the allocation). This lifts the limitation that was discussed quite a bit in https://github.com/rust-lang/rust/pull/137280.

For a concrete piece of code that used to fail and now works properly consider this example doing a byte-for-byte memcpy in const without using intrinsics:
```rust
use std::{mem::{self, MaybeUninit}, ptr};

type Byte = MaybeUninit<u8>;

const unsafe fn memcpy(dst: *mut Byte, src: *const Byte, n: usize) {
    let mut i = 0;
    while i < n {
        *dst.add(i) = *src.add(i);
        i += 1;
    }
}

const _MEMCPY: () = unsafe {
    let ptr = &42;
    let mut ptr2 = ptr::null::<i32>();
    // Copy from ptr to ptr2.
    memcpy(&mut ptr2 as *mut _ as *mut _, &ptr as *const _ as *const _, mem::size_of::<&i32>());
    assert!(*ptr2 == 42);
};
```
What makes this code tricky is that pointers are "opaque blobs" in const-eval, we cannot just let people look at the individual bytes since *we don't know what those bytes look like* -- that depends on the absolute address the pointed-to object will be placed at. The code above "breaks apart" a pointer into individual bytes, and then puts them back together in the same order elsewhere. This PR implements the logic to properly track how those individual bytes relate to the original pointer, and to recognize when they are in the right order again.

We still reject constants where the final value contains a not-fully-put-together pointer: I have no idea how one could construct an LLVM global where one byte is defined as "the 3rd byte of a pointer to that other global over there" -- and even if LLVM supports this somehow, we can leave implementing that to a future PR. It seems unlikely to me anyone would even want this, but who knows.^^

This also changes the behavior of Miri, by tracking the order of bytes with provenance and only considering a pointer to have valid provenance if all bytes are in the original order again. This is related to https://github.com/rust-lang/unsafe-code-guidelines/issues/558. It means one cannot implement XOR linked lists with strict provenance any more, which is however only of theoretical interest. Practically I am curious if anyone will show up with any code that Miri now complains about - that would be interesting data. Cc `@rust-lang/opsem`
2025-08-17 04:33:31 +00:00
..
async_iter Put all coretests in a separate crate 2025-01-26 10:26:36 +00:00
ffi Delegate <CStr as Debug> to ByteStr 2025-06-12 12:53:14 -04:00
floats Hoist zero and one out into TestableFloat 2025-08-07 22:51:46 -04:00
fmt Add test for new format_args!() temporary lifetimes. 2025-06-18 10:20:20 +02:00
hash library: Use size_of from the prelude instead of imported 2025-03-06 20:20:38 -08:00
io Rollup merge of #142741 - a1phyr:fix_unsoundness, r=Mark-Simulacrum 2025-08-14 11:39:33 +02:00
iter fix load-bearing typo 2025-07-19 15:17:04 +00:00
net Recognise new IPv6 non-global range from RFC9602 2025-03-21 17:53:29 +11:00
num Clean up some resolved test regressions of const trait removals in std 2025-08-08 00:58:54 +09:00
ops moved simple test to coretests, introduced more fleshed out doctests for break_ok/continue_ok 2025-04-26 12:57:12 +02:00
panic Fix Ord, Eq and Hash implementation of panic::Location 2025-07-29 22:15:44 +02:00
alloc.rs library: Use size_of from the prelude instead of imported 2025-03-06 20:20:38 -08:00
any.rs Add opaque TypeId handles for CTFE 2025-07-09 16:37:11 +00:00
array.rs Add #[must_use] to Array::map 2025-05-13 12:34:53 +09:00
ascii.rs Put all coretests in a separate crate 2025-01-26 10:26:36 +00:00
ascii_char.rs Implement Extend<AsciiChar> for String 2025-02-08 16:51:04 -05:00
asserting.rs Put all coretests in a separate crate 2025-01-26 10:26:36 +00:00
atomic.rs atomic tests: remove static mut 2025-06-19 12:08:24 +00:00
bool.rs Add methods for converting bool to Result<(), E> 2025-06-19 23:20:06 +02:00
bstr.rs Actually run the bstr test 2025-01-26 10:45:09 +00:00
cell.rs make RefCell unstably const 2025-06-25 17:06:49 -07:00
char.rs Clean up some resolved test regressions of const trait removals in std 2025-08-08 00:58:54 +09:00
clone.rs Put all coretests in a separate crate 2025-01-26 10:26:36 +00:00
cmp.rs Put all coretests in a separate crate 2025-01-26 10:26:36 +00:00
const_ptr.rs Put all coretests in a separate crate 2025-01-26 10:26:36 +00:00
convert.rs Clean up some resolved test regressions of const trait removals in std 2025-08-08 00:58:54 +09:00
error.rs Put all coretests in a separate crate 2025-01-26 10:26:36 +00:00
ffi.rs Put all coretests in a separate crate 2025-01-26 10:26:36 +00:00
future.rs Put all coretests in a separate crate 2025-01-26 10:26:36 +00:00
hint.rs Fix drop handling in hint::select_unpredictable 2025-04-17 17:30:53 +02:00
intrinsics.rs Put all coretests in a separate crate 2025-01-26 10:26:36 +00:00
lazy.rs Put all coretests in a separate crate 2025-01-26 10:26:36 +00:00
lib.rs Rollup merge of #144963 - rossmacarthur-forks:stabilize-core-iter-chain, r=jhpratt 2025-08-15 18:13:29 -04:00
macros.rs Add regression test for matches! + non_exhaustive_omitted_patterns lint 2025-07-23 14:35:44 +02:00
manually_drop.rs Put all coretests in a separate crate 2025-01-26 10:26:36 +00:00
mem.rs Add core::mem::DropGuard 2025-07-28 12:12:40 +02:00
nonzero.rs Clean up some resolved test regressions of const trait removals in std 2025-08-08 00:58:54 +09:00
ops.rs Put all coretests in a separate crate 2025-01-26 10:26:36 +00:00
option.rs Clean up some resolved test regressions of const trait removals in std 2025-08-08 00:58:54 +09:00
panic.rs Put all coretests in a separate crate 2025-01-26 10:26:36 +00:00
pattern.rs Put all coretests in a separate crate 2025-01-26 10:26:36 +00:00
pin.rs Put pin!() tests in the right file. 2025-03-28 10:18:04 +01:00
pin_macro.rs update cfg(bootstrap) 2025-05-12 15:33:37 +02:00
ptr.rs const-eval: full support for pointer fragments 2025-07-30 08:13:58 +02:00
result.rs Constify additional Result functions 2025-08-01 08:55:50 +03:00
simd.rs Put all coretests in a separate crate 2025-01-26 10:26:36 +00:00
slice.rs Remove [T]::array_chunks(_mut) 2025-07-27 23:03:07 -07:00
str.rs tidy: Fix paths to coretests and alloctests 2025-04-05 12:15:49 -07:00
str_lossy.rs Put all coretests in a separate crate 2025-01-26 10:26:36 +00:00
task.rs Put all coretests in a separate crate 2025-01-26 10:26:36 +00:00
time.rs Split duration_constructors to get non-controversial bits out faster. 2025-05-09 22:52:39 -04:00
tuple.rs Remove unnecessary parentheses in assert!s 2025-08-10 21:59:04 +00:00
unicode.rs Put all coretests in a separate crate 2025-01-26 10:26:36 +00:00
waker.rs Put all coretests in a separate crate 2025-01-26 10:26:36 +00:00