about summary refs log tree commit diff
path: root/compiler/rustc_middle/src/mir/interpret/error.rs
AgeCommit message (Collapse)AuthorLines
2025-09-24const validation: better error for maybe-null referencesRalf Jung-0/+2
2025-09-24const-eval: improve and actually test the errors when pointers might be ↵Ralf Jung-4/+1
outside the range of a scalar
2025-08-17Auto merge of #144081 - RalfJung:const-ptr-fragments, r=oli-obkbors-3/+0
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-04small refactor of `InterpResult`Waffle Lapkin-20/+21
- don't need type alias to default type argument - `Residual` impl allows to use more std APIs (like `<[T; N]>::try_map`)
2025-07-30const-eval: full support for pointer fragmentsRalf Jung-3/+0
2025-07-27miri: for ABI mismatch errors, say which argument is the problemRalf Jung-1/+6
2025-07-23Remove useless lifetime parameter.Camille GILLOT-1/+1
2025-07-16add `const_make_global`; err for `const_allocate` ptrs if didn't callDeadbeef-1/+1
Co-Authored-By: Ralf Jung <post@ralfj.de> Co-Authored-By: Oli Scherer <github333195615777966@oli-obk.de>
2025-07-09Add opaque TypeId handles for CTFEOli Scherer-0/+2
2025-07-03setup CI and tidy to use typos for spellchecking and fix few typosklensy-1/+1
2025-06-26const-eval: allow constants to refer to mutable/external memory, but reject ↵Ralf Jung-8/+44
such constants as patterns
2025-04-30interpret: better error message for out-of-bounds pointer arithmetic and ↵Ralf Jung-5/+3
accesses
2025-02-25Teach structured errors to display short `Ty`Esteban Küber-4/+4
Make it so that every structured error annotated with `#[derive(Diagnostic)]` that has a field of type `Ty<'_>`, the printing of that value into a `String` will look at the thread-local storage `TyCtxt` in order to shorten to a length appropriate with the terminal width. When this happen, the resulting error will have a note with the file where the full type name was written to. ``` error[E0618]: expected function, found `((..., ..., ..., ...), ..., ..., ...)`` --> long.rs:7:5 | 6 | fn foo(x: D) { //~ `x` has type `(... | - `x` has type `((..., ..., ..., ...), ..., ..., ...)` 7 | x(); //~ ERROR expected function, found `(... | ^-- | | | call expression requires function | = note: the full name for the type has been written to 'long.long-type-14182675702747116984.txt' = note: consider using `--verbose` to print the full type name to the console ```
2025-02-19Make fewer crates depend on rustc_ast_irMichael Goulet-2/+1
2025-02-10compiler: die immediately instead of handling unknown target codegenJubilee Young-4/+0
We cannot produce anything useful if asked to compile unknown targets. We should handle the error immediately at the point of discovery instead of propagating it upward, and preferably in the simplest way: Die. This allows cleaning up our "error-handling" spread across 5 crates.
2025-02-06Clean up trivial traversal/lift impl generator macro calls.Nicholas Nethercote-2/+0
We have four macros for generating trivial traversal (fold/visit) and lift impls. - `rustc_ir::TrivialTypeTraversalImpls` - `rustc_middle::TrivialTypeTraversalImpls` - `rustc_middle::TrivialLiftImpls` - `rustc_middle::TrivialTypeTraversalAndLiftImpls` The first two are very similar. The last one just combines the second and third one. The macros themselves are ok, but their use is a mess. This commit does the following. - Removes types that no longer need a lift and/or traversal impl from the macro calls. - Consolidates the macro calls into the smallest number of calls possible, with each one mentioning as many types as possible. - Orders the types within those macro calls alphabetically, and makes the module qualification more consistent. - Eliminates `rustc_middle::mir::type_foldable`, because the macro calls were merged and the manual `TypeFoldable` impls are better placed in `structural_impls.rs`, alongside all the other ones. This makes the code more concise. Moving forward, it also makes it more obvious where new types should be added.
2024-12-31Convert some Into impls into From implsMichael Goulet-3/+3
2024-12-18Variants::Single: do not use invalid VariantIdx for uninhabited enumsRalf Jung-1/+1
2024-12-09fix ICE on type error in promotedRalf Jung-16/+17
2024-12-01fix ICE when promoted has layout size overflowRalf Jung-17/+9
2024-11-18interpret: do not ICE when a promoted fails with OOMRalf Jung-2/+13
2024-11-03compiler: Directly use rustc_abi in metadata and middleJubilee Young-2/+2
Stop reexporting ReprOptions from middle::ty
2024-10-19interpret errors: add map_err_kind, rename InterpError -> InterpErrorKindRalf Jung-25/+36
2024-10-12mark InterpResult as must_useRalf Jung-0/+1
2024-10-01make InterpResult a dedicated type to avoid accidentally discarding the errorRalf Jung-55/+184
2024-09-30panic when an interpreter error gets unintentionally discardedRalf Jung-3/+58
2024-09-23Check vtable projections for validity in miriMichael Goulet-4/+8
2024-09-22Reformat using the new identifier sorting from rustfmtMichael Goulet-3/+3
2024-09-02chore: Fix typos in 'compiler' (batch 2)Alexander Cyon-3/+3
2024-08-01on a signed deref check, mention the right pointer in the errorRalf Jung-3/+4
2024-07-29Rollup merge of #128277 - RalfJung:offset_from_wildcard, r=oli-obkMatthias Krüger-3/+8
miri: fix offset_from behavior on wildcard pointers offset_from wouldn't behave correctly when the "end" pointer was a wildcard pointer (result of an int2ptr cast) just at the end of the allocation. Fix that by expressing the "same allocation" check in terms of two `check_ptr_access_signed` instead of something specific to offset_from, which is both more canonical and works better with wildcard pointers. The second commit just improves diagnostics: I wanted the "pointer is dangling (has no provenance)" message to say how many bytes of memory it expected to see (since if it were 0 bytes, this would actually be legal, so it's good to tell the user that it's not 0 bytes). And then I was annoying that the error looks so different for when you deref a dangling pointer vs an out-of-bounds pointer so I made them more similar. Fixes https://github.com/rust-lang/miri/issues/3767
2024-07-29Reformat `use` declarations.Nicholas Nethercote-5/+5
The previous commit updated `rustfmt.toml` appropriately. This commit is the outcome of running `x fmt --all` with the new formatting options.
2024-07-27improve dangling/oob errors and make them more uniformRalf Jung-3/+8
2024-07-18valtree construction: keep track of which type was valtree-incompatibleRalf Jung-3/+5
2024-06-22don't ICE when encountering an extern type field during validationRalf Jung-0/+2
2024-06-17Remove an unused validation error variantOli Scherer-3/+0
2024-05-23Allow const eval failures if the cause is a type layout issueOli Scherer-0/+3
2024-05-21interpret: make overflowing binops just normal binopsRalf Jung-6/+13
2024-05-13interpret: move error macros into error.rsRalf Jung-3/+118
2024-04-29Remove `extern crate rustc_data_structures` from numerous crates.Nicholas Nethercote-1/+1
2024-04-29Remove `extern crate rustc_macros` from `rustc_middle`.Nicholas Nethercote-1/+1
2024-04-21Miri: detect wrong vtables in wide pointersRalf Jung-21/+78
2024-04-18Simplify `static_assert_size`s.Nicholas Nethercote-1/+1
We want to run them on all 64-bit platforms.
2024-04-03Check `x86_64` size assertions on `aarch64`, tooZalathar-1/+1
This makes it easier for contributors on aarch64 workstations (e.g. Macs) to notice when these assertions have been violated.
2024-03-17Print a backtrace in const eval if interruptedBen Kimock-0/+2
2024-03-11Rename `IntoDiagnosticArg` as `IntoDiagArg`.Nicholas Nethercote-9/+9
Also rename `into_diagnostic_arg` as `into_diag_arg`, and `NotIntoDiagnosticArg` as `NotInotDiagArg`.
2024-03-05Rename `DiagnosticMessage` as `DiagMessage`.Nicholas Nethercote-4/+2
2024-02-29Rollup merge of #121782 - RalfJung:mutable-ref-in-static, r=oli-obkMatthias Krüger-1/+0
allow statics pointing to mutable statics Fixes https://github.com/rust-lang/rust/issues/120450 for good. We can even simplify our checks: no need to specifically go looking for mutable references in const, we can just reject any reference that points to something mutable. r? `@oli-obk`
2024-02-29allow statics pointing to mutable staticsRalf Jung-1/+0
2024-02-28Auto merge of #121489 - nnethercote:diag-renaming, r=davidtwcobors-8/+8
Diagnostic renaming Renaming various diagnostic types from `Diagnostic*` to `Diag*`. Part of https://github.com/rust-lang/compiler-team/issues/722. There are more to do but this is enough for one PR. r? `@davidtwco`