rust/compiler
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
..
rustc rename stable_mir to rustc_public, and rustc_smir to rustc_public_bridge 2025-07-14 09:25:54 +00:00
rustc_abi Reuse sign_extend helper 2025-07-29 14:17:48 +00:00
rustc_arena Introduce Arena::try_alloc_from_iter. 2025-04-19 01:13:18 +00:00
rustc_ast Extract ast TraitImplHeader 2025-08-11 17:05:36 -05:00
rustc_ast_ir Tidy up Cargo.toml files. 2025-07-31 19:58:04 +10:00
rustc_ast_lowering Pass the target type down to parse_attribute_list 2025-08-14 18:11:56 +02:00
rustc_ast_passes Rollup merge of #144907 - ShoyuVanilla:no-const-async, r=fmease 2025-08-15 18:13:28 -04:00
rustc_ast_pretty Extract ast TraitImplHeader 2025-08-11 17:05:36 -05:00
rustc_attr_parsing Specify the list of allowed targets per attribute 2025-08-14 18:18:22 +02:00
rustc_baked_icu_data Add unreachable_pub to RUSTC_LINT_FLAGS for compiler/ crates. 2025-03-11 13:14:21 +11:00
rustc_borrowck Auto merge of #142071 - lcnr:revealing-use, r=compiler-errors 2025-08-15 18:52:12 +00:00
rustc_builtin_macros Rollup merge of #144922 - Kobzol:derive-from, r=nnethercote 2025-08-15 18:13:28 -04:00
rustc_codegen_cranelift Port the #[linkage] attribute to the new attribute system 2025-08-13 21:01:37 +02:00
rustc_codegen_gcc Complete functionality and general cleanup 2025-08-14 16:30:15 +00:00
rustc_codegen_llvm Rollup merge of #145275 - StackOverflowExcept1on:fix-wasm32v1-none, r=alexcrichton 2025-08-15 16:16:38 +10:00
rustc_codegen_ssa Rollup merge of #145004 - bjorn3:remove_unused_fields, r=WaffleLapkin 2025-08-15 16:16:33 +10:00
rustc_const_eval Auto merge of #144081 - RalfJung:const-ptr-fragments, r=oli-obk 2025-08-17 04:33:31 +00:00
rustc_data_structures Allow pretty printing paths with -Zself-profile-events=args 2025-07-25 22:24:21 +02:00
rustc_driver Remove recursion_limit increases. 2025-04-02 16:25:27 +11:00
rustc_driver_impl Tidy up Cargo.toml files. 2025-07-31 19:58:04 +10:00
rustc_error_codes Rollup merge of #144944 - He1pa:E0793, r=compiler-errors 2025-08-15 16:16:32 +10:00
rustc_error_messages Remove unused feature gates 2025-06-28 23:36:46 +00:00
rustc_errors Rollup merge of #137872 - estebank:extra-vert, r=compiler-errors 2025-08-14 21:48:41 +02:00
rustc_expand Rollup merge of #145153 - joshtriplett:macro-kinds-plural, r=petrochenkov 2025-08-13 18:43:01 +02:00
rustc_feature Auto merge of #145304 - m-ou-se:simplify-panic, r=oli-obk 2025-08-16 10:15:46 +00:00
rustc_fluent_macro Remove all unused feature gates from the compiler 2025-06-08 14:50:42 +00:00
rustc_fs_util Retry if creating temp fails with access denied 2025-04-25 11:28:36 +00:00
rustc_graphviz Remove #![warn(unreachable_pub)] from all compiler/ crates. 2025-03-11 13:14:21 +11:00
rustc_hashes Revert "Use workspace lints for crates in compiler/ #138084" 2025-03-10 18:12:47 +08:00
rustc_hir Auto merge of #145304 - m-ou-se:simplify-panic, r=oli-obk 2025-08-16 10:15:46 +00:00
rustc_hir_analysis Rollup merge of #145235 - compiler-errors:comment, r=BoxyUwU 2025-08-15 16:16:38 +10:00
rustc_hir_pretty Propagate TraitImplHeader to hir 2025-08-11 17:05:42 -05:00
rustc_hir_typeck Rollup merge of #145235 - compiler-errors:comment, r=BoxyUwU 2025-08-15 16:16:38 +10:00
rustc_incremental Auto merge of #139758 - Zoxc:thread-local-graph, r=oli-obk 2025-05-07 12:39:54 +00:00
rustc_index Tidy up Cargo.toml files. 2025-07-31 19:58:04 +10:00
rustc_index_macros Tidy up Cargo.toml files. 2025-07-31 19:58:04 +10:00
rustc_infer remove from_forall 2025-08-11 09:18:46 +02:00
rustc_interface Fix parallel rustc not being reproducible due to unstable sorting of items. 2025-08-13 08:59:32 +08:00
rustc_lexer test(lexer): Add frontmatter unit test 2025-07-10 10:25:29 -05:00
rustc_lint Fix wrong spans with external macros in the dropping_copy_types lint 2025-08-15 11:59:23 +02:00
rustc_lint_defs Rollup merge of #144865 - WaffleLapkin:track-tail, r=lqd 2025-08-15 16:16:31 +10:00
rustc_llvm Auto merge of #145093 - nikic:dead-on-return, r=nnethercote 2025-08-13 04:18:43 +00:00
rustc_log Add documentation for init_logger_with_additional_layer 2025-06-12 12:11:15 +02:00
rustc_macros cleanup: Remove useless [T].iter().last() 2025-08-14 16:28:56 +00:00
rustc_metadata Switch to a bitflags MacroKinds to support macros with more than one kind 2025-08-12 09:24:45 -07:00
rustc_middle Auto merge of #144081 - RalfJung:const-ptr-fragments, r=oli-obk 2025-08-17 04:33:31 +00:00
rustc_mir_build Auto merge of #145423 - Zalathar:rollup-9jtefpl, r=Zalathar 2025-08-15 09:13:10 +00:00
rustc_mir_dataflow Escape diff strings in graphviz 2025-08-08 00:20:55 -05:00
rustc_mir_transform Rollup merge of #145392 - Zalathar:create-mappings, r=petrochenkov 2025-08-15 16:16:43 +10:00
rustc_monomorphize Rollup merge of #144865 - WaffleLapkin:track-tail, r=lqd 2025-08-15 16:16:31 +10:00
rustc_next_trait_solver update comment 2025-08-14 16:07:24 +02:00
rustc_parse Rollup merge of #145378 - xizheyin:144968, r=davidtwco 2025-08-15 16:16:41 +10:00
rustc_parse_format update to literal-escaper-0.0.5 2025-07-08 10:16:44 +00:00
rustc_passes Remove the old target checking logic 2025-08-14 18:18:42 +02:00
rustc_pattern_analysis avoid unnecessary type sanity checks 2025-08-14 09:44:22 +02:00
rustc_privacy Propagate TraitImplHeader to hir 2025-08-11 17:05:42 -05:00
rustc_proc_macro Tidy up Cargo.toml files. 2025-07-31 19:58:04 +10:00
rustc_public fix missing parenthesis in pretty discriminant 2025-08-09 01:35:50 +08:00
rustc_public_bridge Remove useless lifetime parameter. 2025-07-23 23:54:37 +00:00
rustc_query_impl Remove unused allow attrs 2025-07-07 12:58:16 +00:00
rustc_query_system remove rustc_attr_data_structures 2025-07-31 14:19:27 +02:00
rustc_resolve Rollup merge of #145369 - estebank:issue-145367, r=compiler-errors 2025-08-15 16:16:40 +10:00
rustc_sanitizers rename TraitRef::from_method to from_assoc 2025-08-09 14:22:01 +08:00
rustc_serialize use div_ceil instead of manual logic 2025-07-05 10:55:42 +02:00
rustc_session Rollup merge of #145408 - Kobzol:deduplicate-search-paths, r=petrochenkov 2025-08-15 16:03:58 +02:00
rustc_span Auto merge of #145304 - m-ou-se:simplify-panic, r=oli-obk 2025-08-16 10:15:46 +00:00
rustc_symbol_mangling Rollup merge of #144949 - nnethercote:more-Printer-cleanups, r=davidtwco 2025-08-13 18:42:59 +02:00
rustc_target Rollup merge of #145004 - bjorn3:remove_unused_fields, r=WaffleLapkin 2025-08-15 16:16:33 +10:00
rustc_thread_pool Drop rust-version from rustc_thread_pool 2025-08-04 15:03:49 -07:00
rustc_trait_selection Rollup merge of #145235 - compiler-errors:comment, r=BoxyUwU 2025-08-15 16:16:38 +10:00
rustc_traits Auto merge of #144446 - nnethercote:opt-region-constraints, r=lcnr 2025-08-01 04:06:21 +00:00
rustc_transmute Tidy up Cargo.toml files. 2025-07-31 19:58:04 +10:00
rustc_ty_utils use ty::Value instead of manual pairs of types and valtrees 2025-08-14 09:44:22 +02:00
rustc_type_ir ignore head usages of ignored candidates 2025-08-14 14:38:00 +02:00
rustc_type_ir_macros Split TypeFolder and FallibleTypeFolder 2025-04-15 18:30:35 +00:00