Repository navigation
Evaluate substring operands once - #577
Conversation
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>
Signed-off-by: Perfloop Agent <agent@perfloop.ai> Signed-off-by: Tomás Senart <ts@perfloop.ai>
|
One tradeoff to flag before you review. To keep C++17 order (the arguments can modify the receiver, e.g. If you'd rather avoid the box, a plain local receiver could be read in place after the arguments are evaluated, and only receivers with side effects would keep the evaluate-once capture. That adds a second code shape for the rule, so I left it out. Happy to switch to it, or to match the The |
The field-receiver case kept a std::string by value in a struct, so the refcount expected output recorded libstdc++'s 32-byte layout and failed on libc++, where std::string is 24 bytes. Reach the string through a reference member instead: substr still takes its receiver from a struct field, and the emitted layout is only the pointer-sized field. Signed-off-by: Perfloop Agent <agent@perfloop.ai> Signed-off-by: Tomás Senart <ts@perfloop.ai>
Make substring calls evaluate their receiver and arguments once in both output models. Duplicating an operand during inline substitution can change both the selected bytes and caller state; this handles the substring case of #575.
The side-effect cases in tests/unit/string.cpp compare substring bytes and final counters with C++ for position, count and receiver expressions.
The result remains an independent, null-terminated copy of the selected slice. No whole-source clone is introduced; reference-counted receivers must now be addressable. Existing range clipping and overflow handling are unchanged.
Testing
litontests/unit/string.cppandtests/unit/string_substr.cpp(Linux, Release build): pass. Both fail without this change.string_substr.cpphard-codes the libstdc++std::stringsize; a fix for that follows.clang-format --dry-run --Werroron the changed C++ files,rustfmt --checkon the changed rule files, andcargo clippy --all-targets --all-features -- -Dwarningsonrules/: clean.tests/unit/string_substr.cpp checks receiver-before-argument ordering, source mutations in either argument, independent result storage, and temporary, pointer, field and container-element receivers. The generated Rust expectations cover both models.
Related work: #576 changes vector proxy handling in the converter and binds repeated operands in vector rules; this change handles substring expansion.
Case and evidence
Generated by Perfloop. Human sponsor: Tomás Senart.