Commit Graph

3010 Commits

Author SHA1 Message Date
Yara
2b7d180842 reflection: add non-exhaustive to meme::type_info::Variant 2026-08-15 01:03:06 +02:00
Yara
b685be1643 reflection: Add TypeId::variant() returning new Variant 2026-08-15 01:02:32 +02:00
bjorn3
0417c4e028 Add comments
Co-authored-by: Ralf Jung <post@ralfj.de>
2026-08-13 14:20:02 +02:00
bjorn3
cc73c4d283 Avoid computing FnAbi for llvm intrinsics in miri 2026-08-13 14:20:01 +02:00
Jonathan Brouwer
5d8a3c9f20 Rollup merge of #160925 - yara-blue:is_signed, r=oli-obk
Add `TypeId::is_signed` method

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

r? @oli-obk
2026-08-12 13:52:41 +02:00
Yara
0ea82de957 Add TypeId::is_signed method 2026-08-11 18:36:33 +02:00
Jonathan Brouwer
fca480be1f Rollup merge of #160775 - RalfJung:pattern-unsafe-transparent, r=oli-obk
interpret: treat pattern and unsafe-binder as ABI-transparent

This was suggested by @Jules-Bertholet in https://github.com/rust-lang/rust/pull/160658. I didn't follow all the discussions for those types. Making them transparent makes sense to me though we should make sure this is explicitly discussed as a stable guarantee when it comes to stabilizing those types.

Tracking issues: https://github.com/rust-lang/rust/issues/123646, https://github.com/rust-lang/rust/issues/130516.

r? @oli-obk
2026-08-11 11:14:55 +02:00
Jonathan Brouwer
cbc9fddd47 Rollup merge of #160749 - RalfJung:maybe-dangling-address-space, r=WaffleLapkin
MaybeDangling: ensure references fit inside the address space

In the RFC we left open the question of the exact validity invariant for references inside `MaybeDangling`. This PR implements the strictest invariant I can think of: we already require references to be aligned, now we also require "addr + size" to be computable without overflow. This ensures that whatever niches we add to references in the future, `MaybeDangling` preserves those niches.

Cc @rust-lang/opsem @WaffleLapkin

Tracking issue: https://github.com/rust-lang/rust/issues/118166
r? @oli-obk
2026-08-10 19:02:02 +02:00
Chris Denton
4641990929 Remove ensure_sufficient_stack 2026-08-09 19:58:29 +00:00
mejrs
196dd2a174 Fix up lang_items import paths 2026-08-09 15:20:29 +02:00
Ralf Jung
b7ebb506dd interpret: treat pattern and unsafe-binder as ABI-transparent 2026-08-08 22:34:31 +02:00
Jonathan Brouwer
945feb063f Rollup merge of #160658 - RalfJung:trivial-abi, r=oli-obk
miri: implement more restrictive trivial-ABI checks

This implements the rules from rust-lang/rust#157973 in Miri. We unfortunately have to reject some code that we thought was okay. Let's land this in Miri ahead of anything official also to see if there's any major breakage from that.

r? @oli-obk
2026-08-08 16:32:45 +02:00
Ralf Jung
0226819649 MaybeDangling: ensure references fit inside the address space 2026-08-08 13:46:26 +02:00
Ralf Jung
179b9dfe5d miri: implement more restrictive trivial-ABI checks 2026-08-08 10:55:46 +02:00
bors
ae45457594 Auto merge of #160160 - ChayimFriedman2:if-empty-opt, r=TaKO8Ki
Optimize handling of solver errors



- Check for the empty case in `collect_remaining_errors()`.
- Instead of representing trait errors as `Vec<Error>`, use a special type

   This has multiple advantages:

    - Performance. The new type is 1/3 the size of `Vec` (being equivalent in layout to `Option<ThinVec>`) and can be kept in a register.
    - Type safety. We mark the type `#[must_use]`, and thinks requiring errors take `ThinVec`, which requires unwrapping the type and verifying there is indeed an error. We still provide conversions to slices, `ThinVec`, and iteration, because some code needs this and I saw no benefit in changing it, but we deliberately do not provide `Deref<Target = [E]>` or things like that.
2026-08-07 06:00:49 +00:00
Jonathan Brouwer
592096bca8 Rollup merge of #160398 - moulins:layout-is-variant-uninhabited, r=oli-obk
rustc_abi: Add `LayoutData::is_variant_uninhabited` method

