Repository navigation
Keep temporaries alive for pointers to their fields in refcount - #614
Merged
nunoplopes merged 2 commits intoNov 8, 2026
Merged
Conversation
In the refcount model, a pointer to a field of a temporary, such as the
receiver of `f().s.substr(1, 2)`, was made inside the block of the
field_ref!, field_ptr! or array_field_ptr! macro. Rust 2024 drops the
temporaries of the tail expression of a block at the end of the block,
so the temporary was dropped before the pointer was used, and the
program panicked with "ub: dangling pointer".
Bind a pointer to the temporary in a match around the field macros
instead, so that the temporary lives until the end of the enclosing
statement or block, which covers uses within the same expression, such
as substr. The temporary is materialized like other temporaries whose
address is taken, which also makes pointers to fields of aggregate
temporaries, such as `T{...}.v`, compile.
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
Signed-off-by: Tomás Senart <ts@perfloop.ai>
Two pointers to fields of temporaries still dangle in the refcount model: two such arguments of one call, which are bound to variables before the call, and a constructor argument, which is converted in a block of its own. Both programs are valid C++, so add a unit test for each that records the refcount translation's panic as an expected failure. The unsafe translation of the constructor argument reads freed memory, so its result is not compared. Signed-off-by: Perfloop Agent <agent@perfloop.ai> Signed-off-by: Tomás Senart <ts@perfloop.ai>
This was referenced Nov 10, 2026
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.
#577 made the refcount
substrrule take its receiver by pointer, somake().s.substr(7, 6)now takes a pointer to a field of a temporary; it worked before #577. The converter emitted that pointer inside the block offield_ref!(likewisefield_ptr!andarray_field_ptr!), and Rust 2024 drops the temporaries of a block's tail expression at the end of the block. The temporary was dropped beforesubstrread it, and the program panicked withub: dangling pointer. Other pointers to a field of a temporary, such as one passed to aconst std::vector<int> ¶meter, failed the same way.The converter now binds a pointer to the temporary in a
matcharound the outermost field macro, so the temporary lives until the end of the enclosing Rust statement or block. That covers uses within the same expression, such assubstr:This also makes pointers to fields of aggregate temporaries, such as
Outer{...}.inner.values, compile. Pointers to fields of lvalues and the unsafe model are unchanged.Still dangling in refcount, as before:
constreference or range-for bound to a field of a temporary;make().items[1].values, or through a reference that a call returns, such asidentity(make().s).substr(7, 6);two(make().values, make().inner.values), which are bound to variables before the call;H h(make().values);, which are converted in a block of their own (the unsafe translation prints a wrong value there too).Testing
tests/unit/temporary_field_addr.cppcovers a field, a nested field,atwith an argument that reads the counter the temporary's function increments, achararray field and an aggregate temporary. At the base revision, its refcount translation does not compile because of the aggregate case, and each other case on its own panics withub: dangling pointer. It avoidsstd::stringfields, whose size differs between libstdc++ and libc++ and appears in refcount output; the reportedsubstrforms (a field, a nested field, parentheses, arguments that read the counter the call increments, and a secondsubstr) were run separately on Linux and match native output in both models.tests/unit/temporary_field_two_args.cppandtests/unit/temporary_field_ctor_arg.cpprecord the two-argument and constructor-argument cases from the list above. Both are valid C++, so they are intests/unit, and lit records their refcount runs as expected failures because the refcount translation panics; the unsafe translation of the constructor case reads freed memory, so its output is not compared. Fixing either case will make its refcount run fail until itspanicline is removed.On Linux, lit on
tests/unit,tests/ubandtests/multi-filereports 1160 passed and 16 expected failures of 1176 tests in both the Release and the Debug build. The libcc2rs unit tests report 70 passed, and the libcc2rs-macros compile-fail and control-flow tests report 1 and 22 passed, with no failures. Translating the three new tests against libc++ gives the same expected Rust in both models. clang-format 23 reports no changes for the changed C++ files.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.