Skip to content

Commit b780ff6

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
1 parent 71e95b9 commit b780ff6

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
@@ -868,7 +868,7 @@ BUILTIN_MISC_OPERATION_WITH_SILGEN(GetCurrentAsyncTask, "getCurrentAsyncTask", "
868868
BUILTIN_MISC_OPERATION_WITH_SILGEN(CancelAsyncTask, "cancelAsyncTask", "", Special)
869869

870870
/// startAsyncLet()<T>: (
871-
/// __owned @escaping () async throws -> transferring T
871+
/// __owned @escaping () async throws -> sending T
872872
/// ) -> Builtin.RawPointer
873873
///
874874
/// DEPRECATED. startAsyncLetWithLocalBuffer is used instead.
@@ -878,7 +878,7 @@ BUILTIN_MISC_OPERATION_WITH_SILGEN(CancelAsyncTask, "cancelAsyncTask", "", Speci
878878
BUILTIN_MISC_OPERATION(StartAsyncLet, "startAsyncLet", "", Special)
879879

880880
/// startAsyncLetWithLocalBuffer()<T>: (
881-
/// __owned @escaping () async throws -> transferring T,
881+
/// __owned @escaping () async throws -> sending T,
882882
/// _ resultBuf: Builtin.RawPointer
883883
/// ) -> Builtin.RawPointer
884884
///

include/swift/AST/DiagnosticsParse.def

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2200,6 +2200,16 @@ ERROR(transferring_after_parameter_specifier,none,
22002200
"'transferring' must be placed before specifier '%0'", (StringRef))
22012201
ERROR(transferring_repeated,none,
22022202
"parameter may have at most one 'transferring' specifier", ())
2203+
ERROR(sending_before_parameter_specifier,none,
2204+
"'sending' must be placed after specifier '%0'", (StringRef))
2205+
ERROR(sending_repeated,none,
2206+
"parameter may have at most one 'sending' specifier", ())
2207+
ERROR(sending_and_transferring_used_together,none,
2208+
"'transferring' and 'sending' may not be used together", ())
2209+
WARNING(transferring_is_now_sendable,none,
2210+
"'transferring' has been renamed to 'sending' and the 'transferring' spelling will be removed shortly",
2211+
())
2212+
22032213

22042214
#define UNDEFINE_DIAGNOSTIC_MACROS
22052215
#include "DefineDiagnosticMacros.h"

include/swift/AST/DiagnosticsSema.def

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7972,17 +7972,23 @@ ERROR(lifetime_dependence_cannot_infer_ambiguous_candidate, none,
79727972
"Escapable",
79737973
())
79747974

7975-
//===----------------------------------------------------------------------===//
7976-
// MARK: Transferring
7977-
//===----------------------------------------------------------------------===//
7978-
7979-
ERROR(transferring_unsupported_param_specifier, none,
7980-
"'%0' cannot be applied to a 'transferring' parameter", (StringRef))
7975+
//===----------------------------------------------------------------------===//
7976+
// MARK: Transferring
7977+
//===----------------------------------------------------------------------===//
79817978

7982-
ERROR(transferring_only_on_parameters_and_results, none,
7983-
"'transferring' may only be used on parameters and results", ())
7984-
ERROR(transferring_cannot_be_applied_to_tuple_elt, none,
7985-
"'transferring' cannot be applied to tuple elements", ())
7979+
ERROR(transferring_unsupported_param_specifier, none,
7980+
"'%0' cannot be applied to a 'transferring' parameter", (StringRef))
7981+
7982+
ERROR(transferring_only_on_parameters_and_results, none,
7983+
"'transferring' may only be used on parameters and results", ())
7984+
ERROR(transferring_cannot_be_applied_to_tuple_elt, none,
7985+
"'transferring' cannot be applied to tuple elements", ())
7986+
ERROR(sending_unsupported_param_specifier, none,
7987+
"'%0' cannot be applied to a 'sending' parameter", (StringRef))
7988+
ERROR(sending_only_on_parameters_and_results, none,
7989+
"'sending' may only be used on parameters and results", ())
7990+
ERROR(sending_cannot_be_applied_to_tuple_elt, none,
7991+
"'sending' cannot be applied to tuple elements", ())
79867992

79877993
#define UNDEFINE_DIAGNOSTIC_MACROS
79887994
#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
@@ -353,7 +353,14 @@ EXPERIMENTAL_FEATURE(FixedArrays, true)
353353
EXPERIMENTAL_FEATURE(GroupActorErrors, true)
354354

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

358365
// Enable explicit isolation of closures.
359366
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
@@ -878,6 +878,8 @@ BridgedParamDecl BridgedParamDecl_createParsed(
878878
paramDecl->setCompileTimeConst(true);
879879
else if (isa<TransferringTypeRepr>(STR))
880880
paramDecl->setSending(true);
881+
else if (isa<SendingTypeRepr>(STR))
882+
paramDecl->setSending(true);
881883

882884
unwrappedType = STR->getBase();
883885
continue;
@@ -2225,6 +2227,9 @@ BridgedSpecifierTypeRepr BridgedSpecifierTypeRepr_createParsed(
22252227
case BridgedAttributedTypeSpecifierTransferring: {
22262228
return new (context) TransferringTypeRepr(baseType, loc);
22272229
}
2230+
case BridgedAttributedTypeSpecifierSending: {
2231+
return new (context) SendingTypeRepr(baseType, loc);
2232+
}
22282233
case BridgedAttributedTypeSpecifierConst: {
22292234
return new (context) CompileTimeConstTypeRepr(baseType, loc);
22302235
}

lib/AST/ASTDumper.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3450,6 +3450,12 @@ class PrintTypeRepr : public TypeReprVisitor<PrintTypeRepr, void, StringRef>,
34503450
printFoot();
34513451
}
34523452

3453+
void visitSendingTypeRepr(SendingTypeRepr *T, StringRef label) {
3454+
printCommon("sending", label);
3455+
printRec(T->getBase());
3456+
printFoot();
3457+
}
3458+
34533459
void visitCompileTimeConstTypeRepr(CompileTimeConstTypeRepr *T, StringRef label) {
34543460
printCommon("_const", label);
34553461
printRec(T->getBase());

lib/AST/ASTPrinter.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3117,10 +3117,10 @@ static void suppressingFeatureOptionalIsolatedParameters(
31173117
action();
31183118
}
31193119

3120-
static void suppressingFeatureTransferringArgsAndResults(
3121-
PrintOptions &options, llvm::function_ref<void()> action) {
3122-
llvm::SaveAndRestore<bool> scope(options.SuppressTransferringArgsAndResults,
3123-
true);
3120+
static void
3121+
suppressingFeatureSendingArgsAndResults(PrintOptions &options,
3122+
llvm::function_ref<void()> action) {
3123+
llvm::SaveAndRestore<bool> scope(options.SuppressSendingArgsAndResults, true);
31243124
action();
31253125
}
31263126

@@ -3722,8 +3722,6 @@ static void printParameterFlags(ASTPrinter &printer,
37223722
if (!options.excludeAttrKind(TypeAttrKind::NoDerivative) &&
37233723
flags.isNoDerivative())
37243724
printer.printAttrName("@noDerivative ");
3725-
if (!options.SuppressTransferringArgsAndResults && flags.isSending())
3726-
printer.printAttrName("transferring ");
37273725

37283726
switch (flags.getOwnershipSpecifier()) {
37293727
case ParamSpecifier::Default:
@@ -3749,11 +3747,14 @@ static void printParameterFlags(ASTPrinter &printer,
37493747
printer.printKeyword("__owned", options, " ");
37503748
break;
37513749
case ParamSpecifier::ImplicitlyCopyableConsuming:
3752-
// Nothing... we infer from transferring.
3753-
assert(flags.isSending() && "Only valid when transferring is enabled");
3750+
// Nothing... we infer from sending.
3751+
assert(flags.isSending() && "Only valid when sending is enabled");
37543752
break;
37553753
}
3756-
3754+
3755+
if (!options.SuppressSendingArgsAndResults && flags.isSending())
3756+
printer.printAttrName("sending ");
3757+
37573758
if (flags.isIsolated()) {
37583759
if (!(param && param->getInterfaceType()->isOptional() &&
37593760
options.SuppressOptionalIsolatedParams))
@@ -4183,13 +4184,13 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
41834184
}
41844185
}
41854186

4186-
if (!Options.SuppressTransferringArgsAndResults) {
4187+
if (!Options.SuppressSendingArgsAndResults) {
41874188
if (decl->hasSendingResult()) {
4188-
Printer << "transferring ";
4189+
Printer << "sending ";
41894190
} else if (auto *ft = llvm::dyn_cast_if_present<AnyFunctionType>(
41904191
decl->getInterfaceType())) {
41914192
if (ft->hasExtInfo() && ft->hasSendingResult()) {
4192-
Printer << "transferring ";
4193+
Printer << "sending ";
41934194
}
41944195
}
41954196
}
@@ -6652,9 +6653,9 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
66526653

66536654
Printer << " -> ";
66546655

6655-
if (!Options.SuppressTransferringArgsAndResults && T->hasExtInfo() &&
6656+
if (!Options.SuppressSendingArgsAndResults && T->hasExtInfo() &&
66566657
T->hasSendingResult()) {
6657-
Printer.printKeyword("transferring ", Options);
6658+
Printer.printKeyword("sending ", Options);
66586659
}
66596660

66606661
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
@@ -10232,7 +10232,8 @@ FuncDecl *FuncDecl::create(ASTContext &Context, SourceLoc StaticLoc,
1023210232
ClangNode());
1023310233
FD->setParameters(BodyParams);
1023410234
FD->FnRetType = TypeLoc(ResultTyR);
10235-
if (llvm::isa_and_nonnull<TransferringTypeRepr>(ResultTyR))
10235+
if (llvm::isa_and_nonnull<TransferringTypeRepr>(ResultTyR) ||
10236+
llvm::isa_and_nonnull<SendingTypeRepr>(ResultTyR))
1023610237
FD->setSendingResult();
1023710238
return FD;
1023810239
}

lib/AST/FeatureSet.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -644,8 +644,9 @@ UNINTERESTING_FEATURE(BitwiseCopyable)
644644
UNINTERESTING_FEATURE(FixedArrays)
645645
UNINTERESTING_FEATURE(GroupActorErrors)
646646

647-
static bool usesFeatureTransferringArgsAndResults(Decl *decl) {
648-
auto functionTypeUsesTransferring = [](Decl *decl) {
647+
UNINTERESTING_FEATURE(TransferringArgsAndResults)
648+
static bool usesFeatureSendingArgsAndResults(Decl *decl) {
649+
auto functionTypeUsesSending = [](Decl *decl) {
649650
return usesTypeMatching(decl, [](Type type) {
650651
auto fnType = type->getAs<AnyFunctionType>();
651652
if (!fnType)
@@ -666,17 +667,17 @@ static bool usesFeatureTransferringArgsAndResults(Decl *decl) {
666667
return true;
667668
}
668669

669-
if (functionTypeUsesTransferring(pd))
670+
if (functionTypeUsesSending(pd))
670671
return true;
671672
}
672673

673674
if (auto *fDecl = dyn_cast<FuncDecl>(decl)) {
674675
// First check for param decl results.
675676
if (llvm::any_of(fDecl->getParameters()->getArray(), [](ParamDecl *pd) {
676-
return usesFeatureTransferringArgsAndResults(pd);
677+
return usesFeatureSendingArgsAndResults(pd);
677678
}))
678679
return true;
679-
if (functionTypeUsesTransferring(decl))
680+
if (functionTypeUsesSending(decl))
680681
return true;
681682
}
682683

lib/AST/NameLookup.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3152,6 +3152,7 @@ directReferencesForTypeRepr(Evaluator &evaluator,
31523152
case TypeReprKind::ResultDependsOn:
31533153
case TypeReprKind::LifetimeDependentReturn:
31543154
case TypeReprKind::Transferring:
3155+
case TypeReprKind::Sending:
31553156
return result;
31563157

31573158
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
@@ -679,6 +679,8 @@ void importer::getNormalInvocationArguments(
679679
// is supported.
680680
if (LangOpts.hasFeature(Feature::TransferringArgsAndResults))
681681
invocationArgStrs.push_back("-D__SWIFT_ATTR_SUPPORTS_TRANSFERRING=1");
682+
if (LangOpts.hasFeature(Feature::SendingArgsAndResults))
683+
invocationArgStrs.push_back("-D__SWIFT_ATTR_SUPPORTS_SENDING=1");
682684

683685
if (searchPathOpts.getSDKPath().empty()) {
684686
invocationArgStrs.push_back("-Xclang");

lib/ClangImporter/ImportDecl.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8062,6 +8062,17 @@ ClangImporter::Implementation::importSwiftAttrAttributes(Decl *MappedDecl) {
80628062
continue;
80638063
}
80648064

8065+
if (swiftAttr->getAttribute() == "sending") {
8066+
// Swallow this if the feature is not enabled.
8067+
if (!SwiftContext.LangOpts.hasFeature(Feature::SendingArgsAndResults))
8068+
continue;
8069+
auto *funcDecl = dyn_cast<FuncDecl>(MappedDecl);
8070+
if (!funcDecl)
8071+
continue;
8072+
funcDecl->setSendingResult();
8073+
continue;
8074+
}
8075+
80658076
if (swiftAttr->getAttribute() == "sensitive") {
80668077
if (!SwiftContext.LangOpts.hasFeature(Feature::Sensitive))
80678078
continue;

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)