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

Aberta
#674 1 comentário 0 reações 0 responsáveis Ver no GitHub

Mantenedores costumam responder em até 1 dia

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
5/5
Tempo estimado
Mais de uma semana
Facilidade para iniciantes
35/100
Tipo de issue
Funcionalidade
Clareza
Razoavelmente clara
Status de atividade
Ativa
Stack de tecnologia
rust
Domínio
backend, compilers

Direção de pesquisa

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.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

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 ...
    }
}
Linguagem predominante
Rust
Estrelas
310
Forks
18
Merge médio
9h 46min
PRs com merge (30d)
82

Preparar o ambiente

Este projeto não oferece contêiner de desenvolvimento, Dockerfile nem guia de contribuição, então a configuração fica por sua conta: comece pelo README e veja nosso guia da primeira contribuição para os passos gerais.

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de Cpp2Rust/cpp2rust

Todas as issues de Cpp2Rust/cpp2rust

Issues semelhantes

Mais issues de Rust

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.