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
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
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`
```
```
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.
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
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.
diagnostics: Fix ICE building a trait ref in method suggestions
Fixesrust-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.
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.
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>`
```
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>`
```
```
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.