Commit Graph

5739 Commits

Author SHA1 Message Date
Jonathan Brouwer 1e3d3bdb24 Rollup merge of #153874 - bend-n:constify-const-fn-destruct, r=oli-obk
constify const Fn*: Destruct

makes closures const destruct where their upvars are. i think this makes sense
and is how this should be implemented.

r? @oli-obk
2026-03-24 18:14:15 +01:00
bors 6f22f61305 Auto merge of #154273 - JonathanBrouwer:rollup-TJZsnIo, r=JonathanBrouwer
Rollup of 8 pull requests

Successful merges:

 - rust-lang/rust#122668 (Add APIs for dealing with titlecase)
 - rust-lang/rust#153041 (Remove `ATTRIBUTE_ORDER`)
 - rust-lang/rust#153912 (Avoid prematurely choosing a glob import)
 - rust-lang/rust#154093 (const validity checking: do not recurse to references inside MaybeDangling)
 - rust-lang/rust#154257 (Revert eagerly normalize in generalize)
 - rust-lang/rust#154179 (tests/ui/async-await/gat-is-send-across-await.rs: New regression test)
 - rust-lang/rust#154224 (Remove more `BuiltinLintDiag` variants)
 - rust-lang/rust#154245 (Allow applying autodiff macros to trait functions.)
2026-03-23 22:28:18 +00:00
Jonathan Brouwer 7f8f600807 Rollup merge of #154257 - jdonszelmann:revert-eagerly-normalize-in-generalize, r=lcnr
Revert eagerly normalize in generalize

r? @lcnr

Reverts https://github.com/rust-lang/rust/pull/151746

We'll likely pull eager normalization out of generalization

Fixes rust-lang/rust#154173
Fixes rust-lang/rust#154244
2026-03-23 20:18:34 +01:00
bors f66622c7ec Auto merge of #152931 - khyperia:always-check-constarghastype, r=BoxyUwU
Always check `ConstArgHasType` even when otherwise ignoring



fixes https://github.com/rust-lang/rust/issues/149774

helping @BoxyUwU finish up https://github.com/rust-lang/rust/pull/150322, this is a simple tweak/finishing-up of that PR.

this is a breaking change that crater detected has some issues with in Boxy's PR, and hence needs a t-types FCP. I can go and help fix those crates if/once the break is signed off on.
2026-03-23 19:06:58 +00:00
Jana Dönszelmann 4af62bdbac Revert "implement eager normalization in a fresh context during typeck"
This reverts commit 04f2c0191e.
2026-03-23 12:09:34 +01:00
Jana Dönszelmann 304a197ba5 Revert "fixup span in obligation cause"
This reverts commit 2d411a0faa.
2026-03-23 12:09:34 +01:00
Jana Dönszelmann 1fa1611e9f Revert "address review"
This reverts commit d665c0b371.
2026-03-23 12:09:34 +01:00
Jonathan Brouwer 773db3e325 Rollup merge of #154161 - estebank:e0277-ty-impls, r=Kivooeo
On E0277 tweak help when single type impls traits

When encountering an unmet predicate, when we point at the trait impls that do exist, if they are all for the same self type, tweak the wording to make it less verbose.
2026-03-23 12:01:01 +01:00
Esteban Küber 3ad92c9f23 On E0277 tweak help when single type impls traits
When encountering an unmet predicate, when we point at the trait impls that do exist, if they are all for the same self type, tweak the wording to make it less verbose.
2026-03-21 02:24:36 +00:00
Jonathan Brouwer 70d1c586ce Rollup merge of #154118 - malezjaa:dont-suggest-some-traits-for-unions, r=JonathanBrouwer
don't suggest non-deriveable traits for unions

Fixes rust-lang/rust#137587

Before, the compiler suggested adding `#[derive(Debug)]` (other traits too) for unions,
which is misleading because some traits can't be automatically derived.

Only traits that are still suggested are Copy and Clone.

I noticed the error label changed after removing the suggestion. I hope this isn't a big deal, but let me know if that's an issue.

original example:
```rs
union Union {
    member: usize,
}

impl PartialEq<u8> for Union {
    fn eq(&self, rhs: &u8) -> bool {
        unsafe { self.member == (*rhs).into() }
    }
}

fn main() {
    assert_eq!(Union { member: 0 }, 0);
}
```

