Skip to content

Commit bf35e3f

Browse files
committed
[interop][SwiftToCxx] provide a clear unavailable message for protocol decls in C++
1 parent aff3568 commit bf35e3f

File tree

5 files changed

+13
-0
lines changed

5 files changed

+13
-0
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1694,6 +1694,8 @@ ERROR(expose_enum_case_type_to_cxx,none,
16941694
"enum %0 can not be represented in C++ as one of its cases has an associated value with type that can't be represented in C++", (ValueDecl *))
16951695
ERROR(expose_enum_case_tuple_to_cxx,none,
16961696
"enum %0 can not yet be represented in C++ as one of its cases has multiple associated values", (ValueDecl *))
1697+
ERROR(expose_protocol_to_cxx_unsupported,none,
1698+
"protocol %0 can not yet be represented in C++", (ValueDecl *))
16971699
ERROR(unexposed_other_decl_in_cxx,none,
16981700
"%0 %1 is not yet exposed to C++", (DescriptiveDeclKind, ValueDecl *))
16991701
ERROR(unsupported_other_decl_in_cxx,none,

include/swift/AST/SwiftNameTranslation.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ enum RepresentationError {
7777
UnrepresentableIndirectEnum,
7878
UnrepresentableEnumCaseType,
7979
UnrepresentableEnumCaseTuple,
80+
UnrepresentableProtocol,
8081
};
8182

8283
/// Constructs a diagnostic that describes the given C++ representation error.

lib/AST/SwiftNameTranslation.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ swift::cxx_translation::getDeclRepresentation(const ValueDecl *VD) {
221221
genericSignature = AFD->getGenericSignature().getCanonicalSignature();
222222
}
223223
if (const auto *typeDecl = dyn_cast<NominalTypeDecl>(VD)) {
224+
if (isa<ProtocolDecl>(typeDecl))
225+
return {Unsupported, UnrepresentableProtocol};
224226
if (typeDecl->isGeneric()) {
225227
if (isa<ClassDecl>(VD))
226228
return {Unsupported, UnrepresentableGeneric};
@@ -316,5 +318,7 @@ swift::cxx_translation::diagnoseRepresenationError(RepresentationError error,
316318
return Diagnostic(diag::expose_enum_case_type_to_cxx, vd);
317319
case UnrepresentableEnumCaseTuple:
318320
return Diagnostic(diag::expose_enum_case_tuple_to_cxx, vd);
321+
case UnrepresentableProtocol:
322+
return Diagnostic(diag::expose_protocol_to_cxx_unsupported, vd);
319323
}
320324
}

test/Interop/SwiftToCxx/stdlib/swift-stdlib-in-cxx.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
// CHECK-NEXT: private:
119119

120120
// CHECK: class AnyKeyPath { } SWIFT_UNAVAILABLE_MSG("class 'AnyKeyPath' is not yet exposed to C++");
121+
// CHECK: class Comparable { } SWIFT_UNAVAILABLE_MSG("protocol 'Comparable' can not yet be represented in C++");
121122
// CHECK: class FloatingPointSign { } SWIFT_UNAVAILABLE_MSG("enum 'FloatingPointSign' is not yet exposed to C++");
122123
// CHECK: // Unavailable in C++: Swift global function 'abs(_:)'.
123124

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ public func takesTuple(_ x: (Float, Float)) {}
1111
public func takesVoid(_ x: ()) {}
1212

1313
// CHECK: takeFloat
14+
15+
protocol TestProtocol {}
16+
17+
// CHECK: class TestProtocol { } SWIFT_UNAVAILABLE_MSG("protocol 'TestProtocol' can not yet be represented in C++");
18+
1419
// CHECK: // Unavailable in C++: Swift global function 'takesTuple(_:)'.
1520
// CHECK: // Unavailable in C++: Swift global function 'takesVoid(_:)'.
1621

0 commit comments

Comments
 (0)