Skip to content

Evaluate try_emplace arguments before checking the key - #583

Open
perfloop-agent wants to merge 2 commits into
Cpp2Rust:masterfrom
perfloop-oss:perfloop-pr-open-pkav2pt2td
Open

perfloop-agent wants to merge 2 commits into
Cpp2Rust:masterfrom
perfloop-oss:perfloop-pr-open-pkav2pt2td

Conversation

@perfloop-agent

Copy link
Copy Markdown
Contributor

Evaluate std::map::try_emplace(key, args...) arguments before the key lookup, even when the key already exists, while keeping mapped-object construction conditional on insertion. This fixes skipped argument side effects for lvalue and const keys in both refcount and unsafe output.

The rules IR init fragment gains an optional conditional flag, detected from its position in the rule body. The converter uses it to bind argument effects before expanding the rule body without eagerly constructing the stored value. Reference binding and std::move forwarding are preserved: arguments are not copied or moved from on a key hit, and insertion retains the C++ copy/move behavior.

The binding also works in global initializers and with assignment, compound assignment, prefix increment, comma and conditional expressions. Modified lvalue arguments remain addressable without repeating their operand effects.

Testing

The two new unit tests, tests/unit/map_try_emplace.cpp and tests/unit/map_try_emplace_lvalues.cpp, run in both output models. They check argument evaluation, conditional construction, reference binding, copy/move behavior, global initialization and modified lvalue arguments against native C++. The existing-key increment case fails without the fix in both models.

The full Linux lit suite in Release reports 1,156 passed, 14 expected failures and no unexpected failures with the change; the unpatched base reports 1,152 passed, 14 expected failures and no unexpected failures. Existing expected outputs are unchanged.

The changed C++ files pass clang-format. The rule preprocessor passes rustfmt and Clippy with warnings denied.

Coverage does not establish the rvalue-key overload or exception unwinding.

Case and evidence


Made by Perfloop. Human sponsor: Tomás Senart. To ask for a change, comment or review in plain words. How Perfloop handles pull requests.

Signed-off-by: Perfloop Agent <agent@perfloop.ai>
Signed-off-by: Tomás Senart <ts@perfloop.ai>
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
Signed-off-by: Tomás Senart <ts@perfloop.ai>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant