@@ -1362,8 +1362,10 @@ static ProtocolConformanceRef getBuiltinTupleTypeConformance(
1362
1362
return ProtocolConformanceRef (specialized);
1363
1363
}
1364
1364
1365
- // Tuple type are Sendable when all of their element types are Sendable.
1366
- if (protocol->isSpecificProtocol (KnownProtocolKind::Sendable)) {
1365
+ // / For some known protocols like Sendable and Copyable, a tuple type
1366
+ // / conforms to the protocol KP when all of their element types conform to KP.
1367
+ if (protocol->isSpecificProtocol (KnownProtocolKind::Sendable) ||
1368
+ protocol->isSpecificProtocol (KnownProtocolKind::Copyable)) {
1367
1369
1368
1370
// Create the pieces for a generic tuple type (T1, T2, ... TN) and a
1369
1371
// generic signature <T1, T2, ..., TN>.
@@ -1389,9 +1391,9 @@ static ProtocolConformanceRef getBuiltinTupleTypeConformance(
1389
1391
BuiltinConformanceKind::Synthesized));
1390
1392
}
1391
1393
1392
- // Form a generic conformance of (T1, T2, ..., TN): Sendable with signature
1393
- // <T1, T2, ..., TN> and conditional requirements T1: Sendable ,
1394
- // T2: Sendable , ..., TN: Sendable .
1394
+ // Form a generic conformance of (T1, T2, ..., TN): KP with signature
1395
+ // <T1, T2, ..., TN> and conditional requirements T1: KP ,
1396
+ // T2: P , ..., TN: KP .
1395
1397
auto genericTupleType = TupleType::get (genericElements, ctx);
1396
1398
auto genericSig = GenericSignature::get (
1397
1399
genericParams, conditionalRequirements);
@@ -1440,24 +1442,33 @@ static bool isSendableFunctionType(const FunctionType *functionType) {
1440
1442
// / appropriate.
1441
1443
static ProtocolConformanceRef getBuiltinFunctionTypeConformance (
1442
1444
Type type, const FunctionType *functionType, ProtocolDecl *protocol) {
1445
+ ASTContext &ctx = protocol->getASTContext ();
1443
1446
// @Sendable function types are Sendable.
1444
1447
if (protocol->isSpecificProtocol (KnownProtocolKind::Sendable) &&
1445
1448
isSendableFunctionType (functionType)) {
1446
- ASTContext &ctx = protocol->getASTContext ();
1447
1449
return ProtocolConformanceRef (
1448
1450
ctx.getBuiltinConformance (type, protocol, GenericSignature (), { },
1449
1451
BuiltinConformanceKind::Synthesized));
1450
1452
}
1451
1453
1454
+ // Functions cannot destroy move-only vars/lets that they capture, so it's
1455
+ // safe to copy functions, as if they're classes.
1456
+ if (protocol->isSpecificProtocol (KnownProtocolKind::Copyable)) {
1457
+ return ProtocolConformanceRef (
1458
+ ctx.getBuiltinConformance (type, protocol, GenericSignature (), {},
1459
+ BuiltinConformanceKind::Synthesized));
1460
+ }
1461
+
1452
1462
return ProtocolConformanceRef::forMissingOrInvalid (type, protocol);
1453
1463
}
1454
1464
1455
1465
// / Synthesize a builtin metatype type conformance to the given protocol, if
1456
1466
// / appropriate.
1457
1467
static ProtocolConformanceRef getBuiltinMetaTypeTypeConformance (
1458
1468
Type type, const AnyMetatypeType *metatypeType, ProtocolDecl *protocol) {
1459
- // All metatypes are Sendable.
1460
- if (protocol->isSpecificProtocol (KnownProtocolKind::Sendable)) {
1469
+ // All metatypes are Sendable and Copyable
1470
+ if (protocol->isSpecificProtocol (KnownProtocolKind::Sendable) ||
1471
+ protocol->isSpecificProtocol (KnownProtocolKind::Copyable)) {
1461
1472
ASTContext &ctx = protocol->getASTContext ();
1462
1473
return ProtocolConformanceRef (
1463
1474
ctx.getBuiltinConformance (type, protocol, GenericSignature (), { },
@@ -1471,8 +1482,9 @@ static ProtocolConformanceRef getBuiltinMetaTypeTypeConformance(
1471
1482
// / appropriate.
1472
1483
static ProtocolConformanceRef getBuiltinBuiltinTypeConformance (
1473
1484
Type type, const BuiltinType *builtinType, ProtocolDecl *protocol) {
1474
- // All builtin are Sendable.
1475
- if (protocol->isSpecificProtocol (KnownProtocolKind::Sendable)) {
1485
+ // All builtin are Sendable and Copyable
1486
+ if (protocol->isSpecificProtocol (KnownProtocolKind::Sendable) ||
1487
+ protocol->isSpecificProtocol (KnownProtocolKind::Copyable)) {
1476
1488
ASTContext &ctx = protocol->getASTContext ();
1477
1489
return ProtocolConformanceRef (
1478
1490
ctx.getBuiltinConformance (type, protocol, GenericSignature (), { },
0 commit comments