Skip to content

Commit ddf9268

Browse files
authored
Merge pull request #59292 from WANGJIEKE/cxx-interop-not-expose-async-and-transparent-func
[cxx-interop] Don't expose async or @_alwaysEmitIntoClient functions
2 parents e7e8eb1 + 50c1541 commit ddf9268

5 files changed

+86
-0
lines changed

lib/PrintAsClang/DeclAndTypePrinter.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,6 +1097,13 @@ class DeclAndTypePrinter::Implementation
10971097

10981098
void visitFuncDecl(FuncDecl *FD) {
10991099
if (outputLang == OutputLanguageMode::Cxx) {
1100+
// Don't expose async functions or @_alwaysEmitIntoClient functions
1101+
// because they're currently unsupported
1102+
if (FD->hasAsync() ||
1103+
FD->getAttrs().hasAttribute<AlwaysEmitIntoClientAttr>()) {
1104+
return;
1105+
}
1106+
11001107
// Emit the underlying C signature that matches the Swift ABI
11011108
// in the generated C++ implementation prologue for the module.
11021109
std::string cFuncDecl;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend %s -typecheck -module-name Functions -clang-header-expose-public-decls -emit-clang-header-path %t/functions.h
3+
// RUN: %FileCheck %s < %t/functions.h
4+
5+
// RUN: %check-interop-cxx-header-in-clang(%t/functions.h)
6+
7+
// CHECK-NOT: SWIFT_EXTERN bool $s9Functions24alwaysEmitIntoClientFuncyS2bF(bool x) SWIFT_NOEXCEPT SWIFT_CALL; // alwaysEmitIntoClientFunc(_:)
8+
9+
// CHECK: namespace Functions {
10+
// CHECK-EMPTY:
11+
// CHECK-EMPTY:
12+
// CHECK-EMPTY:
13+
// CHECK-EMPTY:
14+
// CHECK-NEXT: } // namespace Functions
15+
16+
// CHECK-NOT: inline bool alwaysEmitIntoClientFunc(bool x) noexcept SWIFT_WARN_UNUSED_RESULT {
17+
// CHECK-NOT: return _impl::$s9Functions24alwaysEmitIntoClientFuncyS2bF(x);
18+
// CHECK-NOT: }
19+
20+
@_alwaysEmitIntoClient
21+
public func alwaysEmitIntoClientFunc(_ x: Bool) -> Bool { return !x }
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend %s -typecheck -module-name Functions -disable-availability-checking -clang-header-expose-public-decls -emit-clang-header-path %t/functions.h
3+
// RUN: %FileCheck %s < %t/functions.h
4+
5+
// RUN: %check-interop-cxx-header-in-clang(%t/functions.h)
6+
7+
// CHECK-NOT: SWIFT_EXTERN double $s9Functions9asyncFuncyS2dYaF(double x) SWIFT_NOEXCEPT SWIFT_CALL; // asyncFunc(_:)
8+
9+
// CHECK: namespace Functions {
10+
// CHECK-EMPTY:
11+
// CHECK-EMPTY:
12+
// CHECK-EMPTY:
13+
// CHECK-EMPTY:
14+
// CHECK-NEXT: } // namespace Functions
15+
16+
// CHECK-NOT: inline double asyncFunc(double x) noexcept SWIFT_WARN_UNUSED_RESULT {
17+
// CHECK-NOT: return _impl::$s9Functions9asyncFuncyS2dYaF(x);
18+
// CHECK-NOT: }
19+
20+
// REQUIRES: concurrency
21+
22+
public func asyncFunc(_ x: Double) async -> Double { return 2 * x }
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend %s -typecheck -module-name Functions -clang-header-expose-public-decls -emit-clang-header-path %t/functions.h
3+
// RUN: %FileCheck %s < %t/functions.h
4+
5+
// RUN: %check-interop-cxx-header-in-clang(%t/functions.h)
6+
7+
// CHECK: SWIFT_EXTERN ptrdiff_t $s9Functions24transparentPrimitiveFuncyS2iF(ptrdiff_t x) SWIFT_NOEXCEPT SWIFT_CALL; // transparentPrimitiveFunc(_:)
8+
9+
// CHECK: inline swift::Int transparentPrimitiveFunc(swift::Int x) noexcept SWIFT_WARN_UNUSED_RESULT {
10+
// CHECK-NEXT: return _impl::$s9Functions24transparentPrimitiveFuncyS2iF(x);
11+
// CHECK-NEXT: }
12+
13+
@_transparent
14+
public func transparentPrimitiveFunc(_ x: Int) -> Int { return x * x }
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// RUN: %target-swift-frontend %S/swift-transparent-functions-cxx-bridging.swift -typecheck -module-name Functions -clang-header-expose-public-decls -emit-clang-header-path %t/functions.h
4+
5+
// RUN: %target-interop-build-clangxx -c %s -I %t -o %t/swift-functions-execution.o
6+
// RUN: %target-interop-build-swift %S/swift-transparent-functions-cxx-bridging.swift -o %t/swift-functions-execution -Xlinker %t/swift-functions-execution.o -module-name Functions -Xfrontend -entry-point-function-name -Xfrontend swiftMain
7+
8+
// RUN: %target-codesign %t/swift-functions-execution
9+
// RUN: %target-run %t/swift-functions-execution
10+
11+
// REQUIRES: executable_test
12+
13+
#include <cassert>
14+
#include "functions.h"
15+
16+
int main() {
17+
using namespace Functions;
18+
19+
assert(transparentPrimitiveFunc(12) == 144);
20+
21+
return 0;
22+
}

0 commit comments

Comments
 (0)