Skip to content

Commit 5b91fd8

Browse files
authored
Merge pull request swiftlang#74097 from gottesmm/release/6.0-128961672
[6.0][sending] Change CheckedContinuation/AsyncThrowingStream.Continuation APIs to use sending parameters and results.
2 parents 1206144 + 1a6f055 commit 5b91fd8

File tree

54 files changed

+707
-309
lines changed

Some content is hidden

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

54 files changed

+707
-309
lines changed

include/swift/AST/ASTMangler.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,8 @@ class ASTMangler : public Mangler {
446446
GenericSignature sig,
447447
ModuleDecl *fromModule);
448448
void appendImplFunctionType(SILFunctionType *fn, GenericSignature sig,
449-
const ValueDecl *forDecl = nullptr);
449+
const ValueDecl *forDecl = nullptr,
450+
bool isInRecursion = true);
450451
void appendOpaqueTypeArchetype(ArchetypeType *archetype,
451452
OpaqueTypeDecl *opaqueDecl,
452453
SubstitutionMap subs,
@@ -522,25 +523,29 @@ class ASTMangler : public Mangler {
522523
FunctionMangling,
523524
};
524525

525-
void appendFunction(AnyFunctionType *fn, GenericSignature sig,
526-
FunctionManglingKind functionMangling = NoFunctionMangling,
527-
const ValueDecl *forDecl = nullptr);
526+
void
527+
appendFunction(AnyFunctionType *fn, GenericSignature sig,
528+
FunctionManglingKind functionMangling = NoFunctionMangling,
529+
const ValueDecl *forDecl = nullptr,
530+
bool isRecursedInto = true);
528531
void appendFunctionType(AnyFunctionType *fn, GenericSignature sig,
529532
bool isAutoClosure = false,
530-
const ValueDecl *forDecl = nullptr);
533+
const ValueDecl *forDecl = nullptr,
534+
bool isRecursedInto = true);
531535
void appendClangType(AnyFunctionType *fn);
532536
template <typename FnType>
533537
void appendClangType(FnType *fn, llvm::raw_svector_ostream &os);
534538

535-
void appendFunctionSignature(AnyFunctionType *fn,
536-
GenericSignature sig,
539+
void appendFunctionSignature(AnyFunctionType *fn, GenericSignature sig,
537540
const ValueDecl *forDecl,
538-
FunctionManglingKind functionMangling);
541+
FunctionManglingKind functionMangling,
542+
bool isRecursedInto = true);
539543

540544
void appendFunctionInputType(ArrayRef<AnyFunctionType::Param> params,
541545
LifetimeDependenceInfo lifetimeDependenceInfo,
542546
GenericSignature sig,
543-
const ValueDecl *forDecl = nullptr);
547+
const ValueDecl *forDecl = nullptr,
548+
bool isRecursedInto = true);
544549
void appendFunctionResultType(Type resultType,
545550
GenericSignature sig,
546551
const ValueDecl *forDecl = nullptr);

include/swift/AST/Builtins.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -515,14 +515,14 @@ BUILTIN_SIL_OPERATION(ApplyDerivative, "applyDerivative", Special)
515515
/// applyTranspose
516516
BUILTIN_SIL_OPERATION(ApplyTranspose, "applyTranspose", Special)
517517

518-
/// withUnsafeContinuation<T> : (Builtin.RawUnsafeContinuation -> ()) async -> T
518+
/// withUnsafeContinuation<T> : (Builtin.RawUnsafeContinuation -> ()) async -> sending T
519519
///
520520
/// Unsafely capture the current continuation and pass it to the given
521521
/// function value. Returns a value of type T when the continuation is
522522
/// resumed.
523523
BUILTIN_SIL_OPERATION(WithUnsafeContinuation, "withUnsafeContinuation", Special)
524524

525-
/// withUnsafeThrowingContinuation<T> : (Builtin.RawUnsafeContinuation -> ()) async throws -> T
525+
/// withUnsafeThrowingContinuation<T> : (Builtin.RawUnsafeContinuation -> ()) async throws -> sending T
526526
///
527527
/// Unsafely capture the current continuation and pass it to the given
528528
/// function value. Returns a value of type T or throws an error when

