Skip to content

Error in derived PartialOrd impl on structs with mut reference fields #43816

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

Closed
arkeet opened this issue Aug 11, 2017 · 2 comments
Closed

Error in derived PartialOrd impl on structs with mut reference fields #43816

arkeet opened this issue Aug 11, 2017 · 2 comments
Labels
C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@arkeet
Copy link

arkeet commented Aug 11, 2017

This fails to compile, with a rather cryptic error message: [playground]

#[derive(PartialEq, PartialOrd)]
struct Bad<'a>(&'a mut u8);

This is one of the errors in the expanded code (the rest are similar):

// error[E0389]: cannot borrow data mutably in a `&` reference
fn lt(&self, __arg_0: &Bad<'a>) -> bool {
    match *__arg_0 {
        Bad(ref __self_1_0) =>
//          -------------- consider changing this to `ref mut __self_1_0`
        match *self {
            Bad(ref __self_0_0) =>
            (*__self_0_0) < (*__self_1_0) ||
//                          ^^^^^^^^^^^^^ assignment into an immutable reference
                !((*__self_1_0) < (*__self_0_0)) && false,
        },
    }
}

Seems quite similar to #15689.

@Mark-Simulacrum Mark-Simulacrum added C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Aug 13, 2017
@durka
Copy link
Contributor

durka commented Aug 16, 2017

Why is the derive code dereferencing the captures‽ There's a forwarding impl!

rust/src/libcore/cmp.rs

Lines 943 to 951 in 1e60a47

// &mut pointers
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, 'b, A: ?Sized, B: ?Sized> PartialEq<&'b mut B> for &'a mut A where A: PartialEq<B> {
#[inline]
fn eq(&self, other: &&'b mut B) -> bool { PartialEq::eq(*self, *other) }
#[inline]
fn ne(&self, other: &&'b mut B) -> bool { PartialEq::ne(*self, *other) }
}

@Mark-Simulacrum
Copy link
Member

Appears to work today. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants