Preserve side effects of mapped-call arguments omitted from rule bodies

perfloop-oss/cpp2rust · Uncatalogued correctness

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

Verdict

VERIFIED · settled 2026-12-02 · merged as Cpp2Rust/cpp2rust#693

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

Hypothesis

When cpp2rust expands a translation rule for a mapped C or C++ call, it silently drops the entire evaluation and side effects of any argument whose placeholder does not appear in the rule body. That causes translated Rust programs in both the default refcount model and the unsafe model to silently skip mutations, state updates, or helper calls that C++ always executes. Because standard library and compiler-builtin rules are enabled by default and at least 17 shipped rules omit a parameter, our labelled engineering prior estimates reach as rare_input in the default configuration: ordinary programs hit the defect whenever they pass a side-effectful expression such as a getter call, increment, or assignment to an omitted parameter like std::array::size() or __builtin_expect. This estimate assumes real-world C/C++ codebases occasionally call mapped APIs on side-effectful receivers or arguments rather than exclusively on pure identifiers, an assumption measurable by scanning translated corpora.

In cpp2rust/converter/translation_rule.cpp:331-403, TranslationRule::ExprRule::argsToBind counts placeholder uses in the rule body into uses[i] and then decides which arguments Converter::GetMappedAsString must bind to temporaries before emitting the body. Inside the side-effect branch at lines 379-386, bind_rest |= uses[i] > 1 || (i < params.size() && takesTemporaryAddress(ctx, expr, i, arg)) || (uses[i] == 1 && first[i] > next_first) checks uses[i] > 1 and uses[i] == 1, but never handles uses[i] == 0. Consequently, when uses[i] == 0 and takesTemporaryAddress is false, bind[i] remains false. Back in Converter::GetMappedAsString (cpp2rust/converter/converter.cpp:5821-5940), arguments with bind[i] == false are only emitted where their PlaceholderFragment appears in the rule body, so an omitted side-effectful argument is completely discarded.

At least 17 shipped rules omit at least one parameter in their target body, including rules/array/src.cpp f2 (std::array::size(), which expands to the constant template length N without referencing the receiver), rules/builtin/src.c f1 (__builtin_expect, which drops its second argument) and f18, as well as rules in compare, fcntl, locale, socket, stdio, string, and time.

A bounded differential check in the sandbox (24 calls spanning 3 deltas {1, 2, 5} and 3 values {-1, 0, 1} across std::array<int,1>::size(), std::array<int,3>::size(), __builtin_expect(v, side_effect), and 9 used-argument controls) confirmed 15/15 omitted-argument mismatches in both refcount and unsafe models while all 9 used-argument controls matched native clang++ -std=c++17. On the reduced 1-call replay (pick_arr1().size() where pick_arr1 increments a counter), native C++ prints n=1 calls=1 while both refcount and unsafe Rust print n=1 calls=0.

Open PRs (#679, #683) and prior findings on argsToBind (init-pack aliasing, hoisted init construction, and namespace-scope calls) do not address uses[i] == 0 in ExprRule::argsToBind.

Independent proof target (provisional check layout in tests/property/mapped_unused_arg_side_effects/check.py): translate and differentially execute C++17 programs calling mapped rules with omitted side-effectful arguments (both receivers and positional arguments) in both refcount and unsafe models; the fix is confirmed when every omitted side-effectful argument executes exactly once in C++ evaluation order and stdout/exit status match native C++17 across the replay and full grid.

Change to test: Bind side-effectful mapped-call arguments whose rule-body use count is zero in ExprRule::argsToBind and preserve pre-binding order for preceding arguments so Converter::GetMappedAsString evaluates omitted arguments once before expanding the rule body.

Where it lives

perfloop-oss/cpp2rust · cpp2rust/converter/translation_rule.cpp

Evidence

The check fails before this change and passes with it: `In the C++17 mapped-call fixtures (deltas 1, 2, 5; values -1, 0, 1), refcount and unsafe translations evaluate omitted receivers and positional operands once in call order whenever native Clang evaluates them, preserve preceding operand values and Clang's constant-builtin folding, and match native stdout and exit status.`

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

Timeline