Skip to content

Cannot impl Clone trait for [T; $N] with T from this crate #25822

Closed
@muggenhor

Description

@muggenhor

Test case:

use std::collections::BitSet;

#[derive(Clone)]
struct Cell {
    content: BitSet,
}

#[derive(Clone)]
struct Field {
    cells: [[Cell; 9]; 9],
}
tst.rs:10:5: 10:26 error: the trait `core::marker::Copy` is not implemented for the type `Cell` [E0277]
tst.rs:10     cells: [[Cell; 9]; 9],
              ^~~~~~~~~~~~~~~~~~~~~
tst.rs:8:10: 8:15 note: in expansion of #[derive_Clone]
tst.rs:8:10: 8:15 note: expansion site
error: aborting due to previous error

Apparently implementing Clone on the "scalar" Cell isn't enough to be able to use it on arrays of Cell, so I try to manually implement it:

use std::collections::BitSet;

#[derive(Clone)]
struct Cell {
    content: BitSet,
}

impl Clone for [Cell; 9] {
    fn clone(&self) -> [Cell; 9] {
        [self[0].clone(), self[1].clone(), self[2].clone(),
         self[3].clone(), self[4].clone(), self[5].clone(),
         self[6].clone(), self[7].clone(), self[8].clone()]
    }
}

impl Clone for [[Cell; 9]; 9] {
    fn clone(&self) -> [[Cell; 9]; 9] {
        [self[0].clone(), self[1].clone(), self[2].clone(),
         self[3].clone(), self[4].clone(), self[5].clone(),
         self[6].clone(), self[7].clone(), self[8].clone()]
    }
}

#[derive(Clone)]
struct Field {
    cells: [[Cell; 9]; 9],
}

That however gets me two problems:

tst.rs:8:1: 14:2 error: the impl does not reference any types defined in this crate; only traits defined in the current crate can be implemented for arbitrary types [E0117]
tst.rs:8 impl Clone for [Cell; 9] {
tst.rs:9     fn clone(&self) -> [Cell; 9] {
tst.rs:10         [self[0].clone(), self[1].clone(), self[2].clone(),
tst.rs:11          self[3].clone(), self[4].clone(), self[5].clone(),
tst.rs:12          self[6].clone(), self[7].clone(), self[8].clone()]
tst.rs:13     }
          ...
tst.rs:16:1: 22:2 error: the impl does not reference any types defined in this crate; only traits defined in the current crate can be implemented for arbitrary types [E0117]
tst.rs:16 impl Clone for [[Cell; 9]; 9] {
tst.rs:17     fn clone(&self) -> [[Cell; 9]; 9] {
tst.rs:18         [self[0].clone(), self[1].clone(), self[2].clone(),
tst.rs:19          self[3].clone(), self[4].clone(), self[5].clone(),
tst.rs:20          self[6].clone(), self[7].clone(), self[8].clone()]
tst.rs:21     }
          ...
tst.rs:16:1: 22:2 error: conflicting implementations for trait `core::clone::Clone` [E0119]
tst.rs:16 impl Clone for [[Cell; 9]; 9] {
tst.rs:17     fn clone(&self) -> [[Cell; 9]; 9] {
tst.rs:18         [self[0].clone(), self[1].clone(), self[2].clone(),
tst.rs:19          self[3].clone(), self[4].clone(), self[5].clone(),
tst.rs:20          self[6].clone(), self[7].clone(), self[8].clone()]
tst.rs:21     }
          ...
tst.rs:16:1: 22:2 note: conflicting implementation in crate `core`
tst.rs:16 impl Clone for [[Cell; 9]; 9] {
tst.rs:17     fn clone(&self) -> [[Cell; 9]; 9] {
tst.rs:18         [self[0].clone(), self[1].clone(), self[2].clone(),
tst.rs:19          self[3].clone(), self[4].clone(), self[5].clone(),
tst.rs:20          self[6].clone(), self[7].clone(), self[8].clone()]
tst.rs:21     }
          ...
error: aborting due to 3 previous errors

The first being that I cannot implement Clone for [T; $N] (where T is either Cell or [Cell; 9] and N is 9). The second refers to a conflict in core, probably that's caused by array.rs and [[T; 9]; 9 somehow implementing the Copy trait (though I don't see how). Part of this can probably be solved by adding a separate implementation of Clone for [T; $N] where T implements the Clone trait (but not the Copy trait). I'm not sure how to prevent the conflict though.

PS This looks similar to #24745 which is mentioned as being caused by #23086 (RFC 1023)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions