Skip to content

Wrong Clone bound suggestion #110446

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

Open
Banyc opened this issue Apr 17, 2023 · 1 comment · May be fixed by #127997
Open

Wrong Clone bound suggestion #110446

Banyc opened this issue Apr 17, 2023 · 1 comment · May be fixed by #127997
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Banyc
Copy link

Banyc commented Apr 17, 2023

Code

Playground: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=cda41942df9b5feddbbf2417959e717a

use std::collections::HashMap;

#[derive(Clone)]
struct Ctx<A> {
    a_map: HashMap<String, B<A>>,
}

#[derive(Clone)]
// help: consider annotating `B<A>` with `#[derive(Clone)]`
struct B<A> {
    // ----------- doesn't satisfy `B<A>: Clone`
    a: A,
}

fn foo<A>(ctx: &mut Ctx<A>) {
    let a_map = ctx.a_map.clone();
    //                    ^^^^^ method cannot be called on `HashMap<String, B<A>>` due to unsatisfied trait bounds
}

Current output

error[E0599]: the method `clone` exists for struct `HashMap<String, B<A>>`, but its trait bounds were not satisfied
   --> src/lib.rs:15:27
    |
10  | struct B<A> {
    | ----------- doesn't satisfy `B<A>: Clone`
...
15  |     let a_map = ctx.a_map.clone();
    |                           ^^^^^ method cannot be called on `HashMap<String, B<A>>` due to unsatisfied trait bounds
    |
   ::: /Users/USERNAME/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/std/src/collections/hash/map.rs:214:1
    |
214 | pub struct HashMap<K, V, S = RandomState> {
    | ----------------------------------------- doesn't satisfy `HashMap<String, B<A>>: Clone`
    |
    = note: the following trait bounds were not satisfied:
            `B<A>: Clone`
            which is required by `HashMap<String, B<A>>: Clone`
help: consider annotating `B<A>` with `#[derive(Clone)]`
    |
10  | #[derive(Clone)]
    |

For more information about this error, try `rustc --explain E0599`.
error: could not compile `test_clone_err` (lib) due to previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `test_clone_err` (lib test) due to previous error

Desired output

error[E0599]: the method `clone` exists for mutable reference `&mut Ctx<A>`, but its trait bounds were not satisfied
  --> src/lib.rs:16:9
   |
4  | struct Ctx<A> {
   | ------------- doesn't satisfy `Ctx<A>: Clone`
...
16 |     ctx.clone();
   |         ^^^^^ method cannot be called on `&mut Ctx<A>` due to unsatisfied trait bounds
   |
note: trait bound `A: Clone` was not satisfied
  --> src/lib.rs:3:10
   |
3  | #[derive(Clone)]
   |          ^^^^^ unsatisfied trait bound introduced in this `derive` macro
help: consider restricting the type parameter to satisfy the trait bound
   |
15 | fn foo<A>(ctx: &mut Ctx<A>) where A: Clone {
   |                             ++++++++++++++

For more information about this error, try `rustc --explain E0599`.
error: could not compile `test_clone_err` (lib test) due to previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `test_clone_err` (lib) due to previous error

Rationale and extra context

The root cause is that P in foo<P> has not been restricted by the Clone bound, but the compiler suggests users to add #[derive(Clone)] on struct B<A>, which has already been done.

Other cases

No response

Anything else?

rustc versions:

rustc 1.70.0-nightly (88fb1b922 2023-04-10)
rustc 1.71.0-nightly (d0f204e4d 2023-04-16)

I have tested the same code under both of the versions and the outputs are the same.

@Banyc Banyc 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 Apr 17, 2023
@Banyc Banyc changed the title Wrong Clone bound suggestion Wrong Clone bound suggestion Apr 17, 2023
@jyn514 jyn514 added the D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. label Apr 17, 2023
@Ezrashaw
Copy link
Contributor

Ezrashaw commented Apr 18, 2023

I think the problem here is that, for some reason, rustc isn't "peeling back" the trait bounds through the auto-derived impl (seems the same with a manual impl too). It says:

note: the following trait bounds were not satisfied:
            `B<A>: Clone`
            which is required by `HashMap<String, B<A>>: Clone`

Well, B<A>: Clone iff A: Clone which it doesn't seem to be picking up on.

@rustbot claim

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 D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. 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