Skip to content

Derived Clone impl is not simplified if Clone is derived before Copy #124794

Open
@LunarLambda

Description

@LunarLambda

Of these 4 ways to derive Copy and Clone:

#[derive(Copy, Clone)]
struct Foo(i32);

#[derive(Clone, Copy)]
struct Bar(i32);

#[derive(Copy)]
#[derive(Clone)]
struct Baz(i32);

#[derive(Clone)]
#[derive(Copy)]
struct Qux(i32);

The last one (derive Clone, then derive Copy) does not use the simplified form of the Clone derive for Copy types:

struct Foo(i32);
#[automatically_derived]
impl ::core::marker::Copy for Foo { }
#[automatically_derived]
impl ::core::clone::Clone for Foo {
    #[inline]
    fn clone(&self) -> Foo {
        let _: ::core::clone::AssertParamIsClone<i32>;
        *self
    }
}

struct Bar(i32);
#[automatically_derived]
impl ::core::clone::Clone for Bar {
    #[inline]
    fn clone(&self) -> Bar {
        let _: ::core::clone::AssertParamIsClone<i32>;
        *self
    }
}
#[automatically_derived]
impl ::core::marker::Copy for Bar { }

struct Baz(i32);
#[automatically_derived]
impl ::core::clone::Clone for Baz {
    #[inline]
    fn clone(&self) -> Baz {
        let _: ::core::clone::AssertParamIsClone<i32>;
        *self
    }
}
#[automatically_derived]
impl ::core::marker::Copy for Baz { }

struct Qux(i32);
#[automatically_derived]
impl ::core::marker::Copy for Qux { }
#[automatically_derived]
impl ::core::clone::Clone for Qux {
    #[inline]
    fn clone(&self) -> Qux { Qux(::core::clone::Clone::clone(&self.0)) }
}

I don't think this leads to any problems currently, but it's an odd (kind of amusing) inconsistency and may (citation needed) make for slightly worse codegen.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions