Commit Graph

328 Commits

Author SHA1 Message Date
Haoran Wang c328459c20 Clarify "infinite size" in cyclic-type diagnostic refers to the type name 2026-05-23 12:53:44 +08:00
danieljofficial 3429bdf127 add issue links and bless 2026-05-13 15:03:47 +01:00
danieljofficial b502d55288 move closures related ui tests into its folder 2026-05-13 14:03:45 +01:00
Jonathan Brouwer 01cccf92dc Rollup merge of #156301 - TaKO8Ki:fix-156299-closure-receiver-suggestion-ice, r=wesleywiser
Avoid ICE when suggesting as_ref for ill-typed closure receivers

Fixes rust-lang/rust#156299

When building mismatch suggestions, `can_use_as_ref` may inspect the receiver of a method call that is itself an ill-typed closure expression. In that recovery path, the receiver may not have a recorded type in `TypeckResults`.

Use `expr_ty_opt` instead of `expr_ty` so the optional `as_ref()` suggestion is skipped when the receiver type is unavailable.
2026-05-08 16:18:42 +02:00
Takayuki Maeda 83d39fbaa3 add regression test for closure receiver suggestion ICE 2026-05-08 15:05:49 +09:00
Jonathan Brouwer e56e388df9 Rollup merge of #156280 - TaKO8Ki:issue-155893-closure-return-ice, r=lqd
Add regression test for closure return ICE

Fixes rust-lang/rust#155893
2026-05-07 22:44:04 +02:00
Takayuki Maeda ddddb4d109 add regression test for closure return ICE 2026-05-07 23:33:00 +09:00
Jonathan Brouwer cb4bb14237 Rollup merge of #155877 - qaijuang:fix-155727-fnmut-diagnostic, r=wesleywiser
Avoid misleading return-type note for foreign `Fn` callees

Fixes rust-lang/rust#155727.

The issue occurred because the code that emitted the `Fn`/`FnMut` suggestion only avoided the return-type fallback when it could point at a local callee argument. The local case from rust-lang/rust#125325 was fixed in rust-lang/rust#126226, but nested cross-crate cases could still suggest changing the enclosing function's return type even though the relevant `Fn` requirement came from the callee argument.

This is also the broader diagnostic shape discussed in rust-lang/rust#119985.

This PR checks the instantiated callee predicates for an exact `Fn` bound
on the closure argument, and extends `wrong-closure-arg-suggestion-125325` with cross-crate function and method cases for that path.
2026-04-29 23:51:40 +02:00
SynapLink b19b0e5629 tests: mark simple UI tests as check-pass 2026-04-28 23:10:46 +02:00
Qai Juang 4de0d24c3f borrowck: avoid misleading return-type note for foreign Fn callees 2026-04-27 08:24:04 -04:00
yukang 7616881793 Avoid misleading closure return type note 2026-04-27 18:57:50 +08:00
yukang 5e00484c38 Avoid misleading closure return type note 2026-04-25 16:27:05 +08:00
yukang eedf870de9 Suggest returning a reference for unsized place from a closure 2026-04-19 13:15:41 +08:00
Jacob Adam 028e00b495 Add a test for an ICE with closure captures in erroneous code 2026-04-04 17:59:54 +01:00
bors 12ab1cf1fd Auto merge of #154456 - chenyukang:yukang-fix-154434-unused-assignments, r=estebank
Label overwritten for never read assignments



Fixes rust-lang/rust#154434
Closes rust-lang/rust#144079
2026-04-01 02:38:14 +00:00
yukang 86796ddaa0 Label overwritten assignments for never read assignments 2026-03-31 20:44:11 +08:00
cyrgani d24ebcb47c rename some issues-* tests 2026-03-30 20:40:15 +00:00
cyrgani 9b0be7857a allow incomplete_features in most UI tests 2026-03-21 20:10:07 +00:00
mu001999 15032749bc Emit ForbiddenBound fatally if meeting complex bound 2026-03-17 21:06:06 +08:00
Matthias Krüger 81549a3cc1 Rollup merge of #153881 - estebank:const-e0308, r=Kivooeo
Provide more context on type errors in const context

