Skip to content

Derived Clone implementations don't utilize clone_from implementations from field types #98374

Closed as not planned
@AngelicosPhosphoros

Description

@AngelicosPhosphoros

I tried this code:

struct A;

impl Clone for A {
    fn clone(&self) -> Self {
        println!("Basic clone");
        Self
    }

    fn clone_from(&mut self, other: &Self) {
        println!("Clone from");
        *self = Self;
    }
}

#[derive(Clone)]
struct B(A);

fn main() {
    let b = B(A);
    let mut bb = B(A);
    bb.clone_from(&b);
}

I expected to see this happen: prints Clone from

Instead, this happened: prints Basic clone

I think, this can lead to little losses for types which have multiple vector and string fields.

Meta

rustc --version --verbose:

rustc 1.61.0 (fe5b13d68 2022-05-18)
binary: rustc
commit-hash: fe5b13d681f25ee6474be29d748c65adcd91f69e
commit-date: 2022-05-18
host: x86_64-unknown-linux-gnu
release: 1.61.0
LLVM version: 14.0.0

rustc 1.63.0-nightly (5750a6aa2 2022-06-20)
binary: rustc
commit-hash: 5750a6aa2777382bf421b726f234da23f990a953
commit-date: 2022-06-20
host: x86_64-unknown-linux-gnu
release: 1.63.0-nightly
LLVM version: 14.0.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.T-libsRelevant to the library 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