Skip to content

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

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

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

@egorzhdan egorzhdan added the c++ interop Feature: Interoperability with C++ label Apr 8, 2024
@egorzhdan egorzhdan requested review from zoecarver and hyp as code owners April 8, 2024 15:01
@egorzhdan egorzhdan requested a review from ahatanaka April 8, 2024 15:01
@fahadnayyar fahadnayyar self-requested a review April 8, 2024 16:41
@egorzhdan egorzhdan force-pushed the egorzhdan/zero-init branch 2 times, most recently from 6a27eb8 to 9dbd3ed Compare April 8, 2024 18:51
@egorzhdan
Copy link
Contributor Author

@swift-ci please test

…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
@egorzhdan egorzhdan force-pushed the egorzhdan/zero-init branch from 9dbd3ed to bac5d0e Compare April 16, 2024 12:42
@egorzhdan
Copy link
Contributor Author

@swift-ci please test

@egorzhdan
Copy link
Contributor Author

@swift-ci please test source compatibility

@egorzhdan
Copy link
Contributor Author

@swift-ci please test source compatibility debug

Copy link
Contributor

@beccadax beccadax left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Copy link
Contributor

@aschwaighofer aschwaighofer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm

@egorzhdan
Copy link
Contributor Author

The source compatibility failure is unrelated to this PR.

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.

3 participants