Skip to content

🍒[cxx-interop] Import iterator types that are not typedef-ed #67486

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
Jul 24, 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
9 changes: 9 additions & 0 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2742,6 +2742,15 @@ getClangOwningModule(ClangNode Node, const clang::ASTContext &ClangCtx) {
originalDecl = pattern;
}
}
if (!originalDecl->hasOwningModule()) {
if (auto cxxRecordDecl = dyn_cast<clang::CXXRecordDecl>(D)) {
if (auto pattern = cxxRecordDecl->getTemplateInstantiationPattern()) {
// Class template instantiations sometimes don't have an owning Clang
// module, if the instantiation is not typedef-ed.
originalDecl = pattern;
}
}
}

return ExtSource->getModule(originalDecl->getOwningModuleID());
}
Expand Down
2 changes: 1 addition & 1 deletion lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2793,7 +2793,7 @@ namespace {

// If this module is declared as a C++ module, try to synthesize
// conformances to Swift protocols from the Cxx module.
auto clangModule = decl->getOwningModule();
auto clangModule = Impl.getClangOwningModule(result->getClangNode());
if (clangModule && requiresCPlusPlus(clangModule)) {
auto nominalDecl = cast<NominalTypeDecl>(result);
conformToCxxIteratorIfNeeded(Impl, nominalDecl, decl);
Expand Down
2 changes: 2 additions & 0 deletions test/Interop/Cxx/stdlib/Inputs/std-vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
#define TEST_INTEROP_CXX_STDLIB_INPUTS_STD_VECTOR_H

#include <vector>
#include <string>

using Vector = std::vector<int>;
using VectorOfString = std::vector<std::string>;

inline Vector initVector() { return {}; }

Expand Down
34 changes: 30 additions & 4 deletions test/Interop/Cxx/stdlib/use-std-vector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import CxxStdlib.vector

var StdVectorTestSuite = TestSuite("StdVector")

StdVectorTestSuite.test("init") {
StdVectorTestSuite.test("VectorOfInt.init") {
let v = Vector()
expectEqual(v.size(), 0)
expectTrue(v.empty())
}

StdVectorTestSuite.test("push back") {
StdVectorTestSuite.test("VectorOfInt.push_back") {
var v = Vector()
let _42: CInt = 42
v.push_back(_42)
Expand All @@ -33,7 +33,7 @@ func fill(vector v: inout Vector) {
v.push_back(CInt(3))
}

StdVectorTestSuite.test("for loop") {
StdVectorTestSuite.test("VectorOfInt for loop") {
var v = Vector()
fill(vector: &v)

Expand All @@ -45,12 +45,38 @@ StdVectorTestSuite.test("for loop") {
expectEqual(count, 4)
}

StdVectorTestSuite.test("map") {
StdVectorTestSuite.test("VectorOfString for loop") {
var v = VectorOfString()
var count = 0
for _ in v {
count += 1
}
expectEqual(count, 0)

v.push_back(std.string("abc"))
v.push_back(std.string("ab"))
for it in v {
count += it.length()
}
expectEqual(count, 5)
}

StdVectorTestSuite.test("VectorOfInt.map") {
var v = Vector()
fill(vector: &v)

let a = v.map { $0 + 5 }
expectEqual(a, [6, 7, 8])
}

StdVectorTestSuite.test("VectorOfString.map") {
var v = VectorOfString()
v.push_back(std.string("abc"))
v.push_back(std.string("a"))
v.push_back(std.string("ab"))

let a = v.map { $0.length() }
expectEqual(a, [3, 1, 2])
}

runAllTests()