Skip to content

[cxx-interop] Make automatic conformances work with the bridging header #71243

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
Feb 1, 2024
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
2 changes: 1 addition & 1 deletion lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2808,7 +2808,7 @@ namespace {
// If this module is declared as a C++ module, try to synthesize
// conformances to Swift protocols from the Cxx module.
auto clangModule = Impl.getClangOwningModule(result->getClangNode());
if (clangModule && requiresCPlusPlus(clangModule)) {
if (!clangModule || requiresCPlusPlus(clangModule)) {
if (auto nominalDecl = dyn_cast<NominalTypeDecl>(result)) {
conformToCxxIteratorIfNeeded(Impl, nominalDecl, decl);
conformToCxxSequenceIfNeeded(Impl, nominalDecl, decl);
Expand Down
5 changes: 5 additions & 0 deletions test/Interop/Cxx/stdlib/Inputs/std-vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ class VectorSubclass: public Vector {
public:
};

class VectorOfStringSubclass : public std::vector<std::string> {
public:
using std::vector<std::string>::vector;
};

#endif // TEST_INTEROP_CXX_STDLIB_INPUTS_STD_VECTOR_H
10 changes: 9 additions & 1 deletion test/Interop/Cxx/stdlib/use-std-map.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop)
// RUN: %target-run-simple-swift(-I %S/Inputs -cxx-interoperability-mode=upcoming-swift)
//

// Also test this with a bridging header instead of the StdMap module.
// RUN: %empty-directory(%t2)
// RUN: cp %S/Inputs/std-map.h %t2/std-map-bridging-header.h
// RUN: %target-run-simple-swift(-D BRIDGING_HEADER -import-objc-header %t2/std-map-bridging-header.h -Xfrontend -enable-experimental-cxx-interop)
// RUN: %target-run-simple-swift(-D BRIDGING_HEADER -import-objc-header %t2/std-map-bridging-header.h -cxx-interoperability-mode=upcoming-swift)

// REQUIRES: executable_test
//
// REQUIRES: OS=macosx || OS=linux-gnu

import StdlibUnittest
#if !BRIDGING_HEADER
import StdMap
#endif
import CxxStdlib
import Cxx

Expand Down
10 changes: 9 additions & 1 deletion test/Interop/Cxx/stdlib/use-std-set.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop)
// RUN: %target-run-simple-swift(-I %S/Inputs -cxx-interoperability-mode=upcoming-swift)
//

// Also test this with a bridging header instead of the StdSet module.
// RUN: %empty-directory(%t2)
// RUN: cp %S/Inputs/std-set.h %t2/std-set-bridging-header.h
// RUN: %target-run-simple-swift(-D BRIDGING_HEADER -import-objc-header %t2/std-set-bridging-header.h -Xfrontend -enable-experimental-cxx-interop)
// RUN: %target-run-simple-swift(-D BRIDGING_HEADER -import-objc-header %t2/std-set-bridging-header.h -cxx-interoperability-mode=upcoming-swift)

// REQUIRES: executable_test
//
// Enable this everywhere once we have a solution for modularizing other C++ stdlibs: rdar://87654514
// REQUIRES: OS=macosx || OS=linux-gnu

import StdlibUnittest
#if !BRIDGING_HEADER
import StdSet
#endif
import CxxStdlib
import Cxx

Expand Down
21 changes: 21 additions & 0 deletions test/Interop/Cxx/stdlib/use-std-vector.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop)
// RUN: %target-run-simple-swift(-I %S/Inputs -cxx-interoperability-mode=upcoming-swift)

// Also test this with a bridging header instead of the StdVector module.
// RUN: %empty-directory(%t2)
// RUN: cp %S/Inputs/std-vector.h %t2/std-vector-bridging-header.h
// RUN: %target-run-simple-swift(-D BRIDGING_HEADER -import-objc-header %t2/std-vector-bridging-header.h -Xfrontend -enable-experimental-cxx-interop)
// RUN: %target-run-simple-swift(-D BRIDGING_HEADER -import-objc-header %t2/std-vector-bridging-header.h -cxx-interoperability-mode=upcoming-swift)

// FIXME: also run in C++20 mode when conformance works properly on UBI platform (rdar://109366764):
// %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-experimental-cxx-interop -Xcc -std=gnu++20)
//
// REQUIRES: executable_test

import StdlibUnittest
#if !BRIDGING_HEADER
import StdVector
#endif
import CxxStdlib

var StdVectorTestSuite = TestSuite("StdVector")
Expand Down Expand Up @@ -132,4 +141,16 @@ StdVectorTestSuite.test("VectorOfInt subclass for loop") {
expectEqual(count, 2)
}

StdVectorTestSuite.test("VectorOfString subclass for loop") {
var v = VectorOfStringSubclass()
v.push_back(std.string("abc"))

var count: CInt = 0
for e in v {
expectEqual(std.string("abc"), e)
count += 1
}
expectEqual(count, 1)
}

runAllTests()