Wrong translation for iterator into moved container

Abierto
#578 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
Bastante claro
Estado de actividad
Activo
Stack tecnológico
cpp, rust
Área
compilers

Línea de trabajo

The issue names no source files or tests, so start by locating how the translator maps C++ std::vector iterators to Rust, which the issue describes as a Value<Vec<T>> wrapper with Weak pointers. Reproduce the moved-container case with the snippet in the issue to confirm the mismatch. Settling the design (the costly Value<Value<Vec<T>>> option or a different invalidation rule) needs maintainer input before any patch.

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

Descripción

std::vector<int> src{1, 2, 3};
std::vector<int> dst;

auto it = src.begin();        // (1) iterator to src's first element
dst = std::move(src);         // (2) dst steals src's heap buffer; src is left empty
assert(&*it == &dst[0]);      // (3) C++: true, the element never moved, dst owns it now
let src: Value<Vec<i32>> = Rc::new(RefCell::new(vec![1, 2, 3]));   // Value<T> = Rc<RefCell<T>>
let dst: Value<Vec<i32>> = Rc::new(RefCell::new(Vec::new()));

let it = src.as_pointer().begin();   // (1) Ptr = (Weak to src's Rc, index 0)
dst.write(std::mem::take(&mut *src.borrow_mut()));
                                     // (2) the Vec moves into dst's Rc; src's Rc keeps an empty Vec;
                                     //     `it` still refers to src's Rc
assert!(it.deref() == dst[0]);       // (3) panics: index 0 out of bounds of the empty Vec in src

In C++ the iterator for vector (or other sequential container) stays valid after src is moved because it points into the same buffer. In Rust the iterator is a Weak reference into the src Value<Vec<T>>. It still points into src after the move.

I think the correct solution is to translate vector as Value<Value<Vec<T>> but this is too costly. Also I am not sure if the behavior in C++ is standard behavior or implementation behavior. I found no explicit sentence in the standard that enforces this.

Lenguaje dominante
Rust
Estrellas
310
Forks
18
Merge medio
10 h 18 min
PR fusionados (30 d)
125

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.