Skip to content

[interop][SwiftToCxx] do not expose actor classes in the generated he… #61444

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
Oct 5, 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 @@ -1506,6 +1506,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_actor_to_cxx,none,
"actor %0 can not be exposed to C++", (DeclName))

ERROR(attr_methods_only,none,
"only methods can be declared %0", (DeclAttribute))
Expand Down
21 changes: 21 additions & 0 deletions include/swift/AST/SwiftNameTranslation.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,27 @@ StringRef
getNameForCxx(const ValueDecl *VD,
CustomNamesOnly_t customNamesOnly = objc_translation::Normal);

enum RepresentationKind { Representable, Unsupported };

enum RepresentationError { UnrepresentableActorClass };

struct DeclRepresentation {
RepresentationKind kind;
llvm::Optional<RepresentationError> error;

/// Returns true if the given Swift node is unsupported in Clang in any
/// language mode.
bool isUnsupported() const { return kind == Unsupported; }
};

/// Returns the C++ representation info for the given declaration.
DeclRepresentation getDeclRepresentation(const ValueDecl *VD);

/// Returns true if the given value decl is exposable to C++.
inline bool isExposableToCxx(const ValueDecl *VD) {
return !getDeclRepresentation(VD).isUnsupported();
}

/// Returns true if the given value decl D is visible to C++ of its
/// own accord (i.e. without considering its context)
bool isVisibleToCxx(const ValueDecl *VD, AccessLevel minRequiredAccess,
Expand Down
10 changes: 10 additions & 0 deletions lib/AST/SwiftNameTranslation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,16 @@ swift::cxx_translation::getNameForCxx(const ValueDecl *VD,
return VD->getBaseIdentifier().str();
}

swift::cxx_translation::DeclRepresentation
swift::cxx_translation::getDeclRepresentation(const ValueDecl *VD) {
if (auto *CD = dyn_cast<ClassDecl>(VD)) {
// Actors are not exposable to C++.
if (CD->isAnyActor())
return {Unsupported, UnrepresentableActorClass};
}
return {Representable, llvm::None};
}

bool swift::cxx_translation::isVisibleToCxx(const ValueDecl *VD,
AccessLevel minRequiredAccess,
bool checkParent) {
Expand Down
3 changes: 2 additions & 1 deletion lib/PrintAsClang/DeclAndTypePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2609,7 +2609,8 @@ static bool hasExposeAttr(const ValueDecl *VD, bool isExtension = false) {
bool DeclAndTypePrinter::shouldInclude(const ValueDecl *VD) {
return !VD->isInvalid() && (!requiresExposedAttribute || hasExposeAttr(VD)) &&
(outputLang == OutputLanguageMode::Cxx
? cxx_translation::isVisibleToCxx(VD, minRequiredAccess)
? cxx_translation::isVisibleToCxx(VD, minRequiredAccess) &&
cxx_translation::isExposableToCxx(VD)
: isVisibleToObjC(VD, minRequiredAccess)) &&
!VD->getAttrs().hasAttribute<ImplementationOnlyAttr>() &&
!isAsyncAlternativeOfOtherDecl(VD);
Expand Down
12 changes: 12 additions & 0 deletions lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1905,6 +1905,18 @@ void AttributeChecker::visitExposeAttr(ExposeAttr *attr) {
decl->getName());
}

// Verify that the declaration is exposable.
auto repr = cxx_translation::getDeclRepresentation(VD);
if (repr.isUnsupported()) {
using namespace cxx_translation;
switch (*repr.error) {
case UnrepresentableActorClass:
diagnose(attr->getLocation(), diag::expose_unsupported_actor_to_cxx,
decl->getName());
break;
}
}

// Verify that the name mentioned in the expose
// attribute matches the supported name pattern.
if (!attr->Name.empty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend %s -typecheck -module-name Functions -verify -clang-header-expose-decls=has-expose-attr -disable-availability-checking -emit-clang-header-path %t/functions.h

// RUN: cat %s | grep -v _expose > %t/clean.swift
// RUN: %target-swift-frontend %t/clean.swift -typecheck -module-name Functions -clang-header-expose-decls=all-public -disable-availability-checking -emit-clang-header-path %t/header.h
// RUN: %FileCheck %s < %t/header.h

// REQUIRES: concurrency

// CHECK-NOT: Unsupported
// CHECK: supported

public func supported() {}

@_expose(Cxx) // expected-error {{actor 'ActorClassUnsupported' can not be exposed to C++}}
public actor ActorClassUnsupported {
public func test() {}
}