-
Notifications
You must be signed in to change notification settings - Fork 13.3k
#[derive_PartialEq] does not work if struct/enum carries a Boxed trait. #24047
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
Comments
This is currently working as intended in terms of that it doesn't compile. The |
The derived PartialEq is not object-safe, but in my actual code I was able to implement an object-safe PartialEq which satisfied my needs in the case of the trait that was boxed. Deriving Specifically in my case - and here may be Doing It Wrong - my boxed trait defines a method which a group of memberless structs all implement, expressively defining different modes of operation. I only needed An example of what I did (which needs nightly to compile) is below:
|
I've also run into this issue, using |
Using that trait struct TestStruct {
member: Box<Foo>
}
// Compiles
impl PartialEq for TestStruct {
fn eq(&self, rhs: &Self) -> bool {
&self.member == &rhs.member
}
}
// ERROR: cannot move out of borrowed content
impl PartialEq for TestStruct {
fn eq(&self, rhs: &Self) -> bool {
self.member == rhs.member
}
} I don't see why the second should fail to compile. The |
The code in the initial post however will never compile, so maybe edit that to clarify. |
Seems like @bluss' code can be fixed by changing |
cleanups and fixes for #[derive] This contains a bunch of little cleanups and fixes to `#[derive]`. There are more extensive comments on each individual commit. - hygiene cleanups - use `discriminant_value` instead of variant index in `#[derive(Hash)]` - ~~don't move out of borrowed content in `#[derive(PartialOrd, PartialEq)]`~~ - use `intrinsics::unreachable()` instead of `unreachable!()` I don't believe there are any breaking changes in here, but I do want some more eyes on that. Fixes rust-lang#2810 (!), I believe (we still assume that "std" or "core" is the standard library but so does the rest of rustc...). Fixes rust-lang#21714 (cc @apasel422). ~~Fixes~~ (postponed) rust-lang#24047 (cc @withoutboats @bluss). Fixes rust-lang#31714 (cc @alexcrichton @bluss). Fixes rust-lang#31886 (cc @oli-obk).
cleanups and fixes for #[derive] This contains a bunch of little cleanups and fixes to `#[derive]`. There are more extensive comments on each individual commit. - hygiene cleanups - use `discriminant_value` instead of variant index in `#[derive(Hash)]` - ~~don't move out of borrowed content in `#[derive(PartialOrd, PartialEq)]`~~ - use `intrinsics::unreachable()` instead of `unreachable!()` I don't believe there are any breaking changes in here, but I do want some more eyes on that. Fixes #2810 (!), I believe (we still assume that "std" or "core" is the standard library but so does the rest of rustc...). Fixes #21714 (cc @apasel422). ~~Fixes~~ (postponed) #24047 (cc @withoutboats @bluss). Fixes #31714 (cc @alexcrichton @bluss). Fixes #31886 (cc @oli-obk).
…Ord)] The derived code was doing this (assume `struct Foo(i32);`): ``` fn lt(&self, other: &Foo) -> bool { match *other { Foo(ref oi) => match *self { Foo(ref si) => *si < *oi } } } ``` When the struct member is a boxed trait, this can cause borrowck errors. A solution is to change the derived code to this: ``` fn lt(&self, other: &Foo) -> bool { match *other { Foo(ref oi) => match *self { Foo(ref si) => &*si < &*oi } } } ``` I don't see any reason that this could break code, but I am a little nervous.
I'm not sure I quite understand what needs to be done here. The original issue implies that Could someone give a summary of this issue, preferably giving some concrete things that need to be done? |
I was pretty new to Rust when I posted this and I don't think implementing derive of PartialEq for trait objects by using their type ids is a very good idea; looks like @bluss's issue was fixed a year ago, I'm just going to close this. |
http://is.gd/HvndbI
Results in a compiler error:
cannot move out of borrowed content ... in expansion of #[derive_PartialEq]
It'd be great if the PartialEq derivation were written in such a way that this were possible.
The text was updated successfully, but these errors were encountered: