2659 Commits

Author SHA1 Message Date
Mark Rousskov e936dad761 Bump beta stage0 to released 1.95.0
This also re-adds assert_matches features everywhere.
2026-04-18 20:53:28 -04: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
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
Esteban Küber 75511819a4 Turn label into structured suggestion for .as_ref() and .as_mut()
```
error[E0382]: use of moved value: `foo`
  --> $DIR/as-ref-2.rs:10:14
   |
LL |     let foo = Some(Struct);
   |         --- move occurs because `foo` has type `Option<Struct>`, which does not implement the `Copy` trait
LL |     let _x: Option<Struct> = foo.map(|s| bar(&s));
   |                                  ---------------- `foo` moved due to this method call
LL |     let _y = foo;
   |              ^^^ value used here after move
   |
note: `Option::<T>::map` takes ownership of the receiver `self`, which moves `foo`
  --> $SRC_DIR/core/src/option.rs:LL:COL
help: consider calling `.as_ref()` to borrow the value's contents
   |
LL |     let _x: Option<Struct> = foo.as_ref().map(|s| bar(&s));
   |                                 +++++++++
help: consider calling `.as_mut()` to mutably borrow the value's contents
   |
LL |     let _x: Option<Struct> = foo.as_mut().map(|s| bar(&s));
   |                                 +++++++++
```
2026-03-14 22:40:28 +00:00
Jonathan Brouwer ce0374c6ae Rollup merge of #153635 - arferreira:unify-move-error-span-labels, r=estebank
Unify same-span labels in move error diagnostics

Fixes rust-lang/rust#153506.

When there's a single binding in a move error, we emit "data moved here" and "move occurs because ... does not implement the Copy trait" as two separate labels on the same span. This combines them into one label via a new `TypeNoCopy::LabelMovedHere` variant.

The multi-binding case still uses separate labels + a note since they point at different spans.

cc @estebank
2026-03-11 22:05:45 +01:00
arferreira 99e1940adb Show named lifetime in closure upvar diagnostics instead of synthetic '1 2026-03-11 11:58:31 -04:00
Romain Perier 965f1e7681 Introduce #[diagnostic::on_move]
This might be helpful for smart pointers to explains why they aren't Copy
and what to do instead or just to let the user know that .clone() is very
cheap and can be called without a performance penalty.
2026-03-11 16:56:19 +01:00
Nicholas Nethercote c12ab08c14 Move Spanned.
It's defined in `rustc_span::source_map` which doesn't make any sense
because it has nothing to do with source maps. This commit moves it to
the crate root, a more sensible spot for something this basic.
2026-03-11 06:25:23 +11:00
arferreira 407e421f7f Unify same-span labels in move error diagnostics 2026-03-10 09:35:03 -04:00
Zalathar 985b41d387 Remove the rustc_data_structures::assert_matches! re-exports 2026-03-08 22:02:23 +11:00
bors c7b206bba4 Auto merge of #153383 - nnethercote:overhaul-ensure_ok, r=Zalathar
Overhaul `ensure_ok`

The interaction of `ensure_ok` and the `return_result_from_ensure_ok` query modifier is weird and hacky. This PR cleans it up. Details in the individual commits.

r? @Zalathar
2026-03-08 07:03:35 +00:00
Nicholas Nethercote d4503b017e Overhaul ensure_ok.
`ensure_ok` provides a special, more efficient way of calling a query
when its return value isn't needed. But there is a complication: if the
query is marked with the `return_result_from_ensure_ok` modifier, then
it will return `Result<(), ErrorGuaranteed`. This is clunky and feels
tacked on. It's annoying to have to add a modifier to a query to declare
information present in its return type, and it's confusing that queries
called via `ensure_ok` have different return types depending on the
modifier.

This commit:

- Eliminates the `return_result_from_ensure_ok` modifier. The proc macro
  now looks at the return type and detects if it matches `Result<_,
  ErrorGuarantee>`. If so, it adds the modifier
  `returns_error_guaranteed`. (Aside: We need better terminology to
  distinguish modifiers written by the user in a `query` declaration
  (e.g. `cycle_delayed_bug`) from modifiers added by the proc macro
  (e.g. `cycle_error_handling`.))

- Introduces `ensure_result`, which replaces the use of `ensure_ok` for
  queries that return `Result<_, ErrorGuarantee>`. As a result,
  `ensure_ok` can now only be used for the "ignore the return value"
  case.
2026-03-08 09:39:39 +11:00
Josh Stone 32bae1353e Update cfg(bootstrap) 2026-03-07 10:42:02 -08:00
Nicholas Nethercote bea3803300 Insert missing ensure_ok calls.
Two places where `ensure_ok` can be used but currently isn't. (These
queries are marked with `return_result_from_ensure_ok`, which means that
`ensure_ok` returns `Result<(), ErrorGuaranteed>`.) This is potentially
a perf win, because `ensure_ok` query calls can be faster.
2026-03-07 21:29:23 +11:00
Jonathan Brouwer f540b27f90 Rollup merge of #153508 - JonathanBrouwer:improved_eager_format, r=GuillaumeGomez
Clean up the eager formatting API

