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

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

Closed
japaric opened this issue Nov 27, 2014 · 1 comment
Closed

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

japaric opened this issue Nov 27, 2014 · 1 comment
Labels
A-syntaxext Area: Syntax extensions

Comments

@japaric
Copy link
Member

japaric commented Nov 27, 2014

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() {}
@kmcallister kmcallister added the A-syntaxext Area: Syntax extensions label Jan 16, 2015
@alexcrichton
Copy link
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
Labels
A-syntaxext Area: Syntax extensions
Projects
None yet
Development

No branches or pull requests

3 participants