Commit Graph

217 Commits

Author SHA1 Message Date
bors
88638df1e7 Auto merge of #161827 - GuillaumeGomez:trait-mut-mismatch, r=mejrs
Fix suggestion for mutability mismatch between trait def and trait impl in 2015 edition



This code:

```rust
mod bar {
    pub struct X;
}

pub trait A {
    fn f(x: &mut bar::X);
}

mod b {
    pub struct X;

    impl crate::A for X {
        fn f(_x: &crate::bar::X) {}
    }
}
```

emits:

```console
error[E0053]: method `f` has an incompatible type for trait
  --> foo.rs:13:18
   |
13 |         fn f(_x: &crate::bar::X) {}
   |                  ^^^^^^^^^^^^^^ types differ in mutability
   |
note: type in trait
  --> foo.rs:6:13
   |
 6 |     fn f(x: &mut bar::X);
   |             ^^^^^^^^^^^
   = note: expected signature `fn(&mut X)`
              found signature `fn(&X)`
help: change the parameter type to match the trait
   |
13 -         fn f(_x: &crate::bar::X) {}
13 +         fn f(_x: &mut bar::X) {}
   |
```

Which is wrong. This PR fixes it. However the `Ty` doesn't hold a context (or at least not a correct one) allowing to have a correct path to the `X` type. So instead, in case of a mutability mismatch, we (try to) retrieve the code snippet, and remove/add the `mut` keyword. If we fail to, we revert to the current behaviour.

r? @mejrs
2026-09-21 22:48:22 +00:00
Guillaume Gomez
754219139c Allow crate:: prepending in 2015 edition in Ty display 2026-09-21 15:20:26 +02:00
Jonathan Brouwer
8c5a762b23 Rollup merge of #163057 - estebank:required-bound-context, r=oli-obk
Use span context for enclosing item(s) of unmet bound

Point at item that introduced an unmet bound and in the case of associated items, their container (trait/impl), without using a span label. This adds bits of code to the diagnostic, without explicitly pointing to it.

```
error[E0277]: cannot add `<T as SubEncoder>::ActualSize` to `<T as SubEncoder>::ActualSize`
  --> $DIR/issue-54108.rs:23:17
   |
LL |     type Size = <Self as SubEncoder>::ActualSize;
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ no implementation for `<T as SubEncoder>::ActualSize + <T as SubEncoder>::ActualSize`
   |
   = help: the trait `Add` is not implemented for `<T as SubEncoder>::ActualSize`
note: required by a bound in `Encoder::Size`
  --> $DIR/issue-54108.rs:8:16
   |
LL | pub trait Encoder {
LL |     type Size: Add<Output = Self::Size>;
   |                ^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `Encoder::Size`
```

r? @oli-obk
2026-09-21 15:19:26 +02:00
Esteban Küber
132e321b89 Point at impl start 2026-09-20 19:17:59 +00:00
Esteban Küber
65dd911de7 Use span context for enclosing item(s) of unmet bound
Point at item that introduced an unmet bound and in the case of associated items, their container (trait/impl), without using a span label. This adds bits of code to the diagnostic, without explicitly pointing to it.

