Skip to content

Suggest borrowing if a trait implementation is found for &/&mut <type> regression #85865

Closed
@pickfire

Description

@pickfire

When both &S and &mut S is implemented,

Given the following code:

trait Tr {}
struct S {}

impl Tr for &S {}
impl Tr for &mut S {}

fn foo<T: Tr>(t: T) {}

fn main() {
    let s = S {};
    foo(s);
}

The previous output was:

error[E0277]: the trait bound `S: Tr` is not satisfied
  --> src/main.rs:11:9
   |
7  | fn foo<T: Tr>(t: T) {}
   |           -- required by this bound in `foo`
...
11 |     foo(s);
   |         ^ the trait `Tr` is not implemented for `S`
   |
   = help: the following implementations were found:
             <&S as Tr>
             <&mut S as Tr>

The current output is:

error[E0277]: the trait bound `S: Tr` is not satisfied
  --> src/main.rs:11:9
   |
7  | fn foo<T: Tr>(t: T) {}
   |           -- required by this bound in `foo`
...
11 |     foo(s);
   |         ^
   |         |
   |         expected an implementor of trait `Tr`
   |         help: consider borrowing here: `&s`

Ideally the output should look like:

Not sure but it should show both &s and &mut s. The previous output seemed to do a better job there. Maybe we could keep the last part of the previous output.

   = help: the following implementations were found:
             <&S as Tr>
             <&mut S as Tr>

Caused by #85369, although it is better in the sense it is more specific but I think it omitted quite some useful details.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions