Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
felipesere opened this issue May 6, 2021 · 0 comments · Fixed by #85369
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@felipesere
Copy link

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.

@felipesere felipesere added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 6, 2021
@felipesere felipesere changed the title Suggest borrowing argument of function with generic argument a matching impl is found. Suggest borrowing parameter into function with generic argument where a matching impl is found May 6, 2021
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue May 18, 2021
Suggest borrowing if a trait implementation is found for &/&mut <type>

This pull request fixes rust-lang#84973 by suggesting to borrow if a trait is not implemented for some type `T`, but it is for `&T` or `&mut T`. For instance:
```rust
trait Ti {}
impl<T> Ti for &T {}
fn foo<T: Ti>(_: T) {}

trait Tm {}
impl<T> Tm for &mut T {}
fn bar<T: Tm>(_: T) {}

fn main() {
    let a: i32 = 5;
    foo(a);

    let b: Box<i32> = Box::new(42);
    bar(b);
}
```
gives, on current nightly:
```
error[E0277]: the trait bound `i32: Ti` is not satisfied
  --> t2.rs:11:9
   |
3  | fn foo<T: Ti>(_: T) {}
   |           -- required by this bound in `foo`
...
11 |     foo(a);
   |         ^ the trait `Ti` is not implemented for `i32`

error[E0277]: the trait bound `Box<i32>: Tm` is not satisfied
  --> t2.rs:14:9
   |
7  | fn bar<T: Tm>(_: T) {}
   |           -- required by this bound in `bar`
...
14 |     bar(b);
   |         ^ the trait `Tm` is not implemented for `Box<i32>`

error: aborting due to 2 previous errors
```
whereas with my changes, I get:
```
error[E0277]: the trait bound `i32: Ti` is not satisfied
  --> t2.rs:11:9
   |
3  | fn foo<T: Ti>(_: T) {}
   |           -- required by this bound in `foo`
...
11 |     foo(a);
   |         ^
   |         |
   |         expected an implementor of trait `Ti`
   |         help: consider borrowing here: `&a`

error[E0277]: the trait bound `Box<i32>: Tm` is not satisfied
  --> t2.rs:14:9
   |
7  | fn bar<T: Tm>(_: T) {}
   |           -- required by this bound in `bar`
...
14 |     bar(b);
   |         ^
   |         |
   |         expected an implementor of trait `Tm`
   |         help: consider borrowing mutably here: `&mut b`

error: aborting due to 2 previous errors
```
In my implementation, I have added a "blacklist" to make these suggestions flexible. In particular, suggesting to borrow can interfere with other suggestions, such as to add another trait bound to a generic argument. I have tried to configure this blacklist to cause the least amount of test case failures, i.e. to model the current behavior as closely as possible (I only had to change one existing test case, and this change was quite clearly an improvement).
@bors bors closed this as completed in fad04ff May 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant