Skip to content

Commit 74e87a9

Browse files
committed
[interop][SwiftToCxx] do not expose move-only Swift types
Swift's consume semantics are not yet supported in C++
1 parent 4010357 commit 74e87a9

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1696,6 +1696,8 @@ 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 *))
16971697
ERROR(expose_protocol_to_cxx_unsupported,none,
16981698
"protocol %0 can not yet be represented in C++", (ValueDecl *))
1699+
ERROR(expose_move_only_to_cxx,none,
1700+
"move-only %0 %1 can not yet be represented in C++", (DescriptiveDeclKind, ValueDecl *))
16991701
ERROR(unexposed_other_decl_in_cxx,none,
17001702
"%0 %1 is not yet exposed to C++", (DescriptiveDeclKind, ValueDecl *))
17011703
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
@@ -78,6 +78,7 @@ enum RepresentationError {
7878
UnrepresentableEnumCaseType,
7979
UnrepresentableEnumCaseTuple,
8080
UnrepresentableProtocol,
81+
UnrepresentableMoveOnly,
8182
};
8283

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

lib/AST/SwiftNameTranslation.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,9 @@ swift::cxx_translation::getDeclRepresentation(const ValueDecl *VD) {
226226
if (const auto *typeDecl = dyn_cast<NominalTypeDecl>(VD)) {
227227
if (isa<ProtocolDecl>(typeDecl))
228228
return {Unsupported, UnrepresentableProtocol};
229+
// Swift's consume semantics are not yet supported in C++.
230+
if (typeDecl->isMoveOnly())
231+
return {Unsupported, UnrepresentableMoveOnly};
229232
if (typeDecl->isGeneric()) {
230233
if (isa<ClassDecl>(VD))
231234
return {Unsupported, UnrepresentableGeneric};
@@ -323,5 +326,8 @@ swift::cxx_translation::diagnoseRepresenationError(RepresentationError error,
323326
return Diagnostic(diag::expose_enum_case_tuple_to_cxx, vd);
324327
case UnrepresentableProtocol:
325328
return Diagnostic(diag::expose_protocol_to_cxx_unsupported, vd);
329+
case UnrepresentableMoveOnly:
330+
return Diagnostic(diag::expose_move_only_to_cxx, vd->getDescriptiveKind(),
331+
vd);
326332
}
327333
}

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,27 @@ public func takesVoid(_ x: ()) {}
1212

1313
// CHECK: takeFloat
1414

15-
protocol TestProtocol {}
15+
@_moveOnly
16+
public enum MoveOnlyEnum {
17+
case a
18+
}
19+
20+
// CHECK: class MoveOnlyEnum { } SWIFT_UNAVAILABLE_MSG("move-only enum 'MoveOnlyEnum' can not yet be represented in C++");
21+
22+
@_moveOnly
23+
public struct MoveOnlyStruct {
24+
let x: Int
25+
}
26+
27+
// CHECK: class MoveOnlyStruct { } SWIFT_UNAVAILABLE_MSG("move-only struct 'MoveOnlyStruct' can not yet be represented in C++");
28+
29+
public protocol TestProtocol {}
1630

1731
// CHECK: class TestProtocol { } SWIFT_UNAVAILABLE_MSG("protocol 'TestProtocol' can not yet be represented in C++");
1832

1933
// CHECK: // Unavailable in C++: Swift global function 'takesTuple(_:)'.
2034
// CHECK: // Unavailable in C++: Swift global function 'takesVoid(_:)'.
2135

22-
typealias unsupportedTypeAlias = () -> (Float, Float)
36+
public typealias unsupportedTypeAlias = () -> (Float, Float)
2337

2438
// CHECK: // Unavailable in C++: Swift type alias 'unsupportedTypeAlias'

0 commit comments

Comments
 (0)