Skip to content

Commit aef88b7

Browse files
Xazax-hunGabor Horvath
authored andcommitted
🍒[cxx-interop] Do not expose enums with optional generic cases
Explanation: The code path trying to generate the C++ header for enums that have cases with associated values of a generic optional type resulted in null pointer dereference. The proper fix is a bit more involved so this PR just temporarily avoids exposing the problematic enums to C++. Scope: C++ Reverse Interop Risk: Low, we stop exposing enums that were crashing the compiler before. Testing: Added a regression test. Issue: rdar://129250756 Reviewer: @egorzhdan Original PR: #74966
1 parent b68ecdc commit aef88b7

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

lib/PrintAsClang/DeclAndTypePrinter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2910,6 +2910,9 @@ static bool isEnumExposableToCxx(const ValueDecl *VD,
29102910
if (auto *params = elementDecl->getParameterList()) {
29112911
for (const auto *param : *params) {
29122912
auto paramType = param->getInterfaceType();
2913+
// TODO: properly support exporting these optionals. rdar://131112273
2914+
if (paramType->isOptional() && paramType->getOptionalObjectType()->isTypeParameter())
2915+
return false;
29132916
if (DeclAndTypeClangFunctionPrinter::getTypeRepresentation(
29142917
printer.getTypeMapping(), printer.getInteropContext(),
29152918
printer, enumDecl->getModuleContext(), paramType)

test/Interop/SwiftToCxx/generics/generic-enum-in-cxx.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: %target-swift-frontend %s -typecheck -module-name Generics -clang-header-expose-decls=all-public -emit-clang-header-path %t/generics.h
2+
// RUN: %target-swift-frontend %s -typecheck -module-name Generics -emit-clang-header-path %t/generics.h
33
// RUN: %FileCheck %s < %t/generics.h
44
// RUN: %check-interop-cxx-header-in-clang(%t/generics.h -Wno-reserved-identifier)
55

66
// RUN: %empty-directory(%t)
7-
// RUN: %target-swift-frontend %s -enable-library-evolution -typecheck -module-name Generics -clang-header-expose-decls=all-public -emit-clang-header-path %t/generics.h
7+
// RUN: %target-swift-frontend %s -enable-library-evolution -typecheck -module-name Generics -emit-clang-header-path %t/generics.h
88
// RUN: %FileCheck %s < %t/generics.h
99
// RUN: %check-interop-cxx-header-in-clang(%t/generics.h -Wno-reserved-identifier)
1010

@@ -89,6 +89,11 @@ public func inoutConcreteOpt(_ x: inout GenericOpt<UInt16>) {
8989
}
9090
}
9191

92+
@frozen public enum GenericCustomType<T> {
93+
case success(T?)
94+
case failure
95+
}
96+
9297
// CHECK: template<class T_0_0>
9398
// CHECK-NEXT: #ifdef __cpp_concepts
9499
// CHECK-NEXT: requires swift::isUsableInGenericContext<T_0_0>

0 commit comments

Comments
 (0)