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

Really confusing error when clone falls back to &T instance #54377

Closed
RalfJung opened this issue Sep 20, 2018 · 2 comments
Closed

Really confusing error when clone falls back to &T instance #54377

RalfJung opened this issue Sep 20, 2018 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@RalfJung
Copy link
Member

RalfJung commented Sep 20, 2018

Consider the following code:

trait Foo {
    type Assoc;
}

#[derive(Clone)]
struct Bar<F: Foo>(F::Assoc);

fn try_clone<F: Foo>(x: &Bar<F>) -> Bar<F>
    where F::Assoc: Clone
{
    x.clone()
}

This fails saying

error[E0308]: mismatched types
  --> src/lib.rs:11:5
   |
8  | fn try_clone<F: Foo>(x: &Bar<F>) -> Bar<F>
   |                                     ------ expected `Bar<F>` because of return type
...
11 |     x.clone()
   |     ^^^^^^^^^ expected struct `Bar`, found &Bar<F>
   |
   = note: expected type `Bar<F>`
              found type `&Bar<F>`

error: aborting due to previous error

I just stared at this for at least 5 minutes wondering how in the world clone could fail to return an owned copy... until I realized that what really happens is that it does (&x).clone() instead of Bar<F>::clone(x) because the latter fails to satisfy all trait constraints.

Might be worth having a specialized diagnostic for when clone and auto-(de)ref come together? Or more general, for when auto-(de)ref does something, and then fails, it should indicate somehow that auto-(de)ref happened, and maybe remark that some other possibilities were skipped because some bounds were not satisfied.

@csmoe csmoe added the A-diagnostics Area: Messages for errors, warnings, and lints label Sep 20, 2018
@earthengine
Copy link

earthengine commented Sep 21, 2018

FYI: A relevent internal post.

For short, if you incidently called clone on a reference that the type itself does not implement Clone, the error message is confusing.

My propose there is to have a separate lint on using clone on a reference.

@Mark-Simulacrum
Copy link
Member

Duplicate of #34896.

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
Projects
None yet
Development

No branches or pull requests

4 participants