Constructors don't receive `this` as the first argument

Đang mở
#674 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
35/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
rust
Lĩnh vực
backend, compilers

Hướng nghiên cứu

Start by tracing constructor translation and the generated Rust representations for normal, copy, and move constructors; the issue gives no source files or tests to target. Read the related issues #582 and #248 for context, then locate the translation tests covering constructors. Done means constructors receive prebuilt storage through an explicit this argument, and copy/move translation avoids the extra construction and Clone behavior described here.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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 (#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 ...
    }
}
Ngôn ngữ chính
Rust
Star
310
Fork
18
Merge trung bình
9 giờ 46 phút
Pull request đã merge (30 ngày)
82

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của Cpp2Rust/cpp2rust

Tất cả issue của Cpp2Rust/cpp2rust

Issue tương tự

Thêm issue về Rust

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.