include/swift/Basic/Features.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ SUPPRESSIBLE_LANGUAGE_FEATURE(ConformanceSuppression, 426, "Suppressible inferre
197197
SUPPRESSIBLE_LANGUAGE_FEATURE(BitwiseCopyable2, 426, "BitwiseCopyable feature")
198198
LANGUAGE_FEATURE(BodyMacros, 415, "Function body macros")
199199
LANGUAGE_FEATURE(BuiltinAddressOfRawLayout, 0, "Builtin.addressOfRawLayout")
200+
LANGUAGE_FEATURE(TransferringArgsAndResults, 430, "Transferring args and results")
201+
SUPPRESSIBLE_LANGUAGE_FEATURE(SendingArgsAndResults, 430, "Sending arg and results")
200202

201203
// Swift 6
202204
UPCOMING_FEATURE(ConciseMagicFile, 274, 6)
@@ -215,8 +217,6 @@ UPCOMING_FEATURE(DynamicActorIsolation, 423, 6)
215217
UPCOMING_FEATURE(NonfrozenEnumExhaustivity, 192, 6)
216218
UPCOMING_FEATURE(GlobalActorIsolatedTypesUsability, 0434, 6)
217219
UPCOMING_FEATURE(BorrowingSwitch, 432, 6)
218-
UPCOMING_FEATURE(TransferringArgsAndResults, 430, 6)
219-
SUPPRESSIBLE_UPCOMING_FEATURE(SendingArgsAndResults, 430, 6)
220220

221221
// Swift 7
222222
UPCOMING_FEATURE(ExistentialAny, 335, 7)

include/swift/Demangling/TypeDecoder.h

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,14 @@ class ImplFunctionParam {
177177
return {};
178178
}
179179

180+
static OptionsType getSending() {
181+
OptionsType result;
182+
183+
result |= ImplParameterInfoFlags::Sending;
184+
185+
return result;
186+
}
187+
180188
ImplFunctionParam(BuiltType type, ImplParameterConvention convention,
181189
OptionsType options)
182190
: Type(type), Convention(convention), Options(options) {}
@@ -247,6 +255,12 @@ class ImplFunctionResult {
247255
return {};
248256
}
249257

258+
static OptionsType getSending() {
259+
OptionsType result;
260+
result |= ImplResultInfoFlags::IsSending;
261+
return result;
262+
}
263+
250264
ImplFunctionResult(BuiltType type, ImplResultConvention convention,
251265
ImplResultInfoOptions options = {})
252266
: Type(type), Convention(convention), Options(options) {}
@@ -1571,8 +1585,9 @@ class TypeDecoder {
15711585
if (depth > TypeDecoder::MaxDepth)
15721586
return true;
15731587

1574-
// Children: `convention, differentiability?, type`
1575-
if (node->getNumChildren() != 2 && node->getNumChildren() != 3)
1588+
// Children: `convention, differentiability?, sending?, type`
1589+
if (node->getNumChildren() != 2 && node->getNumChildren() != 3 &&
1590+
node->getNumChildren() != 4)
15761591
return true;
15771592

15781593
auto *conventionNode = node->getChild(0);
@@ -1590,7 +1605,7 @@ class TypeDecoder {
15901605
return true;
15911606

15921607
typename T::OptionsType options;
1593-
if (node->getNumChildren() == 3) {
1608+
if (node->getNumChildren() == 3 || node->getNumChildren() == 4) {
15941609
auto diffKindNode = node->getChild(1);
15951610
if (diffKindNode->getKind() !=
15961611
Node::Kind::ImplParameterResultDifferentiability)
@@ -1602,6 +1617,13 @@ class TypeDecoder {
16021617
options |= *optDiffOptions;
16031618
}
16041619

1620+
if (node->getNumChildren() == 4) {
1621+
auto sendingKindNode = node->getChild(2);
1622+
if (sendingKindNode->getKind() != Node::Kind::ImplParameterSending)
1623+
return true;
1624+
options |= T::getSending();
1625+
}
1626+
16051627
results.emplace_back(result.getType(), *convention, options);
16061628

16071629
return false;

include/swift/SIL/ApplySite.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,21 @@ class ApplySite {
470470
llvm_unreachable("covered switch");
471471
}
472472

473+
/// Return the sil_isolated operand if we have one.
474+
Operand *getIsolatedArgumentOperandOrNullPtr() {
475+
switch (ApplySiteKind(Inst->getKind())) {
476+
case ApplySiteKind::ApplyInst:
477+
return cast<ApplyInst>(Inst)->getIsolatedArgumentOperandOrNullPtr();
478+
case ApplySiteKind::BeginApplyInst:
479+
return cast<BeginApplyInst>(Inst)->getIsolatedArgumentOperandOrNullPtr();
480+
case ApplySiteKind::TryApplyInst:
481+
return cast<TryApplyInst>(Inst)->getIsolatedArgumentOperandOrNullPtr();
482+
case ApplySiteKind::PartialApplyInst:
483+
llvm_unreachable("Unhandled case");
484+
}
485+
llvm_unreachable("covered switch");
486+
}
487+
473488
/// Return a list of applied arguments without self.
474489
OperandValueArrayRef getArgumentsWithoutSelf() const {
475490
switch (ApplySiteKind(Inst->getKind())) {

include/swift/SIL/SILInstruction.h

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2972,14 +2972,15 @@ class ApplyInstBase<Impl, Base, true>
29722972
const Impl &asImpl() const { return static_cast<const Impl &>(*this); }
29732973

29742974
public:
2975+
using super::getArgument;
2976+
using super::getArgumentOperands;
2977+
using super::getArguments;
29752978
using super::getCallee;
2976-
using super::getSubstCalleeType;
2979+
using super::getCalleeOperand;
2980+
using super::getNumArguments;
29772981
using super::getSubstCalleeConv;
2982+
using super::getSubstCalleeType;
29782983
using super::hasSubstitutions;
2979-
using super::getNumArguments;
2980-
using super::getArgument;
2981-
using super::getArguments;
2982-
using super::getArgumentOperands;
29832984

29842985
/// The collection of following routines wrap the representation difference in
29852986
/// between the self substitution being first, but the self parameter of a
@@ -3064,6 +3065,21 @@ class ApplyInstBase<Impl, Base, true>
30643065
return getSubstCalleeType()->hasSelfParam();
30653066
}
30663067

3068+
Operand *getIsolatedArgumentOperandOrNullPtr() {
3069+
SILFunctionConventions conv = getSubstCalleeConv();
3070+
for (Operand &argOp : getOperandsWithoutIndirectResults()) {
3071+
// Skip the callee.
3072+
if (getCalleeOperand() == &argOp)
3073+
continue;
3074+
3075+
auto opNum = argOp.getOperandNumber() - 1;
3076+
auto paramInfo = conv.getParamInfoForSILArg(opNum);
3077+
if (paramInfo.getOptions().contains(SILParameterInfo::Isolated))
3078+
return &argOp;
3079+
}
3080+
return nullptr;
3081+
}
3082+
30673083
bool hasGuaranteedSelfArgument() const {
30683084
auto C = getSubstCalleeType()->getSelfParameter().getConvention();
30693085
return C == ParameterConvention::Direct_Guaranteed;
@@ -3077,7 +3093,7 @@ class ApplyInstBase<Impl, Base, true>
30773093
return getArguments().slice(getNumIndirectResults());
30783094
}
30793095

3080-
MutableArrayRef<Operand> getOperandsWithoutIndirectResults() const {
3096+
MutableArrayRef<Operand> getOperandsWithoutIndirectResults() {
30813097
return getArgumentOperands().slice(getNumIndirectResults());
30823098
}
30833099

lib/AST/ASTMangler.cpp

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2034,7 +2034,8 @@ getResultDifferentiability(SILResultInfo::Options options) {
20342034

20352035
void ASTMangler::appendImplFunctionType(SILFunctionType *fn,
20362036
GenericSignature outerGenericSig,
2037-
const ValueDecl *forDecl) {
2037+
const ValueDecl *forDecl,
2038+
bool isInRecursion) {
20382039

20392040
llvm::SmallVector<char, 32> OpArgs;
20402041

@@ -2159,7 +2160,7 @@ void ASTMangler::appendImplFunctionType(SILFunctionType *fn,
21592160
}
21602161

21612162
// Mangle if we have a transferring result.
2162-
if (fn->hasSendingResult())
2163+
if (isInRecursion && fn->hasSendingResult())
21632164
OpArgs.push_back('T');
21642165

21652166
// Mangle the results.
@@ -2942,7 +2943,7 @@ void ASTMangler::appendAnyGenericType(const GenericTypeDecl *decl,
29422943

29432944
void ASTMangler::appendFunction(AnyFunctionType *fn, GenericSignature sig,
29442945
FunctionManglingKind functionMangling,
2945-
const ValueDecl *forDecl) {
2946+
const ValueDecl *forDecl, bool isRecursedInto) {
29462947
// Append parameter labels right before the signature/type.
29472948
auto parameters = fn->getParams();
29482949
auto firstLabel = std::find_if(
@@ -2962,19 +2963,20 @@ void ASTMangler::appendFunction(AnyFunctionType *fn, GenericSignature sig,
29622963
}
29632964

29642965
if (functionMangling != NoFunctionMangling) {
2965-
appendFunctionSignature(fn, sig, forDecl, functionMangling);
2966+
appendFunctionSignature(fn, sig, forDecl, functionMangling, isRecursedInto);
29662967
} else {
2967-
appendFunctionType(fn, sig, /*autoclosure*/ false, forDecl);
2968+
appendFunctionType(fn, sig, /*autoclosure*/ false, forDecl, isRecursedInto);
29682969
}
29692970
}
29702971

29712972
void ASTMangler::appendFunctionType(AnyFunctionType *fn, GenericSignature sig,
29722973
bool isAutoClosure,
2973-
const ValueDecl *forDecl) {
2974+
const ValueDecl *forDecl,
2975+
bool isRecursedInto) {
29742976
assert((DWARFMangling || fn->isCanonical()) &&
29752977
"expecting canonical types when not mangling for the debugger");
29762978

2977-
appendFunctionSignature(fn, sig, forDecl, NoFunctionMangling);
2979+
appendFunctionSignature(fn, sig, forDecl, NoFunctionMangling, isRecursedInto);
29782980

29792981
bool mangleClangType = fn->getASTContext().LangOpts.UseClangFunctionTypes &&
29802982
fn->hasNonDerivableClangType();
@@ -3042,10 +3044,11 @@ void ASTMangler::appendClangType(AnyFunctionType *fn) {
30423044
void ASTMangler::appendFunctionSignature(AnyFunctionType *fn,
30433045
GenericSignature sig,
30443046
const ValueDecl *forDecl,
3045-
FunctionManglingKind functionMangling) {
3047+
FunctionManglingKind functionMangling,
3048+
bool isRecursedInto) {
30463049
appendFunctionResultType(fn->getResult(), sig, forDecl);
30473050
appendFunctionInputType(fn->getParams(), fn->getLifetimeDependenceInfo(), sig,
3048-
forDecl);
3051+
forDecl, isRecursedInto);
30493052
if (fn->isAsync())
30503053
appendOperator("Ya");
30513054
if (fn->isSendable())
@@ -3091,7 +3094,7 @@ void ASTMangler::appendFunctionSignature(AnyFunctionType *fn,
30913094
break;
30923095
}
30933096

3094-
if (fn->hasSendingResult()) {
3097+
if (isRecursedInto && fn->hasSendingResult()) {
30953098
appendOperator("YT");
30963099
}
30973100

@@ -3137,19 +3140,25 @@ getDefaultOwnership(const ValueDecl *forDecl) {
31373140

31383141
static ParameterTypeFlags
31393142
getParameterFlagsForMangling(ParameterTypeFlags flags,
3140-
ParamSpecifier defaultSpecifier) {
3143+
ParamSpecifier defaultSpecifier,
3144+
bool isInRecursion = true) {
3145+
bool initiallySending = flags.isSending();
3146+
3147+
// If we have been recursed into, then remove sending from our flags.
3148+
if (!isInRecursion) {
3149+
flags = flags.withSending(false);
3150+
}
3151+
31413152
switch (auto specifier = flags.getOwnershipSpecifier()) {
31423153
// If no parameter specifier was provided, mangle as-is, because we are by
31433154
// definition using the default convention.
31443155
case ParamSpecifier::Default:
31453156
// If the legacy `__shared` or `__owned` modifier was provided, mangle as-is,
31463157
// because we need to maintain compatibility with their existing behavior.
3147-
case ParamSpecifier::LegacyShared:
31483158
case ParamSpecifier::LegacyOwned:
31493159
// `inout` should already be specified in the flags.
31503160
case ParamSpecifier::InOut:
31513161
return flags;
3152-
31533162
case ParamSpecifier::ImplicitlyCopyableConsuming:
31543163
case ParamSpecifier::Consuming:
31553164
case ParamSpecifier::Borrowing:
@@ -3158,13 +3167,23 @@ getParameterFlagsForMangling(ParameterTypeFlags flags,
31583167
flags = flags.withOwnershipSpecifier(ParamSpecifier::Default);
31593168
}
31603169
return flags;
3170+
case ParamSpecifier::LegacyShared:
3171+
// If we were originally sending and by default we are borrowing, suppress
3172+
// this and set ownership specifier to default so we do not mangle in
3173+
// __shared.
3174+
//
3175+
// This is a work around in the short term since shared borrow is not
3176+
// supported.
3177+
if (initiallySending && ParamSpecifier::Borrowing == defaultSpecifier)
3178+
return flags.withOwnershipSpecifier(ParamSpecifier::Default);
3179+
return flags;
31613180
}
31623181
}
31633182

31643183
void ASTMangler::appendFunctionInputType(
31653184
ArrayRef<AnyFunctionType::Param> params,
31663185
LifetimeDependenceInfo lifetimeDependenceInfo, GenericSignature sig,
3167-
const ValueDecl *forDecl) {
3186+
const ValueDecl *forDecl, bool isRecursedInto) {
31683187
auto defaultSpecifier = getDefaultOwnership(forDecl);
31693188

31703189
switch (params.size()) {
@@ -3187,7 +3206,7 @@ void ASTMangler::appendFunctionInputType(
31873206
appendParameterTypeListElement(
31883207
Identifier(), type,
31893208
getParameterFlagsForMangling(param.getParameterFlags(),
3190-
defaultSpecifier),
3209+
defaultSpecifier, isRecursedInto),
31913210
lifetimeDependenceInfo.getLifetimeDependenceOnParam(/*paramIndex*/ 0),
31923211
sig, nullptr);
31933212
break;
@@ -3209,7 +3228,7 @@ void ASTMangler::appendFunctionInputType(
32093228
appendParameterTypeListElement(
32103229
Identifier(), param.getPlainType(),
32113230
getParameterFlagsForMangling(param.getParameterFlags(),
3212-
defaultSpecifier),
3231+
defaultSpecifier, isRecursedInto),
32133232
lifetimeDependenceInfo.getLifetimeDependenceOnParam(paramIndex), sig,
32143233
nullptr);
32153234
appendListSeparator(isFirstParam);
@@ -3873,7 +3892,8 @@ void ASTMangler::appendDeclType(const ValueDecl *decl,
38733892
: decl->getDeclContext()->getGenericSignatureOfContext());
38743893

38753894
if (AnyFunctionType *FuncTy = type->getAs<AnyFunctionType>()) {
3876-
appendFunction(FuncTy, sig, functionMangling, decl);
3895+
appendFunction(FuncTy, sig, functionMangling, decl,
3896+
false /*is recursed into*/);
38773897
} else {
38783898
appendType(type, sig, decl);
38793899
}

0 commit comments

Comments
 (0)