Skip to content

Commit 879b04f

Browse files
authored
Merge pull request #69327 from apple/egorzhdan/circular-requests
[cxx-interop] Avoid circular reference errors when importing C++ structs
2 parents 5fed479 + 4632d89 commit 879b04f

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2199,6 +2199,13 @@ namespace {
21992199
// FIXME: Figure out what to do with superclasses in C++. One possible
22002200
// solution would be to turn them into members and add conversion
22012201
// functions.
2202+
if (auto cxxRecordDecl = dyn_cast<clang::CXXRecordDecl>(decl)) {
2203+
for (auto base : cxxRecordDecl->bases()) {
2204+
if (auto *baseRecordDecl = base.getType()->getAsCXXRecordDecl()) {
2205+
Impl.importDecl(baseRecordDecl, getVersion());
2206+
}
2207+
}
2208+
}
22022209

22032210
// Import each of the members.
22042211
SmallVector<VarDecl *, 4> members;

test/Interop/Cxx/class/inheritance/Inputs/module.modulemap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ module Polymorphism {
1111
requires cplusplus
1212
}
1313

14+
module ReferenceToDerived {
15+
header "reference-to-derived.h"
16+
requires cplusplus
17+
}
18+
1419
module SubTypes {
1520
header "sub-types.h"
1621
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class E;
2+
3+
class C {
4+
public:
5+
E *getE() const;
6+
};
7+
8+
class D : public C {};
9+
10+
class E : public D {
11+
public:
12+
D *getD() const;
13+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: %target-swift-emit-ir -I %S/Inputs -enable-experimental-cxx-interop %s -validate-tbd-against-ir=none
2+
// We should not fail with a circular reference error here.
3+
4+
import ReferenceToDerived
5+
6+
func foo(_ x: D) {}

0 commit comments

Comments
 (0)