Skip to content

Constructors don't receive this as the first argument #674

Open

Description

@lucic71

Normal constructors and copy/move constructors/assignments all receive this as an implicit first argument.

Our constructor translation instead receives no this and returns by value.

This has performance implications as described in #582 but most importantly it has wrong semantics:

  1. self referential constructors are broken (Self initialization doesn't work #248)
  2. copy/move constructors and assignments introduce calls that C++ does not have:
impl S {
    pub fn copy_from(o: Ptr<S>) -> Self {
        let __this: Counted = Self {
            ... // It constructs a fake __this that might have side effects
        };
        ... body of copy ctor ...
        __this
    }
}
impl Clone for S {
    fn clone(&self) -> Self {
        // builds a fake argument for copy_from that might have side effects 
        let __src: Value<S> = Rc::new(RefCell::new(S { ... }));
        S::copy_from(__src.as_pointer())
        // drops the fake __src and the fields inside it
    }
}

For 1, the solution is to build the storage before calling the constructor:

impl S {
  pub fn new(this: Ptr<Self>) {
    this.a = 1;
    this.b = 2;
  }
}

let s: Value<S> = Rc::new(RefCell::new(/* uninitialized storage of S */));
S::new(s.as_pointer());

For 2, the solution is to explicitly pass this to the copy ctor and stop using the Clone trait:

impl S for CopyConstructible {
    pub fn copy_from(this: Ptr<S>, o: Ptr<S>) {
        ... body of copy ctor ...
    }
}

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