Skip to content

Suggest borrowing parameter into function with generic argument where a matching impl is found #84973

Closed

Description

Given the following code: Rust Playground

fn main() {
    let f = Fancy{};
    let o = Other::new(f);
                 // ^^^^^^ needs to borrow here but the error does not say so
}

struct Fancy {}

impl <'a> SomeTrait for &'a Fancy {
}

trait SomeTrait {}

struct Other<'a, G> {
    a: &'a str,
    g: G,
}

// Broadly copied from https://docs.rs/petgraph/0.5.1/src/petgraph/dot.rs.html#70
impl<'a, G> Other<'a, G>
where
    G: SomeTrait,
{
    pub fn new(g: G) -> Self {
        Other {
            a: "hi",
            g: g,
        }
    }
}

The current output is:

error[E0277]: the trait bound `Fancy: SomeTrait` is not satisfied
  --> src/main.rs:3:24
   |
3  |     let o = Other::new(f);
   |                        ^ the trait `SomeTrait` is not implemented for `Fancy`
...
24 |     pub fn new(g: G) -> Self {
   |     ------------------------ required by `Other::<'a, G>::new`
   |
   = help: the following implementations were found:
             <&'a Fancy as SomeTrait>

Not really sure about the precise wording, but the help section should suggest borrowing f when passing it to Other::new(f) as rustc has figured out that there is an impl that just differs by the ownership.

The output is the same on Nightly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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