Unsigned and promoted compound assignments double-evaluate targets and drop results
perfloop-oss/cpp2rust · Uncatalogued correctness
https://perfloop.ai/t/oss/case_d7pqm0rj4q
Verdict
VERIFIED · settled 2026-11-24 · pull request opened as Cpp2Rust/cpp2rust#679
What happened: The check fails before this change and passes with it.
Hypothesis
For unsigned compound assignments and compound assignments whose computation type differs from the left-hand type (such as narrowing types like short or char, or mixed integer and floating-point arithmetic), the refcount converter computes the new value from the left-hand expression and then emits a separate store to that same left-hand expression. This causes two defects: any side effect in the left-hand target runs twice even in a void statement, silently reading one element and writing to another; and when the compound assignment is used as a value, the generated Rust evaluates to unit `()` instead of the assigned value, failing to compile. How often external C++ inputs hit these compound assignments remains unmeasured.
In cpp2rust/converter/models/converter_refcount.cpp, ConverterRefCount::ConvertBinaryOperator (lines 1587-1645) handles type-mismatched compound assignments at lines 1594-1599 via ConvertCompoundAssignValue(assign) and unsigned compound assignments at lines 1601-1623 via ConvertRValue(lhs) followed by ConvertUnsignedArithBinaryOperator. Both branches pass the computed value string to ConverterRefCount::EmitCompoundSetOrAssign (lines 2435-2446), which calls EmitSetOrAssign(lhs, ...) a second time on lhs without ever emitting the stored value when isRValue() is true. Translating the same probes with `--model=unsafe` showed the sibling Converter::ConvertBinaryOperator in cpp2rust/converter/converter.cpp also emits lhs twice and produces a unit assignment expression in value context.
Differential runs with clang++-22 (-std=c++17), cpp2rust --model=refcount, and rustc +1.100.0 confirmed both failures: - Void unsigned compound assignment `unsigned a[4] = {10, 20, 30, 40}; int i = 0; a[i++] += 5;` emitted `let rhs_0 = (a[(i.postfix_inc()) as usize]).wrapping_add(5_u32); a[(i.postfix_inc()) as usize] = rhs_0` and produced `NATIVE: i=1 a=15,20,30,40` versus `RUST: i=2 a=10,15,30,40`. - Void promoted `short` compound assignment `short a[4] = {10, 20, 30, 40}; int i = 0; a[i++] += 5;` produced `NATIVE: i=1 a=15,20,30,40` versus `RUST: i=2 a=10,15,30,40`. - Value-used unsigned compound assignment `unsigned x = 10; unsigned y = (x += 5);` emitted `let mut y: u32 = ({ x = { (x).wrapping_add(5_u32) } });` and failed rustc with `error[E0308]: mismatched types` (`expected u32, found ()`). - Value-used `short` compound assignment `short x = 10; int y = (x += 5);` failed rustc with `error[E0605]: non-primitive cast: () as i32`.
The Case should verify with differential C++ and Rust tests across both refcount and unsafe models that unsigned and type-converting compound assignments (`+=`, `-=`, `*=`, `/=`, `%=`, bitwise and shift variants) evaluate side-effectful left-hand targets exactly once in void and value contexts, compile cleanly when used as values, and match native C++ outputs. An upstream PR check via upstreamWork showed zero open pull requests, and priorFindings showed no overlapping finding for ConvertBinaryOperator or EmitCompoundSetOrAssign.
Change to test: Lower unsigned and type-converting compound assignments by resolving the target place once, computing the updated value from that single place, writing it back, and returning the updated value when the expression is used as a value. Update the sibling unsafe-model lowering in Converter::ConvertBinaryOperator so both models preserve single evaluation and value results.
Where it lives
perfloop-oss/cpp2rust · cpp2rust/converter/converter.cpp
Evidence
The check fails before this change and passes with it: `For the tested well-defined C++17 assignment programs, both refcount and unsafe translations evaluate unsigned and promoted compound-assignment targets once in discarded and value contexts, evaluate value-used simple-assignment targets once, and match native C++ results.`
The check fails before this change and passes with it: `For the tested well-defined C++17 unsigned and promoted compound assignments, both refcount and unsafe translations compile in value contexts and return the native assigned value after evaluating the target once.`
Checks: 3 of 3 passed. Verification: no defect found.
Timeline
2026-11-24· Case opened2026-11-24· PR opened