Skip to content

Wrong inference for clone() #2435

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

Closed
ice1000 opened this issue Nov 27, 2019 · 3 comments · Fixed by #2463
Closed

Wrong inference for clone() #2435

ice1000 opened this issue Nov 27, 2019 · 3 comments · Fixed by #2463

Comments

@ice1000
Copy link
Contributor

ice1000 commented Nov 27, 2019

image

#[derive(Clone)]
struct Test();
fn test() {
    let t = &Test();
    let t = t.clone();
}
@flodiebold
Copy link
Member

Yeah, I've been working on this for a while already (on and off). Getting the method resolution order right is a bit tricky.

The problem is that &T has a Clone implementation as well. So we look at the type &Test, see that it has a Clone implementation, and choose that, never even looking at implementations for Test. What rustc does instead is look at the candidates in order of receiver type, not self type. So it looks first at the implementation for Test, because the clone method has receiver type &Test. Only if that doesn't exist does it try to autoref.

Of course, the second problem here is that we don't support derives yet, so even if the above problem was fixed this would still be inferred this way.

@ice1000
Copy link
Contributor Author

ice1000 commented Nov 28, 2019

Ok, but isn't derive simply adds a trait implementation to a type at all? Or is it a bigger problem I don't know?

@flodiebold
Copy link
Member

Well, that 'simply' glosses over a few details 🙂 See #1427.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants