Skip to content

[interop][SwiftToCxx] diagnose when certain generic decls can not yet… #63970

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
Mar 1, 2023
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
4 changes: 4 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -1615,6 +1615,10 @@ ERROR(expose_unsupported_actor_isolated_to_cxx,none,
"actor-isolated %0 %1 can not be exposed to C++", (DescriptiveDeclKind, ValueDecl *))
ERROR(expose_unsupported_client_emission_to_cxx,none,
"%0 %1 can not be exposed to C++ as it requires code to be emitted into client", (DescriptiveDeclKind, ValueDecl *))
ERROR(expose_generic_decl_to_cxx,none,
"generic %0 %1 can not yet be exposed to C++", (DescriptiveDeclKind, ValueDecl *))
ERROR(expose_generic_requirement_to_cxx,none,
"generic requirements for %0 %1 can not yet be represented in C++", (DescriptiveDeclKind, ValueDecl *))

ERROR(attr_methods_only,none,
"only methods can be declared %0", (DeclAttribute))
Expand Down
2 changes: 2 additions & 0 deletions include/swift/AST/SwiftNameTranslation.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ enum RepresentationError {
UnrepresentableAsync,
UnrepresentableIsolatedInActor,
UnrepresentableRequiresClientEmission,
UnrepresentableGeneric,
UnrepresentableGenericRequirements
};

struct DeclRepresentation {
Expand Down
14 changes: 14 additions & 0 deletions lib/AST/SwiftNameTranslation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,28 @@ swift::cxx_translation::DeclRepresentation
swift::cxx_translation::getDeclRepresentation(const ValueDecl *VD) {
if (getActorIsolation(const_cast<ValueDecl *>(VD)).isActorIsolated())
return {Unsupported, UnrepresentableIsolatedInActor};
Optional<CanGenericSignature> genericSignature;
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(VD)) {
if (AFD->hasAsync())
return {Unsupported, UnrepresentableAsync};
// Don't expose @_alwaysEmitIntoClient functions as they require their
// bodies to be emitted into client.
if (AFD->getAttrs().hasAttribute<AlwaysEmitIntoClientAttr>())
return {Unsupported, UnrepresentableRequiresClientEmission};
if (AFD->isGeneric())
genericSignature = AFD->getGenericSignature().getCanonicalSignature();
}
if (const auto *typeDecl = dyn_cast<NominalTypeDecl>(VD)) {
if (typeDecl->isGeneric()) {
if (isa<ClassDecl>(VD))
return {Unsupported, UnrepresentableGeneric};
genericSignature =
typeDecl->getGenericSignature().getCanonicalSignature();
}
}
// Generic requirements are not yet supported in C++.
if (genericSignature && !genericSignature->getRequirements().empty())
return {Unsupported, UnrepresentableGenericRequirements};
return {Representable, llvm::None};
}

Expand Down
3 changes: 1 addition & 2 deletions lib/PrintAsClang/PrintClangValueType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,7 @@ void ClangValueTypePrinter::printValueTypeDecl(
if (typeDecl->isGeneric()) {
genericSignature = typeDecl->getGenericSignature().getCanonicalSignature();
// FIXME: Support generic requirements.
if (!genericSignature->getRequirements().empty())
return;
assert(genericSignature->getRequirements().empty());
// FIXME: Can we make some better layout than opaque layout for generic
// types.
} else if (!typeDecl->isResilient()) {
Expand Down
8 changes: 8 additions & 0 deletions lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1996,6 +1996,14 @@ void AttributeChecker::visitExposeAttr(ExposeAttr *attr) {
diag::expose_unsupported_client_emission_to_cxx,
VD->getDescriptiveKind(), VD);
break;
case UnrepresentableGeneric:
diagnose(attr->getLocation(), diag::expose_generic_decl_to_cxx,
VD->getDescriptiveKind(), VD);
break;
case UnrepresentableGenericRequirements:
diagnose(attr->getLocation(), diag::expose_generic_requirement_to_cxx,
VD->getDescriptiveKind(), VD);
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend %s -typecheck -module-name Decls -verify -clang-header-expose-decls=has-expose-attr -disable-availability-checking -emit-clang-header-path %t/decls.h

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

// CHECK-NOT: unsupported
// CHECK: supported

public protocol Proto { init() }

@_expose(Cxx)
public func supportedFunc<T>(_ x: T) {}

@_expose(Cxx) // expected-error {{generic requirements for global function 'unsupportedFunc' can not yet be represented in C++}}
public func unsupportedFunc<T: Proto>(_ x: T) {}

@_expose(Cxx) // expected-error {{generic generic class 'unsupportedGenericClass' can not yet be exposed to C++}}
public class unsupportedGenericClass<T> {
var v: T?

public init() {
v = nil
}
}

@_expose(Cxx) // expected-error {{generic requirements for generic struct 'unsupportedGenericStruct' can not yet be represented in C++}}
public struct unsupportedGenericStruct<T: Proto> {
var v: T
}

@_expose(Cxx) // expected-error {{generic requirements for generic enum 'unsupportedGenericEnum' can not yet be represented in C++}}
public enum unsupportedGenericEnum<T: Proto> {
case A
case B(T)
}