References get pointer semantics In rules

Abierto
#678 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
5/5
Tiempo estimado
Más de una semana
Aptitud para principiantes
15/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
cpp, rust
Área
compilers

Línea de trabajo

The issue proposes splitting the translation of C++ pointers and references into separate Rust types (Ptr, LvalueRef, RvalueRef) so that rules and STL types like std::pair<int&, int> get correct semantics. Start from the examples in the issue, then find where the translator currently maps both T* and T& to Ptr<T>. Done means the assignment and reference-collapsing examples behave as C++ does, and the design is agreed in the thread.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

Both T& and T* are translated as Ptr<T>. When a rule is applied on a reference, the rule sees a Ptr<T> and applies pointer semantics on it.

int x = 1, y = 2;
std::pair<int&, int> a{x, 0}, b{y, 0};
// C++:        ✅ assigns through a.first, x == 2
// Translated: ❌ a.first now points to y, x == 1
a = b;
a = std::move(b);            // same

std::pair<int&, int> and std::pair<int*, int> are the same translated type so no rule can give them different semantics. Same happens for any STL type that can hold references.


I think that the complete solution is to translate pointers and references as:

  • T* -> Ptr<T>
  • T& -> LvalueRef<T>
  • T&& -> RvalueRef<T>

All share the layout of Ptr: PtrKind + offset, but the operations differ:

std::pair<int*, int> a(&x, 0), b(&y, 0);
a = b;    // T1 = Ptr<i32>: copy_assign copies the address; a.first points to y

std::pair<int&, int> c(x, 0), d(y, 0);
c = d;    // T1 = LvalueRef<i32>: copy_assign writes y's value into x; c.first still refers to x

c == d;   // LvalueRef<i32>'s == compares x and y, not their addresses

The distinction between Ptr<T>, LvalueRef<T> and RvalueRef<T> is also relevant for reference collapsing which master gets wrong:

int x = 1;
std::pair<int&, int> a(x, 0);
// C++:        ✅ writes x, prints x=7
// Translated: ❌ does not compile, std::get<0>(a)  is Ptr<Ptr<i32>> instead of Ptr<i32>
std::get<0>(a) = 7;
std::printf("x=%d\n", x);

The reference collapsing rules would be encoded as:

LvalueRef<LvalueRef<T>>  =>  LvalueRef<T>      // T& &   -> T&
RvalueRef<LvalueRef<T>>  =>  LvalueRef<T>      // T& &&  -> T&
LvalueRef<RvalueRef<T>>  =>  LvalueRef<T>      // T&& &  -> T&
RvalueRef<RvalueRef<T>>  =>  RvalueRef<T>      // T&& && -> T&&
Lenguaje dominante
Rust
Estrellas
310
Forks
18
Merge medio
9 h 46 min
PR fusionados (30 d)
82

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de Cpp2Rust/cpp2rust

Todos los issues de Cpp2Rust/cpp2rust

Issues similares

Más issues de Rust

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.