Skip to content

Using derive(PartialEq) on an enum with a variant that accepts(Box<dyn SomeTrait>) causes cryptic build error. #123056

Closed
@mkoscumb

Description

@mkoscumb

Code

trait Animal {
    fn noise(&self) -> String;
}

struct Cat {}

impl Animal for Cat {
    fn noise(&self) -> String {
        "Meow".to_owned()
    }
}

impl PartialEq for dyn Animal {
    fn eq(&self, other: &Self) -> bool {
        self.noise() == other.noise()
    }
}

#[derive(PartialEq)]
enum Things {
    Animal(Box<dyn Animal>),
    Vegitable,
}

fn use_thing(thing: Things) {
    match thing {
        Things::Animal(animal) => {
            println!("{}", animal.noise())
        }
        Things::Vegitable => {
            println!("Yuck!")
        }
    }
}

fn main() {
    let kitty = Cat {};
    use_thing(Things::Animal(Box::new(kitty)));
    use_thing(Things::Vegitable);
}

Current output

Compiling playground v0.0.1 (/playground)
error[E0507]: cannot move out of `*__arg1_0` which is behind a shared reference
  --> src/main.rs:21:12
   |
19 | #[derive(PartialEq)] // To fix error, comment out this line and uncomment 25-34
   |          --------- in this derive macro expansion
20 | enum Things {
21 |     Animal(Box<dyn Animal>),
   |            ^^^^^^^^^^^^^^^ move occurs because `*__arg1_0` has type `Box<dyn Animal>`, which does not implement the `Copy` trait
   |
   = note: this error originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0507`.
error: could not compile `playground` (bin "playground") due to 1 previous error

Desired output

Either it should succeed or generate a better error message.

Rationale and extra context

No response

Other cases

No response

Rust Version

Repro in playground.
rustc 1.77.0

Anything else?

Playground link:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=423832065e26b689e7aa16b31c96275c

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-confusingDiagnostics: Confusing error or lint that should be reworked.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions