2712 Commits

Author SHA1 Message Date
mejrs 48fe89f994 add default field values to diagnostic FormatArgs 2026-04-26 14:23:53 +02:00
yuk1ty 61ff157bd4 Address custom type implementing Derefs to suggest UFCS clone 2026-04-25 08:46:06 +09:00
yuk1ty f69946ac64 Avoid redundant clone suggestions in borrowck diagnostics 2026-04-24 23:00:28 +09:00
bors 1bfcb284f7 Auto merge of #155392 - WaffleLapkin:alias-termmm, r=BoxyUwU
`AliasTerm` refactor

follow up to https://github.com/rust-lang/rust/pull/154758
tracking issue: https://github.com/rust-lang/rust/issues/154941
2026-04-22 02:57:46 +00:00
Jonathan Brouwer 63b6bd933e Rollup merge of #155425 - nnethercote:rm-dup-Flags-methods, r=nikomatsakis
Remove duplicated `Flags` methods.

The `Flags` trait has two methods: `flags` and `outer_exclusive_binder`. Multiple types impl this trait and then also have duplicate inherent methods with the same names; these are all marked with "Think about removing this" comments. This is left over from things being moved into `rustc_type_ir`.

This commit removes those inherent methods. This requires adding `use Flags` to a number of files.

r? @lcnr
2026-04-21 20:42:51 +02:00
Waffle Lapkin 54abf6d683 remove unused parameter from opt_alias_variances 2026-04-21 12:28:26 +02:00
Jonathan Brouwer eceed7c1c2 Rollup merge of #155549 - nnethercote:rm-lifetimes, r=JohnTitor
Remove some unnecessary lifetimes.

We have a number of structs with more lifetimes than necessary. This commit removes them.

LLM disclosure: I asked Claude Code to check for unnecessary lifetimes in all types with three or more lifetimes, and it produced a list of candidates (half of which were invalid). I did the modifications for the valid cases myself, and found a couple more cases along the way.

r? @JohnTitor
2026-04-20 13:52:07 +02:00
Jonathan Brouwer 6245e371b9 Rollup merge of #154895 - Enselic:user-arg-index, r=jdonszelmann
borrowck: Apply `user_arg_index` nomenclature more broadly

No change in behavior. Only renames and a couple of added comments.

As requested here: https://github.com/rust-lang/rust/pull/154674#discussion_r3027381648

r? jdonszelmann
2026-04-20 13:52:04 +02:00
Nicholas Nethercote 5e00b8e7d5 Remove some unnecessary lifetimes.
We have a number of structs with more lifetimes than necessary. This
commit removes them.

LLM disclosure: I asked Claude Code to check for unnecessary lifetimes
in all types with three or more lifetimes, and it produced a list of
candidates (half of which were invalid). I did the modifications for the
valid cases myself, and found a couple more cases along the way.
2026-04-20 19:25:51 +10:00
Nicholas Nethercote e192eec395 Remove duplicated Flags methods.
The `Flags` trait has two methods: `flags` and `outer_exclusive_binder`.
Multiple types impl this trait and then also have duplicate inherent
methods with the same names; these are all marked with "Think about
removing this" comments. This is left over from things being moved into
`rustc_type_ir`.

This commit removes those inherent methods. This requires adding `use
Flags` to a number of files.
2026-04-20 10:13:53 +10:00
Adwin White 6279106e72 fix all errors 2026-04-20 00:18:28 +08:00
Adwin White 3e1e35b5d1 make common normalization routines take Unnormalized 2026-04-20 00:08:14 +08:00
bors 6f109d8a2d Auto merge of #155223 - teor2345:fndef-refactor, r=mati865
Refactor FnDecl and FnSig non-type fields into a new wrapper type





#### Why this Refactor?

This PR is part of an initial cleanup for the [arg splat experiment](https://github.com/rust-lang/rust/issues/153629), but it's a useful refactor by itself.

It refactors the non-type fields of `FnDecl`, `FnSig`, and `FnHeader` into a new packed wrapper types, based on this comment in the `splat` experiment PR:
https://github.com/rust-lang/rust/pull/153697#discussion_r3004637413

It also refactors some common `FnSig` creation settings into their own methods. I did this instead of creating a struct with defaults.

#### Relationship to `splat` Experiment

I don't think we can use functional struct updates (`..default()`) to create `FnDecl` and `FnSig`, because we need the bit-packing for the `splat` experiment.

Bit-packing will avoid breaking "type is small" assertions for commonly used types when `splat` is added.
This PR packs these types:
- ExternAbi: enum + `unwind` variants (38) -> 6 bits
- ImplicitSelfKind: enum variants (5) -> 3 bits
- lifetime_elision_allowed, safety, c_variadic: bool -> 1 bit

#### Minor Changes

Fixes some typos, and applies rustfmt to clippy files that got skipped somehow.
2026-04-18 23:46:37 +00:00
bors b2f1ccf524 Auto merge of #155258 - ShoyuVanilla:eq-constraint, r=lcnr
Make region equality emits Eq constraints



For context, see..

- The box named *coroutine witness Send lifetime requirements now considered by leakcheck* from [this roadmap](https://raw.githubusercontent.com/hexcatnl/roadmap/6f23e638f65249ef02af86a5454275103a71552d/next-solver.svg)
- [#t-types/trait-system-refactor > A question on #251 @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/364551-t-types.2Ftrait-system-refactor/topic/A.20question.20on.20.23251/near/584166935)
- Comments on `rustc_type_ir::predicate::RegionEqPredicate`
2026-04-18 16:04:22 +00:00
Shoyu Vanilla ace3aa319c Make region equality emits Eq constraints 2026-04-18 22:02:01 +09:00
Jonathan Brouwer 0e54c26ac8 Rollup merge of #154843 - nataliakokoromyti:fix-154826-deref-help, r=mati865
Fix conflicting deref move suggestion for LazyLock patterns

fixes rust-lang/rust#154826.

Rust was suggesting *V -> V for let (v,) = *V, which then triggered a follow-up error suggesting the opposite. This patch makes that case suggest borrowing (&*V) instead. Also handles destructuring assignment separately so we don’t emit a misleading &*... fix-it there.
2026-04-18 11:41:31 +02:00
Jacob Pratt e3676b5ab9 Rollup merge of #155428 - lapla-cogito:issue_139089, r=mejrs
Fix ICE in borrowck mutability suggestion with multi-byte ref sigil

Fixes rust-lang/rust#139089

Similarly to rust-lang/rust#155068, this is another instance where span arithmetic did not account for multi-byte characters. (Note that the ampersand in the test is full-width)

This change also results in correcting some inappropriate suggestions.
2026-04-18 00:05:19 -04:00
Jacob Pratt eb52efbbd2 Rollup merge of #155295 - lapla-cogito:issue_154350, r=JohnTitor
Fix misleading "borrowed data escapes outside of function" diagnostic

Fixes rust-lang/rust#154350

Fall back to `report_general_error()` when `fr_name_and_span` is `None` in function items, since the "escaping data" framing is only appropriate for closures capturing outside variables.
2026-04-18 00:05:18 -04:00
lapla a779e054a9 Fix ICE in borrowck mutability suggestion with multi-byte ref sigil 2026-04-18 08:52:11 +09:00
Stuart Cook 350e0d7fad Rollup merge of #155095 - lms0806:issue_155030, r=nnethercote
changed the information provided by (mut x) to mut x (Fix 155030)

When trying to change a value without using mut in a for loop, the recommendation for this change is incorrect, so I am correcting it.

resolve: rust-lang/rust#155030
2026-04-17 16:17:54 +10:00
teor dafb6bb801 Refactor FnDecl and FnSig flags into packed structs 2026-04-16 07:08:08 +10:00
nataliakokoromyti 7d680171f0 borrowck: suggest borrowing for destructuring moves from overloaded deref 2026-04-15 00:59:25 -07:00
lms0806 be3f77c7ff resolve : addressing incorrect recommendation methods 2026-04-15 13:23:37 +09:00
lapla b17a3e2f2d Fix misleading "borrowed data escapes outside of function" diagnostic 2026-04-15 00:35:52 +09:00
Jacob Adam 189305eda3 Skip the closure signature annotation check for tainted bodies
When a coroutine has too many parameters, `check_match` fails and
`construct_error` builds a MIR body with only the coroutine's computed
args (env + resume type). The user-provided signature, however, still
reflects all the parameters the user wrote. `check_signature_annotation`
then tries to `zip_eq` these two mismatched iterators, causing a panic.
Checking `tainted_by_errors` and bailing early avoids this, since
`construct_error` bodies cannot meaningfully be compared against user
annotations.
2026-04-13 11:45:07 +01:00
WilliamTakeshi a133bb8f27 replace @ ty::AliasTy matches with just args 2026-04-11 18:00:32 +00:00
Jonathan Brouwer 9c01701171 Rollup merge of #154646 - m4rch3n1ng:144792-cow-diag, r=davidtwco
Add suggestion to `.to_owned()` used on `Cow` when borrowing

fixes rust-lang/rust#144792
supersedes rust-lang/rust#144925 with the review comments addressed

the tests suggested from @Kivooeo from https://github.com/rust-lang/rust/pull/144925#discussion_r2252703007 didn't work entirely, because these tests failed due to error `[E0308]` mismatched types, which actually already provides a suggestion, that actually makes the code compile:

```
$ cargo check
error[E0308]: mismatched types
 --> src/main.rs:3:5
  |
1 | fn test_cow_suggestion() -> String {
  |                             ------ expected `std::string::String` because of return type
2 |     let os_string = std::ffi::OsString::from("test");
3 |     os_string.to_string_lossy().to_owned()
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `String`, found `Cow<'_, str>`
  |
  = note: expected struct `std::string::String`
               found enum `std::borrow::Cow<'_, str>`
help: try using a conversion method
  |
3 |     os_string.to_string_lossy().to_owned().to_string()
  |                                           ++++++++++++
```

now this suggestion is of course not good or efficient code, but via clippy with `-Wclippy::nursery` lint group you can actually get to the correct code, so i don't think this is too much of an issue:

<details>
<summary>the clippy suggestions</summary>

```
$ cargo clippy -- -Wclippy::nursery
warning: this `to_owned` call clones the `Cow<'_, str>` itself and does not cause its contents to become owned
 --> src/main.rs:3:5
  |
3 |     os_string.to_string_lossy().to_owned().to_string()
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/beta/index.html#suspicious_to_owned
  = note: `#[warn(clippy::suspicious_to_owned)]` on by default
help: depending on intent, either make the `Cow` an `Owned` variant
  |
3 |     os_string.to_string_lossy().into_owned().to_string()
  |                                 ++
help: or clone the `Cow` itself
  |
3 -     os_string.to_string_lossy().to_owned().to_string()
3 +     os_string.to_string_lossy().clone().to_string()
  |
$ # apply first suggestion
$ cargo c -- -Wclippy::nursery
warning: redundant clone
 --> src/main.rs:3:45
  |
3 |     os_string.to_string_lossy().into_owned().to_string()
  |                                             ^^^^^^^^^^^^ help: remove this
  |
note: this value is dropped without further use
 --> src/main.rs:3:5
  |
3 |     os_string.to_string_lossy().into_owned().to_string()
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  = help: for further information visit https://rust-lang.github.io/rust-clippy/beta/index.html#redundant_clone
  = note: `-W clippy::redundant-clone` implied by `-W clippy::nursery`
  = help: to override `-W clippy::nursery` add `#[allow(clippy::redundant_clone)]`
```

</details>

the actual error that we are looking for is error `[E0515]`, cannot return value referencing local variable, which was only present in the original issue due to automatic type inference assuming you want to return a cloned `Cow<'_, str>`, which is of course not possible. this is why i took the original test functions and turned them into closures, where it's less obvious that it's trying to return the wrong type.

r? davidtwco

(because you reviewed the last attempt)
(also, let me know if i should squash this down to one commit and add myself as the co-contributor)
2026-04-10 18:38:14 +02:00
bors c3bd6289f6 Auto merge of #154758 - WaffleLapkin:aliassss, r=lcnr
`ty::Alias` refactor



This PR changes the following alias-related types from this:

```rust
pub enum AliasTyKind {
    Projection,
    Inherent,
    Opaque,
    Free,
}

pub struct AliasTy<I: Interner> {
    pub args: I::GenericArgs,
    pub def_id: I::DefId,
}

pub enum TyKind<I: Interner> {
    ...
    Alias(AliasTyKind, AliasTy<I>),
}
```
Into this:

```rust
pub enum AliasTyKind<I: Interner> {
    Projection { def_id: I::DefId },
    Inherent { def_id: I::DefId },
    Opaque { def_id: I::DefId },
    Free { def_id: I::DefId },
}

pub struct AliasTy<I: Interner> {
    pub args: I::GenericArgs,
    pub kind: AliasTyKind<I>,
}

pub enum TyKind<I: Interner> {
    ...
    Alias(AliasTy<I>),
}
```

... and then does a thousand other changes to accommodate for this change everywhere.

This brings us closer to being able to have `AliasTyKind`s which don't require a `DefId` (and thus can be more easily created, etc). Although notably we depend on both `AliasTyKind -> DefId` and `DefId -> AliasTyKind` conversions in a bunch of places still.

r? lcnr

----

A lot of these changes were done either by search & replace (via `ast-grep`) or on auto pilot, so I'm not quite sure I didn't mess up somewhere, but at least tests pass...
2026-04-07 12:56:57 +00:00
Waffle Lapkin 0f767084b8 ty::Alias refactor: fixup all the compiler code 2026-04-07 10:08:12 +02:00
Martin Nordholts 2d14e741ca borrowck: Apply user_arg_index nomenclature more broadly
No change in behavior. Only renames and a couple of added comments.
2026-04-06 17:34:21 +02:00
bors 906ca7ff5e Auto merge of #154877 - JonathanBrouwer:rollup-rbarkwZ, r=JonathanBrouwer
Rollup of 3 pull requests

Successful merges:

 - rust-lang/rust#154554 (emit error on `#[track_caller]` with `extern fn`)
 - rust-lang/rust#154858 (Change api of formatting diagnostic attribute strings.)
 - rust-lang/rust#154876 (attr parsing: make sure we pass the right target when errors could be emitted)
2026-04-06 09:56:16 +00:00
Jonathan Brouwer 0a46f824dd Rollup merge of #154657 - chenyukang:yukang-fix-145564-pattern-assignment-help, r=Kivooeo
Fix pattern assignment suggestions for uninitialized bindings

Fixes rust-lang/rust#145564
2026-04-06 08:27:51 +02:00
mejrs 7791ac737b Rename OnUnimplementedNote to CustomDiagnostic 2026-04-05 21:43:56 +02:00
mejrs 4c97a924c2 Force all diagnostic attr formatting through eval. 2026-04-05 21:37:24 +02:00
Jonathan Brouwer 9b2b299841 Rollup merge of #154674 - Enselic:unused-outlive, r=jdonszelmann
borrowck: Don't mention unused vars in closure outlive errors

When there is a closure arg lifetime error, the code does its best to find variables that correspond to the lifetimes involved. This commit stops mentioning arguments that are unused in closures, since they are not relevant. This removes the "red herring" of rust-lang/rust#113121.

The `user_arg_index` nomenclature can be applied more broadly, but then the diff becomes annoyingly big, so I chose not to do that.

Review each commit individually for a nicer experience.
2026-04-05 10:05:18 +02:00
Tim (Theemathas) Chirananthavat 707c0d0b5e Add comment to borrow-checker
As requested by @lcnr
2026-04-03 17:10:26 +07:00
Martin Nordholts 94ee70ed59 borrowck: Don't mention unused vars in outlive errors
When there is a lifetime error, the code does its best to find variables
that correspond to the lifetimes involved. This commit stops mentioning
arguments that are unused in closures, since they are not relevant.

See the diff of the blessed test to get a good clue of what that means.
2026-04-01 12:17:59 +02:00
Martin Nordholts a40ea0af41 borrowck: Add user_arg_index_to_local() helper to avoid + 1 magic
The `user_arg_index` nomenclature can be applied more broadly, but then
the diff becomes annoyingly big, so I chose not to do that.
2026-04-01 12:12:09 +02:00
yukang b994a05bc3 Fix pattern assignment suggestions for uninitialized bindings 2026-04-01 10:06:20 +08:00
may 41f7c2d4e2 apply review suggestions and move Cow sym to rustc 2026-03-31 21:32:31 +02:00
Anne Stijns beee9e71c6 Add suggestion to .to_owned() used on Cow when borrowing 2026-03-31 18:09:11 +02:00
Jonathan Brouwer 2a18b885ce Rollup merge of #154074 - dianne:dbg-temp-scopes, r=Mark-Simulacrum
don't drop arguments' temporaries in `dbg!`

Fixes rust-lang/rust#153850

Credit to @theemathas for help with macro engineering ^^

r? libs
2026-03-29 21:39:26 +02:00
dianne 7d1b41cbb3 update diagnostic for variables moved by dbg! 2026-03-29 09:48:41 -07:00
Daria Sukhonina 1b68d921a7 Add typeck_root_def_id_local 2026-03-27 15:36:28 +03:00
Daria Sukhonina 08bb61bb88 Use tcx.is_typeck_child() more often 2026-03-27 15:36:28 +03:00
Jana Dönszelmann 632ed10838 Revert "eagerly normalize during generalization"
This reverts commit 5d0863b147.
2026-03-23 12:09:34 +01:00
bors ac7f9ec7da Auto merge of #151746 - jdonszelmann:eagerly-normalize-in-generalize, r=lcnr
Eagerly normalize in generalize

*[View all comments](https://triagebot.infra.rust-lang.org/gh-comments/rust-lang/rust/pull/151746)*

r? @lcnr 

cc: https://github.com/rust-lang/trait-system-refactor-initiative/issues/262
2026-03-20 18:22:43 +00:00
Stuart Cook 734cca0053 Rollup merge of #150935 - rperier:provide_diagnostic_on_move_for_smart_pointers, r=JonathanBrouwer
Introduce #[diagnostic::on_move(message)]

cc rust-lang/rust#149862

This is a first proposal. I have deliberately kept it simpler than `diagnostic::on_unimplemented`.

Few questions/remarks:

- Do I need to move the OnMoveDirective logic into a dedicated module perhaps ? let's say into compiler/rustc_borrowck/src/diagnostics/on_move.rs
- No problems to depend on crates like `rustc_ast` from the borrowck ?
- Notes are not supported yet. While message and label are very static , in the sense that they are emitted in the same way from the same place in the borrowck, it is not the case for the notes. It would make the code more complex. But, I can add support for notes if it does make sense.

Suggestions are welcomed !
2026-03-20 15:33:05 +11:00
Ralf Jung 38aa32217b borrowck/type_check: remove helper left-over from unsized locals 2026-03-18 08:43:49 +01:00
Stuart Cook 3e1052c6d5 Rollup merge of #153724 - arferreira:fix-lifetime-naming-in-closures, r=jieyouxu
Show named lifetime in closure upvar diagnostics

Fixes rust-lang/rust#153545.

When a closure captures a variable whose type involves a named lifetime from the parent function (e.g., `'a`), the borrow checker's region renumbering creates a separate `RegionVid` for each occurrence of the erased lifetime in the closure's upvar type. These new `RegionVid`s have no `external_name`, so diagnostics fall back to synthetic names like `'1`.

This PR recovers the original lifetime name by matching the closure's upvar type against the parent function's parameter type (obtained via `liberate_late_bound_regions`). Both types have the same structure but different region representations, the parent has `ReLateParam`/`ReEarlyParam` with real names, the closure has `ReVar` from renumbering. Walking them in parallel with `for_each_free_region` lets us find which named lifetime corresponds to the anonymous `RegionVid`.

Before:

```rust
error[E0716]: temporary value dropped while borrowed
 --> test.rs:11:21
  |
6 | fn apply<'a>(
  |          -- lifetime `'1` defined here
```

After:

```rust
error[E0716]: temporary value dropped while borrowed
 --> test.rs:11:21
  |
6 | fn apply<'a>(
  |          -- lifetime `'a` defined here
```

The fix gracefully falls back to the existing `'1` naming when it can't match, nested closures, local variables (not parameters), destructuring patterns, or partial captures where the type structures diverge.
2026-03-16 15:01:34 +11:00