Skip to content

Commit db424e2

Browse files
authored
Merge pull request #71243 from apple/egorzhdan/std-vector-bridging-header
[cxx-interop] Make automatic conformances work with the bridging header
2 parents 80205ec + c26fe86 commit db424e2

File tree

5 files changed

+45
-3
lines changed

5 files changed

+45
-3
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2808,7 +2808,7 @@ namespace {
28082808
// If this module is declared as a C++ module, try to synthesize
28092809
// conformances to Swift protocols from the Cxx module.
28102810
auto clangModule = Impl.getClangOwningModule(result->getClangNode());
2811-
if (clangModule && requiresCPlusPlus(clangModule)) {
2811+
if (!clangModule || requiresCPlusPlus(clangModule)) {
28122812
if (auto nominalDecl = dyn_cast<NominalTypeDecl>(result)) {
28132813
conformToCxxIteratorIfNeeded(Impl, nominalDecl, decl);
28142814
conformToCxxSequenceIfNeeded(Impl, nominalDecl, decl);

test/Interop/Cxx/stdlib/Inputs/std-vector.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,9 @@ class VectorSubclass: public Vector {
1717
public:
1818
};
1919

20+
class VectorOfStringSubclass : public std::vector<std::string> {
21+
public:
22+
using std::vector<std::string>::vector;
23+
};
24+
2025
#endif // TEST_INTEROP_CXX_STDLIB_INPUTS_STD_VECTOR_H

test/Interop/Cxx/stdlib/use-std-map.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop)
22
// RUN: %target-run-simple-swift(-I %S/Inputs -cxx-interoperability-mode=upcoming-swift)
3-
//
3+
4+
// Also test this with a bridging header instead of the StdMap module.
5+
// RUN: %empty-directory(%t2)
6+
// RUN: cp %S/Inputs/std-map.h %t2/std-map-bridging-header.h
7+
// RUN: %target-run-simple-swift(-D BRIDGING_HEADER -import-objc-header %t2/std-map-bridging-header.h -Xfrontend -enable-experimental-cxx-interop)
8+
// RUN: %target-run-simple-swift(-D BRIDGING_HEADER -import-objc-header %t2/std-map-bridging-header.h -cxx-interoperability-mode=upcoming-swift)
9+
410
// REQUIRES: executable_test
511
//
612
// REQUIRES: OS=macosx || OS=linux-gnu
713

814
import StdlibUnittest
15+
#if !BRIDGING_HEADER
916
import StdMap
17+
#endif
1018
import CxxStdlib
1119
import Cxx
1220

test/Interop/Cxx/stdlib/use-std-set.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop)
22
// RUN: %target-run-simple-swift(-I %S/Inputs -cxx-interoperability-mode=upcoming-swift)
3-
//
3+
4+
// Also test this with a bridging header instead of the StdSet module.
5+
// RUN: %empty-directory(%t2)
6+
// RUN: cp %S/Inputs/std-set.h %t2/std-set-bridging-header.h
7+
// RUN: %target-run-simple-swift(-D BRIDGING_HEADER -import-objc-header %t2/std-set-bridging-header.h -Xfrontend -enable-experimental-cxx-interop)
8+
// RUN: %target-run-simple-swift(-D BRIDGING_HEADER -import-objc-header %t2/std-set-bridging-header.h -cxx-interoperability-mode=upcoming-swift)
9+
410
// REQUIRES: executable_test
511
//
612
// Enable this everywhere once we have a solution for modularizing other C++ stdlibs: rdar://87654514
713
// REQUIRES: OS=macosx || OS=linux-gnu
814

915
import StdlibUnittest
16+
#if !BRIDGING_HEADER
1017
import StdSet
18+
#endif
1119
import CxxStdlib
1220
import Cxx
1321

test/Interop/Cxx/stdlib/use-std-vector.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop)
22
// RUN: %target-run-simple-swift(-I %S/Inputs -cxx-interoperability-mode=upcoming-swift)
3+
4+
// Also test this with a bridging header instead of the StdVector module.
5+
// RUN: %empty-directory(%t2)
6+
// RUN: cp %S/Inputs/std-vector.h %t2/std-vector-bridging-header.h
7+
// RUN: %target-run-simple-swift(-D BRIDGING_HEADER -import-objc-header %t2/std-vector-bridging-header.h -Xfrontend -enable-experimental-cxx-interop)
8+
// RUN: %target-run-simple-swift(-D BRIDGING_HEADER -import-objc-header %t2/std-vector-bridging-header.h -cxx-interoperability-mode=upcoming-swift)
9+
310
// FIXME: also run in C++20 mode when conformance works properly on UBI platform (rdar://109366764):
411
// %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop -Xcc -std=gnu++20)
512
//
613
// REQUIRES: executable_test
714

815
import StdlibUnittest
16+
#if !BRIDGING_HEADER
917
import StdVector
18+
#endif
1019
import CxxStdlib
1120

1221
var StdVectorTestSuite = TestSuite("StdVector")
@@ -132,4 +141,16 @@ StdVectorTestSuite.test("VectorOfInt subclass for loop") {
132141
expectEqual(count, 2)
133142
}
134143

144+
StdVectorTestSuite.test("VectorOfString subclass for loop") {
145+
var v = VectorOfStringSubclass()
146+
v.push_back(std.string("abc"))
147+
148+
var count: CInt = 0
149+
for e in v {
150+
expectEqual(std.string("abc"), e)
151+
count += 1
152+
}
153+
expectEqual(count, 1)
154+
}
155+
135156
runAllTests()

0 commit comments

Comments
 (0)