Skip to content

[interop][SwiftToCxx] report an error for attempts to explicitly expo… #61916

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -1538,6 +1538,8 @@ ERROR(expose_inside_unexposed_decl,none,
ERROR(expose_invalid_name_pattern_init,none,
"invalid declaration name '%0' specified in an @_expose attribute; "
"exposed initializer name must start with 'init'", (StringRef))
ERROR(expose_unsupported_async_decl_to_cxx,none,
"async %0 %1 can not be exposed to C++", (DescriptiveDeclKind, ValueDecl *))
ERROR(expose_unsupported_actor_isolated_to_cxx,none,
"actor-isolated %0 %1 can not be exposed to C++", (DescriptiveDeclKind, ValueDecl *))

Expand Down
5 changes: 4 additions & 1 deletion include/swift/AST/SwiftNameTranslation.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ getNameForCxx(const ValueDecl *VD,

enum RepresentationKind { Representable, Unsupported };

enum RepresentationError { UnrepresentableIsolatedInActor };
enum RepresentationError {
UnrepresentableAsync,
UnrepresentableIsolatedInActor
};

struct DeclRepresentation {
RepresentationKind kind;
Expand Down
4 changes: 4 additions & 0 deletions lib/AST/SwiftNameTranslation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ swift::cxx_translation::DeclRepresentation
swift::cxx_translation::getDeclRepresentation(const ValueDecl *VD) {
if (getActorIsolation(const_cast<ValueDecl *>(VD)).isActorIsolated())
return {Unsupported, UnrepresentableIsolatedInActor};
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(VD)) {
if (AFD->hasAsync())
return {Unsupported, UnrepresentableAsync};
}
return {Representable, llvm::None};
}

Expand Down
3 changes: 1 addition & 2 deletions lib/PrintAsClang/DeclAndTypePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1590,8 +1590,7 @@ class DeclAndTypePrinter::Implementation
if (outputLang == OutputLanguageMode::Cxx) {
// Don't expose async functions or @_alwaysEmitIntoClient functions
// because they're currently unsupported
if (FD->hasAsync() ||
FD->getAttrs().hasAttribute<AlwaysEmitIntoClientAttr>()) {
if (FD->getAttrs().hasAttribute<AlwaysEmitIntoClientAttr>()) {
return;
}

Expand Down
4 changes: 4 additions & 0 deletions lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1957,6 +1957,10 @@ void AttributeChecker::visitExposeAttr(ExposeAttr *attr) {
if (repr.isUnsupported()) {
using namespace cxx_translation;
switch (*repr.error) {
case UnrepresentableAsync:
diagnose(attr->getLocation(), diag::expose_unsupported_async_decl_to_cxx,
VD->getDescriptiveKind(), VD);
break;
case UnrepresentableIsolatedInActor:
diagnose(attr->getLocation(),
diag::expose_unsupported_actor_isolated_to_cxx,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
// CHECK: namespace Functions {
// CHECK-EMPTY:
// CHECK-EMPTY:
// CHECK-EMPTY:
// CHECK-EMPTY:
// CHECK-NEXT: } // namespace Functions

// CHECK-NOT: inline double asyncFunc(double x) noexcept SWIFT_WARN_UNUSED_RESULT {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,20 @@ public actor ActorClass {

@_expose(Cxx) // ok
nonisolated public var prop2: Int { 42 }

@_expose(Cxx) // expected-error {{async instance method 'methodAsync()' can not be exposed to C++}}
public nonisolated func methodAsync() async {
}
}

@_expose(Cxx)
public distributed actor DistributedActorClass {
public typealias ActorSystem = LocalTestingDistributedActorSystem

public init(actorSystem: LocalTestingDistributedActorSystem) {
self.actorSystem = actorSystem
}

@_expose(Cxx) // expected-error {{actor-isolated instance method 'unsupported()' can not be exposed to C++}}
public func unsupported() {}

Expand Down