This is a cheaper alternative to `TyAndLayout::for_variant(_, idx).is_uninhabited()`, which avoids the extra work done by `TyAndLayout::for_variant`.

EDIT: Let me expand on the motivation from this [comment](https://github.com/rust-lang/rust/pull/160398#issuecomment-5167761369):

`TyAndLayout::for_variant` is a curious API: the resulting layout doesn't represent a Rust type in the strict sense (not even `pattern_type!(Enum is Variant { .. })`; instead, it only describes the "variant-specific" fields, and fields shared between variants (e.g. the enum tag or coroutine captures) are entirely missing.

As such, I believe alternative APIs should be used instead to clarify intent, when possible. This PR deals with the simplest cases, and a future PR will add `TyAndLayout::variant_field(self, cx, variant_idx, field_idx)` to deal with the next-most common case of only needing a variant layout for field lookups.
2026-08-06 12:41:07 +02:00
Jonathan Brouwer
852afd5ab3 Rollup merge of #160156 - RalfJung:cast-kind-const-check, r=oli-obk
check_consts: exhaustively match on CastKind
2026-08-06 12:41:05 +02:00
Jonathan Brouwer
84127afd41 Rollup merge of #160555 - nnethercote:split-apply_terminator, r=cjgillot
Split `apply_primary_terminator_effect`

It currently does two distinct things, and it's a bit nicer it they are separated. Details in individual commits.

r? @cjgillot
2026-08-06 12:41:03 +02:00
Jonathan Brouwer
3657c82cdc Rollup merge of #160006 - estebank:issue-89567, r=chenyukang
Account for desugaring in method call move errors

When encountering a move error caused by a desugared method call, talk about the user-facing feature (`await`/`?`/`for`-loop), instead of only the internal API it got desugared to.

```
error[E0382]: use of moved value: `entry`
  --> $DIR/moved-into-question-mark.rs:11:18
   |
LL |     for entry in fs::read_dir(".")? {
   |         ----- move occurs because `entry` has type `Result<DirEntry, std::io::Error>`, which does not implement the `Copy` trait
LL |
LL |         let file_type = entry?.file_type()?;
   |                         ------ `entry` moved due to usage in the question mark operator
...
LL |             dbg!(entry?.file_name());
   |                  ^^^^^ value used here after move
   |
note: the question mark operator is expanded into a call to `branch`, which takes ownership of the receiver `self`, which moves `entry`
  --> $SRC_DIR/core/src/ops/try_trait.rs:LL:COL
help: you could `clone` the value and consume it, if the following trait bounds could be satisfied: `DirEntry: Clone` and `std::io::Error: Clone`
   |
LL |         let file_type = entry.clone()?.file_type()?;
   |                              ++++++++
```

Fix rust-lang/rust#89567.
2026-08-06 12:40:59 +02:00
Ralf Jung
71b84560aa check_consts: exhaustively match on CastKind 2026-08-06 10:15:03 +02:00
Chayim Refael Friedman
5491ec85e2 Instead of representing trait errors as Vec<Error>, use a special type
This has multiple advantages:

 - Performance. The new type is 1/3 the size of `Vec` (being equivalent in layout to `Option<ThinVec>`) and can be kept in a register.
 - Type safety. We mark the type `#[must_use]`, and thinks requiring errors take `ThinVec`, which requires unwrapping the type and verifying there is indeed an error. We still provide conversions to slices, `ThinVec`, and iteration, because some code needs this and I saw no benefit in changing it, but we deliberately do not provide `Deref<Target = [E]>` or things like that.
2026-08-06 07:18:55 +03:00
Moulins
fee8922aa3 rustc_abi: Add LayoutData::is_variant_uninhabited method
This is a cheaper alternative to `TyAndLayout::for_variant(_, idx).is_uninhabited()`
2026-08-05 15:38:43 +02:00
Nicholas Nethercote
6377970b4c Split apply_primary_terminator_effect
This method currently does two things: it applies the effect, and also
computes the edges. However:
- Three of the four call sites don't use the edges.
- Most analyses just return `terminator.edges()` unconditionally.

This commit separates the edge computation into a new method,
`get_terminator_edges()`. It defaults to `terminator.edges()`, which
means that most analyses don't need to define it. And now edges are only
obtained when they are needed (in `Forward::apply_effects_in_block`).
2026-08-05 15:27:54 +10:00
Jonathan Brouwer
ad0ba38714 Rollup merge of #159727 - oli-obk:reflection_stuff, r=JohnTitor
Various steps in moving away from the big reflection enum to reflection functions

tracking issue: rust-lang/rust#146922

Some more of these and we can turn the big TyKind enum into one without any data, but I think these are enough steps for one PR, don't wanna turn it into a mega PR
2026-08-04 18:35:27 +02:00
Jonathan Brouwer
015a98950e Rollup merge of #160399 - RalfJung:interpret-deref-validity, r=oli-obk
interpret: skip deref-projection validity checks when they are not needed

Trying to claw back the perf regression from https://github.com/rust-lang/rust/pull/160012.

We could also, like, skip the entire check in const-eval when we don't care about validity. But the slowdown will also affect Miri so I want to first try what we can do without doing less UB checking.
2026-08-04 14:41:27 +02:00
Oli Scherer
cecd4a4fcb Add reflection method for getting a type's generic args 2026-08-04 11:20:25 +02:00
Oli Scherer
9381472c71 Generalize some arguments to allow more kinds of input 2026-08-04 11:20:06 +02:00
Oli Scherer
538f556305 Add reflection method for fetching non-exhaustiveness 2026-08-04 11:20:06 +02:00
Oli Scherer
20d5e784af Add reflection method for field offsets 2026-08-04 11:20:06 +02:00
Oli Scherer
c7fc5f2646 Add reflection method for field names 2026-08-04 11:13:53 +02:00
Jakub Beránek
83604a6575 Rollup merge of #160011 - RalfJung:map_err_info, r=oli-obk
remove InterpError::map_err_info

That would let us replace the backtrace attached to this interpreter error, which we don't want to allow. We also don't need it, all uses of `map_err_info` can be covered by a `inspect_err_info` instead if we reorganize the helper methods around debug-printing interpreter errors a bit.
2026-08-04 11:10:20 +02:00
Ralf Jung
75505d90a9 skip ptr-deref validity checks when validity checking is disabled 2026-08-03 12:11:19 +02:00
Ralf Jung
e0ad63cd35 interpret: skip deref-projection validity checks when they are not needed 2026-08-03 10:17:44 +02:00
Jonathan Brouwer
9956011498 Rollup merge of #160012 - RalfJung:deref-validity, r=oli-obk
miri: ensure validity of references and pointers we dereference and cast

Conceptually, when we evaluate the place expression `*place`, there are two steps that happen:
- perform a (typed) load from `place`, which yields a value of pointer/ref type
- construct a new place from that value

Since this is a typed load, we have to ensure that the value satisfies the validity invariant. We forgot to do that, which led to Miri missing a bunch of UB. This PR fixes that by adding the missing checks.

Triggering this UB is a bit non-trivial: you cannot just store an invalid value into `place` using regular assignment (that would already be caught as part of the assignment). However, you can take a raw pointer to `place` and then use that to mutate the contents of `place` in a way that its validity invariant no longer holds. For example:
```rust
fn main() {
    let mut x = &();
    // Overwrite `x` with null.
    unsafe { (&raw mut x).cast::<usize>().write(0) };
    let _val = *x; //~ERROR: null reference
}
```
This program clearly (IMO) should have UB, and has UB in MiniRust, but Miri accepted that program before this PR. The same applies to references that are invalid because they are unaligned (even if we end up only accessing a 1-aligned field, i.e. the rest of the place expression evaluates just fine), and to references that are invalid because they are not dereferenceable (even if we end up projecting to a zero-sized field, i.e., the rest of the place expression evaluates just fine).

Even raw pointers have this problem: a raw pointer can be invalid if its vtable pointer is wrong, and we did not check that.

And finally, this also affects `Box`, and this is where things get tricky. `Box` derefs are not even present any more in the MIR Miri sees; they have been replaced by derefs of the underlying raw pointer. But that means we have no way to know that we should check all those things. So to fix that I adjusted the ElaborateBoxDerefs to emit a new special kind of cast that's almost a transmute but also checks `Box` validity.

Fixes https://github.com/rust-lang/miri/issues/5226
Fixes https://github.com/rust-lang/unsafe-code-guidelines/issues/617
Cc @rust-lang/opsem
2026-08-01 15:56:37 +02:00
Ralf Jung
907a5620f2 also catch invalid pointers coming into PtrMetadata
test case due to @maxdexh
2026-08-01 09:27:16 +02:00
Ralf Jung
b3724b650e also validate the inputs of casts 2026-08-01 09:27:16 +02:00
Ralf Jung
fcf68f21cf introduce a new CastKind for ElaborateBoxDerefs 2026-08-01 09:27:16 +02:00
Ralf Jung
14d28b13c7 miri: ensure validity of references and pointers we dereference 2026-08-01 09:27:16 +02:00
Jacob Pratt
31e12ee467 Rollup merge of #160124 - scottmcm:simd-count, r=oli-obk
Structurally prevent zero-count `BackendRepr::SimdVector`s

This is already *supposed* to be impossible in layout, but this emphasizes that better.  This PR makes no behaviour changes; just `BackendLaneCount` instead of `u64` in https://doc.rust-lang.org/nightly/nightly-rustc/rustc_abi/enum.BackendRepr.html

Ironically I was inspired to do this as part of [looking at making `Simd<T, 0>` *work*](https://rust-lang.zulipchat.com/#narrow/channel/257879-project-portable-simd/topic/Allow.20N.3D.3D0.3F/with/613345298), but importantly if that's going to happen I think it should be `BackendRepr::Memory` like other ZSTs, *not* a `BackendRepr::ScalarVector` that would need to carry around a useless LLVM value in `OperandValue::Immediate` (where it's not even clear what the LLVM type of that value would be).
2026-07-31 22:25:12 -04:00
Jacob Pratt
f5811a13ed Rollup merge of #160079 - RalfJung:const-atomic, r=folkertdev
make atomic operations const

In a similar vein to https://github.com/rust-lang/rust/issues/159094, it makes sense to make these `const fn` so that code can be shared between compile-time and run-time even if the runtime version has to deal with concurrency.

This constifies the following:
```rust
// core::sync::atomic

impl AtomicBool {
    pub const fn get_mut(&mut self) -> &mut bool;
    pub const fn from_mut(v: &mut bool) -> &mut Self;
    pub const fn get_mut_slice(this: &mut [Self]) -> &mut [bool];
    pub const fn from_mut_slice(v: &mut [bool]) -> &mut [Self];
    pub const fn load(&self, order: Ordering) -> bool;
    pub const fn store(&self, val: bool, order: Ordering);
    pub const fn swap(&self, val: bool, order: Ordering) -> bool;
    pub const fn compare_and_swap(&self, current: bool, new: bool, order: Ordering) -> bool;
    pub const fn compare_exchange(
        &self,
        current: bool,
        new: bool,
        success: Ordering,
        failure: Ordering,
    ) -> Result<bool, bool>;
    pub const fn compare_exchange_weak(
        &self,
        current: bool,
        new: bool,
        success: Ordering,
        failure: Ordering,
    ) -> Result<bool, bool>;
    pub const fn fetch_and(&self, val: bool, order: Ordering) -> bool;
    pub const fn fetch_nand(&self, val: bool, order: Ordering) -> bool;
    pub const fn fetch_or(&self, val: bool, order: Ordering) -> bool;
    pub const fn fetch_xor(&self, val: bool, order: Ordering) -> bool;
    pub const fn fetch_not(&self, order: Ordering) -> bool;
}

impl AtomicPtr<T> {
    pub const fn get_mut(&mut self) -> &mut *mut T;
    pub const fn from_mut(v: &mut *mut T) -> &mut Self;
    pub const fn get_mut_slice(this: &mut [Self]) -> &mut [*mut T];
    pub const fn from_mut_slice(v: &mut [*mut T]) -> &mut [Self];
    pub const fn load(&self, order: Ordering) -> *mut T;
    pub const fn store(&self, ptr: *mut T, order: Ordering);
    pub const fn swap(&self, ptr: *mut T, order: Ordering) -> *mut T;
}

impl Atomic$Int {
    pub const fn get_mut(&mut self) -> &mut $int_type;
    pub const fn from_mut(v: &mut $int_type) -> &mut Self;
    pub const fn get_mut_slice(this: &mut [Self]) -> &mut [$int_type];
    pub const fn from_mut_slice(v: &mut [$int_type]) -> &mut [Self];
    pub const fn load(&self, order: Ordering) -> $int_type;
    pub const fn store(&self, val: $int_type, order: Ordering);
    pub const fn swap(&self, val: $int_type, order: Ordering) -> $int_type;
    pub const fn compare_and_swap(&self,
                                    current: $int_type,
                                    new: $int_type,
                                    order: Ordering) -> $int_type;
    pub const fn compare_exchange(&self,
                                    current: $int_type,
                                    new: $int_type,
                                    success: Ordering,
                                    failure: Ordering) -> Result<$int_type, $int_type>;
    pub const fn compare_exchange_weak(&self,
                                         current: $int_type,
                                         new: $int_type,
                                         success: Ordering,
                                         failure: Ordering) -> Result<$int_type, $int_type>;
    pub const fn fetch_add(&self, val: $int_type, order: Ordering) -> $int_type;
    pub const fn fetch_sub(&self, val: $int_type, order: Ordering) -> $int_type;
    pub const fn fetch_and(&self, val: $int_type, order: Ordering) -> $int_type;
    pub const fn fetch_nand(&self, val: $int_type, order: Ordering) -> $int_type;
    pub const fn fetch_or(&self, val: $int_type, order: Ordering) -> $int_type;
    pub const fn fetch_xor(&self, val: $int_type, order: Ordering) -> $int_type;
    pub const fn fetch_max(&self, val: $int_type, order: Ordering) -> $int_type;
    pub const fn fetch_min(&self, val: $int_type, order: Ordering) -> $int_type;
}

pub const fn fence(order: Ordering);
pub const fn compiler_fence(order: Ordering);
```

However, this excludes the `fetch_*` and `compare_*` operations on `AtomicPtr`, as that kind of pointer arithmetic is not possible at compile time.

Tracking issue: https://github.com/rust-lang/rust/issues/160078
Cc @rust-lang/wg-const-eval @rust-lang/opsem
2026-07-31 22:25:11 -04:00
Jonathan Brouwer
a621f18f80 Rollup merge of #160085 - mejrs:bye_lints, r=JonathanBrouwer,mu001999
Remove various superfluous lint attributes
2026-07-31 16:02:32 +02:00
Scott McMurray
6da3077db9 Structurally prevent zero-count BackendRepr::SimdVectors
This is already *supposed* to be impossible in layout, but this emphasizes that better.

Ironically I was inspired to do this as part of looking at making `Simd<T, 0>` *work*, but importantly if that's going to happen I think it should be `BackendRepr::Memory` like other ZSTs, *not* a `BackendRepr::ScalarVector` that would need to carry around a useless LLVM value in `OperandValue::Immediate` (where it's not even clear what the LLVM type of that value would be).
2026-07-30 19:48:24 -07:00
Ralf Jung
d0da15a955 interpret: rename validate_operand → validate_place 2026-07-30 17:27:36 +02:00
Esteban Küber
2713b5fc27 Account for for await in move errors 2026-07-30 12:47:59 +00:00
mejrs
c955c638ca Remove various superfluous lint attributes 2026-07-29 13:01:52 +02:00
Ralf Jung
aa1b09a99d make atomic operations const 2026-07-28 18:46:57 +02:00
Nicholas Nethercote
ff161b18b5 Many "predicate"-to-"clause" renamings
Lots of names weren't changed to account for the introduction of the
`Clause` type as a specialized form of `Predicate`, presumably because
it was tedious. This commit converts as many of them as I could find.
It's mostly very tedious renamings of types, functions, variables, and
queries. The more interesting ones are a couple of files
(`predicates_of.rs`, `impossible_predicates.rs`) and a MIR pass
(`ImpossiblePredicates`).
2026-07-28 11:48:45 +10:00
Ralf Jung
87e2c0b041 remove InterpError::map_err_info 2026-07-27 15:05:45 +02:00
Oli Scherer
e9ad00c5af Remove 'static requirement on try_as_dyn 2026-07-23 10:59:01 +02:00
Guillaume Gomez
f791d707db Rollup merge of #159504 - RalfJung:generic-in-pat, r=BoxyUwU,Nadrieril
Abort const-eval queries early when there are generics in the type

Since [recently](https://github.com/rust-lang/rust/pull/156977), const validation has a `has_param` check, which means ConstToPat also implicitly has that check. But it seems better to check this again explicitly here rather then rely on an undocumented property of some other component. The new test behaves the same with or without the PR.

I recommend hiding whitespace difference, since rustfmt re-indendet a bunch of stuff.
Fixes https://github.com/rust-lang/rust/issues/150296
r? @BoxyUwU
2026-07-23 00:29:23 +02:00