38334 Commits

Author SHA1 Message Date
bors
e765b0de03 Auto merge of #141295 - Kivooeo:if-let-guard-stable, r=fee1-dead,est31
Stabilize `if let` guards (`feature(if_let_guard)`)



## Summary

This proposes the stabilization of `if let` guards (tracking issue: rust-lang/rust#51114, RFC: rust-lang/rfcs#2294). This feature allows `if let` expressions to be used directly within match arm guards, enabling conditional pattern matching within guard clauses.

## What is being stabilized

The ability to use `if let` expressions within match arm guards.

Example:

```rust
enum Command {
    Run(String),
    Stop,
    Pause,
}

fn process_command(cmd: Command, state: &mut String) {
    match cmd {
        Command::Run(name) if let Some(first_char) = name.chars().next() && first_char.is_ascii_alphabetic() => {
            // Both `name` and `first_char` are available here
            println!("Running command: {} (starts with '{}')", name, first_char);
            state.push_str(&format!("Running {}", name));
        }
        Command::Run(name) => {
            println!("Cannot run command '{}'. Invalid name.", name);
        }
        Command::Stop if state.contains("running") => {
            println!("Stopping current process.");
            state.clear();
        }
        _ => {
            println!("Unhandled command or state.");
        }
    }
}
```

## Motivation

The primary motivation for `if let` guards is to reduce nesting and improve readability when conditional logic depends on pattern matching. Without this feature, such logic requires nested `if let` statements within match arms:

```rust
// Without if let guards
match value {
    Some(x) => {
        if let Ok(y) = compute(x) {
            // Both `x` and `y` are available here
            println!("{}, {}", x, y);
        }
    }
    _ => {}
}

// With if let guards
match value {
    Some(x) if let Ok(y) = compute(x) => {
        // Both `x` and `y` are available here
        println!("{}, {}", x, y);
    }
    _ => {}
}
```

## Implementation and Testing

The feature has been implemented and tested comprehensively across different scenarios:

### Core Functionality Tests

**Scoping and variable binding:**
- [`scope.rs`](5796073c13/tests/ui/rfcs/rfc-2294-if-let-guard/scope.rs) - Verifies that bindings created in `if let` guards are properly scoped and available in match arms
- [`shadowing.rs`](5796073c13/tests/ui/rfcs/rfc-2294-if-let-guard/shadowing.rs) - Tests that variable shadowing works correctly within guards
- [`scoping-consistency.rs`](5796073c13/tests/ui/rfcs/rfc-2294-if-let-guard/scoping-consistency.rs) - Ensures temporaries in guards remain valid for the duration of their match arms

**Type system integration:**
- [`type-inference.rs`](5796073c13/tests/ui/rfcs/rfc-2294-if-let-guard/type-inference.rs) - Confirms type inference works correctly in `if let` guards  
- [`typeck.rs`](5796073c13/tests/ui/rfcs/rfc-2294-if-let-guard/typeck.rs) - Verifies type mismatches are caught appropriately

**Pattern matching semantics:**
- [`exhaustive.rs`](5796073c13/tests/ui/rfcs/rfc-2294-if-let-guard/exhaustive.rs) - Validates that `if let` guards are correctly handled in exhaustiveness analysis
- [`move-guard-if-let.rs`](5796073c13/tests/ui/rfcs/rfc-2294-if-let-guard/move-guard-if-let.rs) and [`move-guard-if-let-chain.rs`](5796073c13/tests/ui/rfcs/rfc-2294-if-let-guard/move-guard-if-let-chain.rs) - Test that conditional moves in guards are tracked correctly by the borrow checker

### Error Handling and Diagnostics

- [`warns.rs`](5796073c13/tests/ui/rfcs/rfc-2294-if-let-guard/warns.rs) - Tests warnings for irrefutable patterns and unreachable code in guards
- [`parens.rs`](5796073c13/tests/ui/rfcs/rfc-2294-if-let-guard/parens.rs) - Ensures parentheses around `let` expressions are properly rejected
- [`macro-expanded.rs`](5796073c13/tests/ui/rfcs/rfc-2294-if-let-guard/macro-expanded.rs) - Verifies macro expansions that produce invalid constructs are caught
- [`guard-mutability-2.rs`](5796073c13/tests/ui/rfcs/rfc-2294-if-let-guard/guard-mutability-2.rs) - Tests mutability and ownership violations in guards
- [`ast-validate-guards.rs`](5796073c13/tests/ui/rfcs/rfc-2497-if-let-chains/ast-validate-guards.rs) - Validates AST-level syntax restrictions

### Drop Order and Temporaries

**Key insight:** Unlike `let_chains` in regular `if` expressions, `if let` guards do not have drop order inconsistencies because:
1. Match guards are clearly scoped to their arms
2. There is no "else block" equivalent that could cause temporal confusion

- [`drop-order.rs`](5796073c13/tests/ui/rfcs/rfc-2294-if-let-guard/drop-order.rs) - Check drop order of temporaries create in match guards
- [`compare-drop-order.rs`](aef3f5fdf0/tests/ui/rfcs/rfc-2294-if-let-guard/compare-drop-order.rs) - Compares drop order between `if let` guards and nested `if let` in match arms, confirming they behave identically across all editions
- rust-lang/rust#140981 - A complicated drop order test involved `let chain` was made by @est31
- [`drop-order-comparisons-let-chains.rs`](902b4d2878/tests/ui/drop/drop-order-comparisons-let-chains.rs) - Compares drop order between `let chains` in `if let guard` and regular `if` expressions
- [`if-let-guards.rs`](5650d716e0/tests/ui/drop/if-let-guards.rs) - Test correctness of drop order for bindings and temporaries
- [`if-let-guards-2`](3a6c8c8f3d/tests/ui/drop/if-let-guards-2.rs) - The same test as above but more comprehensive and tests more interactions between different features and their drop order, checking that drop order is correct, created by @traviscross 

## Edition Compatibility

This feature stabilizes on all editions, unlike `let chains` which was limited to edition 2024. This is safe because:

1. `if let` guards don't suffer from the drop order issues that affected `let chains` in regular `if` expressions
2. The scoping is unambiguous - guards are clearly tied to their match arms
3. Extensive testing confirms identical behavior across all editions

## Interactions with Future Features

The lang team has reviewed potential interactions with planned "guard patterns" and determined that stabilizing `if let` guards now does not create obstacles for future work. The scoping and evaluation semantics established here align with what guard patterns will need.

## Unresolved Issues

- [x] - rust-lang/rust#140981
- [x] - added tests description by @jieyouxu request
- [x] - Concers from @scottmcm about stabilizing this across all editions
- [x] - check if drop order in all edition when using `let chains` inside `if let` guard is the same
- [x] - interactions with guard patters
- [x] - pattern bindings drops before guard bindings https://github.com/rust-lang/rust/pull/143376
- [x] - documentaion (https://github.com/rust-lang/reference/pull/1957)
- [ ] (non-blocking) add tests for [this](https://github.com/rust-lang/rust/issues/145237) and [this](https://github.com/rust-lang/rust/pull/141295#issuecomment-3173059821)

---

**Related:**
- Tracking Issue: rust-lang/rust#51114  
- RFC: rust-lang/rfcs#2294
- Documentation PR: https://github.com/rust-lang/reference/pull/1957
2026-02-18 20:49:50 +00:00
bors
e3678c13df Auto merge of #152677 - ehuss:bootstrap-json-target-spec, r=davidtwco,jieyouxu
Support JSON target specs in bootstrap

JSON target specs were destabilized in https://github.com/rust-lang/rust/pull/150151 and https://github.com/rust-lang/rust/pull/151534. However, this broke trying to build rustc itself with a JSON target spec. This is because in a few places bootstrap is manually calling `rustc` without the ability for the user to provide additional flags (primarily, `-Zunstable-options` to enable JSON targets).

There's a few different ways to fix this. One would be to change these calls to `rustc` to include flags provided by the user (such as `RUSTFLAGS_NOT_BOOTSTRAP`). Just to keep things simple, this PR proposes to just unconditionally pass `-Zunstable-options`.

Another consideration here is how maintainable this is. A possible improvement here would be to have a function somewhere (BootstrapCommand, TargetSelection, free function) that would handle appropriately adding the `--target` flag. For example, that's what cargo does in [`CompileKind::add_target_arg`](592058c7ce/src/cargo/core/compiler/compile_kind.rs (L144-L154)).

I have only tested building the compiler and a few tools like rustdoc. I have not tested doing things like building other tools, running tests, etc.

This would be much easier if there was a Docker image for testing the use case of building rustc with a custom target spec (and even better if that ran in CI).

After the next beta branch, using target JSON specs will become more cumbersome because target specs with the `.json` extension will now require passing `-Zjson-target-spec` (from
https://github.com/rust-lang/cargo/pull/16557). This does not affect target specs without the `.json` extension (such as those from RUST_TARGET_PATH). From my testing, it should be sufficient to pass `CARGOFLAGS_NOT_BOOTSTRAP="-Zjson-target-spec"`. I think that should be fine, since this is not a particularly common use case AFAIK. We could extend bootstrap to auto-detect if the target is a file path, and pass `-Zjson-target-spec` appropriately. I tried something similar in f0bdd35483, which could be adapted if desired.

It would be nice if all of this is documented somewhere. https://rustc-dev-guide.rust-lang.org/building/new-target.html does not really say how to build the compiler with a custom json target.

Fixes https://github.com/rust-lang/rust/issues/151729
2026-02-17 17:35:50 +00:00
bors
2b0826915c Auto merge of #152738 - workingjubilee:revert-unsound-ice-patch, r=mati865
Revert "Fix an ICE in the vtable iteration for a trait reference"

The ICE fix appears to be unsound, causing a miscompilation involving `dyn Trait` and `async {}` which induces segfaults in safe Rust code. As the patch only hid an ICE, it does not seem worth the risk.

This addresses the problem in rust-lang/rust#152735 but it may still merit team discussion even if this PR is merged.

This reverts commit 8afd63610b0a261d3c39e24140c6a87d3bff075c, reversing changes made to 19122c03c7bea6bde1dfa5c81fca5810953d9f39.
2026-02-17 09:36:10 +00:00
bors
4871ecef41 Auto merge of #152717 - zetanumbers:fix-152375, r=JonathanBrouwer,petrochenkov,jieyouxu
Fix wrong par_slice implementation

https://github.com/rust-lang/rust/pull/152375#discussion_r2812669219
2026-02-17 06:20:05 +00:00
bors
66a154871b Auto merge of #152739 - Zalathar:rollup-8qaJ8F2, r=Zalathar
Rollup of 11 pull requests

Successful merges:

 - rust-lang/rust#152700 (miri subtree update)
 - rust-lang/rust#152715 (`rust-analyzer` subtree update)
 - rust-lang/rust#151783 (Implement RFC 3678: Final trait methods)
 - rust-lang/rust#152512 (core: Implement feature `float_exact_integer_constants`)
 - rust-lang/rust#152661 (Avoid ICE in From/TryFrom diagnostic under -Znext-solver)
 - rust-lang/rust#152703 (Remove `rustc_query_system`)
 - rust-lang/rust#152206 (misc doc improvements)
 - rust-lang/rust#152664 (Fix mis-constructed `file_span` when generating scraped examples)
 - rust-lang/rust#152698 (Suppress unstable-trait notes under `-Zforce-unstable-if-unmarked`)
 - rust-lang/rust#152727 (`probe_op` silence ambiguity errors if tainted)
 - rust-lang/rust#152728 (Port #![default_lib_allocator] to the new attribute parser)
2026-02-17 02:03:25 +00:00
Stuart Cook
b11bb5e58a
Rollup merge of #152728 - Ozzy1423:default-lib, r=JonathanBrouwer
Port #![default_lib_allocator] to the new attribute parser

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

r? @JonathanBrouwer
2026-02-17 13:02:25 +11:00
Stuart Cook
58e6cd997c
Rollup merge of #152727 - lcnr:method-ambig-err-taint, r=jackh726
`probe_op` silence ambiguity errors if tainted

see the `proc-macro/quote/not-repeatable.rs` test for a case where this is useful

r? types
2026-02-17 13:02:25 +11:00
Stuart Cook
725d9dcae3
Rollup merge of #152698 - Zalathar:zforce, r=jieyouxu
Suppress unstable-trait notes under `-Zforce-unstable-if-unmarked`

- Fixes https://github.com/rust-lang/rust/issues/152692.
---

https://github.com/rust-lang/rust/pull/151036 adds extra diagnostic text (“the nightly-only, unstable trait”) to note when a not-implemented trait is unstable.

However, that extra text is usually unhelpful when building a crate graph with `-Zforce-unstable-if-unmarked` (such as the compiler or stdlib), because *any* trait not explicitly marked stable will be treated as unstable.

(For typical compiler contributors using the stage0 compiler, this fix won't take effect until the next bootstrap beta bump.)
2026-02-17 13:02:24 +11:00
Stuart Cook
85646c8488
Rollup merge of #152664 - Embers-of-the-Fire:fix-152601, r=GuillaumeGomez
Fix mis-constructed `file_span` when generating scraped examples

Fixes rust-lang/rust#152601. Seemingly relative with rust-lang/rust#147399 but I could not reproduce the original ICE.

This PR removes the `file_span` logic from scraped example generation. The original implementation did not read or write items using `file_span`; it only used it to locate a source file, `context.href_from_span`. However, the span was validated against the wrong file, which could trigger ICEs on inputs such as multibyte characters due to an incorrectly constructed span. Since scraped examples do not use the span and the `url` is already given, the safest and simplest fix is to remove it.

Tested against the crate and MCVE documented in the original issue.

P.S. there seems to be some bug when rendering call sites, but since fixing mis-behavior is a change rather than a bug-fix that would be implemented in another PR.
2026-02-17 13:02:24 +11:00
Stuart Cook
c3a4e3c500
Rollup merge of #152206 - tshepang:misc, r=davidtwco
misc doc improvements

These are things I collected as I was looking at code and docs
2026-02-17 13:02:23 +11:00
Stuart Cook
b594957688
Rollup merge of #152703 - nnethercote:rm-rustc_query_system, r=Zalathar
Remove `rustc_query_system`

The end point of the PR sequence rust-lang/rust#152160, rust-lang/rust#152419, rust-lang/rust#152516.

r? @Zalathar
2026-02-17 13:02:23 +11:00
Stuart Cook
120d823efc
Rollup merge of #152661 - BartSimpson001:fix-next-solver-from-ice, r=petrochenkov
Avoid ICE in From/TryFrom diagnostic under -Znext-solver

Fixes rust-lang/rust#152518.

Under `-Znext-solver=globally`, `trait_ref.args` may contain fewer
elements than expected. The diagnostic logic in
`fulfillment_errors.rs` assumed at least two elements and
unconditionally called `type_at(1)`, which could lead to an
index out-of-bounds panic during error reporting.

This change adds a defensive check before accessing the second
argument to avoid the ICE. A UI regression test has been added.
2026-02-17 13:02:22 +11:00
Stuart Cook
30c4baafb3
Rollup merge of #152512 - okaneco:exact_integer, r=tgross35
core: Implement feature `float_exact_integer_constants`

Accepted ACP - https://github.com/rust-lang/libs-team/issues/713#issuecomment-3880122239
Tracking issue - https://github.com/rust-lang/rust/issues/152466

Implement accepted ACP for `MAX_EXACT_INTEGER` and `MIN_EXACT_INTEGER` on `f16`, `f32`, `f64`, and `f128`
Add tests to `coretests/tests/floats/mod.rs`
2026-02-17 13:02:22 +11:00
Stuart Cook
4fc94eb558
Rollup merge of #151783 - mu001999-contrib:impl/final-method, r=fee1-dead
Implement RFC 3678: Final trait methods

Tracking: https://github.com/rust-lang/rust/issues/131179

This PR is based on rust-lang/rust#130802, with some minor changes and conflict resolution.

Futhermore, this PR excludes final methods from the vtable of a dyn Trait.

And some excerpt from the original PR description:
> Implements the surface part of https://github.com/rust-lang/rfcs/pull/3678.
>
> I'm using the word "method" in the title, but in the diagnostics and the feature gate I used "associated function", since that's more accurate.

cc @joshtriplett
2026-02-17 13:02:21 +11:00
Stuart Cook
a38e31898b
Rollup merge of #152715 - lnicola:sync-from-ra, r=lnicola
`rust-analyzer` subtree update

Subtree update of `rust-analyzer` to c97b011f91.

Created using https://github.com/rust-lang/josh-sync.

r? @ghost
2026-02-17 13:02:21 +11:00
Laurențiu Nicola
c97b011f91
Merge pull request #21643 from Shourya742/2026-02-13-migrate-generate-impl-to-use-astnodeedit
migrate generate_impl assist to use AstNodeEdit
2026-02-16 11:11:40 +00:00
Laurențiu Nicola
744b554ce7
Merge pull request #21653 from rust-lang/rustc-pull
minor: Rustc pull update
2026-02-16 09:03:05 +00:00
The rustc-josh-sync Cronjob Bot
0f35173e50 Merge ref '139651428df8' from rust-lang/rust
Pull recent changes from https://github.com/rust-lang/rust via Josh.

Upstream ref: rust-lang/rust@139651428d
Filtered ref: rust-lang/rust-analyzer@3efb0c2db4
Upstream diff: ba284f468c...139651428d

This merge was created using https://github.com/rust-lang/josh-sync.
2026-02-16 04:54:45 +00:00
The rustc-josh-sync Cronjob Bot
4d34bb5680 Prepare for merging from rust-lang/rust
This updates the rust-version file to 139651428df86cf88443295542c12ea617cbb587.
2026-02-16 04:49:19 +00:00
Chayim Refael Friedman
00a9173e57
Merge pull request #21648 from alexanderkjall/borsh-error-compilation-failure
fix smol_str compilation error
2026-02-16
2026-02-15 16:42:02 +00:00
Chayim Refael Friedman
4d29f32409
Merge pull request #21649 from Albab-Hasan/fix/assignment-rhs-never-coercion
fix: use `ExprIsRead::Yes` for rhs of ordinary assignments
2026-02-15 14:16:24 +00:00
bors
3efb0c2db4 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
Albab-Hasan
c0d8e3445f fix: use ExprIsRead::Yes for rhs of ordinary assignments
the rhs of an ordinary assignment `x = *never_ptr` was inferred with
`ExprIsRead::No`, which prevented `NeverToAny` coercion for place
expressions of type `!`. this caused false type mismatches and missing
divergence detection. the destructuring assignment path and let binding
path both correctly use `ExprIsRead::Yes` for the rhs value, since the
value is always consumed (read). this makes the ordinary assignment path
consistent with both.
2026-02-15 17:37:51 +06:00
Chayim Refael Friedman
b03c8420d9
Merge pull request #21647 from Albab-Hasan/fix/contains-explicit-ref-binding-missing-refmut
fix: handle `ref mut` bindings in `contains_explicit_ref_binding`
2026-02-15 11:36:30 +00:00
bors
a23e343452 Auto merge of #152375 - Zoxc:rayon-scope-loops, r=jieyouxu,lqd
Use `scope` for `par_slice` instead of `join`

This uses `scope` instead of nested `join`s in `par_slice` so that each group of items are independent and do not end up blocking on another.
2026-02-15 09:55:40 +00:00
Alexander Kjäll
f9ca8b23d8
gate borsh tests on std also, as they depend on both 2026-02-15 10:28:47 +01:00
bors
7e7aa26900 Auto merge of #151380 - ShoyuVanilla:shallow-resolve-to-root-var, r=lcnr
Shallow resolve ty and const vars to their root vars

Continuation of https://github.com/rust-lang/rust/pull/147193
2026-02-15 03:04:28 +00:00
Albab-Hasan
a78e87329d fix: handle ref mut bindings in contains_explicit_ref_binding
the standalone `contains_explicit_ref_binding` function only checked for
`BindingAnnotation::Ref`, missing `BindingAnnotation::RefMut`. this caused
`let ref mut x = expr` to incorrectly take the coercion path instead of
preserving the exact type of the rhs expression. the method version used
for match arms already handles both `Ref` and `RefMut` correctly.
2026-02-15 04:21:34 +06:00
Alexander Kjäll
3f8230687b
fix smol_str compilation error
the command 'cargo build --no-default-features --features borsh' failed with:
error[E0599]: no function or associated item named 'other' found for struct 'borsh::io::Error' in the current scope
   --> lib/smol_str/src/borsh.rs:33:39
    |
 33 |                 .ok_or_else(|| Error::other("u8::vec_from_reader unexpectedly returned None"))?;
    |                                       ^^^^^ function or associated item not found in 'borsh::io::Error'
    |
2026-02-14 21:06:34 +01:00
bors
e187c30070 Auto merge of #152627 - JonathanBrouwer:rollup-wjHXhK0, r=JonathanBrouwer
Rollup of 8 pull requests

Successful merges:

 - rust-lang/rust#152618 (stdarch subtree update)
 - rust-lang/rust#152001 (mGCA: Validate const literal against expected type)
 - rust-lang/rust#152120 (Don't ICE on layout error in vtable computation)
 - rust-lang/rust#152531 (`proc_macro::bridge`: simplify `ExecutionStrategy` and `DispatcherTrait`)
 - rust-lang/rust#152577 (Port #[rustc_proc_macro_decls] to the new attribute parser.)
 - rust-lang/rust#152570 (Port #[rustc_test_marker] to the attribute parser)
 - rust-lang/rust#152590 (DepGraphQuery: correctly skip adding edges with not-yet-added nodes)
 - rust-lang/rust#152612 (Rename `inline_fluent!` to `msg!`)
2026-02-14 17:56:54 +00:00
Jonathan Brouwer
657b06d655
Rollup merge of #152612 - JonathanBrouwer:rename_to_msg, r=jdonszelmann
Rename `inline_fluent!` to `msg!`

This was the most popular name for the macro as voted in [#t-compiler > Bikeshed the new `inline_fluent!` macro @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/131828-t-compiler/topic/Bikeshed.20the.20new.20.60inline_fluent!.60.20macro/near/572751863)
2026-02-14 18:55:38 +01:00
Jonathan Brouwer
80d0fa75ed
Rollup merge of #152590 - petrochenkov:skipedge, r=mati865
DepGraphQuery: correctly skip adding edges with not-yet-added nodes

Fixes https://github.com/rust-lang/rust/issues/142152.

The current logic already skips some edges, so I'm not sure how critical it is to have *all* the edges recorded, the logic seems to only be used for debug dumping.
Recording all edges requires supporting holes in the `LinkedGraph` data structure, to add nodes and edges out of order, https://github.com/rust-lang/rust/pull/151821 implements that at cost of complicating the data structure.
2026-02-14 18:55:38 +01:00
Jonathan Brouwer
a0ebafff86
Rollup merge of #152570 - Ozzy1423:attr-parse, r=JonathanBrouwer
Port #[rustc_test_marker] to the attribute parser

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

Targets:
Const is for normal tests (const test::TestDescAndFn is inserted before the test fn)
Const/Static/Fn is for custom_test_framework's #[test_case] e.g. tests/ui/custom_test_frameworks/full.rs

r? @JonathanBrouwer

Again I left the use-sites as is since they are early uses.
2026-02-14 18:55:37 +01:00
Jonathan Brouwer
fca52a30a2
Rollup merge of #152577 - Ozzy1423:macro-attr, r=JonathanBrouwer
Port #[rustc_proc_macro_decls] to the new attribute parser.

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

r? @JonathanBrouwer
2026-02-14 18:55:36 +01:00
Jonathan Brouwer
923cfb056f
Rollup merge of #152531 - cyrgani:pm-yet-another-cleanup, r=petrochenkov
`proc_macro::bridge`: simplify `ExecutionStrategy` and `DispatcherTrait`

Also includes another tiny cleanup (functions can only have one return type).
2026-02-14 18:55:36 +01:00
Jonathan Brouwer
030c7bc39b
Rollup merge of #152618 - folkertdev:stdarch-sync-2026-02-14, r=folkertdev
stdarch subtree update

Subtree update of `stdarch` to 1239a9f83a.

Created using https://github.com/rust-lang/josh-sync.

r? @ghost
2026-02-14 18:55:34 +01:00
bors
7b4dcb503a 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
Lukas Wirth
b7d3e7bc4e
Merge pull request #21639 from rust-lang/dependabot/npm_and_yarn/editors/code/qs-6.14.2
build(deps): bump qs from 6.14.1 to 6.14.2 in /editors/code
2026-02-14 07:55:56 +00:00
Lukas Wirth
bbc84d335f
Merge pull request #21644 from Veykril/push-zrzoswxlqlqq
Revert "fix: Stale diagnostics with rust-project.json and rustc JSON"
2026-02-13 18:28:52 +00:00
Lukas Wirth
6d550d341f Revert "fix: Stale diagnostics with rust-project.json and rustc JSON"
This reverts commit 2cefe47e6d55c186b68687bf677bf0d5eb65a922.
2026-02-13 19:19:26 +01:00
bit-aloo
ec1d872aad
migrate generate_impl to use AstNodeEdit 2026-02-13 22:58:11 +05:30
Lukas Wirth
5db21f169d
Merge pull request #21640 from Veykril/push-rutrqvxousks
minor: Remove incorrect warning log
2026-02-13 15:33:46 +00:00
Lukas Wirth
3202861785 Remove incorrect warning log 2026-02-13 16:24:28 +01:00
dependabot[bot]
2bdc080a90
build(deps): bump qs from 6.14.1 to 6.14.2 in /editors/code
Bumps [qs](https://github.com/ljharb/qs) from 6.14.1 to 6.14.2.
- [Changelog](https://github.com/ljharb/qs/blob/main/CHANGELOG.md)
- [Commits](https://github.com/ljharb/qs/compare/v6.14.1...v6.14.2)

---
updated-dependencies:
- dependency-name: qs
  dependency-version: 6.14.2
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
2026-02-13 11:43:39 +00:00
Lukas Wirth
797b1f61e5
Merge pull request #21609 from Veykril/push-qknzslvkmtyt
internal: Use rayon for proc-macro loading
2026-02-13 11:33:11 +00:00
cyrgani
72f3833a9a inline SameThread and CrossThread 2026-02-13 11:24:50 +00:00
Lukas Wirth
7d3705a43f internal: Use rayon for proc-macro loading 2026-02-13 12:24:47 +01:00
Shoyu Vanilla (Flint)
4dba1b4042
Merge pull request #21633 from ChayimFriedman2/proc-macro-is-not-fn
fix: Do not resolve proc macros in value ns (as functions), only in macro ns, outside their defining crate
2026-02-13 08:47:17 +00:00
Shoyu Vanilla (Flint)
50863f4113
Merge pull request #21636 from Shourya742/2026-02-13-remove-edit-in-place-in-migrated-handlers
Remove mutable edit in place with edit::AstNodeEdit in migrated  assist handlers
2026-02-13 08:45:05 +00:00
bit-aloo
eef6d70d1d
remove edit_in_place from generate_new assist with edit::AstNodeEdit 2026-02-13 13:14:06 +05:30