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

Derived PartialEq for Box<dyn Trait> is incorrect. #74284

Closed
davidli2010 opened this issue Jul 13, 2020 · 2 comments
Closed

Derived PartialEq for Box<dyn Trait> is incorrect. #74284

davidli2010 opened this issue Jul 13, 2020 · 2 comments

Comments

@davidli2010
Copy link

pub trait T {}

impl PartialEq for dyn T {
    fn eq(&self, _other: &Self) -> bool {
        todo!()
    }
}

#[derive(PartialEq)]
pub struct A {
    child: Box<dyn T>,
}

impl T for A {}

// impl PartialEq for A {
//     fn eq(&self, other: &Self) -> bool {
//         *self.child == *other.child
//     }
// }

fn main() {}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0507]: cannot move out of `*__self_1_0` which is behind a shared reference
  --> src/main.rs:11:5
   |
11 |     child: Box<dyn T>,
   |     ^^^^^^^^^^^^^^^^^ move occurs because `*__self_1_0` has type `std::boxed::Box<dyn T>`, which does not implement the `Copy` trait
   |
   = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error

For more information about this error, try `rustc --explain E0507`.
error: could not compile `playground`.

To learn more, run the command again with --verbose.

@davidli2010
Copy link
Author

If I remove #[derive(PartialEq)] and impl it manually:

impl PartialEq for A {
    fn eq(&self, other: &Self) -> bool {
        *self.child == *other.child
    }
}

It works.

@sfackler
Copy link
Member

Duplicate of #39128.

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

No branches or pull requests

2 participants