Repository navigation
Evaluate try_emplace arguments before checking the key - #583
Merged
nunoplopes merged 1 commit intoNov 16, 2026
Merged
Conversation
nunoplopes
reviewed
Nov 15, 2026
|
|
||
| // A bound lvalue must stay addressable even when its evaluation also writes | ||
| // it, as in ++counter or counter = value. | ||
| void AddAddressUse(const clang::Expr *expr) { |
Contributor
There was a problem hiding this comment.
this should use a visitor
Contributor
Author
There was a problem hiding this comment.
Done: the address-use check is now AddressUseVisitor, a clang::ConstStmtVisitor in boxed_vars.cpp, instead of the hand-written recursion.
nunoplopes
reviewed
Nov 15, 2026
| return false; | ||
| } | ||
|
|
||
| void Converter::ConvertAssignmentRef(clang::BinaryOperator *expr) { |
Contributor
There was a problem hiding this comment.
This function doesn't follow Cpp2Rust's style. We try to avoid creating new AST nodes.
Contributor
Author
There was a problem hiding this comment.
Done: ConvertAssignmentRef no longer creates AST nodes. It evaluates the right operand and the target pointer into Rust bindings, writes through the pointer, and returns the same place (match ({ rhs }, ptr) { (r, p) => { p.write(r); p } }).
Signed-off-by: Perfloop Agent <agent@perfloop.ai> Signed-off-by: Tomás Senart <ts@perfloop.ai>
perfloop-agent
force-pushed
the
perfloop-pr-open-pkav2pt2td
branch
from
November 16, 2026 07:33
def575b to
b060986
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
initfragment gains an optionalconditionalflag, 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 andstd::moveforwarding 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: a statement visitor finds their address uses, and an assignment used as a reference is lowered to Rust bindings rather than new AST nodes.
In unsafe output,
try_emplacenow lowers totry_emplace(key, || value)instead ofemplace((key, value)), sotests/unit/out/unsafe/map.rschanges in three places.Testing
The two new unit tests,
tests/unit/map_try_emplace.cppandtests/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.Rebased on
master(5eeb075).ninja checkin Release passes with the change: lit reports 615 passed, 13 expected failures and no unexpected failures; the unpatched base reports 613 passed, 13 expected failures and no unexpected failures. The runtime, compile-fail and control-flow checks pass on both.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.