Destroy for-loop body locals before running the loop increment
perfloop-oss/cpp2rust · Uncatalogued correctness
https://perfloop.ai/t/oss/case_frzwa2cs6v
Verdict
VERIFIED · settled 2026-11-24 · pull request opened as Cpp2Rust/cpp2rust#681
What happened: The check fails before this change and passes with it.
Hypothesis
In C++, local objects declared inside a `for`-loop body are destroyed at the end of each iteration before the loop's increment expression runs, including when the iteration exits via `continue`. Translated Rust runs the increment expression before those body-local destructors execute in both the default `refcount` model and the `unsafe` model, reversing observable side effects and potentially releasing locks or guards too late. Prevalence in downstream codebases is unmeasured.
In `cpp2rust/converter/converter.cpp`, `Converter::VisitForStmt` (lines 1626-1657) opens a single loop block (`PushBrace brace(*this)` at line 1645), emits the loop body statements directly into that block via `ConvertBodyStmts(stmt->getBody())` (line 1651), and immediately emits `Convert(stmt->getInc())` (line 1653) in the same block. Because Rust drops `DropGuard` locals at the end of their enclosing block, `_dtor_g` runs after the increment expression instead of before it. On `continue`, `Converter::VisitContinueStmt` (lines 1690-1698) similarly emits `Convert(curr_for_inc_)` inline before `continue 'loop_`, so body locals live until `continue` unwinds the loop block after the increment has already executed.
A bounded differential check covering normal `for` iteration, `continue` inside a `for` loop, and a `while`-loop control with a scoped local guard confirmed that native C++ (`clang++-22 -std=c++20`) exits 0 (`trace` equals `1919`, `2929`, and `3939`), while both `refcount` and `unsafe` Rust binaries exit 3 (`0b011`): only the `while` control passes, and both normal `for` and `continue` paths run `step(i)` before `~G()`.
The attached provisional check (`tests/property/for_body_destructor_order/check.py`) replays the reduced `for`-loop guard fixture in both `refcount` and `unsafe` models. Confirming the repair requires matching native destructor-before-increment ordering on both normal iteration and `continue` (including nested loops and `break` controls) in both models. Note: lines 4201-6479 of `cpp2rust/converter/converter.cpp` have not yet been read in this session.
Change to test: Wrap the translated for-loop body in an inner block scope so body-local DropGuards run before the increment expression, and lower continue inside a for-loop to exit that inner body scope before executing the increment expression.
Where it lives
Evidence
The check fails before this change and passes with it: `For the normal, continue, nested-loop and break scenarios in tests/unit/for_body_destructor.cpp, refcount and unsafe translation preserve native C++ destructor-before-increment order, with condition and initializer objects retaining their enclosing lifetimes.`
Checks: 3 of 3 passed. Verification: no defect found.
Timeline
2026-11-24· Case opened2026-11-24· PR opened