Skip to content
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

*x == *y for trait objects produce move error, so it is not equivalent to PartialEq::eq(&*x, &*y) even though the reference says it is #127215

Open
estebank opened this issue Jul 1, 2024 · 5 comments
Labels
A-borrow-checker Area: The borrow checker A-diagnostics Area: Messages for errors, warnings, and lints A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools A-trait-objects Area: trait objects, vtable layout C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

estebank commented Jul 1, 2024

The following code produces a move error:

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
}
error[E0507]: cannot move out of `*a2` which is behind a shared reference
  --> src/lib.rs:12:27
   |
12 |     println!("{}", *a1 == *a2); // doesn't work
   |                           ^^^ move occurs because `*a2` has type `Box<dyn Animal>`, which does not implement the `Copy` trait

But according to the reference, it should be equivalent to the following, which does work:

fn f(a1: &Box<dyn Animal>, a2: &Box<dyn Animal>) {
    println!("{}", PartialEq::eq(&*a1, &*a2)); // works
}

It seems to be specific to trait objects; e.g. replacing Box<dyn Animal> with Box<[String]> will make both versions of f compile.

Originally reported in #123056 (comment)

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints A-borrow-checker Area: The borrow checker T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools A-trait-objects Area: trait objects, vtable layout labels Jul 1, 2024
@eggyal
Copy link
Contributor

eggyal commented Jul 2, 2024

Interestingly it works if the arguments to f are Box<dyn Animal> or &dyn Animal rather than &Box<dyn Animal>.

@finnbear
Copy link
Contributor

finnbear commented Aug 11, 2024

Same issue occurs if the container, in this case RcPtrEq, implements PartialEq independently of the dyn trait object:

#![feature(coerce_unsized)]
#![feature(unsize)]

use std::rc::Rc;
use std::marker::Unsize;
use std::ops::{CoerceUnsized};

pub fn main() {
    //#[derive(PartialEq)]
    struct Foo {
        field: RcPtrEq<dyn Fn()>,
    }
    
    // Doesn't compile.
    #[automatically_derived]
    impl ::core::cmp::PartialEq for Foo {
        #[inline]
        // error[E0507]: cannot move out of `other.field` which is behind a shared reference
        fn eq(&self, other: &Foo) -> bool { self.field == other.field }
    }
    
    // Works.
    /*
    impl PartialEq for Foo {
        #[inline]
        fn eq(&self, other: &Foo) -> bool { PartialEq::eq(&self.field, &other.field) }
    }
    */
}

pub struct RcPtrEq<T: ?Sized>(Rc<T>);

impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<RcPtrEq<U>> for RcPtrEq<T> {}

impl<T: ?Sized> PartialEq for RcPtrEq<T> {
    fn eq(&self, other: &Self) -> bool {
        Rc::ptr_eq(&self.0, &other.0)
    }
}

Playground link

Strangely, removing CoercedUnized makes it compile.

@estebank estebank added the C-bug Category: This is a bug. label Aug 12, 2024
@Artikae
Copy link

Artikae commented Sep 12, 2024

This also breaks the PartialEq derive macro when used on structs.

use std::rc::Rc;
trait Trait {}
impl PartialEq for dyn Trait {
    fn eq(&self, _: &Self) -> bool { todo!() }
}
#[derive(PartialEq)]
struct User(Rc<dyn Trait>);

Playground link

@SaltyKitkat
Copy link
Contributor

This also breaks the PartialEq derive macro when used on structs.

use std::rc::Rc;
trait Trait {}
impl PartialEq for dyn Trait {
fn eq(&self, _: &Self) -> bool { todo!() }
}
#[derive(PartialEq)]
struct User(Rc);
Playground link

I met this problem. It's quite annoying for me since I cannot use PartialEq derive macro.

So is there any progress on this recently?

@QuineDot
Copy link

An earlier duplicate:

#31740

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-borrow-checker Area: The borrow checker A-diagnostics Area: Messages for errors, warnings, and lints A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools A-trait-objects Area: trait objects, vtable layout C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants