Hoisted element construction can precede receiver side effects
perfloop-oss/cpp2rust · Uncatalogued performance
https://perfloop.ai/t/oss/case_gebc08y8wa
Verdict
VERIFIED · settled 2026-11-20 · merged as Cpp2Rust/cpp2rust#675
What happened: The check fails before this change and passes with it.
Hypothesis
Mapped element construction can observe stale data because it runs before a receiver-selection call that updates the source. Both translation models insert the old value in a supported C++14 example, even though the receiver is evaluated exactly once. This is a correctness issue; consumer prevalence and any performance effect remain unmeasured.
GetMappedAsString chooses init_first at 5744–5747 when direct-alias analysis requests construction before borrowing. Its operand-binding loop skips operands with neither bind[i] nor first[i] at 5755–5757. It then appends the constructed element to the match inputs at 5817–5823. An unbound, side-effectful receiver remains in the match body, so construction precedes that receiver's effects. rules/vector/src_until_cxx17.cpp:8–10 and the single-use f122 target in rules/vector/tgt_unsafe.rs:242–244 provide an eligible mapping; the refcount model inherits it. ExprRule::argsToBind at translation_rule.cpp:379–389 does not require capture of this once-used receiver.
The deterministic example calls select_rows(&rows, &calls)->emplace_back(rows[0].data(), rows[0].data() + 3). select_rows increments calls, changes the first row's last element from 3 to 9, and returns the outer vector. The outer vector reserves two entries. Native element construction occurs inside emplace_back after receiver evaluation, so copying 3 is not an allowed alternative C++14 operand order.
The provisional check bash .hunt/check_order.sh at ca627379186ea3724ecfa47a819c083760df8ac2 exited 1 on its native-equivalence assertion. Receipt call_fb38f5a9de5246809fa5eadb79ee8d90, output:1, records native output 'calls=1 source=9 inserted=9' and output 'calls=1 source=9 inserted=3' from both refcount and unsafe Rust. All three programs returned 0; this is a wrong-result witness, not the earlier borrow panic. Both generated programs visibly construct the copied vector in a match input and invoke select_rows in the match body. A separately evaluated receiver control produced inserted=9 in both models. The build used LLVM 22.1.8 with only an unused newer-AST-type forward declaration and repository-pinned Rust 1.100.0.
The smallest useful Case check is the saved unbound-receiver replay. Confirmation requires native-equivalent inserted values and once-only operand evaluation while retaining construction-before-borrow protection and the successful receiver control. Scope is eligible unconditional-init mapped calls with side-effectful fixed operands, not only this vector example. The local checker and placement are provisional proof methods, not required delivery artifacts. docs/src/rules/rewriting.md:80–89 documents evaluation before conflicting borrowing. A common scheduling repair should be considered together with hyp_4f0nh6zsyr, but that hypothesis's missing pointee check alone cannot fix this already-hoisted branch, which also fails in unsafe lowering.
Upstream https://github.com/Cpp2Rust/cpp2rust/commit/a495c8d introduced init hoisting; its implementation lacks this prefix-effect capture. The subsequent https://github.com/Cpp2Rust/cpp2rust/commit/ca62737 adds repeated-init handling, not operand scheduling. The examined open implementation at https://github.com/Cpp2Rust/cpp2rust/pull/660 concerns strong-ordering references, and the other current open PR #666 concerns socket lengths. No separate review venue was identified in the checked documentation or bounded recent history. The prior receiver-repetition repair hyp_9vvvr098mv and the spending-held temporary-constructor hypothesis hyp_zx6kbq4rjf do not cover this once-evaluated receiver and stale construction order; their user:4 closure decisions are not being reopened.
Change to test: Keep hoisted init construction after side-effectful fixed operands and before mutable receiver borrowing. Capture the necessary operands once in the shared lowering while preserving value categories, temporary lifetimes, and conditional construction.
Where it lives
perfloop-oss/cpp2rust · cpp2rust/converter/converter.cpp
Evidence
The check fails before this change and passes with it: `For the C++14 direct-source, pointer-alias, and separately bound receiver range-emplacement witnesses, and the C++17 map witnesses with receiver and hint effects or only hint effects, default refcount and unsafe output match native inserted values and once-only operand counts without conflicting mutable borrows.`
The check fails before this change and passes with it: `For the C++17 hinted-emplacement witnesses, including a plain map receiver, both default refcount and unsafe output evaluate the hint once before element construction and match native inserted values.`
Checks: 3 of 3 passed. Verification: no defect found.