Skip to content

Keep temporaries alive for pointers to their fields in refcount - #614

Merged
nunoplopes merged 2 commits into
Cpp2Rust:masterfrom
perfloop-oss:perfloop-pr-open-b4e71x1dvd
Nov 8, 2026
Merged

nunoplopes merged 2 commits into
Cpp2Rust:masterfrom
perfloop-oss:perfloop-pr-open-b4e71x1dvd

Conversation

@perfloop-agent

Copy link
Copy Markdown
Contributor

#577 made the refcount substr rule take its receiver by pointer, so make().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 of field_ref! (likewise field_ptr! and array_field_ptr!), and Rust 2024 drops the temporaries of a block's tail expression at the end of the block. The temporary was dropped before substr read it, and the program panicked with ub: dangling pointer. Other pointers to a field of a temporary, such as one passed to a const std::vector<int> & parameter, failed the same way.

The converter now binds a pointer to the temporary in a match around 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 as substr:

(match Rc::new(RefCell::new(({ make_0(calls.as_pointer()) }))).as_pointer() {
    __tmp_1 => field_ref!(__tmp_1, values).as_pointer(),
})

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:

  • a const reference or range-for bound to a field of a temporary;
  • a field reached through an array element of a temporary, such as make().items[1].values, or through a reference that a call returns, such as identity(make().s).substr(7, 6);
  • two such arguments of one call, such as two(make().values, make().inner.values), which are bound to variables before the call;
  • constructor arguments, such as 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.cpp covers a field, a nested field, at with an argument that reads the counter the temporary's function increments, a char array 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 with ub: dangling pointer. It avoids std::string fields, whose size differs between libstdc++ and libc++ and appears in refcount output; the reported substr forms (a field, a nested field, parentheses, arguments that read the counter the call increments, and a second substr) were run separately on Linux and match native output in both models.

tests/unit/temporary_field_two_args.cpp and tests/unit/temporary_field_ctor_arg.cpp record the two-argument and constructor-argument cases from the list above. Both are valid C++, so they are in tests/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 its panic line is removed.

On Linux, lit on tests/unit, tests/ub and tests/multi-file reports 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.

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>
@nunoplopes
nunoplopes merged commit 242534e into Cpp2Rust:master Nov 8, 2026
9 checks passed
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.

2 participants