For https://github.com/rust-lang/rust/issues/151366#event-22181360642

Previously eager formatting worked by throwing the arguments into a diag, formatting, then removing the args again. This is ugly so instead we now just do the formatting completely separately.
This PR has nice commits, so I recommend reviewing commit by commit.

r? @GuillaumeGomez
2026-03-07 01:42:37 +01:00
Jonathan Brouwer 828c0c0668 Remove remove_arg from diagnostics 2026-03-06 18:52:11 +01:00
Jonathan Brouwer 6be5b25aa9 Rollup merge of #152741 - arferreira:fix-invalid-suggestions-destructuring-drop, r=estebank,Kivooeo
Suppress invalid suggestions in destructuring assignment

Fixes rust-lang/rust#152694

When destructuring assignment hits a type with `Drop`, the compiler was emitting two broken suggestions: `ref *&mut String::new()` (invalid syntax) and `.clone()` on a temporary (useless).

Root cause: the suggestion logic didn't know these bindings were synthetic from assign desugaring. The fix reuses the existing `AssignDesugar` detection in `BindingFinder` to collect those spans and skip both suggestions.
2026-03-06 18:49:49 +01:00
Jonathan Brouwer d8092147fe Rename translation -> formatting 2026-03-04 17:47:24 +01:00
lapla 3cecc8d003 Suppress .clone() suggestion inside derive macro expansions 2026-03-03 11:10:19 +09:00
bors 38c0de8dcb Auto merge of #153050 - JayanAXHF:refactor/change-is-type-const, r=BoxyUwU
refactor(mgca): Change `DefKind::Const` and `DefKind::AssocConst` to have a `is_type_const` flag



Addresses rust-lang/rust#152940 

- Changed `DefKind::Const` and `DefKind::AssocConst` to have a `is_type_const` flag.
- changed `is_type_const` query to check for this flag
- removed `is_rhs_type_const` query

r? @BoxyUwU
2026-02-28 18:27:06 +00:00
JayanAXHF efc150e5b3 refactor(mgca): Change DefKind::Const and DefKind::AssocConst to have a is_type_const flag
* refactor: add `is_type_const` flag to `DefKind::Const` and `AssocConst`
* refactor(cleanup) remove the `rhs_is_type_const` query
* style: fix formatting
* refactor: refactor stuff in librustdoc for new Const and AssocConst
* refactor: refactor clippy for the changes
* chore: formatting
* fix: fix test
* fix: fix suggestions
* Update context.rs

Co-authored-by: Boxy <rust@boxyuwu.dev>
* changed AssocKind::Const to store data about being a type const
2026-02-28 17:27:46 +00:00
Jana Dönszelmann 5d0863b147 eagerly normalize during generalization 2026-02-26 11:44:24 +01:00
Guillaume Gomez 4803bb96c1 Replace TyCtxt::node_span_lint with emit_node_span_lint in rustc_borrowck 2026-02-26 09:31:29 +01:00
Guillaume Gomez 2bb3b01af2 Replace TyCtxt::emit_node_span_lint with emit_diag_node_span_lint 2026-02-24 20:34:22 +01:00
Guillaume Gomez 119b87fc61 Migrate rustc_borrowck to use TyCtxt::emit_diag_node_span_lint 2026-02-23 11:35:52 +01:00
Nicholas Nethercote a3d590888b Remove impl IntoQueryParam<P> for &'a P.
`IntoQueryParam` is a trait that lets query callers be a bit sloppy with
the passed-in key.
- Types similar to `DefId` will be auto-converted to `DefId`. Likewise
  for `LocalDefId`.
- Reference types will be auto-derefed.

The auto-conversion is genuinely useful; the auto-derefing much less so.
In practice it's only used for passing `&DefId` to queries that accept
`DefId`, which is an anti-pattern because `DefId` is marked with
`#[rustc_pass_by_value]`.

This commit removes the auto-deref impl and makes the necessary sigil
adjustments. (I generally avoid using `*` to deref manually at call
sites, preferring to deref via `&` in patterns or via `*` in match
expressions. Mostly because that way a single deref often covers
multiple call sites.)
2026-02-22 17:58:54 +11:00
Jana Dönszelmann decec173ec remove AttributeKind everywhere 2026-02-20 09:50:16 +01:00
Jana Dönszelmann 63edc913fa change all uses 2026-02-20 09:50:16 +01:00
Jonathan Brouwer 9330931a2b Rollup merge of #152596 - estebank:multipart_suggestions, r=petrochenkov
Make all multipart suggestions verbose

The ShowAlways style of suggestions is usually easier to understand than the inline style.
2026-02-19 10:56:37 +01:00