-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Type errors with deriving on structures/enums with internal references #15689
Comments
Same thing with #[deriving(Clone)]
enum Test<'a> {
Slice(&'a int)
}
fn main() {}
|
cc me |
@pnkfelix, I did use the most recent nightly, so that PR is probably is present in it, but it happens not only with |
@pnkfelix I believe this has been a problem for a while. I don't think it's related to the recent |
This has been a problem for a while, and (as @netvl correctly worked out) is caused by autoderef going through the reference. It will be properly fixable with UFCS, since #7621 is similar. |
We can likely get a janky version of ufcs today with something like: #[automatically_derived]
impl Eq for Foo {
fn eq(&self, other: &Foo) -> bool {
fn eq<T: Eq>(t1: &T, t2: &T) -> bool { t1.eq(t2) }
eq(&self.field1, &other.field1) && ...
}
} That should also work for clone and friends (it's how libcore used to do some of its tests) |
"poor man's UFCS" |
Fixed by #18578 |
Hm sorry, half-fixed, but the next parts are coming soon in #18467! |
Actually this is #[deriving(PartialEq, PartialOrd)]
struct Foo(&'static int);
//~^ error: mismatched types: expected `&int`, found `&&'static int` (expected int, found &-ptr) The problem is the expansion of the fn partial_cmp(&self, __arg_0: &Foo) ->
::std::option::Option<::std::cmp::Ordering> {
match *__arg_0 {
Foo(ref __self_1_0) =>
match *self {
Foo(ref __self_0_0) => {
let __test = (*__self_0_0).partial_cmp(&(*__self_1_0));
if __test == ::std::option::Some(::std::cmp::Equal) {
::std::option::Some(::std::cmp::Equal)
} else { __test }
}
},
}
}
I'll send a patch later. |
Closes #18738 cc #15689 r? @alexcrichton cc @cmr
This code:
fails to compile with the following error:
Basically I understand the reason for this error, According to
rustc --pretty expanded
output:here
(*__self_0).eq(...)
resolves to correct method call due to auto[de]ref on method target, but its argument is still of&&'a int
type, which leads to the type error.I can see why this can be the intended behavior, but I'd argue that this is counterintuitive and very inconvenient: for example, I have a large enum with a lot of references inside its variants, and I'd like to make it comparable with itself, but autoderiving won't work due to this behavior.
The text was updated successfully, but these errors were encountered: