Skip to content

[cxx-interop] Synthesize a deprecated zero initializer for C++ structs #66220

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
Jun 1, 2023

Conversation

egorzhdan
Copy link
Contributor

When importing a C header in the C++ language mode, Clang/Swift treat C structs as C++ structs.

Currently Swift synthesizes a default initializer that zero-initializes the backing memory of the struct for C structs, but not for C++ structs.

This is causing issues in existing projects that use C libraries and rely on having the default initializer available in Swift. This change enables the synthesis of a default initializer for C++ structs. Since many C++ structs are not designed to be initialized this way, the initializer is marked as deprecated in Swift.

rdar://109727620

@egorzhdan egorzhdan added the c++ interop Feature: Interoperability with C++ label May 30, 2023
@egorzhdan egorzhdan requested review from zoecarver and hyp as code owners May 30, 2023 18:57
@@ -2379,11 +2379,30 @@ namespace {
hasMemberwiseInitializer = false;
}

if (hasZeroInitializableStorage && !cxxRecordDecl) {
if (hasZeroInitializableStorage &&
!(cxxRecordDecl && cxxRecordDecl->hasDefaultConstructor())) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wonder if we should limit this to C-looking C++ structs, e.g. those without base classes / templated parameters / etc.

@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

When importing a C header in the C++ language mode, Clang/Swift treat C structs as C++ structs.

Currently Swift synthesizes a default initializer that zero-initializes the backing memory of the struct for C structs, but not for C++ structs.

This is causing issues in existing projects that use C libraries and rely on having the default initializer available in Swift. This change enables the synthesis of a default initializer for C++ structs. Since many C++ structs are not designed to be initialized this way, the initializer is marked as deprecated in Swift.

rdar://109727620
@egorzhdan egorzhdan force-pushed the egorzhdan/c-default-init branch from cb68fa7 to fec48f9 Compare May 31, 2023 12:41
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test macOS

Copy link
Contributor

@zoecarver zoecarver left a comment

Choose a reason for hiding this comment

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

Looks great. Thank you Egor!

@egorzhdan egorzhdan merged commit 25ef5d7 into main Jun 1, 2023
@egorzhdan egorzhdan deleted the egorzhdan/c-default-init branch June 1, 2023 10:33
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