Skip to content

Commit 56b82bd

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 4b9e512 commit 56b82bd

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
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.
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: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
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 -enable-experimental-cxx-interop -emit-clang-header-path %t/generics.h
33
// RUN: %FileCheck %s < %t/generics.h
4-
// RUN: %check-interop-cxx-header-in-clang(%t/generics.h -Wno-reserved-identifier)
4+
// RUN: %check-interop-cxx-header-in-clang(%t/generics.h -Wno-reserved-identifier -DSWIFT_CXX_INTEROP_HIDE_STL_OVERLAY)
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 -enable-experimental-cxx-interop -emit-clang-header-path %t/generics.h
88
// RUN: %FileCheck %s < %t/generics.h
9-
// RUN: %check-interop-cxx-header-in-clang(%t/generics.h -Wno-reserved-identifier)
9+
// RUN: %check-interop-cxx-header-in-clang(%t/generics.h -Wno-reserved-identifier -DSWIFT_CXX_INTEROP_HIDE_STL_OVERLAY)
1010

1111
// FIXME: remove the need for -Wno-reserved-identifier
1212

@@ -89,6 +89,13 @@ 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+
97+
// CHECK: namespace Generics SWIFT_PRIVATE_ATTR SWIFT_SYMBOL_MODULE("Generics") {
98+
9299
// CHECK: template<class T_0_0>
93100
// CHECK-NEXT: #ifdef __cpp_concepts
94101
// CHECK-NEXT: requires swift::isUsableInGenericContext<T_0_0>

0 commit comments

Comments
 (0)