Skip to content

Commit 1ec8c43

Browse files
committed
[sending] Begin parsing 'sending' while still accepting 'transferring'.
A few things: 1. Internally except for in the parser and the clang importer, we only represent 'sending'. This means that it will be easy to remove 'transferring' once enough time has passed. 2. I included a warning that suggested to the user to change 'transferring' -> 'sending'. 3. I duplicated the parsing diagnostics for 'sending' so both will still get different sets of diagnostics for parsing issues... but anywhere below parsing, I have just changed 'transferring' to 'sending' since transferring isn't represented at those lower levels. 4. Since SendingArgsAndResults is always enabled when TransferringArgsAndResults is enabled (NOTE not vis-a-versa), we know that we can always parse sending. So we import "transferring" as "sending". This means that even if one marks a function with "transferring", the compiler will guard it behind a SendingArgsAndResults -D flag and in the imported header print out sending. rdar://128216574 (cherry picked from commit b780ff6)
1 parent 5678526 commit 1ec8c43

File tree

58 files changed

+947
-237
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+947
-237
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,6 +1552,7 @@ enum ENUM_EXTENSIBILITY_ATTR(open) BridgedAttributedTypeSpecifier : size_t {
15521552
BridgedAttributedTypeSpecifierIsolated,
15531553
BridgedAttributedTypeSpecifierResultDependsOn,
15541554
BridgedAttributedTypeSpecifierTransferring,
1555+
BridgedAttributedTypeSpecifierSending,
15551556
};
15561557

15571558
SWIFT_NAME("BridgedUnqualifiedIdentTypeRepr.createParsed(_:loc:name:)")

