-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Using derive(PartialEq) on an enum with a variant that accepts(Box<dyn SomeTrait>) causes cryptic build error. #123056
Comments
this regressed in nightly-2019-06-05 :)
ending github query because we found starting sha: 6ffb8f5 |
This can occur outside of trait Animal {
fn noise(&self) -> String;
}
impl PartialEq for dyn Animal {
fn eq(&self, other: &Self) -> bool {
self.noise() == other.noise()
}
}
fn f(a1: &Box<dyn Animal>, a2: &Box<dyn Animal>) {
println!("{}", *a1 == *a2); // doesn't work
} According to the reference, this should be equivalent to: fn f(a1: &Box<dyn Animal>, a2: &Box<dyn Animal>) {
println!("{}", PartialEq::eq(&*a1, &*a2)); // works
} That means both versions should compile if I understand correctly (because the latter version does compile). It seems to be specific to trait objects; e.g. replacing |
Do not add leading asterisk in the `PartialEq` I think we should address this issue, however I am not exactly sure, if this is the right way to do it. It is related to the rust-lang#123056. Imagine the simplified code: ```rust trait MyTrait {} impl PartialEq for dyn MyTrait { fn eq(&self, _other: &Self) -> bool { true } } #[derive(PartialEq)] enum Bar { Foo(Box<dyn MyTrait>), } ``` On the nightly compiler, the `derive` produces invalid code with the weird error message: ``` error[E0507]: cannot move out of `*__arg1_0` which is behind a shared reference --> src/main.rs:11:9 | 9 | #[derive(PartialEq)] | --------- in this derive macro expansion 10 | enum Things { 11 | Foo(Box<dyn MyTrait>), | ^^^^^^^^^^^^^^^^ move occurs because `*__arg1_0` has type `Box<dyn MyTrait>`, 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) ``` It may be related to the perfect derive problem, although requiring the _type_ to be `Copy` seems unfortunate because it is not necessary. Besides, we are adding the extra dereference only for the diagnostics?
Do not add leading asterisk in the `PartialEq` I think we should address this issue, however I am not exactly sure, if this is the right way to do it. It is related to the rust-lang#123056. Imagine the simplified code: ```rust trait MyTrait {} impl PartialEq for dyn MyTrait { fn eq(&self, _other: &Self) -> bool { true } } #[derive(PartialEq)] enum Bar { Foo(Box<dyn MyTrait>), } ``` On the nightly compiler, the `derive` produces invalid code with the weird error message: ``` error[E0507]: cannot move out of `*__arg1_0` which is behind a shared reference --> src/main.rs:11:9 | 9 | #[derive(PartialEq)] | --------- in this derive macro expansion 10 | enum Things { 11 | Foo(Box<dyn MyTrait>), | ^^^^^^^^^^^^^^^^ move occurs because `*__arg1_0` has type `Box<dyn MyTrait>`, 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) ``` It may be related to the perfect derive problem, although requiring the _type_ to be `Copy` seems unfortunate because it is not necessary. Besides, we are adding the extra dereference only for the diagnostics?
Triage: the original case is now properly handled, but the move error issue on |
Code
Current output
Desired output
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
The text was updated successfully, but these errors were encountered: