Skip to content

Make is_complete static assertion unconditional for unique_ptr<T> bindings. #1536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

anforowicz
Copy link
Contributor

PTAL?

FWIW I tried to leave a justification of small design/coding decisions here and there in code comments + in the description of the last commit.

Similarly to my last PR, this one is also secretly part of a bigger work item (*). I hope that when the bigger work item is done, then enum UniquePtr, trait ToTypename, and trait ToMangled can be removed altogether from gen/src/write.rs. But even if this doesn't materialize, the changes from this PR under review seem desirable on their own (adding test coverage for C++-side errors + simplifying fn write_unique_ptr_common and removing some uses/deconstructions of UniquePtr).

WDYT?

(*) "bigger work item" has evolved from trying to add support for Vec<[u8; N]> to adding support for Vec<T> where T is not necessarily a simple identifier (e.g. for Vec<Vec<T>>). I am still working at this point on the last few remaining places where write.rs or expand.rs only work with identifier-only/named types (one of those places is conditional_delete, another is is_maybe_trivial, and fn write_unique_ptr_common is another one).

This commit adds minimal test infrastructure for inspecting the error
messages reported by a C++ compiler when compiling the `.cc` file
generated by `cxx_gen::generate_header_and_cc`.
Before this commit, `fn write_unique_ptr_common` in `gen/src/write.rs`
would avoid emitting the following code elements if it the `T` of
`std::unique_ptr<T>` was surely fully defined:

* `static_assert(::rust::is_complete<T>...)`
* `::rust::deleter_if<::rust::detail::is_complete<T>::value>{}(ptr)`

Otherwise, the `static_assert` wasn't emitted, and deletion would be
a simple `ptr->~unique_ptr()` call.

After this commit, the `static_assert`, and the `deleter_if`-guarded
delation are *always* used.  This has the following impact:

* This simplifies the code in `gen/src/write.rs`.  In particular, it
  helps to refactor that code to support arbitrary `T` in
  `UniquePtr<T>`, by avoiding the need to calculate `conditional_delete`
  for arbirary `T`.
* This may potentially impact performance of C++ compilation runtime.
  OTOH:
    - The impact seems relatively small in the grand scheme of things.
    - The optimization before this commit was based on a heuristic.
      For example, the extra code would still be emitted for
      `UniquePtr<u32>`.
@anforowicz anforowicz force-pushed the conditional-delete-should-be-unconditional branch from a513fcb to fd4c08c Compare July 24, 2025 00:29
@anforowicz
Copy link
Contributor Author

Force push above rebases on top of the upstream origin/master - this should make this PR mergeable again.

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