Commit Graph

277 Commits

Author SHA1 Message Date
Ralf Jung
fcf68f21cf introduce a new CastKind for ElaborateBoxDerefs 2026-08-01 09:27:16 +02:00
Folkert de Vries
76298612b0 fix simd tests that use without_provenance{_mut} 2026-07-20 13:10:10 +02:00
Scott McMurray
b098a472b4 Better comment the struct used by {read,write}_unaligned 2026-07-11 17:20:43 -07:00
Peter Jaszkowiak
b3c94df68b introduce Step::forward/backward_overflowing
and optimize the RangeInclusive iterator implementation with them

This changes the `exhausted` field to represent an overflow flag
for the bounds, essentially acting as an extra bit for `Idx`.
This was found to enable optimizations previously only applicable
to the exclusive-ended Range type.

- change end_bound to return Excluded(start) when exhausted
- add contains to tests
- make into_bounds panic when exhausted
  matches From<legacy::RangeInclusive<T>> for RangeInclusive<T>
- add tests for new Step impls
2026-07-06 18:30:20 -06:00
Scott McMurray
3a9c74d361 Implement ptr::{read,write}_aligned via repr(packed) 2026-06-25 20:11:29 -07:00
Scott McMurray
0fc6688ab5 Add a test for current read/write unaligned behaviour 2026-06-25 20:11:29 -07:00
Makai
766428b312 Avoid regression in derive(PartialOrd) for single fieldless variant enums 2026-05-23 20:32:20 +08:00
Makai
c9a560b30d Implement fast path for derive(PartialOrd) 2026-05-21 17:59:41 +08:00
Zalathar
58f8739d55 Migrate mir-opt tests from PreCodegen to runtime-optimized
MIR dumped before/after the dummy `PreCodegen` pass should be identical to MIR
dumped after the phase transition to `runtime-optimized`.
2026-05-13 15:40:23 +10:00
Scott McMurray
c468ee3386 Simplify raw_eq to Transmute+Eq for sizes with primitives 2026-05-10 19:28:57 -07:00
Scott McMurray
52ecad938e Add a mir-opt test for == on arrays 2026-05-10 16:53:48 -07:00
Waffle Lapkin
af5e1da690 bless mir-opt tests 2026-05-06 13:57:36 +02:00
Ralf Jung
e402c1edf0 miri: remove retag statements, make typed copies retag implicitly instead 2026-05-02 17:40:33 +02:00
Zalathar
eb6eed4ba7 Make //@ skip-filecheck a normal compiletest directive 2026-04-22 19:55:11 +10:00
Ohad Ravid
5632001f83 Improve copy_prop and GVN mir-opt passes to remove fewer storage calls 2026-04-17 16:55:43 +03:00
Scott McMurray
63d7f8e7ae Initial methods to start on transmute v2 2026-04-10 01:58:56 -07:00
Jynn Nelson
e61842d378 Update mir-opt 64-bit panic-abort tests for Alignment rename
These seem to have been missed when the PR originally merged.
2026-03-30 10:56:57 +00:00
Pavel Grigorenko
f8d658ab81 core: move Alignment from ptr to mem 2026-03-25 16:18:33 +03:00
Scott McMurray
2c6ec51564 Update __rust_[rd]ealloc to take NonNull<u8> instead of *mut u8
Passing null to it is [already UB](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=8dcd25a549c11de72adc94a668277779) anyway.
2026-02-28 23:49:31 -08:00
Oli Scherer
241cd7eeed Prepare NonNull for pattern types 2026-02-24 08:33:15 +00:00
Camille Gillot
4953a8bc12 Constants of primitive types are always deterministic. 2026-02-22 19:43:57 +00:00
Camille Gillot
8c907860f7 Try harder to evaluate constants. 2026-02-22 19:43:57 +00:00
Ana Hobden
7c011122e9 Fix copy_and_clone.rs 2026-02-20 13:25:11 -08:00
bors
ef70767064 Auto merge of #152689 - scottmcm:also-simplify-of-sized-val-alt, r=cjgillot
Simplify `size/align_of_val<T: Sized>` to `size/align_of<T>` instead

This is relevant to things like `Box<[u8; 1024]>` where the drop looks at the `size_of_val` (since obviously it might be DST in general) but where we don't actually need to do that since it's always that same value for the `Sized` type.

(Equivalent to rust-lang/rust#152681, but flipped in the rebase so it can land before rust-lang/rust#152641 instead of depending on it.)
2026-02-19 22:03:50 +00:00
dianqk
05f417cc46 Add assume to single target 2026-02-17 10:11:01 +08:00
dianqk
adf64bd305 Simplify the canonical enum clone branches to a copy statement 2026-02-17 10:11:00 +08:00
Ralf Jung
5e65109f21 add write_box_via_move intrinsic and use it for vec!
This allows us to get rid of box_new entirely
2026-02-16 17:27:40 +01:00
Scott McMurray
649e027b88 mir-simplify {size,align}_of_val::<impl Sized> 2026-02-15 18:05:30 -08:00
Scott McMurray
61d3b2d046 Add a mir-opt test showing the drop for a box of non-DST 2026-02-15 17:51:26 -08:00
bors
2219766af6 Auto merge of #152605 - scottmcm:box-drop-alignment, r=Mark-Simulacrum
Pass alignments through the shim as `Alignment` (not `usize`)

We're using `Layout` on both sides, so might as well skip the transmutes back and forth to `usize`.

The mir-opt test shows that doing so allows simplifying the boxed-slice drop slightly, for example.
2026-02-15 13:38:45 +00:00
bors
f8463896a9 Auto merge of #150681 - meithecatte:always-discriminate, r=JonathanBrouwer,Nadrieril
Make operational semantics of pattern matching independent of crate and module

The question of "when does matching an enum against a pattern of one of its variants read its discriminant" is currently an underspecified part of the language, causing weird behavior around borrowck, drop order, and UB.

Of course, in the common cases, the discriminant must be read to distinguish the variant of the enum, but currently the following exceptions are implemented:

1. If the enum has only one variant, we currently skip the discriminant read.
     - This has the advantage that single-variant enums behave the same way as structs in this regard.
     - However, it means that if the discriminant exists in the layout, we can't say that this discriminant being invalid is UB. This makes me particularly uneasy in its interactions with niches – consider the following example ([playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=5904a6155cbdd39af4a2e7b1d32a9b1a)), where miri currently doesn't detect any UB (because the semantics don't specify any):

        <details><summary>Example 1</summary>

        ```rust
        #![allow(dead_code)]
        use core::mem::{size_of, transmute};
        
        #[repr(u8)]
        enum Inner {
            X(u8),
        }
        
        enum Outer {
            A(Inner),
            B(u8),
        }
        
        fn f(x: &Inner) {
            match x {
                Inner::X(v) => {
                    println!("{v}");
                }
            }
        }
        
        fn main() {
            assert_eq!(size_of::<Inner>(), 2);
            assert_eq!(size_of::<Outer>(), 2);
            let x = Outer::B(42);
            let y = &x;
            f(unsafe { transmute(y) });
        }
        ```

      </details>

2. For the purpose of the above, enums with marked with `#[non_exhaustive]` are always considered to have multiple variants when observed from foreign crates, but the actual number of variants is considered in the current crate.
    - This means that whether code has UB can depend on which crate it is in: https://github.com/rust-lang/rust/issues/147722
    - In another case of `#[non_exhaustive]` affecting the runtime semantics, its presence or absence can change what gets captured by a closure, and by extension, the drop order: https://github.com/rust-lang/rust/issues/147722#issuecomment-3674554872
    - Also at the above link, there is an example where removing `#[non_exhaustive]` can cause borrowck to suddenly start failing in another crate.
3. Moreover, we currently make a more specific check: we only read the discriminant if there is more than one *inhabited* variant in the enum.
    - This means that the semantics can differ between `foo<!>`, and a copy of `foo` where `T` was manually replaced with `!`: rust-lang/rust#146803
    - Moreover, due to the privacy rules for inhabitedness, it means that the semantics of code can depend on the *module* in which it is located.
    - Additionally, this inhabitedness rule is even uglier due to the fact that closure capture analysis needs to happen before we can determine whether types are uninhabited, which means that whether the discriminant read happens has a different answer specifically for capture analysis.
    - For the two above points, see the following example ([playground](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=a07d8a3ec0b31953942e96e2130476d9)):

        <details><summary>Example 2</summary>

        ```rust
        #![allow(unused)]
        
        mod foo {
            enum Never {}
            struct PrivatelyUninhabited(Never);
            pub enum A {
                V(String, String),
                Y(PrivatelyUninhabited),
            }
            
            fn works(mut x: A) {
                let a = match x {
                    A::V(ref mut a, _) => a,
                    _ => unreachable!(),
                };
                
                let b = match x {
                    A::V(_, ref mut b) => b,
                    _ => unreachable!(),
                };
            
                a.len(); b.len();
            }
            
            fn fails(mut x: A) {
                let mut f = || match x {
                    A::V(ref mut a, _) => (),
                    _ => unreachable!(),
                };
                
                let mut g = || match x {
                    A::V(_, ref mut b) => (),
                    _ => unreachable!(),
                };
            
                f(); g();
            }
        }
        
        use foo::A;
        
        fn fails(mut x: A) {
            let a = match x {
                A::V(ref mut a, _) => a,
                _ => unreachable!(),
            };
            
            let b = match x {
                A::V(_, ref mut b) => b,
                _ => unreachable!(),
            };
        
            a.len(); b.len();
        }
        
        
        fn fails2(mut x: A) {
            let mut f = || match x {
                A::V(ref mut a, _) => (),
                _ => unreachable!(),
            };
            
            let mut g = || match x {
                A::V(_, ref mut b) => (),
                _ => unreachable!(),
            };
        
            f(); g();
        }
        ```

        </details>

In light of the above, and following the discussion at rust-lang/rust#138961 and rust-lang/rust#147722, this PR ~~makes it so that, operationally, matching on an enum *always* reads its discriminant.~~ introduces the following changes to this behavior:

 - matching on a `#[non_exhaustive]` enum will always introduce a discriminant read, regardless of whether the enum is from an external crate
 - uninhabited variants now count just like normal ones, and don't get skipped in the checks

As per the discussion below, the resolution for point (1) above is that it should land as part of a separate PR, so that the subtler decision can be more carefully considered.

Note that this is a breaking change, due to the aforementioned changes in borrow checking behavior, new UB (or at least UB newly detected by miri), as well as drop order around closure captures. However, it seems to me that the combination of this PR with rust-lang/rust#138961 should have smaller real-world impact than rust-lang/rust#138961 by itself.

Fixes rust-lang/rust#142394 
Fixes rust-lang/rust#146590
Fixes rust-lang/rust#146803 (though already marked as duplicate)
Fixes parts of rust-lang/rust#147722
Fixes rust-lang/miri#4778

r? @Nadrieril @RalfJung 

@rustbot label +A-closures +A-patterns +T-opsem +T-lang
2026-02-14 12:53:09 +00:00
Scott McMurray
774268afc1 Pass alignments through the shim as Alignment (not usize)
We're using `Layout` on both sides, so might as well skip the transmutes back and forth to `usize`.

The mir-opt test shows that doing so allows simplifying the boxed-slice drop slightly, for example.
2026-02-14 01:39:16 -08:00
Shoyu Vanilla
f5a8b86695 WF check lifetime bounds for locals with type params 2026-02-13 00:33:47 +09:00
dianqk
9c029d2102 GVN: Do not unify dereferences if they are references 2026-02-04 21:55:57 +08:00
dianqk
bc2bf6b544 GVN: Do not introduce new dereferences if they borrow from non-SSA locals 2026-02-04 21:55:54 +08:00
Scott McMurray
9288c208a2 Adjust Alignment to emphasize that we don't look at its field 2026-01-25 17:24:45 -08:00
Scott McMurray
929e280973 Update ptr::Alignment to go through transmuting 2026-01-25 17:19:28 -08:00
Pavel Grigorenko
bc0cce1595 ptr_aligment_type: add more APIs 2026-01-20 17:15:50 +03:00
Zalathar
2df2c72d7a Ignore #[doc(hidden)] items when computing trimmed paths 2026-01-19 12:27:27 +11:00
Maja Kądziołka
af302a67fd discriminant reads: make semantics independent of module/crate 2026-01-15 19:12:13 +01:00
Scott McMurray
5932078c79 Stop emitting UbChecks on every Vec→Slice
Spotted this in PR148766's test changes.  It doesn't seem like this ubcheck would catch anything useful; let's see if skipping it helps perf.
2026-01-08 17:14:02 -08:00
Deadbeef
a913065d80 fix rustfmt and bless tidy/tests 2026-01-01 19:17:11 -05:00
Deadbeef
47864e80cb address review comments; fix CI 2026-01-01 19:17:11 -05:00
Waffle Lapkin
6b4f4f57e7 bless a mir-opt and a miri tests 2025-12-26 22:02:19 +01:00
Camille GILLOT
f59dfc1a4a Maintain a graph of fulfilled conditions. 2025-11-16 01:38:16 +00:00
Scott McMurray
e5803fceed Move into_try_type to a free function 2025-11-13 19:53:02 -08:00
Scott McMurray
86c3ba754a Implement the alternative try desugaring 2025-11-09 04:09:10 -08:00
Camille Gillot
5dfbf67f94 Replace NullOp::SizeOf and NullOp::AlignOf by lang items. 2025-10-23 00:38:28 +00:00
dianqk
afff0502a6 GVN: Preserve derefs at terminators that cannot write to memory 2025-10-16 23:03:05 +08:00
dianqk
a673575b24 mir-opt: Simplify trivial constants in SimplifyConstCondition 2025-10-16 21:30:06 +08:00