Skip to content

🍒[cxx-interop] Zero-initialize C++ structs when calling their default constructors #73076

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

Merged
merged 1 commit into from
Apr 17, 2024

Conversation

egorzhdan
Copy link
Contributor

@egorzhdan egorzhdan commented Apr 17, 2024

Explanation: When Swift imports C structs, it synthesizes an initializer that takes no arguments and zero-initializes the C struct. However, when C++ interop is enabled, the structs were not zero-initialized.
Scope: IRGen of C++ constructors.
Risk: Low, this only takes effect when C++ interop is enabled.
Testing: Added a compiler test.
Issue: rdar://115909532
Reviewer: @beccadax @aschwaighofer

Original PR: #72903
(cherry picked from commit bac5d0e)

…constructors

When Swift imports C structs, it synthesizes an initializer that takes no arguments and zero-initializes the C struct.

When C++ interop is enabled, Clang treats all C structs as if they were C++ structs. This means that some of the C structs will get a default constructor implicitly generated by Clang. This implicit default constructor will not zero-initialize trivial fields of the struct.

This is a common source of confusion and subtle bugs for developers who try to enable C++ interop in existing projects that use C interop and rely on zero-initialization of C structs.

rdar://115909532
(cherry picked from commit bac5d0e)
@egorzhdan egorzhdan added the c++ interop Feature: Interoperability with C++ label Apr 17, 2024
@egorzhdan
Copy link
Contributor Author

@swift-ci please test

@egorzhdan egorzhdan marked this pull request as ready for review April 17, 2024 12:58
@egorzhdan egorzhdan requested a review from a team as a code owner April 17, 2024 12:58
@egorzhdan egorzhdan merged commit a5f9e25 into release/6.0 Apr 17, 2024
@egorzhdan egorzhdan deleted the egorzhdan/6.0-zero-init branch April 17, 2024 18:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ interop Feature: Interoperability with C++
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants