Skip to content

Can't use #[deriving(Clone)] on struct with Clone bounds #19361

Closed
@japaric

Description

@japaric
Member

STR

#[deriving(Clone)]
//~^ error: trait `Clone` already appears in the list of bounds [E0127]
struct Foo<'a, T: 'a + Clone> {
    foo: &'a [T],
}

fn main() {}

Output

If you look at the --pretty=expanded version, you'll see T is bounded twice by the Clone trait

#![feature(phase)]
#![no_std]
#![feature(globs)]
#[phase(plugin, link)]
extern crate "std" as std;
#[prelude_import]
use std::prelude::*;
//~^ error: trait `Clone` already appears in the list of bounds [E0127]
struct Foo<'a, T: 'a + Clone> {
    foo: &'a [T],
}
#[automatically_derived]
impl <'a, T: ::std::clone::Clone + 'a + Clone> ::std::clone::Clone for
     Foo<'a, T> {
    #[inline]
    fn clone(&self) -> Foo<'a, T> {
        match *self {
            Foo { foo: ref __self_0_0 } =>
            Foo{foo: ::std::clone::Clone::clone(&(*__self_0_0)),},
        }
    }
}

Version

rustc 0.13.0-dev (82fc1aa87 2014-11-27 10:11:19 +0000)

The obvious fix seems to make the deriving syntax extension filter out the duplicated Clone just by looking at the trait name. The problem is that ::std::clone::Clone may not be the same trait as Clone, but AFAIK the syntax extension can't know that, since that (path) information is collected after macro expansion.

Work-around

One workaround to this issue is using the fact that the deriving syntax extension doesn't look at where bounds (see #19358), the following code compiles:

#[deriving(Clone)]
struct Foo<'a, T: 'a> where T: Clone {
    foo: &'a [T],
}

fn main() {}

Activity

alexcrichton

alexcrichton commented on Aug 13, 2015

@alexcrichton
Member

This now works, so I think it was fixed in the meantime? Yay!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-syntaxextArea: Syntax extensions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @alexcrichton@kmcallister@japaric

        Issue actions

          Can't use `#[deriving(Clone)]` on struct with `Clone` bounds · Issue #19361 · rust-lang/rust