```
error[E0277]: cannot add `<T as SubEncoder>::ActualSize` to `<T as SubEncoder>::ActualSize`
  --> $DIR/issue-54108.rs:23:17
   |
LL |     type Size = <Self as SubEncoder>::ActualSize;
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ no implementation for `<T as SubEncoder>::ActualSize + <T as SubEncoder>::ActualSize`
   |
   = help: the trait `Add` is not implemented for `<T as SubEncoder>::ActualSize`
note: required by a bound in `Encoder::Size`
  --> $DIR/issue-54108.rs:8:16
   |
LL | pub trait Encoder {
LL |     type Size: Add<Output = Self::Size>;
   |                ^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `Encoder::Size`
```
2026-09-20 17:36:00 +00:00
Esteban Küber
09fb849143 Detect more cases of method shadowing with incorrect arguments
```
error[E0061]: this method takes 0 arguments but 1 argument was supplied
  --> $DIR/shadowed-intrinsic-method.rs:18:7
   |
LL |     a.borrow(());
   |       ^^^^^^ -- unexpected argument of type `()`
   |
note: the `borrow` call is resolved to the method in `std::borrow::Borrow`, shadowing the method of the same name on the inherent impl for `A`
  --> $DIR/shadowed-intrinsic-method.rs:18:7
   |
LL | use std::borrow::Borrow;
   |     ------------------- `std::borrow::Borrow` imported here
...
LL |     a.borrow(());
   |       ^^^^^^ refers to `std::borrow::Borrow::borrow`
note: method defined here
  --> $SRC_DIR/core/src/borrow.rs:LL:COL
help: you might have meant to call the other method; you can use the fully-qualified path to call it explicitly
   |
LL -     a.borrow(());
LL +     A::borrow(&mut a, ());
   |
help: remove the extra argument
   |
LL -     a.borrow(());
LL +     a.borrow();
   |
```

Account for inherent methods

Tweak wording on "other methods available" note

Handle correct gramar in the face of a single other option, or many.
2026-09-19 21:12:33 +00:00
Esteban Küber
591e0810a2 Use context span more 2026-09-16 22:35:56 +00:00
bors
64af19d44c Auto merge of #159836 - adwinwhite:unify-methods, r=lcnr
Unify two `for_each_relevant_impl` methods




We have two similar methods on `tcx` with the same name. It's confusing.

r? lcnr
2026-07-31 14:00:49 +00:00
Adwin White
a070b70f5f unify for_each_relevant_impl 2026-07-31 14:38:22 +08:00
Yukang
c9dbe2bd15 Use value syntax for associated function suggestions 2026-07-28 17:05:30 +08:00
Yukang
8bfa42975d Add regression test for generic associated function suggestions 2026-07-28 17:05:30 +08:00
Jacob Pratt
ab0ec83890 Rollup merge of #159531 - zedddie:gsoc-batch-28-meow, r=Kivooeo
Reorganize `tests/ui/issues` [28/N]