before:
```
error[E0277]: `Union` doesn't implement `Debug`
  --> src\main.rs:13:5
   |
13 |     assert_eq!(Union { member: 0 }, 0);
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Debug` is not implemented for `Union`
   |
   = note: add `#[derive(Debug)]` to `Union` or manually `impl Debug for Union`
   = note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider annotating `Union` with `#[derive(Debug)]`
   |
 2 + #[derive(Debug)]
 3 | union Union {
   |
```

after (the message doesn't suggest adding #[derive(Debug)] to unions)
```
error[E0277]: `Union` doesn't implement `Debug`
  --> src\main.rs:13:5
   |
13 |     assert_eq!(Union { member: 0 }, 0);
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unsatisfied trait bound
   |
help: the trait `Debug` is not implemented for `Union`
  --> src\main.rs:2:1
   |
 2 | union Union {
   | ^^^^^^^^^^^
   = note: manually `impl Debug for Union`
```
2026-03-21 00:42:48 +01:00
malezjaa 27212bb09d Don't suggest non-deriveable traits for unions.
Don't suggest non-deriveable traits for unions. This also adds enum, struct and union markers to rustc_on_unimplemented
2026-03-20 23:39:28 +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
Jana Dönszelmann d665c0b371 address review 2026-03-20 14:02:24 +01:00
Esteban Küber 49bb371ca7 When single impl can satisfy inference error, suggest type
When encountering an inference error where a return type must be known, like when calling `Iterator::sum::<T>()` without specifying `T`, look for all `T` that would satisfy `Sum<S>`. If only one, suggest it.

```
error[E0283]: type annotations needed
  --> $DIR/cannot-infer-iterator-sum-return-type.rs:4:9
   |
LL |     let sum = v
   |         ^^^
...
LL |         .sum(); // `sum::<T>` needs `T` to be specified
   |          --- type must be known at this point
   |
   = note: cannot satisfy `_: Sum<i32>`
help: the trait `Sum` is implemented for `i32`
  --> $SRC_DIR/core/src/iter/traits/accum.rs:LL:COL
  ::: $SRC_DIR/core/src/iter/traits/accum.rs:LL:COL
   |
   = note: in this macro invocation
note: required by a bound in `std::iter::Iterator::sum`
  --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
   = note: this error originates in the macro `integer_sum_product` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider giving `sum` an explicit type, where the type for type parameter `S` is specified
   |
LL |     let sum: i32 = v
   |            +++++
```
2026-03-18 18:16:47 +00:00
khyperia e9c273e377 Always check ConstArgHasType even when otherwise ignoring 2026-03-18 11:48:09 +01:00
Jonathan Brouwer 6f13b5244c Rollup merge of #153795 - arferreira:turbofish-span-improvement, r=Kivooeo,eggyal
Point turbofish inference errors at the uninferred generic arg

Follow-up to rust-lang/rust#153751.

When a method call has a turbofish with an uninferred generic argument, point the diagnostic span at the specific `_` that couldn't be inferred instead of the method name.

Before:
```rust
LL |     S.f::<u32, _>(42);
   |       ^ cannot infer type of the type parameter `B`
```

After:
```rust
LL |     S.f::<u32, _>(42);
   |                ^ cannot infer type of the type parameter `B`
```

Path expressions (`None::<_>`, `foo::<_>()`) are not handled yet, that's a separate code path.

cc @eggyal
2026-03-17 21:20:02 +01:00
Jonathan Brouwer 888990ba13 Rollup merge of #153967 - estebank:infer_must_implement, r=petrochenkov
Tweak wording of failed predicate in inference error

Special case message talking about `Predicate` that couldn't be satisfied when in inference errors so that we don't say "cannot satisfy `_: Trait`" and instead say "type must implement `Trait`".
2026-03-17 17:51:30 +01:00
bendn b90abe64c1 constify const Fn*: Destruct 2026-03-17 23:47:42 +07:00
Stuart Cook 66c506d44a Rollup merge of #153921 - arferreira:fix-bogus-assoc-type-constraint-suggestion, r=estebank
Suppress self-referential associated type constraint suggestion

Fixes rust-lang/rust#112104

When comparing `Option<&I::Item>` against `Option<I::Item>`, the compiler decomposes the mismatch into `expected = &<I as Iterator>::Item` vs `found = <I as Iterator>::Item`. ARM 2 in `note_and_explain_type_err` matches because `found` is a projection, and would suggest `Item = &<I as Iterator>::Item` — constraining the associated type to something that contains itself.

ARM 1 (`expected_projection`) already guards against this with `found.contains(expected)`. ARM 2 was missing the symmetric check. This adds `expected.contains(found)` to the match guard.

Before:
```rust
help: consider constraining the associated type `<I as Iterator>::Item` to `&<I as Iterator>::Item`
  |
1 | fn foo<I: Iterator<Item = &<I as Iterator>::Item>>(iter: &mut I) {
  |                   +++++++++++++++++++++++++++++++
```

After: bogus suggestion is gone, and the existing `.as_ref()` suggestion from `hir_typeck` still fires correctly.

r? @estebank
2026-03-17 15:53:12 +11:00
Esteban Küber 9b1eb935bc Tweak wording of failed predicate in inference error
Special case message talking about `Predicate` that couldn't be satisfied when in inference errors so that we don't say "cannot satisfy `_: Trait`" and instead say "type must implement `Trait`".
2026-03-16 17:58:49 +00:00
Stuart Cook 7514258efd Rollup merge of #153925 - estebank:issue-129269, r=jieyouxu
Provide better suggestions for inference errors on `.collect()?`

When encountering an inference error when calling `.collect()` followed by `?`, detect the return type of the enclosing function and suggest `.collect::<Result<_, _>>()?` instead of `.collect::<Vec<_>>()?`:

```
error[E0283]: type annotations needed
  --> $DIR/question-mark-type-infer.rs:10:21
   |
LL |     l.iter().map(f).collect()?
   |                     ^^^^^^^ cannot infer type of the type parameter `B` declared on the method `collect`
   |
   = note: cannot satisfy `_: FromIterator<Result<i32, ()>>`
note: required by a bound in `collect`
  --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
help: consider specifying the generic argument
   |
LL |     l.iter().map(f).collect::<Result<_, _>>()?
   |                            ++++++++++++++++
```

On inference error in let binding, account for `?`:
```
error[E0282]: type annotations needed
  --> $DIR/question-mark-type-inference-in-chain.rs:31:9
   |
LL |     let mut tags = lines.iter().map(|e| parse(e)).collect()?;
   |         ^^^^^^^^
...
LL |     tags.sort();
   |     ---- type must be known at this point
   |
help: consider giving `tags` an explicit type
   |
LL |     let mut tags: Vec<_> = lines.iter().map(|e| parse(e)).collect()?;
   |                 ++++++++
```

Address rust-lang/rust#129269.
2026-03-16 15:01:36 +11:00
Esteban Küber 774f232015 Provide better suggestions for inference errors on .collect()?
When encountering an inference error when calling `.collect()` followed by `?`, detect the return type of the enclosing function and suggest `.collect::<Result<_, _>>()?` instead of `.collect::<Vec<_>>()?`:

```
error[E0283]: type annotations needed
  --> $DIR/question-mark-type-infer.rs:10:21
   |
LL |     l.iter().map(f).collect()?
   |                     ^^^^^^^ cannot infer type of the type parameter `B` declared on the method `collect`
   |
   = note: cannot satisfy `_: FromIterator<Result<i32, ()>>`
note: required by a bound in `collect`
  --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
help: consider specifying the generic argument
   |
LL |     l.iter().map(f).collect::<Result<_, _>>()?
   |                            ++++++++++++++++
```

On inference error in let binding, account for `?`:
```
error[E0282]: type annotations needed
  --> $DIR/question-mark-type-inference-in-chain.rs:31:9
   |
LL |     let mut tags = lines.iter().map(|e| parse(e)).collect()?;
   |         ^^^^^^^^
...
LL |     tags.sort();
   |     ---- type must be known at this point
   |
help: consider giving `tags` an explicit type
   |
LL |     let mut tags: Vec<_> = lines.iter().map(|e| parse(e)).collect()?;
   |                 ++++++++
```
2026-03-15 17:39:06 +00:00
arferreira b258fe18ad Use position() instead of enumerate().find() 2026-03-15 12:54:09 -04:00
arferreira 4ecc78d637 Suppress self-referential associated type constraint suggestion 2026-03-15 12:39:39 -04: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 ff0ce4f5fa Make some suggestions verbose 2026-03-14 20:13:43 +00:00
Stuart Cook 4625821025 Rollup merge of #153818 - oli-obk:const-closures, r=fee1-dead
Reimplement const closures

Tracking issue: rust-lang/rust#106003

Best reviewed commit-by-commit

The old solver can't handle `for<'a> |x: &'a()| ()` closures in const contexts, but that feature is unstable itself, so we can just leave it to the next solver to handle.

We need a lot more tests, we're testing the bare minimum of success and failure paths right now.

r? @fee1-dead
2026-03-14 17:30:24 +11:00
Oli Scherer 8629126289 Allow calling const closures on the old solver 2026-03-13 20:29:43 +00:00
Matthias Krüger cc41ba4899 Rollup merge of #153798 - zachs18:remove-pointerlikecandidate, r=JohnTitor
Remove unused `SelectionCandidate::PointerLikeCandidate`

The `PointerLike` trait was removed in https://github.com/rust-lang/rust/pull/143308, as part of `dyn*` being removed.

`!null` pattern types were added in https://github.com/rust-lang/rust/pull/142339 , which added `SelectionCandidate::PointerLikeCandidate`, IIUC to allow pattern types to implement `PointerLike`, but `PointerLike` was removed after that PR was first written, so (I assume) that functionality was mostly removed on a rebase, but this part didn't get removed. (see an earlier version of that PR that mentioned `LangItem::PointerLike`: https://github.com/rust-lang/rust/commit/50cab826283cc94f0eb0180907545fbd7dd3d50a before it got rebased over the rollup containing 143308: https://github.com/rust-lang/rust/pull/142339#issuecomment-3037059069 )

cc @oli-obk
2026-03-13 19:50:14 +01:00
arferreira e485708b84 Point turbofish inference errors at the uninferred generic arg 2026-03-13 07:29:05 -04:00
Zachary S 814c7508a9 Remove PointerLikeCandidate 2026-03-12 18:52:14 -05:00
arferreira 4d47b07353 Detect existing turbofish on method calls to suppress useless suggestion 2026-03-11 22:39:51 -04:00
Jonathan Brouwer 4e64a8b568 Rollup merge of #153581 - nnethercote:rm-cycle_stash, r=oli-obk
Simplify `type_of_opaque`.

There is a bunch of complexity supporting the "cannot check whether the hidden type of opaque type satisfies auto traits" error that shows up in `tests/ui/impl-trait/auto-trait-leak.rs`. This is an obscure error that shows up in a single test. If we are willing to downgrade that error message to a cycle error, we can do the following.

- Simplify the `type_of_opaque` return value.
- Remove the `cycle_stash` query modifier.
- Remove the `CyclePlaceholder` type.
- Remove the `SelectionError::OpaqueTypeAutoTraitLeakageUnknown` variant.
- Remove a `FromCycleError` impl.
- Remove `report_opaque_type_auto_trait_leakage`.
- Remove the `StashKey::Cycle` variant.
- Remove the `CycleErrorHandling::Stash` variant.

That's a lot! I think this is a worthwhile trade-off.

r? @oli-obk
2026-03-11 22:05:43 +01:00
Nicholas Nethercote 3399ed3c9a Simplify type_of_opaque.
There is a bunch of complexity supporting the "cannot check whether the
hidden type of opaque type satisfies auto traits" error that shows up in
`tests/ui/impl-trait/auto-trait-leak.rs`. This is an obscure error that
shows up in a single test. If we are willing to downgrade that error
message to a cycle error, we can do the following.

- Simplify the `type_of_opaque` return value.
- Remove the `cycle_stash` query modifier.
- Remove the `CyclePlaceholder` type.
- Remove the `SelectionError::OpaqueTypeAutoTraitLeakageUnknown`
  variant.
- Remove a `FromCycleError` impl.
- Remove `report_opaque_type_auto_trait_leakage`.
- Remove the `StashKey::Cycle` variant.
- Remove the `CycleErrorHandling::Stash` variant.

That's a lot! I think this is a worthwhile trade-off.
2026-03-11 21:54:42 +11:00
Jonathan Brouwer 3679ba7fb0 Rollup merge of #153674 - estebank:issue-41906, r=petrochenkov
Detect inherent method behind deref being shadowed by trait method

```
error[E0277]: the trait bound `Rc<RefCell<S>>: Borrow<S>` is not satisfied
  --> $DIR/shadowed-intrinsic-method-deref.rs:16:22
   |
LL |     let sb : &S = &s.borrow();
   |                      ^^^^^^ the trait `Borrow<S>` is not implemented for `Rc<RefCell<S>>`
   |
help: the trait `Borrow<S>` is not implemented for `Rc<RefCell<S>>`
      but trait `Borrow<RefCell<S>>` is implemented for it
  --> $SRC_DIR/alloc/src/rc.rs:LL:COL
   = help: for that trait implementation, expected `RefCell<S>`, found `S`
   = note: there's an inherent method on `RefCell<S>` of the same name, which can be auto-dereferenced from `&RefCell<T>`
help: to access the inherent method on `RefCell<S>`, use the fully-qualified path
   |
LL -     let sb : &S = &s.borrow();
LL +     let sb : &S = &RefCell::borrow(&s);
   |
```

In the example above, method `borrow` is available both on `<RefCell<S> as Borrow<S>>` *and* on `RefCell<S>`. Adding the import `use std::borrow::Borrow;` causes `s.borrow()` to find the former instead of the latter. We now point out that the other exists, and provide a suggestion on how to call it.

Fix rust-lang/rust#41906. CC rust-lang/rust#153662.
2026-03-11 10:58:52 +01:00
Esteban Küber 73fe905f3c Detect inherent method behind deref being shadowed by trait method
```
error[E0277]: the trait bound `Rc<RefCell<S>>: Borrow<S>` is not satisfied
  --> $DIR/shadowed-intrinsic-method-deref.rs:16:22
   |
LL |     let sb : &S = &s.borrow();
   |                      ^^^^^^ the trait `Borrow<S>` is not implemented for `Rc<RefCell<S>>`
   |
help: the trait `Borrow<S>` is not implemented for `Rc<RefCell<S>>`
      but trait `Borrow<RefCell<S>>` is implemented for it
  --> $SRC_DIR/alloc/src/rc.rs:LL:COL
   = help: for that trait implementation, expected `RefCell<S>`, found `S`
   = note: there's an inherent method on `RefCell<S>` of the same name, which can be auto-dereferenced from `&RefCell<T>`
help: to access the inherent method on `RefCell<S>`, use the fully-qualified path
   |
LL -     let sb : &S = &s.borrow();
LL +     let sb : &S = &RefCell::borrow(&s);
   |
```

In the example above, method `borrow` is available both on `<RefCell<S> as Borrow<S>>` *and* on `RefCell<S>`. Adding the import `use std::borrow::Borrow;` causes `s.borrow()` to find the former instead of the latter. We now point out that the other exists, and provide a suggestion on how to call it.
2026-03-10 23:24:27 +00:00
Jonathan Brouwer 17a6b83542 Rollup merge of #153643 - TaKO8Ki:issue-153539, r=Kivooeo
Avoid projection-only suggestions for inherent assoc types

Fixes rust-lang/rust#153539.

Type mismatch diagnostics in `note_and_explain_type_err` currently route both `ty::Projectio`n and `ty::Inherent`
through the same associated-type suggestion path. For inherent associated types, that path can reach helpers that
are only valid for trait projections.
2026-03-10 22:44:07 +01:00
Takayuki Maeda a92006f5c9 avoid projection-only suggestions for inherent assoc types 2026-03-10 15:28:29 +09:00
Guillaume Gomez 4e2a519d12 Remove TyCtxt::node_span_lint usage from rustc_trait_selection 2026-03-09 11:35:33 +01: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
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 4e0b64408c Fix newly detected subdiagnostics using variables from parent 2026-03-06 19:00:25 +01:00
Jonathan Brouwer 8c87d0761f Remove eagerly_format from DiagCtxt 2026-03-06 18:52:11 +01:00
Jonathan Brouwer 43221b43b6 Remove eagerly_format from Diag 2026-03-06 18:52:11 +01:00
Jonathan Brouwer c2f1e9d71d Remove stored args from diagnostics 2026-03-06 18:52:11 +01:00
Yuki Okushi 1190845a3d Do not emit ConstEvaluatable goals if type-const 2026-03-06 21:51:31 +09:00