Skip to content

Commit 93e1c78

Browse files
Xazax-hunGabor Horvath
authored andcommitted
[6.2][cxx-interop] Fix a crash when exposing @objc Swift classes
Explanation: There was a null pointer dereference in reverse interop when we wanted to expose an ObjC class written in Swift. There was a crash during generating the scaffolding we do for Clang types. Since the type is written in Swift, no such scaffolding is needed, this patch skips this operation avoiding the null dereference. Scope: Reverse C++ interop when exposing @objc classes written in Swift. Issues: rdar://154252454 Original PRs: #82684 Risk: Low, the fix is narrow to the affected scenario. Testing: Added a compiler test. Reviewers: @egorzhdan
1 parent 6b838fb commit 93e1c78

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

lib/PrintAsClang/ModuleContentsWriter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,8 @@ class ModuleWriter {
580580
else if (isa<StructDecl>(TD) && NTD->hasClangNode())
581581
emitReferencedClangTypeMetadata(NTD);
582582
else if (const auto *cd = dyn_cast<ClassDecl>(TD))
583-
if (cd->isObjC() || cd->isForeignReferenceType())
583+
if ((cd->isObjC() && cd->getClangDecl()) ||
584+
cd->isForeignReferenceType())
584585
emitReferencedClangTypeMetadata(NTD);
585586
} else if (auto TAD = dyn_cast<TypeAliasDecl>(TD)) {
586587
if (TAD->hasClangNode())

test/Interop/ObjCToSwiftToObjCxx/bridge-objc-types-back-to-objcxx.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,17 @@ public func retObjCClassArray() -> [ObjCKlass] {
8888
return []
8989
}
9090

91+
public class KVOCookieMonster {
92+
public static func += (lhs: KVOCookieMonster, rhs: NSKeyValueObservation) {
93+
lhs.cookies.append(rhs)
94+
}
95+
96+
private var cookies = Array<NSKeyValueObservation>()
97+
}
98+
9199
// CHECK: @interface HasBlockField : NSObject
92100
// CHECK: @property (nonatomic, copy) void (^ _Nullable foo)(ObjCKlassState);
93101
// CHECK: @end
94-
95102
// CHECK: SWIFT_EXTERN id <ObjCProtocol> _Nonnull $s9UseObjCTy03retB9CProtocolSo0bE0_pyF(void) SWIFT_NOEXCEPT SWIFT_CALL; // retObjCProtocol()
96103
// CHECK-NEXT: #endif
97104
// CHECK-NEXT: #if defined(__OBJC__)

0 commit comments

Comments
 (0)