Part of [GSoC'26 project](https://rust-lang.zulipchat.com/#narrow/channel/421156-gsoc/topic/Project.3A.20Reorganizing.20tests.2Fui.2Fissues)

r? Kivooeo
@Teapot4195
2026-07-23 19:18:41 -04:00
Jacob Pratt
f12d654e0a Rollup merge of #159041 - zedddie:gsoc-batch-22-meow, r=Kivooeo
Reorganize `tests/ui/issues` [22/N]

Part of [GSoC'26 project](https://rust-lang.zulipchat.com/#narrow/channel/421156-gsoc/topic/Project.3A.20Reorganizing.20tests.2Fui.2Fissues)

r? Kivooeo
@Teapot4195
2026-07-23 19:18:38 -04:00
zedddie
6d6092cd52 bless batch 2026-07-18 18:57:06 +02:00
zedddie
377748d283 move batch 2026-07-18 01:28:15 +02:00
zedddie
ac8a3472ae bless batch 2026-07-17 23:47:37 +02:00
zedddie
87bdc236db move batch 2026-07-17 23:26:13 +02:00
zedddie
e5a03bc9c0 bless batch 2026-07-15 23:38:07 +02:00
zedddie
62fbd98dd9 move batch 2026-07-15 23:38:07 +02:00
Jonathan Brouwer
03ea3045a4 Rollup merge of #159255 - estebank:shortened-ty-placeholder, r=Kivooeo
Replace shortened type with `_` instead of `...` as placeholder

r? @mejrs
2026-07-14 16:49:09 +02:00
Esteban Küber
5f35fc8b8f Replace shortened type with _ instead of ... as placeholder 2026-07-13 22:48:25 +00:00
Jana Dönszelmann
25745ee1f1 fix all tidy annotations 2026-07-13 10:54:53 +02:00
zedddie
dd8996db14 bless batch 2026-07-09 23:34:43 +02:00
zedddie
715a61625e move batch 2026-07-09 23:32:40 +02:00
Obei Sideg
34df033180 Update FIXME(static_mut_refs) comments
update `FIXME(static_mut_refs)` comment to say: use raw pointers instead
of references.
2026-07-03 14:55:08 +03:00
Tam Pham
fd6762120b Bless ui test runners 2026-06-28 03:56:28 -05:00
Tam Pham
b140addbd5 Bless ui tests 2026-06-28 03:48:49 -05:00
Amanieu d'Antras
e4ef1cff89 Move supertrait shadowing tests to tests/ui/supertrait-shadowing 2026-06-26 17:47:43 +01:00
Amanieu d'Antras
3c00af96ba Add supertrait item shadowing for type-level path resolution 2026-06-26 17:47:42 +01:00
Adwin White
3dbe96a5a1 add IsRigid marker to TyKind::Alias and constants 2026-06-25 17:12:10 +08:00
zedddie
05b0947f25 bless batch 2026-06-17 06:16:54 +02:00
zedddie
a7219132bd move batch 2026-06-17 06:16:54 +02:00
Jacob Pratt
9a60b1bc8c Rollup merge of #157547 - zedddie:gsoc-batch-6-meow, r=Kivooeo
Reorganize `tests/ui/issues` [6/N]

Part of [GSoC'26 project](https://rust-lang.zulipchat.com/#narrow/channel/421156-gsoc/topic/Project.3A.20Reorganizing.20tests.2Fui.2Fissues)

r? Kivooeo
@Teapot4195
2026-06-08 23:22:39 -04:00
Jonathan Brouwer
4c561f2ffe Rollup merge of #157413 - Albab-Hasan:option-cloned-non-ref-diagnostic, r=folkertdev
fix: don't suggest .into_iter() for .cloned()/.copied() on non-reference Option

calling `.cloned()` or `.copied()` on `Option<T>` where `T` is an owned type emitted "`Option<T>` is not an iterator" with a suggestion to prepend `.into_iter()`. the suggested code still did not compile: `Option<T>::into_iter()` yields `T` by value not `&T` so `.cloned()`/`.copied()` failed again for the same reason.

`Option<T>` implements `IntoIterator` which is why the `impl_into_iterator_should_be_iterator` branch fires. added a guard before it: when the method is `cloned`/`copied`, the receiver is `Option<T>` and `T` is a concrete non-reference type. emit a targeted label pointing at the correct form (`Option<&T>`) and suggest removing the call instead.

closes https://github.com/rust-lang/rust/issues/151147
2026-06-08 17:11:13 +02:00
albab-hasan
4049f43ef9 fix: don't suggest .into_iter() for .cloned()/.copied() on non-reference Option
calling `.cloned()` or `.copied()` on `Option<T>` where `T` is an owned type
emitted "`Option<T>` is not an iterator" with a suggestion to prepend
`.into_iter()`. the suggested code still did not compile: `Option<T>::into_iter()`
yields `T` by value not `&T`, so `.cloned()`/`.copied()` failed again for the
same reason.

`Option<T>` implements `IntoIterator`, which is why the
`impl_into_iterator_should_be_iterator` branch fires. added a guard before it:
when the method is `cloned`/`copied`, the receiver is `Option<T>`, and `T` is a
concrete non-reference type, emit a targeted label pointing at the correct form
(`Option<&T>`) and suggest removing the call instead.
2026-06-08 14:41:03 +06:00
Jacob Pratt
b4f59aaad2 Rollup merge of #157264 - Dnreikronos:fix/method-suggest-trait-extra-generics-ice, r=chenyukang
diagnostics: Fix ICE building a trait ref in method suggestions

Fixes rust-lang/rust#157189

When a method call doesn't resolve, the no-method-found diagnostic probes every trait for one with a same-named method and builds a trait ref for it to spot trait items duplicated across crate versions. It only ever passed the receiver as the single argument, which works only when the trait's one generic is `Self`. Call `.borrow()` on something and the probe finds `Borrow`, whose extra `Borrowed` parameter leaves the args out of step with the trait's generics, so `debug_assert_args_compatible` fires. Same crash when the receiver type isn't known and there are zero args.

Now the args come from `GenericArgs::for_item`: the receiver fills `Self` when we have it, fresh inference variables cover the rest.
2026-06-07 00:37:48 -04:00
zedddie
5b8bc5d0a2 bless batch 2026-06-07 00:13:04 +02:00
zedddie
66342107b0 move batch 2026-06-07 00:01:05 +02:00
Dnreikronos
10f9f14377 Build a fully-applied trait ref in method-not-found suggestion
When a method call fails to resolve, the suggestion machinery probes
all traits for a method with the same name and builds a trait
reference for the providing trait to detect duplicate trait items
coming from multiple crate versions. It passed only the receiver type
as the single argument, which is correct only for traits whose sole
generic parameter is `Self`. For a trait with further parameters (for
example `Borrow<Borrowed>`), or when the receiver type is unknown, the
argument list no longer matched the trait's generics and tripped the
`debug_assert_args_compatible` assertion, producing an ICE.

Build the argument list with `GenericArgs::for_item` instead, using
the receiver type for `Self` when known and fresh inference variables
for the remaining parameters.
2026-06-01 13:47:30 -03:00
zedddie
01f5cc8073 bless batch 2026-06-01 14:13:08 +02:00
zedddie
3c6c901f6b move batch 2026-06-01 14:12:15 +02:00
cijiugechu
ffcbfc1cf4 Use singular wording for single _ placeholders in type suggestions 2026-04-20 22:21:51 +08:00
jackh726
c681ae6931 For supertrait-shadowing tests, assert str rather than println 2026-04-13 17:19:30 +00:00
jackh726
a4c494472b A couple small comment cleanups, and update tests. 2026-04-11 22:54:47 +00:00
Yuki Okushi
531631c7ad Make messages more generic 2026-03-28 16:18:11 +09:00
Jana Dönszelmann
e613b82e4e Revert "bless some tests"
This reverts commit 6edf58fab8.
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
Jonathan Brouwer
907464d1b7 Rollup merge of #153682 - estebank:tweak-e0599-note, r=wesleywiser
Tweak E0599 note

When not finding a method for a type, but finding it for a single other impl, special case the output to emit a single message instead of a list.

```
error[E0599]: no associated item named `C` found for struct `Fail<i32, u32>` in the current scope
  --> $DIR/wrong-projection-self-ty-invalid-bivariant-arg2.rs:15:23
   |
LL | struct Fail<T: Proj<Assoc = U>, U>(T);
   | ---------------------------------- associated item `C` not found for this struct
...
LL |     Fail::<i32, u32>::C
   |                       ^ associated item not found in `Fail<i32, u32>`
   |
   = note: the associated item was found for `Fail<i32, i32>`
```
instead of

```
   = note: the associated item was found for
           - `Fail<i32, i32>`
```
2026-03-17 21:20:01 +01:00
Esteban Küber
ed25d008a0 Tweak E0599 note
When not finding a method for a type, but finding it for a single other impl, special case the output to emit a single message instead of a list.

```
error[E0599]: no associated item named `C` found for struct `Fail<i32, u32>` in the current scope
  --> $DIR/wrong-projection-self-ty-invalid-bivariant-arg2.rs:15:23
   |
LL | struct Fail<T: Proj<Assoc = U>, U>(T);
   | ---------------------------------- associated item `C` not found for this struct
...
LL |     Fail::<i32, u32>::C
   |                       ^ associated item not found in `Fail<i32, u32>`
   |
   = note: the associated item was found for `Fail<i32, i32>`
```
instead of

```
   = note: the associated item was found for
           - `Fail<i32, i32>`
```
2026-03-11 00:43:04 +00: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