Array delete runs element destructors in forward order

perfloop-oss/cpp2rust · Uncatalogued correctness

https://perfloop.ai/t/oss/case_pgkf7zsf0d

Verdict

VERIFIED · settled 2026-11-26 · merged as Cpp2Rust/cpp2rust#684

What happened: The check fails before this change and passes with it.

Hypothesis

In the default refcount translation model, C++ array deletion (`delete[]`) runs element destructors from first element to last instead of last to first. C++ `[expr.delete]/6` requires reverse destruction order so elements constructed later are destroyed before earlier elements they may depend on, and violating that order silently alters observable teardown behavior. As a labelled engineering prior, reach is default because refcount is the default model and `delete[]` on arrays of destructible objects is standard C++ heap cleanup, assuming translated programs rely on standard LIFO element destruction order.

In cpp2rust/converter/models/converter_refcount.cpp, ConverterRefCount::VisitCXXDeleteExpr (lines 2027-2030) emits `for __i in 0..__p.len() { __p.offset(__i as isize).destructor(); }` when `expr->isArrayForm()` is true. The sibling unsafe model in Converter::VisitCXXDeleteExpr (cpp2rust/converter/converter.cpp:4164) also iterates forward over `0..__n`.

A sandbox differential check with `cpp2rust --model=refcount` and `rustc +1.100.0 --edition=2024` against native `clang++ -std=c++17` allocated `T *a = new T[3]` with ids `1, 2, 3` and executed `delete[] a`. Native C++ exited 0 and logged destructor order `3 2 1`, whereas refcount-translated Rust compiled cleanly, exited 0, and silently logged `1 2 3`.

No open pull request or prior finding covers heap `delete[]` destruction order in VisitCXXDeleteExpr (hyp_e664rvbrd3 covers local and static array variables). The independent proof target is a differential test comparing `delete[]` element destructor order against native C++ under `--model=refcount` (and `--model=unsafe`); the claim is confirmed if destructors run forward and refuted if they run in reverse matching native C++.

Change to test: Iterate heap array indices in reverse order using (0..__p.len()).rev() in ConverterRefCount::VisitCXXDeleteExpr (lines 2027-2030), and apply the same reverse-order loop in sibling Converter::VisitCXXDeleteExpr (cpp2rust/converter/converter.cpp:4164).

Where it lives

perfloop-oss/cpp2rust · cpp2rust/converter/models/converter_refcount.cpp

Evidence

The check fails before this change and passes with it: `Translating delete[] on an initialized three-element Element heap array calls its destructors in reverse element order, matching native C++ output, in both refcount and unsafe models.`

Checks: 3 of 3 passed. Verification: no defect found.

Timeline