-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Confusing error message when deriving PartialOrd
for structs containing mutable slices
#45888
Comments
At least on nightly it has been fixed to deduplicate the errors. However the real problem is code like this (adapted from the derive expansion): struct Foo<'a, T: 'a>(&'a mut [T]);
impl<'a, T: PartialOrd + 'a> Foo<'a, T> {
fn bar(&self) {
match *self {
Foo(ref slice) => { *slice < *slice; } //~ERROR cannot borrow data mutably in a `&` reference
}
}
} For some reason the same issue does not occur with |
It's really weird, manually writing an implementation that calls use std::cmp;
#[derive(Eq, PartialEq)]
struct Foo<'a, T: 'a>(&'a mut [T]);
impl<'a, T: PartialOrd + 'a> PartialOrd for Foo<'a, T> {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
self.0.partial_cmp(&other.0)
}
} |
If you try to build from 65 #[inline]
66 fn lt(&self, __arg_0: &Foo<'a, T>) -> bool {
67 match *__arg_0 {
68 Foo(ref __self_1_0) =>
69 match *self {
70 Foo(ref __self_0_0) =>
71 (*__self_0_0) < (*__self_1_0) ||
72 !((*__self_1_0) < (*__self_0_0)) && false,
73 },
74 }
75 }
I think the actual implementation in question is |
It's happy if you change to an explicit call, like this playground: //(*__self_0_0) < (*__self_1_0) ||
// !((*__self_1_0) < (*__self_0_0)) && false,
::std::cmp::PartialOrd::lt(__self_0_0, __self_1_0) ||
!::std::cmp::PartialOrd::lt(__self_1_0, __self_0_0) && false |
It's very strange that the operators require ownership but don't actually take ownership. Maybe that could be improved in the typechecker/codegen/wherever, but it could lead to confusing behaviour where |
This no longer produces an error and compiles fine on 1.49. Closing. |
Minimal reproduction:
This produces the following error message repeated precisely 8 times. I cannot tell you why this is, I have no idea. I assume it's a failure in the
PartialOrd
derivation code that's not being reported correctly.The text was updated successfully, but these errors were encountered: