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

Clone suggestion appears inconsistently #112857

Closed
strottos opened this issue Jun 20, 2023 · 3 comments
Closed

Clone suggestion appears inconsistently #112857

strottos opened this issue Jun 20, 2023 · 3 comments
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

@strottos
Copy link
Contributor

strottos commented Jun 20, 2023

Code

fn f<T>(a: &T) -> T {
    let ret = a.clone();
    ret
}

Current output

error[E0308]: mismatched types
 --> ./test.rs:3:5
  |
1 | fn f<T>(a: &T) -> T {
  |      -            - expected `T` because of return type
  |      |
  |      this type parameter
2 |     let ret = a.clone();
3 |     ret
  |     ^^^ expected type parameter `T`, found `&T`
  |
  = note: expected type parameter `T`
                  found reference `&T`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.

Desired output

error[E0308]: mismatched types
 --> ./test.rs:3:5
  |
1 | fn f<T>(a: &T) -> T {
  |      -            - expected `T` because of return type
  |      |
  |      this type parameter
2 |     let ret = a.clone();
3 |     ret
  |     ^^^ expected type parameter `T`, found `&T`
  |
  = note: expected type parameter `T`
                  found reference `&T`
note: `T` does not implement `Clone`, so `&T` was cloned instead
 --> ./test.rs:2:5
  |
2 |     let ret = a.clone();
  |               ^
help: consider restricting type parameter `T`
  |
1 | fn f<T: Clone>(a: &T) -> T {
  |       +++++++

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.

Rationale and extra context

This program gives the right hint:

fn f<T>(a: &T) -> T {
    a.clone()
}

Similarly we have the following that gives the hint:

fn f<T>(a: &T) -> T {
    let ret: T = a.clone();
    ret
}

Not the end of the world certainly but the diagnostic is really helpful and it would be nice if we could get the first one to output the issue too.

I think (having gone through the code a bit, though I'm new to this) that &a is being cloned rather than a as a doesn't necessarily implement Clone and then on a line 3 a type mismatch is found on a line that doesn't specify clone. So when it does the not_type_is_not_clone check in the rustc_hir_typeck crate it's only checking ret and it doesn't see that it comes from a clone attempt on the wrong type.

Other cases

No response

Anything else?

No response

@strottos strottos 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 Jun 20, 2023
@strottos
Copy link
Contributor Author

I can see two ways around this, not sure which would be better or indeed if there's a better solution. Would love to have a go myself but would almost certainly need some guidance:

  1. When we find an error like the above we trace back the steps through the statements to see where the type was initialized and perform checks there too.
  2. Before running the check on the first statement we perform some prechecks to see that ret here necessarily had to have type T rather than the &T it thinks it should have. Possibly we could run some type checks in reverse, going from the block expression backwards through the statements before all the check_stmt calls in check_block_with_expected.

Neither seem trivial to me, as I say I'm very new to this and still finding my way round rustc mostly as a hacker.

estebank added a commit to estebank/rust that referenced this issue Jun 23, 2023
Address rust-lang#112857:

```
error[E0308]: mismatched types
  --> $DIR/explain_clone_autoref.rs:28:5
   |
LL | fn clone_thing3(nc: &NotClone) -> NotClone {
   |                                   -------- expected `NotClone` because of return type
...
LL |     nc
   |     ^^ expected `NotClone`, found `&NotClone`
   |
note: `NotClone` does not implement `Clone`, so `&NotClone` was cloned instead
  --> $DIR/explain_clone_autoref.rs:26:14
   |
LL |     let nc = nc.clone();
   |              ^^
help: consider annotating `NotClone` with `#[derive(Clone)]`
   |
LL + #[derive(Clone)]
LL | struct NotClone;
   |
```
@strottos
Copy link
Contributor Author

@rustbot claim

As I'm looking into this still. Ongoing.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jul 25, 2023
…fee1-dead

Check for `<&NotClone as Clone>::clone()` calls and suggest to add Clone trait appropriately

Added recursive checking back up the HIR to see if a `Clone` suggestion would be helpful.

Addresses rust-lang#112857

Largely based on: rust-lang#112977
@strottos
Copy link
Contributor Author

This is now resolved after the above linked PR closed it last night. Checked in later master and no longer happening.

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

No branches or pull requests

1 participant