Skip to content

Copy constructors that modify the input param cannot be translated as Clone #613

Open

Description

@lucic71

We translate copy constructors using the Clone trait. Clone receives an immutable reference. C++ allows copy constructors to receive a mutable reference T(T&).

We cannot express T(T&) using the Clone trait. Our current solutions mistranslate this case. Unsafe translates as

impl Clone for T {
  fn clone(&self) {
    // the cast is UB if copy_from actually modifies self
    T::copy_from(&self as *const T as *mut T)
  }
}

Refcount translates as:

impl Clone for T {
  fn clone(&self) -> Self {
    let __src: Value<T> = Rc::new(RefCell::new(T { a: self.a.clone(), ... }));
    // Any modification on self is lost because __src is a temporary
    T::copy_from(__src.as_pointer())
  }
}

Also the refcount translation is slow, see #510

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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