Skip to content

Commit fdababa

Browse files
authored
Merge pull request #61916 from hyp/eng/async-fns
[interop][SwiftToCxx] report an error for attempts to explicitly expo…
2 parents 938036b + bd63679 commit fdababa

File tree

7 files changed

+23
-5
lines changed

7 files changed

+23
-5
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,6 +1538,8 @@ ERROR(expose_inside_unexposed_decl,none,
15381538
ERROR(expose_invalid_name_pattern_init,none,
15391539
"invalid declaration name '%0' specified in an @_expose attribute; "
15401540
"exposed initializer name must start with 'init'", (StringRef))
1541+
ERROR(expose_unsupported_async_decl_to_cxx,none,
1542+
"async %0 %1 can not be exposed to C++", (DescriptiveDeclKind, ValueDecl *))
15411543
ERROR(expose_unsupported_actor_isolated_to_cxx,none,
15421544
"actor-isolated %0 %1 can not be exposed to C++", (DescriptiveDeclKind, ValueDecl *))
15431545

include/swift/AST/SwiftNameTranslation.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ getNameForCxx(const ValueDecl *VD,
6565

6666
enum RepresentationKind { Representable, Unsupported };
6767

68-
enum RepresentationError { UnrepresentableIsolatedInActor };
68+
enum RepresentationError {
69+
UnrepresentableAsync,
70+
UnrepresentableIsolatedInActor
71+
};
6972

7073
struct DeclRepresentation {
7174
RepresentationKind kind;

lib/AST/SwiftNameTranslation.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@ swift::cxx_translation::DeclRepresentation
169169
swift::cxx_translation::getDeclRepresentation(const ValueDecl *VD) {
170170
if (getActorIsolation(const_cast<ValueDecl *>(VD)).isActorIsolated())
171171
return {Unsupported, UnrepresentableIsolatedInActor};
172+
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(VD)) {
173+
if (AFD->hasAsync())
174+
return {Unsupported, UnrepresentableAsync};
175+
}
172176
return {Representable, llvm::None};
173177
}
174178

lib/PrintAsClang/DeclAndTypePrinter.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,8 +1639,7 @@ class DeclAndTypePrinter::Implementation
16391639
if (outputLang == OutputLanguageMode::Cxx) {
16401640
// Don't expose async functions or @_alwaysEmitIntoClient functions
16411641
// because they're currently unsupported
1642-
if (FD->hasAsync() ||
1643-
FD->getAttrs().hasAttribute<AlwaysEmitIntoClientAttr>()) {
1642+
if (FD->getAttrs().hasAttribute<AlwaysEmitIntoClientAttr>()) {
16441643
return;
16451644
}
16461645

lib/Sema/TypeCheckAttr.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1962,6 +1962,10 @@ void AttributeChecker::visitExposeAttr(ExposeAttr *attr) {
19621962
if (repr.isUnsupported()) {
19631963
using namespace cxx_translation;
19641964
switch (*repr.error) {
1965+
case UnrepresentableAsync:
1966+
diagnose(attr->getLocation(), diag::expose_unsupported_async_decl_to_cxx,
1967+
VD->getDescriptiveKind(), VD);
1968+
break;
19651969
case UnrepresentableIsolatedInActor:
19661970
diagnose(attr->getLocation(),
19671971
diag::expose_unsupported_actor_isolated_to_cxx,

test/Interop/SwiftToCxx/functions/swift-no-expose-unsupported-async-func.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
// CHECK: namespace Functions {
1010
// CHECK-EMPTY:
1111
// CHECK-EMPTY:
12-
// CHECK-EMPTY:
13-
// CHECK-EMPTY:
1412
// CHECK-NEXT: } // namespace Functions
1513

1614
// CHECK-NOT: inline double asyncFunc(double x) noexcept SWIFT_WARN_UNUSED_RESULT {

test/Interop/SwiftToCxx/unsupported/unsupported-decls-in-cxx.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,20 @@ public actor ActorClass {
3131

3232
@_expose(Cxx) // ok
3333
nonisolated public var prop2: Int { 42 }
34+
35+
@_expose(Cxx) // expected-error {{async instance method 'methodAsync()' can not be exposed to C++}}
36+
public nonisolated func methodAsync() async {
37+
}
3438
}
3539

3640
@_expose(Cxx)
3741
public distributed actor DistributedActorClass {
3842
public typealias ActorSystem = LocalTestingDistributedActorSystem
3943

44+
public init(actorSystem: LocalTestingDistributedActorSystem) {
45+
self.actorSystem = actorSystem
46+
}
47+
4048
@_expose(Cxx) // expected-error {{actor-isolated instance method 'unsupported()' can not be exposed to C++}}
4149
public func unsupported() {}
4250

0 commit comments

Comments
 (0)