include/swift/AST/Builtins.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,7 @@ BUILTIN_MISC_OPERATION_WITH_SILGEN(GetCurrentAsyncTask, "getCurrentAsyncTask", "
884884
BUILTIN_MISC_OPERATION_WITH_SILGEN(CancelAsyncTask, "cancelAsyncTask", "", Special)
885885

886886
/// startAsyncLet()<T>: (
887-
/// __owned @escaping () async throws -> transferring T
887+
/// __owned @escaping () async throws -> sending T
888888
/// ) -> Builtin.RawPointer
889889
///
890890
/// DEPRECATED. startAsyncLetWithLocalBuffer is used instead.
@@ -894,7 +894,7 @@ BUILTIN_MISC_OPERATION_WITH_SILGEN(CancelAsyncTask, "cancelAsyncTask", "", Speci
894894
BUILTIN_MISC_OPERATION(StartAsyncLet, "startAsyncLet", "", Special)
895895

896896
/// startAsyncLetWithLocalBuffer()<T>: (
897-
/// __owned @escaping () async throws -> transferring T,
897+
/// __owned @escaping () async throws -> sending T,
898898
/// _ resultBuf: Builtin.RawPointer
899899
/// ) -> Builtin.RawPointer
900900
///

include/swift/AST/DiagnosticsParse.def

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2207,6 +2207,16 @@ ERROR(transferring_after_parameter_specifier,none,
22072207
"'transferring' must be placed before specifier '%0'", (StringRef))
22082208
ERROR(transferring_repeated,none,
22092209
"parameter may have at most one 'transferring' specifier", ())
2210+
ERROR(sending_before_parameter_specifier,none,
2211+
"'sending' must be placed after specifier '%0'", (StringRef))
2212+
ERROR(sending_repeated,none,
2213+
"parameter may have at most one 'sending' specifier", ())
2214+
ERROR(sending_and_transferring_used_together,none,
2215+
"'transferring' and 'sending' may not be used together", ())
2216+
WARNING(transferring_is_now_sendable,none,
2217+
"'transferring' has been renamed to 'sending' and the 'transferring' spelling will be removed shortly",
2218+
())
2219+
22102220

22112221
#define UNDEFINE_DIAGNOSTIC_MACROS
22122222
#include "DefineDiagnosticMacros.h"

include/swift/AST/DiagnosticsSema.def

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7933,17 +7933,23 @@ ERROR(lifetime_dependence_cannot_infer_ambiguous_candidate, none,
79337933
"Escapable",
79347934
())
79357935

7936-
//===----------------------------------------------------------------------===//
7937-
// MARK: Transferring
7938-
//===----------------------------------------------------------------------===//
7939-
7940-
ERROR(transferring_unsupported_param_specifier, none,
7941-
"'%0' cannot be applied to a 'transferring' parameter", (StringRef))
7936+
//===----------------------------------------------------------------------===//
7937+
// MARK: Transferring
7938+
//===----------------------------------------------------------------------===//
79427939

7943-
ERROR(transferring_only_on_parameters_and_results, none,
7944-
"'transferring' may only be used on parameters and results", ())
7945-
ERROR(transferring_cannot_be_applied_to_tuple_elt, none,
7946-
"'transferring' cannot be applied to tuple elements", ())
7940+
ERROR(transferring_unsupported_param_specifier, none,
7941+
"'%0' cannot be applied to a 'transferring' parameter", (StringRef))
7942+
7943+
ERROR(transferring_only_on_parameters_and_results, none,
7944+
"'transferring' may only be used on parameters and results", ())
7945+
ERROR(transferring_cannot_be_applied_to_tuple_elt, none,
7946+
"'transferring' cannot be applied to tuple elements", ())
7947+
ERROR(sending_unsupported_param_specifier, none,
7948+
"'%0' cannot be applied to a 'sending' parameter", (StringRef))
7949+
ERROR(sending_only_on_parameters_and_results, none,
7950+
"'sending' may only be used on parameters and results", ())
7951+
ERROR(sending_cannot_be_applied_to_tuple_elt, none,
7952+
"'sending' cannot be applied to tuple elements", ())
79477953

79487954
#define UNDEFINE_DIAGNOSTIC_MACROS
79497955
#include "DefineDiagnosticMacros.h"

include/swift/AST/PrintOptions.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,8 @@ struct PrintOptions {
382382
/// Suppress 'isolated' and '#isolation' on isolated parameters with optional type.
383383
bool SuppressOptionalIsolatedParams = false;
384384

385-
/// Suppress 'transferring' on arguments and results.
386-
bool SuppressTransferringArgsAndResults = false;
385+
/// Suppress 'sending' on arguments and results.
386+
bool SuppressSendingArgsAndResults = false;
387387

388388
/// Suppress Noncopyable generics.
389389
bool SuppressNoncopyableGenerics = false;

include/swift/AST/TypeRepr.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1116,7 +1116,8 @@ class SpecifierTypeRepr : public TypeRepr {
11161116
T->getKind() == TypeReprKind::CompileTimeConst ||
11171117
T->getKind() == TypeReprKind::ResultDependsOn ||
11181118
T->getKind() == TypeReprKind::LifetimeDependentReturn ||
1119-
T->getKind() == TypeReprKind::Transferring;
1119+
T->getKind() == TypeReprKind::Transferring ||
1120+
T->getKind() == TypeReprKind::Sending;
11201121
}
11211122
static bool classof(const SpecifierTypeRepr *T) { return true; }
11221123

@@ -1217,6 +1218,21 @@ class TransferringTypeRepr : public SpecifierTypeRepr {
12171218
static bool classof(const TransferringTypeRepr *T) { return true; }
12181219
};
12191220

1221+
/// A sending type.
1222+
/// \code
1223+
/// x : sending Int
1224+
/// \endcode
1225+
class SendingTypeRepr : public SpecifierTypeRepr {
1226+
public:
1227+
SendingTypeRepr(TypeRepr *Base, SourceLoc Loc)
1228+
: SpecifierTypeRepr(TypeReprKind::Sending, Base, Loc) {}
1229+
1230+
static bool classof(const TypeRepr *T) {
1231+
return T->getKind() == TypeReprKind::Sending;
1232+
}
1233+
static bool classof(const SendingTypeRepr *T) { return true; }
1234+
};
1235+
12201236
/// A TypeRepr for a known, fixed type.
12211237
///
12221238
/// Fixed type representations should be used sparingly, in places
@@ -1618,6 +1634,7 @@ inline bool TypeRepr::isSimple() const {
16181634
case TypeReprKind::SILBox:
16191635
case TypeReprKind::Isolated:
16201636
case TypeReprKind::Transferring:
1637+
case TypeReprKind::Sending:
16211638
case TypeReprKind::Placeholder:
16221639
case TypeReprKind::CompileTimeConst:
16231640
case TypeReprKind::ResultDependsOn:

include/swift/AST/TypeReprNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ ABSTRACT_TYPEREPR(Specifier, TypeRepr)
7272
SPECIFIER_TYPEREPR(CompileTimeConst, SpecifierTypeRepr)
7373
SPECIFIER_TYPEREPR(ResultDependsOn, SpecifierTypeRepr)
7474
SPECIFIER_TYPEREPR(Transferring, SpecifierTypeRepr)
75+
SPECIFIER_TYPEREPR(Sending, SpecifierTypeRepr)
7576
TYPEREPR(Fixed, TypeRepr)
7677
TYPEREPR(SILBox, TypeRepr)
7778
TYPEREPR(Self, TypeRepr)

include/swift/Basic/Features.def

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,14 @@ EXPERIMENTAL_FEATURE(FixedArrays, true)
350350
EXPERIMENTAL_FEATURE(GroupActorErrors, true)
351351

352352
// Allow for the 'transferring' keyword to be applied to arguments and results.
353-
SUPPRESSIBLE_EXPERIMENTAL_FEATURE(TransferringArgsAndResults, true)
353+
//
354+
// Enables SendingArgsAndResults as well. After parsing, we just represent this
355+
// as 'sendable' implying that since both are always enabled together, this
356+
// doesn't need to be suppressed.
357+
EXPERIMENTAL_FEATURE(TransferringArgsAndResults, true)
358+
359+
// Allow for the 'sending' keyword to be applied to arguments and results.
360+
SUPPRESSIBLE_EXPERIMENTAL_FEATURE(SendingArgsAndResults, true)
354361

355362
// Enable explicit isolation of closures.
356363
EXPERIMENTAL_FEATURE(ClosureIsolation, true)

include/swift/Parse/Parser.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,6 +1206,9 @@ class Parser {
12061206
if (Context.LangOpts.hasFeature(Feature::TransferringArgsAndResults) &&
12071207
Tok.isContextualKeyword("transferring"))
12081208
return true;
1209+
if (Context.LangOpts.hasFeature(Feature::SendingArgsAndResults) &&
1210+
Tok.isContextualKeyword("sending"))
1211+
return true;
12091212
if (Context.LangOpts.hasFeature(Feature::NonescapableTypes) &&
12101213
(Tok.isContextualKeyword("_resultDependsOn") ||
12111214
isLifetimeDependenceToken()))
@@ -1256,6 +1259,7 @@ class Parser {
12561259
SourceLoc ConstLoc;
12571260
SourceLoc ResultDependsOnLoc;
12581261
SourceLoc TransferringLoc;
1262+
SourceLoc SendingLoc;
12591263
SmallVector<TypeOrCustomAttr> Attributes;
12601264
SmallVector<LifetimeDependenceSpecifier> lifetimeDependenceSpecifiers;
12611265

@@ -1573,6 +1577,9 @@ class Parser {
15731577
/// The location of the 'transferring' keyword if present.
15741578
SourceLoc TransferringLoc;
15751579

1580+
/// The location of the 'sending' keyword if present.
1581+
SourceLoc SendingLoc;
1582+
15761583
/// The type following the ':'.
15771584
TypeRepr *Type = nullptr;
15781585

lib/AST/ASTBridging.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,8 @@ BridgedParamDecl BridgedParamDecl_createParsed(
890890
paramDecl->setCompileTimeConst(true);
891891
else if (isa<TransferringTypeRepr>(STR))
892892
paramDecl->setSending(true);
893+
else if (isa<SendingTypeRepr>(STR))
894+
paramDecl->setSending(true);
893895

894896
unwrappedType = STR->getBase();
895897
continue;
@@ -2240,6 +2242,9 @@ BridgedSpecifierTypeRepr BridgedSpecifierTypeRepr_createParsed(
22402242
case BridgedAttributedTypeSpecifierTransferring: {
22412243
return new (context) TransferringTypeRepr(baseType, loc);
22422244
}
2245+
case BridgedAttributedTypeSpecifierSending: {
2246+
return new (context) SendingTypeRepr(baseType, loc);
2247+
}
22432248
case BridgedAttributedTypeSpecifierConst: {
22442249
return new (context) CompileTimeConstTypeRepr(baseType, loc);
22452250
}

lib/AST/ASTDumper.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3445,6 +3445,12 @@ class PrintTypeRepr : public TypeReprVisitor<PrintTypeRepr, void, StringRef>,
34453445
printFoot();
34463446
}
34473447

3448+
void visitSendingTypeRepr(SendingTypeRepr *T, StringRef label) {
3449+
printCommon("sending", label);
3450+
printRec(T->getBase());
3451+
printFoot();
3452+
}
3453+
34483454
void visitCompileTimeConstTypeRepr(CompileTimeConstTypeRepr *T, StringRef label) {
34493455
printCommon("_const", label);
34503456
printRec(T->getBase());

lib/AST/ASTPrinter.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3123,10 +3123,10 @@ static void suppressingFeatureOptionalIsolatedParameters(
31233123
action();
31243124
}
31253125

3126-
static void suppressingFeatureTransferringArgsAndResults(
3127-
PrintOptions &options, llvm::function_ref<void()> action) {
3128-
llvm::SaveAndRestore<bool> scope(options.SuppressTransferringArgsAndResults,
3129-
true);
3126+
static void
3127+
suppressingFeatureSendingArgsAndResults(PrintOptions &options,
3128+
llvm::function_ref<void()> action) {
3129+
llvm::SaveAndRestore<bool> scope(options.SuppressSendingArgsAndResults, true);
31303130
action();
31313131
}
31323132

@@ -3728,8 +3728,6 @@ static void printParameterFlags(ASTPrinter &printer,
37283728
if (!options.excludeAttrKind(TypeAttrKind::NoDerivative) &&
37293729
flags.isNoDerivative())
37303730
printer.printAttrName("@noDerivative ");
3731-
if (!options.SuppressTransferringArgsAndResults && flags.isSending())
3732-
printer.printAttrName("transferring ");
37333731

37343732
switch (flags.getOwnershipSpecifier()) {
37353733
case ParamSpecifier::Default:
@@ -3755,11 +3753,14 @@ static void printParameterFlags(ASTPrinter &printer,
37553753
printer.printKeyword("__owned", options, " ");
37563754
break;
37573755
case ParamSpecifier::ImplicitlyCopyableConsuming:
3758-
// Nothing... we infer from transferring.
3759-
assert(flags.isSending() && "Only valid when transferring is enabled");
3756+
// Nothing... we infer from sending.
3757+
assert(flags.isSending() && "Only valid when sending is enabled");
37603758
break;
37613759
}
3762-
3760+
3761+
if (!options.SuppressSendingArgsAndResults && flags.isSending())
3762+
printer.printAttrName("sending ");
3763+
37633764
if (flags.isIsolated()) {
37643765
if (!(param && param->getInterfaceType()->isOptional() &&
37653766
options.SuppressOptionalIsolatedParams))
@@ -4189,13 +4190,13 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
41894190
}
41904191
}
41914192

4192-
if (!Options.SuppressTransferringArgsAndResults) {
4193+
if (!Options.SuppressSendingArgsAndResults) {
41934194
if (decl->hasSendingResult()) {
4194-
Printer << "transferring ";
4195+
Printer << "sending ";
41954196
} else if (auto *ft = llvm::dyn_cast_if_present<AnyFunctionType>(
41964197
decl->getInterfaceType())) {
41974198
if (ft->hasExtInfo() && ft->hasSendingResult()) {
4198-
Printer << "transferring ";
4199+
Printer << "sending ";
41994200
}
42004201
}
42014202
}
@@ -6658,9 +6659,9 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
66586659

66596660
Printer << " -> ";
66606661

6661-
if (!Options.SuppressTransferringArgsAndResults && T->hasExtInfo() &&
6662+
if (!Options.SuppressSendingArgsAndResults && T->hasExtInfo() &&
66626663
T->hasSendingResult()) {
6663-
Printer.printKeyword("transferring ", Options);
6664+
Printer.printKeyword("sending ", Options);
66646665
}
66656666

66666667
if (T->hasLifetimeDependenceInfo()) {

lib/AST/ASTWalker.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2340,6 +2340,10 @@ bool Traversal::visitTransferringTypeRepr(TransferringTypeRepr *T) {
23402340
return doIt(T->getBase());
23412341
}
23422342

2343+
bool Traversal::visitSendingTypeRepr(SendingTypeRepr *T) {
2344+
return doIt(T->getBase());
2345+
}
2346+
23432347
bool Traversal::visitCompileTimeConstTypeRepr(CompileTimeConstTypeRepr *T) {
23442348
return doIt(T->getBase());
23452349
}

lib/AST/Decl.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10156,7 +10156,8 @@ FuncDecl *FuncDecl::create(ASTContext &Context, SourceLoc StaticLoc,
1015610156
ClangNode());
1015710157
FD->setParameters(BodyParams);
1015810158
FD->FnRetType = TypeLoc(ResultTyR);
10159-
if (llvm::isa_and_nonnull<TransferringTypeRepr>(ResultTyR))
10159+
if (llvm::isa_and_nonnull<TransferringTypeRepr>(ResultTyR) ||
10160+
llvm::isa_and_nonnull<SendingTypeRepr>(ResultTyR))
1016010161
FD->setSendingResult();
1016110162
return FD;
1016210163
}

lib/AST/FeatureSet.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -639,8 +639,9 @@ UNINTERESTING_FEATURE(BitwiseCopyable)
639639
UNINTERESTING_FEATURE(FixedArrays)
640640
UNINTERESTING_FEATURE(GroupActorErrors)
641641

642-
static bool usesFeatureTransferringArgsAndResults(Decl *decl) {
643-
auto functionTypeUsesTransferring = [](Decl *decl) {
642+
UNINTERESTING_FEATURE(TransferringArgsAndResults)
643+
static bool usesFeatureSendingArgsAndResults(Decl *decl) {
644+
auto functionTypeUsesSending = [](Decl *decl) {
644645
return usesTypeMatching(decl, [](Type type) {
645646
auto fnType = type->getAs<AnyFunctionType>();
646647
if (!fnType)
@@ -661,17 +662,17 @@ static bool usesFeatureTransferringArgsAndResults(Decl *decl) {
661662
return true;
662663
}
663664

664-
if (functionTypeUsesTransferring(pd))
665+
if (functionTypeUsesSending(pd))
665666
return true;
666667
}
667668

668669
if (auto *fDecl = dyn_cast<FuncDecl>(decl)) {
669670
// First check for param decl results.
670671
if (llvm::any_of(fDecl->getParameters()->getArray(), [](ParamDecl *pd) {
671-
return usesFeatureTransferringArgsAndResults(pd);
672+
return usesFeatureSendingArgsAndResults(pd);
672673
}))
673674
return true;
674-
if (functionTypeUsesTransferring(decl))
675+
if (functionTypeUsesSending(decl))
675676
return true;
676677
}
677678

lib/AST/NameLookup.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3114,6 +3114,7 @@ directReferencesForTypeRepr(Evaluator &evaluator,
31143114
case TypeReprKind::ResultDependsOn:
31153115
case TypeReprKind::LifetimeDependentReturn:
31163116
case TypeReprKind::Transferring:
3117+
case TypeReprKind::Sending:
31173118
return result;
31183119

31193120
case TypeReprKind::Fixed:

lib/AST/TypeRepr.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,9 @@ void SpecifierTypeRepr::printImpl(ASTPrinter &Printer,
850850
case TypeReprKind::Transferring:
851851
Printer.printKeyword("transferring", Opts, " ");
852852
break;
853+
case TypeReprKind::Sending:
854+
Printer.printKeyword("sending", Opts, " ");
855+
break;
853856
case TypeReprKind::CompileTimeConst:
854857
Printer.printKeyword("_const", Opts, " ");
855858
break;

lib/ClangImporter/ClangImporter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,8 @@ importer::getNormalInvocationArguments(
676676
// is supported.
677677
if (LangOpts.hasFeature(Feature::TransferringArgsAndResults))
678678
invocationArgStrs.push_back("-D__SWIFT_ATTR_SUPPORTS_TRANSFERRING=1");
679+
if (LangOpts.hasFeature(Feature::SendingArgsAndResults))
680+
invocationArgStrs.push_back("-D__SWIFT_ATTR_SUPPORTS_SENDING=1");
679681

680682
if (searchPathOpts.getSDKPath().empty()) {
681683
invocationArgStrs.push_back("-Xclang");

lib/ClangImporter/ImportDecl.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8054,6 +8054,17 @@ ClangImporter::Implementation::importSwiftAttrAttributes(Decl *MappedDecl) {
80548054
continue;
80558055
}
80568056

8057+
if (swiftAttr->getAttribute() == "sending") {
8058+
// Swallow this if the feature is not enabled.
8059+
if (!SwiftContext.LangOpts.hasFeature(Feature::SendingArgsAndResults))
8060+
continue;
8061+
auto *funcDecl = dyn_cast<FuncDecl>(MappedDecl);
8062+
if (!funcDecl)
8063+
continue;
8064+
funcDecl->setSendingResult();
8065+
continue;
8066+
}
8067+
80578068
// Dig out a buffer with the attribute text.
80588069
unsigned bufferID = getClangSwiftAttrSourceBuffer(
80598070
swiftAttr->getAttribute());

lib/ClangImporter/ImportType.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2602,6 +2602,17 @@ static ParamDecl *getParameterInfo(ClangImporter::Implementation *impl,
26022602
}
26032603
}
26042604

2605+
// If TransferringArgsAndResults are enabled and we have a transferring
2606+
// argument, set that the param was transferring.
2607+
if (paramInfo->getASTContext().LangOpts.hasFeature(
2608+
Feature::SendingArgsAndResults)) {
2609+
if (auto *attr = param->getAttr<clang::SwiftAttrAttr>()) {
2610+
if (attr->getAttribute() == "sending") {
2611+
paramInfo->setSending();
2612+
}
2613+
}
2614+
}
2615+
26052616
// Foreign references are already references so they don't need to be passed
26062617
// as inout.
26072618
paramInfo->setSpecifier(isInOut && !swiftParamTy->isForeignReferenceType()

0 commit comments

Comments
 (0)