Skip to content

No hint about implementing clone on type mismatch when cloning reference #95535

New issue

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

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

Already on GitHub? # to your account

Closed
Noratrieb opened this issue Mar 31, 2022 · 2 comments · Fixed by #95585
Closed

No hint about implementing clone on type mismatch when cloning reference #95535

Noratrieb opened this issue Mar 31, 2022 · 2 comments · Fixed by #95585
Assignees
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

@Noratrieb
Copy link
Member

Noratrieb commented Mar 31, 2022

Given the following code: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=ebe1fe6ca75fd88cba9aa3a77cb625bf

struct NotClone;

fn main() {
    clone_thing(&NotClone);
}

fn clone_thing(nc: &NotClone) -> NotClone {
    nc.clone()
}

The current output is:

error[[E0308]](https://doc.rust-lang.org/nightly/error-index.html#E0308): mismatched types
 --> src/main.rs:8:5
  |
7 | fn clone_thing(nc: &NotClone) -> NotClone {
  |                                  -------- expected `NotClone` because of return type
8 |     nc.clone()
  |     ^^^^^^^^^^ expected struct `NotClone`, found `&NotClone`

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` due to previous error

Ideally the output should look like:

error[[E0308]](https://doc.rust-lang.org/nightly/error-index.html#E0308): mismatched types
 --> src/main.rs:8:5
  |
7 | fn clone_thing(nc: &NotClone) -> NotClone {
  |                                  -------- expected `NotClone` because of return type
8 |     nc.clone()
  |     ^^^^^^^^^^ expected struct `NotClone`, found `&NotClone`
  | note: `NotClone` does not implement clone, consider implementing it.

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` due to previous error
@Noratrieb Noratrieb 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 Mar 31, 2022
@compiler-errors
Copy link
Member

Oh this one is a neat one, and I've seen it in the wild quite a few times.

@rustbot claim

@vintium
Copy link

vintium commented Mar 31, 2022

I might suggest that the note be a little more detailed, something like:

note: there is no implementation of `Clone` for `NotClone`, so the implementation of `Clone` for `&T` was used. consider implementing `Clone` on `NotClone`.

# for free to join this conversation on GitHub. Already have an account? # 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.

3 participants