Skip to content

Deriving can't be used within macros because it generates duplicate impls #6976

Closed
@bstrie

Description

@bstrie

This:

macro_rules! define_vec (
    () => (
        mod foo {
            #[deriving(Eq)]
            pub struct bar;
        }
    )
)

define_vec!()

fn main() {}

...doesn't compile:

moon.rs:4:23: 4:25 error: conflicting implementations for a trait
moon.rs:4             #[deriving(Eq)]
                                 ^~
moon.rs:4:23: 4:25 note: note conflicting implementation here
moon.rs:4             #[deriving(Eq)]
                                 ^~
moon.rs:4:23: 4:25 error: conflicting implementations for a trait
moon.rs:4             #[deriving(Eq)]
                                 ^~
moon.rs:4:23: 4:25 note: note conflicting implementation here
moon.rs:4             #[deriving(Eq)]
                                 ^~
error: aborting due to 2 previous errors

Taking a look at the expanded code:

mod foo {
    #[deriving(Eq)]
    pub struct bar;
    #[doc = "Automatically derived."]
    pub impl ::std::cmp::Eq for bar {
        pub fn eq(&self, __arg_0: &bar) -> ::bool {
            match *__arg_0 { bar => match *self { bar => true } }
        }
        pub fn ne(&self, __arg_0: &bar) -> ::bool {
            match *__arg_0 { bar => match *self { bar => false } }
        }
    }
    #[doc = "Automatically derived."]
    pub impl ::std::cmp::Eq for bar {
        pub fn eq(&self, __arg_0: &bar) -> ::bool {
            match *__arg_0 { bar => match *self { bar => true } }
        }
        pub fn ne(&self, __arg_0: &bar) -> ::bool {
            match *__arg_0 { bar => match *self { bar => false } }
        }
    }
}


fn main() { }

Notice that it's generating a second identical impl for the given trait. I've tested that this happens with both Eq and ToStr, so I presume it applies to the deriving code in general.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-syntaxextArea: Syntax extensionsE-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions