From 5ca0b5a0c5ebf5d9eae2fac53a549bf4d0d06ed3 Mon Sep 17 00:00:00 2001 From: Alex Lorenz Date: Fri, 17 Mar 2023 18:17:32 -0700 Subject: [PATCH] [interop][SwiftToCxx] ensure we always expose limited set of APIs from Swift's standard library to C++ Fixes https://github.com/apple/swift/issues/64340 --- lib/PrintAsClang/DeclAndTypePrinter.cpp | 6 +++++- .../enums/swift-enum-implementation.swift | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/PrintAsClang/DeclAndTypePrinter.cpp b/lib/PrintAsClang/DeclAndTypePrinter.cpp index 89ccc75dd1af0..cbde3dd673baf 100644 --- a/lib/PrintAsClang/DeclAndTypePrinter.cpp +++ b/lib/PrintAsClang/DeclAndTypePrinter.cpp @@ -2784,8 +2784,12 @@ static bool isExposedToThisModule(const ModuleDecl &M, const ValueDecl *VD, if (VD->hasClangNode()) return true; auto *mc = VD->getModuleContext(); - if (mc == &M || mc->isStdlibModule()) + if (mc == &M) return true; + // Only certain declarations are exposed from + // the standard library. + if (mc->isStdlibModule()) + return hasExposeAttr(VD); return exposedModules.count(mc->getName().str()); } diff --git a/test/Interop/SwiftToCxx/enums/swift-enum-implementation.swift b/test/Interop/SwiftToCxx/enums/swift-enum-implementation.swift index fff63ddd617d4..a3e864fbbb957 100644 --- a/test/Interop/SwiftToCxx/enums/swift-enum-implementation.swift +++ b/test/Interop/SwiftToCxx/enums/swift-enum-implementation.swift @@ -4,6 +4,9 @@ // RUN: %check-interop-cxx-header-in-clang(%t/enums.h -Wno-unused-private-field -Wno-unused-function) +// RUN: %target-swift-frontend %s -typecheck -module-name Enums -enable-experimental-cxx-interop -emit-clang-header-path %t/enums-default.h +// RUN: %FileCheck %s < %t/enums-default.h + public enum E { case x(Double) case y(UnsafeRawPointer?) @@ -25,6 +28,11 @@ public enum E { } } +public enum E2 { + case foobar + case baz +} + public struct S { public var x: Int64 @@ -166,6 +174,15 @@ public struct S { // CHECK-NEXT: #endif // CHECK-NEXT: vwTable->initializeWithTake(destStorage, srcStorage, metadata._0); // CHECK-NEXT: } + +// CHECK: class SWIFT_SYMBOL({{.*}}) E2 final { +// CHECK: SWIFT_INLINE_THUNK operator cases() const { +// CHECK: } +// CHECK-NEXT: } +// CHECK-EMPTY: +// CHECK-NEXT: SWIFT_INLINE_THUNK swift::Int getHashValue() const SWIFT_SYMBOL({{.*}}); +// CHECK-NEXT: private: + // CHECK: namespace Enums SWIFT_PRIVATE_ATTR SWIFT_SYMBOL_MODULE("Enums") { // CHECK: SWIFT_INLINE_THUNK E E::_impl_x::operator()(double val) const { // CHECK-NEXT: auto result = E::_make();