- On `const` and `static` point at the type (like we do for let bindings)
- On fn calls, point at const parameter in fn definition
- On type, point at const parameter in type definition
- On array type lengths, explain that array length is always `usize`
- On enum variant discriminant, mention `repr`
- On default field value using type parameter, provide more context (Fix rust-lang/rust#147748)
2026-03-15 16:52:44 +01:00
Esteban Küber 8e22bcd7a7 [tiny] Tweak "use of Deref" suggestion message
Surround trait name with backticks.
2026-03-15 02:17:11 +00:00
Esteban Küber 87d8f5885b Provide more context on type errors in const context
- On `const` and `static` point at the type (like we do for let bindings)
- On fn calls, point at const parameter in fn definition
- On type, point at const parameter in type definition
- On array type lengths, explain that array length is always `usize`
- On enum variant discriminant, mention `repr`
2026-03-14 20:13:43 +00:00
Jonathan Brouwer 0381e24e37 Rollup merge of #152165 - JohnTitor:issue-151579, r=lcnr
Normalize capture place `ty`s to prevent ICE

Fixes rust-lang/rust#151579
Fixes https://github.com/rust-lang/rust/issues/120811
r? @lcnr
2026-02-28 12:52:53 +01:00
Yuki Okushi 82b09a177c Normalize the whole Place 2026-02-17 20:32:17 +09:00
Kivooeo 964b63f42e if let guard stabilize 2026-02-16 12:24:15 +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
Alan Egerton c43a33eec7 Feed ErrorGuaranteed from late lifetime resolution to RBV
If late lifetime resolution fails for whatever reason, forward to RBV
the guarantee that an error was emitted - thereby eliminating the need
for a "hack" to suppress subsequent/superfluous error diagnostics.
2026-02-13 16:24:39 +00:00
Jonatan Lindh fb55b5dcf3 diagnostics: fix ICE in closure signature mismatch
This fixes the ICE by renaming conflicting arguments in the diagnostic.
2026-02-08 13:32:50 +01:00
许杰友 Jieyou Xu (Joe) 7b821d1752 Rollup merge of #151278 - estebank:issue-108894, r=davidtwco
Provide more context on trait bounds being unmet due to imperfect derive

When encountering a value that has a borrow checker error where the type was previously moved, when suggesting cloning verify that it is not already being derived. If it is, explain why the `derive(Clone)` doesn't apply:

```
note: if `TypedAddress<T>` implemented `Clone`, you could clone the value
  --> $DIR/derive-clone-implicit-bound.rs:6:1
   |
LL | #[derive(Clone, Copy)]
   |          ----- derived `Clone` adds implicit bounds on type parameters
LL | pub struct TypedAddress<T>{
   | ^^^^^^^^^^^^^^^^^^^^^^^^-^
   | |                       |
   | |                       introduces an implicit `T: Clone` bound
   | consider manually implementing `Clone` for this type
...
LL |         let old = self.return_value(offset);
   |                                     ------ you could clone this value
```

When encountering a bound coming from a derive macro, suggest manual impl of the trait.

Use the span for the specific param when adding bounds in builtin derive macros, so the diagnostic will point at them as well as the derive macro itself.

```
note: required for `Id<SomeNode>` to implement `PartialEq`
  --> $DIR/derive-implicit-bound.rs:5:10
   |
LL | #[derive(PartialEq, Eq)]
   |          ^^^^^^^^^
LL | pub struct Id<T>(PhantomData<T>);
   |               - unsatisfied trait bound introduced in this `derive` macro
   = help: consider manually implementing `PartialEq` to avoid undesired bounds
```

Mention that the trait could be manually implemented in E0599.

Fix rust-lang/rust#108894. Address rust-lang/rust#143714. Address #rust-lang/rust#146515 (but ideally would also suggest constraining the fn bound correctly as well).
2026-02-06 10:25:43 +08:00
Stuart Cook 393ce4ba38 Rollup merge of #151895 - Delta17920:move-ui-tests-batch, r=Kivooeo
Move UI tests batch

moved few tests

r? @Kivooeo
2026-02-02 10:28:33 +11:00
Esteban Küber dffec20dee Tweak help to unify formatting and wording 2026-02-01 18:20:31 +00:00
Esteban Küber 879633f97b Change note to help 2026-02-01 18:20:31 +00:00
delta17920 a7dea5504b rename various regression tests 2026-02-01 04:09:25 +00:00
zedddie 086dc0258b clean up some tests 2026-01-31 14:01:54 +01:00
zedddie 4df307acc1 move some tests 2026-01-31 14:00:12 +01:00
delta17920 85ae47f83e moved 7 tests to organized locations 2026-01-31 06:44:24 +00:00
Esteban Küber 4a27be6972 Do not mention -Zmacro-backtrace for std macros that are a wrapper around a compiler intrinsic 2026-01-26 17:34:31 +00:00
Stuart Cook bd909dd5c3 Rollup merge of #151207 - always-discriminate-prelim, r=Zalathar
Preliminary match/capture test cleanup for PR 150681

Review for rust-lang/rust#150681 requested that this cleanup gets extracted to a separate PR.

r? @Zalathar
2026-01-17 11:47:19 +11:00
Maja Kądziołka ee1a6f4e88 match in closure: capture non_exhaustive even if defined in current crate 2026-01-15 18:35:11 +01:00
Maja Kądziołka 628bacac33 add tests for emergent behavior of partial captures 2026-01-15 17:53:55 +01:00
Maja Kądziołka 940a48966f non-exhaustive-match.rs: actually test what the comments say 2026-01-15 17:32:45 +01:00
Maja Kądziołka 4e090078b4 capture-enums.rs: get rid of feature gate noise 2026-01-15 17:24:35 +01:00
Maja Kądziołka 8536979cdb Move some match edge case tests
While these test cases were inspired by issue 137467, they aren't
directly related, and better fit into match-edge-cases_2.rs
2026-01-15 17:22:58 +01:00
Heath Dutton🕴️ afe76df79c Don't suggest replacing closure parameter with type name
When a closure has an inferred parameter type like `|ch|` and the
expected type differs in borrowing (e.g., `char` vs `&char`), the
suggestion code would incorrectly suggest `|char|` instead of the
valid `|ch: char|`.

This happened because the code couldn't walk explicit `&` references
in the HIR when the type is inferred, and fell back to replacing the
entire parameter span with the expected type name.

Fix by only emitting the suggestion when we can properly identify the
`&` syntax to remove.
2026-01-12 18:07:38 -05:00
reddevilmidzy 4578082361 Cleaned up some tests
add comment to closure-move-use-after-move-diagnostic.rs

add comment to missing-operator-after-float.rs

add comment to closure-array-break-length.rs

add comment to box-lifetime-argument-not-allowed.rs

add comment to const-return-outside-fn.rs

add comment to drop-conflicting-impls.rs

add comment to unbalanced-doublequote-2.rs

add comment to borrow-immutable-deref-box.rs

add comment to for-in-const-eval.rs

add comment to borrowck-annotated-static-lifetime.rs

cleaned up cast-rfc0401.rs

add comment to nll-anon-to-static.rs

add comment to cast-to-dyn-any.rs

add comment to missing-associated-items.rs

add comment to enum-discriminant-missing-variant.rs
2025-12-23 21:13:24 +09:00
reddevilmidzy 7ca2eb9c6f moved test 2025-12-23 20:33:37 +09:00
Maja Kądziołka 55bbe0531a Re-bless tests 2025-12-17 20:47:49 +01:00
Maja Kądziołka ae19274fb2 Add more variations from the PR thread 2025-12-17 20:47:48 +01:00
Maja Kądziołka d05b1d76f3 Add a test for deref projections in new pattern capture behavior 2025-12-17 20:47:48 +01:00
Maja Kądziołka 3d5d1d72e0 Mark crash 140011 as fixed 2025-12-17 20:47:48 +01:00