Skip to content

[cxx-interop] Make sure to create empty initializers for C++ structs #67873

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
Aug 14, 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
8 changes: 6 additions & 2 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2381,8 +2381,12 @@ namespace {
hasMemberwiseInitializer = false;
}

if (hasZeroInitializableStorage &&
!(cxxRecordDecl && cxxRecordDecl->hasDefaultConstructor())) {
bool needsEmptyInitializer = true;
if (cxxRecordDecl) {
needsEmptyInitializer = !cxxRecordDecl->hasDefaultConstructor() ||
cxxRecordDecl->ctors().empty();
}
if (hasZeroInitializableStorage && needsEmptyInitializer) {
// Add default constructor for the struct if compiling in C mode.
// If we're compiling for C++:
// 1. If a default constructor is declared, don't synthesize one.
Expand Down
6 changes: 6 additions & 0 deletions test/Interop/Cxx/objc-correctness/pthread_mutexattr_t.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// RUN: %target-typecheck-verify-swift -verify-ignore-unknown -I %S/Inputs -enable-objc-interop -enable-experimental-cxx-interop
// REQUIRES: OS=macosx

import Darwin

_ = pthread_mutexattr_t() // expected-warning {{'init()' is deprecated: This zero-initializes the backing memory of the struct}}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public func useConcreteTemplate() {
// CHECK-EMPTY:
// CHECK-NEXT: public struct TemplateRecord {
// CHECK-EMPTY:
// CHECK-NEXT: @available(*, deprecated, message:
// CHECK-NEXT: public init()
// CHECK-EMPTY:
// CHECK-NEXT: public func methodFunc(_ x: Any)
// CHECK-NEXT:}
// CHECK-NEXT:}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,24 @@ import CxxModule
// CHECK-EMPTY:
// CHECK-NEXT: struct TemplateRecord {
// CHECK-EMPTY:
// CHECK-NEXT: @available(*, deprecated, message:
// CHECK-NEXT: public init()
// CHECK-EMPTY:
// CHECK-NEXT: mutating func methodFunc(_ x: Any)
// CHECK-EMPTY:
// CHECK-NEXT: struct InnerRecord {
// CHECK-EMPTY:
// CHECK-NEXT: @available(*, deprecated, message:
// CHECK-NEXT: public init()
// CHECK-EMPTY:
// CHECK-NEXT: mutating func innerMethod(_ y: Any)
// CHECK-NEXT: }
// CHECK-EMPTY:
// CHECK-NEXT: struct InnerTemplate {
// CHECK-EMPTY:
// CHECK-NEXT: @available(*, deprecated, message:
// CHECK-NEXT: public init()
// CHECK-EMPTY:
// CHECK-NEXT: mutating func innerTemplateMethod()
// CHECK-NEXT: }
// CHECK-EMPTY:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,17 @@ public:
// CHECK-NEXT: var y: Int32
// CHECK-NEXT: }
// CHECK-NEXT: struct TemplateRecord {
// CHECK-NEXT: @available(*, deprecated, message:
// CHECK-NEXT: init()
// CHECK-NEXT: mutating func methodFunc(_ x: Any)
// CHECK-NEXT: struct InnerRecord {
// CHECK-NEXT: @available(*, deprecated, message:
// CHECK-NEXT: init()
// CHECK-NEXT: mutating func innerMethod(_ y: Any)
// CHECK-NEXT: }
// CHECK-NEXT: struct InnerTemplate {
// CHECK-NEXT: @available(*, deprecated, message:
// CHECK-NEXT: init()
// CHECK-NEXT: mutating func innerTemplateMethod()
// CHECK-NEXT: }
// CHECK-NEXT: mutating func returnsTemplateMethod()
Expand All @@ -79,7 +85,11 @@ public:
// CHECK-NEXT: }
// CHECK-NEXT: typealias MyType = ns.TemplateRecord
// CHECK-NEXT: struct OuterTemp2 {
// CHECK-NEXT: @available(*, deprecated, message:
// CHECK-NEXT: init()
// CHECK-NEXT: struct InnerTemp2 {
// CHECK-NEXT: @available(*, deprecated, message:
// CHECK-NEXT: init()
// CHECK-NEXT: init(x2: Any)
// CHECK-NEXT: mutating func testMe(_ p: Any, _ x: Any)
// CHECK-NEXT: var x2: Any
Expand Down