Skip to content

Commit a304967

Browse files
authored
Merge pull request swiftlang#35197 from zoecarver/cxx/forward-declared-subdecl
[cxx-interop] Fix forward declared nested structs.
2 parents 5022b72 + 8746d04 commit a304967

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3374,6 +3374,11 @@ namespace {
33743374
}
33753375

33763376
if (isa<TypeDecl>(member)) {
3377+
// Only import definitions. Otherwise, we might add the same member
3378+
// twice.
3379+
if (auto tagDecl = dyn_cast<clang::TagDecl>(nd))
3380+
if (tagDecl->getDefinition() != tagDecl)
3381+
continue;
33773382
// A struct nested inside another struct will either be logically
33783383
// a sibling of the outer struct, or contained inside of it, depending
33793384
// on if it has a declaration name or not.
@@ -3534,7 +3539,7 @@ namespace {
35343539
decl->needsImplicitDefaultConstructor()) {
35353540
clang::CXXConstructorDecl *ctor =
35363541
clangSema.DeclareImplicitDefaultConstructor(
3537-
const_cast<clang::CXXRecordDecl *>(decl));
3542+
const_cast<clang::CXXRecordDecl *>(decl->getDefinition()));
35383543
if (!ctor->isDeleted())
35393544
clangSema.DefineImplicitDefaultConstructor(clang::SourceLocation(),
35403545
ctor);

test/Interop/Cxx/class/Inputs/nested-records.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ struct S10 {
4545
};
4646
};
4747

48+
struct HasForwardDeclaredNestedType {
49+
struct ForwardDeclaredType;
50+
struct NormalSubType { };
51+
struct ForwardDeclaredType { };
52+
};
53+
4854
// TODO: Nested class templates (SR-13853).
4955

5056
#endif // TEST_INTEROP_CXX_CLASS_INPUTS_NESTED_RECORDS_H

test/Interop/Cxx/class/nested-records-module-interface.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,13 @@
5555
// CHECK: }
5656
// CHECK: }
5757
// CHECK: }
58+
59+
// CHECK: struct HasForwardDeclaredNestedType {
60+
// CHECK: struct NormalSubType {
61+
// CHECK: init()
62+
// CHECK: }
63+
// CHECK: struct ForwardDeclaredType {
64+
// CHECK: init()
65+
// CHECK: }
66+
// CHECK: init()
67+
// CHECK: }

0 commit comments

Comments
 (0)