Skip to content

Derive Copy/Clone on generic Union working incorrectly. #62427

Closed
@rickvanprim

Description

@rickvanprim

The following code reports an error about the value being moved:

use std::marker::PhantomData;

trait Foo {}

struct Bar {}

impl Foo for Bar {}

#[derive(Copy, Clone)]
union U<T>
where T: Foo
{
    value1: u32,
    value2: *const (),
    phantom: PhantomData<*const T>
}

fn test()
{
    let a: U<Bar> = U { value1: 5 };
    let b = a;
    let c = a;
}

While the following code works as expected:

use std::marker::PhantomData;

trait Foo {}

struct Bar {}

impl Foo for Bar {}

union U<T>
where T: Foo
{
    value1: u32,
    value2: *const (),
    phantom: PhantomData<*const T>
}

impl<T> Copy for U<T> where T: Foo {}

impl<T> Clone for U<T> where T: Foo
{
    fn clone(&self) -> Self
    {
        *self
    }
}

fn test()
{
    let a: U<Bar> = U { value1: 5 };
    let b = a;
    let c = a;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions