Skip to content

Commit 0ae0528

Browse files
authored
[cxx-interop] convert CXXForeignReferenceTypeInitializers into SuppressCXXForeignReferenceTypeInitializers to be an opt-out flag
Turning the feature "ctor of C++ foreign reference types is callable as Swift Initializers" on by default. rdar://148285972
1 parent dd2f465 commit 0ae0528

12 files changed

+48
-37
lines changed

include/swift/Basic/Features.def

+1-1
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ EXPERIMENTAL_FEATURE(ImportNonPublicCxxMembers, true)
467467

468468
/// Synthesize static factory methods for C++ foreign reference types and import
469469
/// them as Swift initializers.
470-
EXPERIMENTAL_FEATURE(CXXForeignReferenceTypeInitializers, true)
470+
EXPERIMENTAL_FEATURE(SuppressCXXForeignReferenceTypeInitializers, true)
471471

472472
// Isolated deinit
473473
SUPPRESSIBLE_LANGUAGE_FEATURE(IsolatedDeinit, 371, "isolated deinit")

lib/AST/FeatureSet.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ UNINTERESTING_FEATURE(StrictMemorySafety)
425425
UNINTERESTING_FEATURE(SafeInteropWrappers)
426426
UNINTERESTING_FEATURE(AssumeResilientCxxTypes)
427427
UNINTERESTING_FEATURE(ImportNonPublicCxxMembers)
428-
UNINTERESTING_FEATURE(CXXForeignReferenceTypeInitializers)
428+
UNINTERESTING_FEATURE(SuppressCXXForeignReferenceTypeInitializers)
429429
UNINTERESTING_FEATURE(CoroutineAccessorsUnwindOnCallerError)
430430
UNINTERESTING_FEATURE(AllowRuntimeSymbolDeclarations)
431431

lib/ClangImporter/ImportDecl.cpp

+14-3
Original file line numberDiff line numberDiff line change
@@ -2555,8 +2555,8 @@ namespace {
25552555
result->addMember(ctor);
25562556
}
25572557
} else {
2558-
if (Impl.SwiftContext.LangOpts.hasFeature(
2559-
Feature::CXXForeignReferenceTypeInitializers)) {
2558+
if (!Impl.SwiftContext.LangOpts.hasFeature(
2559+
Feature::SuppressCXXForeignReferenceTypeInitializers)) {
25602560
assert(
25612561
isa<ClassDecl>(result) &&
25622562
"Expected result to be a ClassDecl as it cannot be a StructDecl");
@@ -3625,7 +3625,18 @@ namespace {
36253625
isa<clang::FunctionDecl>(decl)
36263626
? cast<clang::FunctionDecl>(decl)->getReturnType()
36273627
: cast<clang::ObjCMethodDecl>(decl)->getReturnType();
3628-
if (isForeignReferenceTypeWithoutImmortalAttrs(retType)) {
3628+
clang::QualType pointeeType = retType;
3629+
if (retType->isPointerType() || retType->isReferenceType()) {
3630+
pointeeType = retType->getPointeeType();
3631+
}
3632+
3633+
clang::RecordDecl *recordDecl = nullptr;
3634+
if (const auto *recordType = pointeeType->getAs<clang::RecordType>()) {
3635+
recordDecl = recordType->getDecl();
3636+
}
3637+
3638+
if (recordDecl && recordHasReferenceSemantics(recordDecl) &&
3639+
!hasImmortalAttrs(recordDecl)) {
36293640
if (returnsRetainedAttrIsPresent && returnsUnretainedAttrIsPresent) {
36303641
Impl.diagnose(loc, diag::both_returns_retained_returns_unretained,
36313642
decl);

lib/ClangImporter/SwiftDeclSynthesizer.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -2538,6 +2538,9 @@ llvm::SmallVector<clang::CXXMethodDecl *, 4>
25382538
SwiftDeclSynthesizer::synthesizeStaticFactoryForCXXForeignRef(
25392539
const clang::CXXRecordDecl *cxxRecordDecl) {
25402540

2541+
if (cxxRecordDecl->isAbstract())
2542+
return {};
2543+
25412544
clang::ASTContext &clangCtx = cxxRecordDecl->getASTContext();
25422545
clang::Sema &clangSema = ImporterImpl.getClangSema();
25432546

test/Interop/Cxx/class/constructors-diagnostics.swift

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
// RUN: %target-swift-frontend -typecheck -verify -I %S/Inputs %s -cxx-interoperability-mode=default -enable-experimental-feature CXXForeignReferenceTypeInitializers -disable-availability-checking -verify-additional-file %S/Inputs/constructors.h -Xcc -Wno-nullability-completeness
1+
// RUN: %target-swift-frontend -typecheck -verify -I %S/Inputs %s -cxx-interoperability-mode=default -disable-availability-checking -verify-additional-file %S/Inputs/constructors.h -Xcc -Wno-nullability-completeness
22

3-
// This test uses -verify-additional-file, which do not work well on Windows:
3+
// This test uses -verify-additional-file, which do not work well on Windows.
44
// UNSUPPORTED: OS=windows-msvc
5-
// REQUIRES: swift_feature_CXXForeignReferenceTypeInitializers
65

76
import Constructors
87

test/Interop/Cxx/class/constructors-executable.swift

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
// RUN: %target-run-simple-swift(-I %S/Inputs/ -Xfrontend -cxx-interoperability-mode=default -enable-experimental-feature CXXForeignReferenceTypeInitializers -Xfrontend -disable-availability-checking)
1+
// RUN: %target-run-simple-swift(-I %S/Inputs/ -Xfrontend -cxx-interoperability-mode=default -Xfrontend -disable-availability-checking)
22
//
33
// REQUIRES: executable_test
4-
// REQUIRES: swift_feature_CXXForeignReferenceTypeInitializers
54

65
import Constructors
76
import CxxStdlib

test/Interop/Cxx/foreign-reference/Inputs/inheritance.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ __attribute__((swift_attr("release:immortal"))) ImmortalRefType {};
109109
ImmortalRefType *returnImmortalRefType() { return new ImmortalRefType(); };
110110

111111
struct DerivedFromImmortalRefType : ImmortalRefType {};
112-
DerivedFromImmortalRefType *returnDerivedFromImmortalRefType() {
112+
DerivedFromImmortalRefType *returnDerivedFromImmortalRefType() { // expected-warning {{'returnDerivedFromImmortalRefType' should be annotated with either SWIFT_RETURNS_RETAINED or SWIFT_RETURNS_UNRETAINED as it is returning a SWIFT_SHARED_REFERENCE}}
113113
return new DerivedFromImmortalRefType();
114114
};
115115

@@ -138,7 +138,7 @@ DerivedFromValueTypeAndAnnotated *returnDerivedFromValueTypeAndAnnotated() { //
138138
}
139139

140140
struct DerivedFromRefType final : RefType {};
141-
DerivedFromRefType *returnDerivedFromRefType() {
141+
DerivedFromRefType *returnDerivedFromRefType() { // expected-warning {{'returnDerivedFromRefType' should be annotated with either SWIFT_RETURNS_RETAINED or SWIFT_RETURNS_UNRETAINED as it is returning a SWIFT_SHARED_REFERENCE}}
142142
return new DerivedFromRefType();
143143
}
144144

@@ -209,7 +209,7 @@ __attribute__((swift_attr("release:RCRelease"))) RefType {};
209209
RefType *returnRefType() { return new RefType(); }; // expected-warning {{'returnRefType' should be annotated with either SWIFT_RETURNS_RETAINED or SWIFT_RETURNS_UNRETAINED as it is returning a SWIFT_SHARED_REFERENC}}
210210

211211
struct DerivedFromRefType final : RefType {};
212-
DerivedFromRefType *returnDerivedFromRefType() { // TODO: rdar://145098078 Missing SWIFT_RETURNS_(UN)RETAINED annotation warning should trigger for inferred foreeign reference types as well
212+
DerivedFromRefType *returnDerivedFromRefType() { // expected-warning {{'returnDerivedFromRefType' should be annotated with either SWIFT_RETURNS_RETAINED or SWIFT_RETURNS_UNRETAINED as it is returning a SWIFT_SHARED_REFERENCE}}
213213
return new DerivedFromRefType();
214214
};
215215
} // namespace BasicInheritanceExample
@@ -236,7 +236,7 @@ DerivedFromBaseRef1AndBaseRef2 *returnDerivedFromBaseRef1AndBaseRef2() {
236236
};
237237

238238
struct DerivedFromBaseRef3 : BaseRef3 {};
239-
DerivedFromBaseRef3 *returnDerivedFromBaseRef3() {
239+
DerivedFromBaseRef3 *returnDerivedFromBaseRef3() { // expected-warning {{'returnDerivedFromBaseRef3' should be annotated with either SWIFT_RETURNS_RETAINED or SWIFT_RETURNS_UNRETAINED as it is returning a SWIFT_SHARED_REFERENCE}}
240240
return new DerivedFromBaseRef3();
241241
};
242242
} // namespace MultipleInheritanceExample1
@@ -312,7 +312,7 @@ __attribute__((swift_attr("release:samerelease"))) B2 {}; // expected-error {{m
312312

313313
struct D : B1, B2 {}; // expected-error {{multiple functions 'sameretain' found; there must be exactly one retain function for reference type 'D'}}
314314
// expected-error@-1 {{multiple functions 'samerelease' found; there must be exactly one release function for reference type 'D'}}
315-
D *returnD() { return new D(); };
315+
D *returnD() { return new D(); }; // expected-warning {{'returnD' should be annotated with either SWIFT_RETURNS_RETAINED or SWIFT_RETURNS_UNRETAINED as it is returning a SWIFT_SHARED_REFERENCE}}
316316
} // namespace OverloadedRetainRelease
317317

318318
void sameretain(OverloadedRetainRelease::B1 *v) {}
@@ -339,7 +339,7 @@ struct BVirtual : virtual A {};
339339
struct CVirtual : virtual A {};
340340

341341
struct VirtualDiamond : BVirtual, CVirtual {};
342-
VirtualDiamond *returnVirtualDiamond() { return new VirtualDiamond(); };
342+
VirtualDiamond *returnVirtualDiamond() { return new VirtualDiamond(); }; // expected-warning {{'returnVirtualDiamond' should be annotated with either SWIFT_RETURNS_RETAINED or SWIFT_RETURNS_UNRETAINED as it is returning a SWIFT_SHARED_REFERENCE}}
343343
} // namespace RefTypeDiamondInheritance
344344

345345
void retainA(RefTypeDiamondInheritance::A *a) {};
@@ -355,7 +355,7 @@ __attribute__((swift_attr("release:releaseB"))) B : A {};
355355
struct C : A {};
356356

357357
struct Diamond : B, C {};
358-
Diamond *returnDiamond() { return new Diamond(); };
358+
Diamond *returnDiamond() { return new Diamond(); }; // expected-warning {{'returnDiamond' should be annotated with either SWIFT_RETURNS_RETAINED or SWIFT_RETURNS_UNRETAINED as it is returning a SWIFT_SHARED_REFERENCE}}
359359

360360
} // namespace NonRefTypeDiamondInheritance
361361

@@ -384,7 +384,7 @@ __attribute__((swift_attr("retain:forestRetain"))) __attribute__((
384384
};
385385

386386
class Forest : public IntrusiveRefCountedTemplate<Forest> {};
387-
Forest *returnForest() { return new Forest(); };
387+
Forest *returnForest() { return new Forest(); }; // expected-warning {{'returnForest' should be annotated with either SWIFT_RETURNS_RETAINED or SWIFT_RETURNS_UNRETAINED as it is returning a SWIFT_SHARED_REFERENCE}}
388388
} // namespace InheritingTemplatedRefType
389389

390390
void forestRetain(InheritingTemplatedRefType::IntrusiveRefCountedTemplate<

test/Interop/Cxx/foreign-reference/no-ctor-errors.swift renamed to test/Interop/Cxx/foreign-reference/cxx-frt-ctor-callable.swift

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// RUN: rm -rf %t
22
// RUN: split-file %s %t
3-
// RUN: not %target-swift-frontend -typecheck -I %t/Inputs %t/test.swift -enable-experimental-cxx-interop 2>&1 | %FileCheck %s
3+
// RUN: %target-swift-frontend -typecheck -verify -I %t/Inputs %t/test.swift -enable-experimental-cxx-interop -disable-availability-checking
44

55
//--- Inputs/module.modulemap
66
module Test {
@@ -21,5 +21,4 @@ HasCtor {
2121

2222
import Test
2323

24-
// CHECK: error: 'HasCtor' cannot be constructed because it has no accessible initializers
25-
let x = HasCtor(42)
24+
let x = HasCtor(42)

test/Interop/Cxx/foreign-reference/move-only-module-interface.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
// RUN: %target-swift-ide-test -print-module -module-to-print=MoveOnly -I %S/Inputs -source-filename=x -enable-experimental-cxx-interop | %FileCheck %s
22

33
// CHECK: class MoveOnly {
4-
// CHECK-NOT: init
4+
// CHECK: init
55
// CHECK: func test() -> Int32
66
// CHECK: func testMutable() -> Int32
77
// CHECK: class func create() -> MoveOnly
88
// CHECK: }
99
// CHECK-NOT: func moveIntoResult(_ x: MoveOnly) -> MoveOnly
1010

1111
// CHECK: class HasMoveOnlyChild {
12-
// CHECK-NOT: init
12+
// CHECK: init
1313
// CHECK-NOT: var child: MoveOnly
1414
// CHECK: class func create() -> HasMoveOnlyChild
1515
// CHECK: }
1616
// CHECK-NOT: func moveIntoResult(_ x: HasMoveOnlyChild) -> HasMoveOnlyChild
1717

1818
// CHECK: class PrivateCopyCtor {
19-
// CHECK-NOT: init
19+
// CHECK: init
2020
// CHECK: func test() -> Int32
2121
// CHECK: func testMutable() -> Int32
2222
// CHECK: class func create() -> PrivateCopyCtor
2323
// CHECK: }
2424
// CHECK-NOT: func moveIntoResult(_ x: PrivateCopyCtor) -> PrivateCopyCtor
2525

2626
// CHECK: class BadCopyCtor {
27-
// CHECK-NOT: init
27+
// CHECK: init
2828
// CHECK: func test() -> Int32
2929
// CHECK: func testMutable() -> Int32
3030
// CHECK: class func create() -> BadCopyCtor

test/Interop/Cxx/foreign-reference/pod-module-interface.swift

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %target-swift-ide-test -print-module -module-to-print=POD -I %S/Inputs -source-filename=x -enable-experimental-cxx-interop | %FileCheck %s
22

33
// CHECK: class Empty {
4-
// CHECK-NOT: init
4+
// CHECK: init
55
// CHECK: func test() -> Int32
66
// CHECK: func testMutable() -> Int32
77
// CHECK: class func create() -> Empty
@@ -12,14 +12,14 @@
1212
// CHECK-NOT: func passThroughByValue(_ x: Empty) -> Empty
1313

1414
// CHECK: class MultipleAttrs {
15-
// CHECK-NOT: init
15+
// CHECK: init
1616
// CHECK: func test() -> Int32
1717
// CHECK: func testMutable() -> Int32
1818
// CHECK: class func create() -> MultipleAttrs
1919
// CHECK: }
2020

2121
// CHECK: class IntPair {
22-
// CHECK-NOT: init
22+
// CHECK: init
2323
// CHECK: func test() -> Int32
2424
// CHECK: func testMutable() -> Int32
2525
// CHECK: func instancePassThroughByRef(_ ref: IntPair) -> IntPair
@@ -33,7 +33,7 @@
3333
// CHECK: func passThroughByRef(_ x: IntPair) -> IntPair
3434

3535
// CHECK: class RefHoldingPair {
36-
// CHECK-NOT: init
36+
// CHECK: init
3737
// CHECK-NOT: pair
3838
// CHECK: func test() -> Int32
3939
// CHECK: func testMutable() -> Int32
@@ -42,7 +42,7 @@
4242
// CHECK: }
4343

4444
// CHECK: class RefHoldingPairRef {
45-
// CHECK-NOT: init
45+
// CHECK: init
4646
// CHECK: func test() -> Int32
4747
// CHECK: func testMutable() -> Int32
4848
// CHECK: class func create() -> RefHoldingPairRef
@@ -51,7 +51,7 @@
5151
// CHECK: }
5252

5353
// CHECK: class RefHoldingPairPtr {
54-
// CHECK-NOT: init
54+
// CHECK: init
5555
// CHECK: func test() -> Int32
5656
// CHECK: func testMutable() -> Int32
5757
// CHECK: class func create() -> RefHoldingPairPtr
@@ -60,7 +60,6 @@
6060
// CHECK: }
6161

6262
// CHECK: struct ValueHoldingPair {
63-
// CHECK-NOT: init
6463
// CHECK-NOT: pair
6564
// CHECK: init()
6665
// CHECK: func test() -> Int32
@@ -77,7 +76,7 @@
7776
// CHECK: }
7877

7978
// CHECK: class BigType {
80-
// CHECK-NOT: init
79+
// CHECK: init
8180
// CHECK: func test() -> Int32
8281
// CHECK: func testMutable() -> Int32
8382
// CHECK: class func create() -> BigType

test/Interop/Cxx/foreign-reference/singleton-module-interface.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %target-swift-ide-test -print-module -module-to-print=Singleton -I %S/Inputs -source-filename=x -enable-experimental-cxx-interop | %FileCheck %s
22

33
// CHECK: class DeletedDtor {
4-
// CHECK-NOT: init
4+
// CHECK: init
55
// CHECK: func test() -> Int32
66
// CHECK: func testMutable() -> Int32
77
// CHECK: class func create() -> DeletedDtor
@@ -10,7 +10,7 @@
1010
// CHECK: func mutateIt(_ x: DeletedDtor)
1111

1212
// CHECK: class PrivateDtor {
13-
// CHECK-NOT: init
13+
// CHECK: init
1414
// CHECK: func test() -> Int32
1515
// CHECK: func testMutable() -> Int32
1616
// CHECK: class func create() -> PrivateDtor
@@ -19,7 +19,7 @@
1919
// CHECK: func mutateIt(_ x: PrivateDtor)
2020

2121
// CHECK: class DeletedSpecialMembers {
22-
// CHECK-NOT: init
22+
// CHECK: init
2323
// CHECK: func test() -> Int32
2424
// CHECK: func testMutable() -> Int32
2525
// CHECK: class func create() -> DeletedSpecialMembers
@@ -28,7 +28,7 @@
2828
// CHECK: func mutateIt(_ x: DeletedSpecialMembers)
2929

3030
// CHECK: class PrivateSpecialMembers {
31-
// CHECK-NOT: init
31+
// CHECK: init
3232
// CHECK: func test() -> Int32
3333
// CHECK: func testMutable() -> Int32
3434
// CHECK: class func create() -> PrivateSpecialMembers

test/Interop/Cxx/symbolic-imports/print-symbolic-module-interface.swift

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ struct NonCopyable {
119119
// CHECK-NEXT: }
120120
// CHECK-NEXT: }
121121
// CHECK: class MyImmortal {
122+
// CHECK-NEXT: init
122123
// CHECK-NEXT: func foo()
123124
// CHECK-NEXT: }
124125
// CHECK-NEXT: struct NonCopyable {

0 commit comments

Comments
 (0)