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

Offen
#674 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Anfängerfreundlichkeit
35/100
Issue-Typ
Feature
Klarheit
Größtenteils klar
Aktivitätsstatus
Aktiv
Tech-Stack
rust
Bereich
backend, compilers

Rechercherichtung

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.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

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 ...
    }
}
Vorherrschende Sprache
Rust
Sterne
310
Forks
18
Ø Merge
9 Std. 46 Min.
Gemergte PRs (30 T.)
82

Entwicklungsumgebung

Dieses Projekt bietet weder Dev-Container noch Dockerfile noch Beitragsleitfaden – die Einrichtung liegt bei Ihnen. Beginnen Sie mit der README; die allgemeinen Schritte stehen in unserem Leitfaden für den ersten Beitrag.

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus Cpp2Rust/cpp2rust

Alle Issues in Cpp2Rust/cpp2rust

Ähnliche Issues

Weitere Issues zu Rust

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.