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

rustc doesn't correctly derive Copy/Clone in the presence of associated type members #23575

Closed
eduardoleon opened this issue Mar 21, 2015 · 3 comments
Labels
A-typesystem Area: The type system

Comments

@eduardoleon
Copy link

In the following snippet, PairFoo<F>'s members are all Copy (resp. Clone) whenever F::T is Copy (resp. Clone). In spite of this, neither does #[derive(Copy)] #[derive(Clone)] work, nor can I provide my own Copy impl.

use std::marker::Copy;
use std::clone::Clone;

#[derive(Copy)]
#[derive(Clone)]
struct Pair<T>(pub T, pub T);

trait Foo { type T; }

// #[derive(Copy)]
// #[derive(Clone)]
struct PairFoo<F: Foo>(pub F::T, pub F::T);

// this one doesn't work
impl<F, T> Copy for PairFoo<F>
    where F: Foo<T = T>,
          T: Copy
{
}

// this one does work
impl<F, T> Clone for PairFoo<F>
    where F: Foo<T = T>,
          T: Clone
{
    fn clone(&self) -> PairFoo<F>
    {
        PairFoo(self.0.clone(), self.1.clone())
    }
}

rustc's output:

<anon>:12:1: 16:2 error: the trait `Copy` may not be implemented for this type; field `<unnamed_field>` does not implement `Copy` [E0204]
<anon>:12 impl<F, T> Copy for PairFoo<F>
<anon>:13     where F: Foo<T = T>,
<anon>:14           T: Copy
<anon>:15 {
<anon>:16 }
error: aborting due to previous error
playpen: application terminated with error code 101
@lambda-fairy
Copy link
Contributor

Note that the Copy instance passes if we inline the T:

impl<F> Copy for PairFoo<F>
    where F: Foo,
          <F as Foo>::T: Copy
{
}

So there are two orthogonal issues here:

  1. #[derive] doesn't take associated types into account (should be fixed by Add associated types support for #[derive(...)] #21237)
  2. The Copy instance check doesn't follow equality constraints

@steveklabnik
Copy link
Member

Triage: still true today.

@Mark-Simulacrum
Copy link
Member

Closing in favor of #26925.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-typesystem Area: The type system
Projects
None yet
Development

No branches or pull requests

4 participants