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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.

// Add default constructor for the struct if compiling in C mode.
// If we're compiling for C++, we'll import the C++ default constructor
// (if there is one), so we don't need to synthesize one here.
ctors.push_back(synthesizer.createDefaultConstructor(result));
// If we're compiling for C++:
// 1. If a default constructor is declared, don't synthesize one.
// 2. If a default constructor is deleted, don't try to synthesize one.
// 3. If there is no default constructor, synthesize a C-like default
// constructor that zero-initializes the backing memory of the
// struct. This is important to maintain source compatibility when a
// client enables C++ interop in an existing project that uses C
// interop and might rely on the fact that C structs have a default
// constructor available in Swift.
ConstructorDecl *defaultCtor =
synthesizer.createDefaultConstructor(result);
ctors.push_back(defaultCtor);
if (cxxRecordDecl) {
auto attr = AvailableAttr::createPlatformAgnostic(
defaultCtor->getASTContext(),
"This zero-initializes the backing memory of the struct, which "
"is unsafe for some C++ structs. Consider adding an explicit "
"default initializer for this C++ struct.",
"", PlatformAgnosticAvailabilityKind::Deprecated);
defaultCtor->getAttrs().add(attr);
}
}

// We can assume that it is possible to correctly construct the object by
Expand Down
4 changes: 0 additions & 4 deletions lib/ClangImporter/SwiftDeclSynthesizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,10 +438,6 @@ synthesizeStructDefaultConstructorBody(AbstractFunctionDecl *afd,
ASTContext &ctx = constructor->getASTContext();
auto structDecl = static_cast<StructDecl *>(context);

// We should call into C++ constructors directly.
assert(!isa<clang::CXXRecordDecl>(structDecl->getClangDecl()) &&
"Should not synthesize a C++ object constructor.");

// Use a builtin to produce a zero initializer, and assign it to self.

// Construct the left-hand reference to self.
Expand Down
8 changes: 8 additions & 0 deletions test/Interop/Cxx/class/constructors-module-interface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@
// CHECK-NEXT: }
// CHECK-NEXT: struct ConstructorWithParam {
// CHECK-NEXT: init(_ val: Int32)
// CHECK-NEXT: @available(*, deprecated, message
// CHECK-NEXT: init()
// CHECK-NEXT: var x: Int32
// CHECK-NEXT: }
// CHECK-NEXT: struct CopyAndMoveConstructor {
// CHECK-NEXT: @available(*, deprecated, message
// CHECK-NEXT: init()
// CHECK-NEXT: }
// CHECK-NEXT: struct Base {
// CHECK-NEXT: init()
Expand All @@ -38,10 +42,14 @@
// CHECK-NEXT: }
// CHECK: struct TemplatedConstructor {
// CHECK-NEXT: init<T>(_ value: T)
// CHECK-NEXT: @available(*, deprecated, message
// CHECK-NEXT: init()
// CHECK-NEXT: var value: ArgType
// CHECK-NEXT: }
// CHECK: struct TemplatedConstructorWithExtraArg {
// CHECK-NEXT: init<T>(_: Int32, _ value: T)
// CHECK-NEXT: init<T>(_ value: T, _: Int32)
// CHECK-NEXT: init<T, U>(_ value: T, _ other: U)
// CHECK-NEXT: @available(*, deprecated, message
// CHECK-NEXT: init()
// CHECK-NEXT: }
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@

// CHECK: struct ConstructorWithNSArrayParam {
// CHECK-NEXT: init(_ array: [Any]!)
// CHECK-NEXT: @available(*, deprecated, message
// CHECK-NEXT: init()
// CHECK-NEXT: }
2 changes: 1 addition & 1 deletion test/Interop/Cxx/class/constructors-typechecker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ let explicit = ExplicitDefaultConstructor()

let implicit = ImplicitDefaultConstructor()

let deletedImplicitly = ConstructorWithParam() // expected-error {{missing argument for parameter #1 in call}}
let deletedImplicitly = ConstructorWithParam() // expected-warning {{'init()' is deprecated}}

let deletedExplicitly = DefaultConstructorDeleted() // expected-error {{missing argument for parameter 'a' in call}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// CHECK: class C {
// CHECK-NEXT: }
// CHECK-NEXT: struct S {
// CHECK-NEXT: @available(*, deprecated, message
// CHECK-NEXT: init()
// CHECK-NEXT: mutating func f() -> Int32
// CHECK-NEXT: }
// CHECK-NEXT: func getSPtr() -> UnsafeMutablePointer<S>!