@@ -4079,6 +4079,16 @@ class HandlerResult {
4079
4079
// / single parameter of `Result` type.
4080
4080
enum class HandlerType { INVALID, PARAMS, RESULT };
4081
4081
4082
+ // / A single return type of a refactored async function. If the async function
4083
+ // / returns a tuple, each element of the tuple (represented by a \c
4084
+ // / LabeledReturnType) might have a label, otherwise the \p Label is empty.
4085
+ struct LabeledReturnType {
4086
+ Identifier Label;
4087
+ swift::Type Ty;
4088
+
4089
+ LabeledReturnType (Identifier Label, swift::Type Ty) : Label(Label), Ty(Ty) {}
4090
+ };
4091
+
4082
4092
// / Given a function with an async alternative (or one that *could* have an
4083
4093
// / async alternative), stores information about the completion handler.
4084
4094
// / The completion handler can be either a variable (which includes a parameter)
@@ -4327,12 +4337,26 @@ struct AsyncHandlerDesc {
4327
4337
}
4328
4338
}
4329
4339
4340
+ // / If the async function returns a tuple, the label of the \p Index -th
4341
+ // / element in the returned tuple. If the function doesn't return a tuple or
4342
+ // / the element is unlabeled, an empty identifier is returned.
4343
+ Identifier getAsyncReturnTypeLabel (size_t Index) const {
4344
+ assert (Index < getSuccessParams ().size ());
4345
+ if (getSuccessParams ().size () <= 1 ) {
4346
+ // There can't be any labels if the async function doesn't return a tuple.
4347
+ return Identifier ();
4348
+ } else {
4349
+ return getSuccessParams ()[Index].getInternalLabel ();
4350
+ }
4351
+ }
4352
+
4330
4353
// / Gets the return value types for the async equivalent of this handler.
4331
- ArrayRef<swift::Type>
4332
- getAsyncReturnTypes (SmallVectorImpl<swift::Type> &Scratch) const {
4333
- for (auto &Param : getSuccessParams ()) {
4334
- auto Ty = Param.getParameterType ();
4335
- Scratch.push_back (getSuccessParamAsyncReturnType (Ty));
4354
+ ArrayRef<LabeledReturnType>
4355
+ getAsyncReturnTypes (SmallVectorImpl<LabeledReturnType> &Scratch) const {
4356
+ for (size_t I = 0 ; I < getSuccessParams ().size (); ++I) {
4357
+ auto Ty = getSuccessParams ()[I].getParameterType ();
4358
+ Scratch.emplace_back (getAsyncReturnTypeLabel (I),
4359
+ getSuccessParamAsyncReturnType (Ty));
4336
4360
}
4337
4361
return Scratch;
4338
4362
}
@@ -6371,7 +6395,7 @@ class AsyncConverter : private SourceEntityWalker {
6371
6395
return ;
6372
6396
}
6373
6397
6374
- SmallVector<Type , 2 > Scratch;
6398
+ SmallVector<LabeledReturnType , 2 > Scratch;
6375
6399
auto ReturnTypes = TopHandler.getAsyncReturnTypes (Scratch);
6376
6400
if (ReturnTypes.empty ()) {
6377
6401
OS << " " ;
@@ -6385,7 +6409,14 @@ class AsyncConverter : private SourceEntityWalker {
6385
6409
OS << " (" ;
6386
6410
6387
6411
llvm::interleave (
6388
- ReturnTypes, [&](Type Ty) { Ty->print (OS); }, [&]() { OS << " , " ; });
6412
+ ReturnTypes,
6413
+ [&](LabeledReturnType TypeAndLabel) {
6414
+ if (!TypeAndLabel.Label .empty ()) {
6415
+ OS << TypeAndLabel.Label << tok::colon << " " ;
6416
+ }
6417
+ TypeAndLabel.Ty ->print (OS);
6418
+ },
6419
+ [&]() { OS << " , " ; });
6389
6420
6390
6421
if (ReturnTypes.size () > 1 )
6391
6422
OS << " )" ;
@@ -7164,7 +7195,14 @@ class AsyncConverter : private SourceEntityWalker {
7164
7195
// completion(result.0, result.1)
7165
7196
// }
7166
7197
// }
7167
- OS << ResultName << tok::period << Index;
7198
+ OS << ResultName << tok::period;
7199
+
7200
+ auto Label = HandlerDesc.getAsyncReturnTypeLabel (Index);
7201
+ if (!Label.empty ()) {
7202
+ OS << Label;
7203
+ } else {
7204
+ OS << Index;
7205
+ }
7168
7206
} else {
7169
7207
OS << ResultName;
7170
7208
}
@@ -7212,9 +7250,14 @@ class AsyncConverter : private SourceEntityWalker {
7212
7250
// / returned results via a completion handler described by \p HandlerDesc.
7213
7251
void addAsyncFuncReturnType (const AsyncHandlerDesc &HandlerDesc) {
7214
7252
// Type or (Type1, Type2, ...)
7215
- SmallVector<Type , 2 > Scratch;
7253
+ SmallVector<LabeledReturnType , 2 > Scratch;
7216
7254
addTupleOf (HandlerDesc.getAsyncReturnTypes (Scratch), OS,
7217
- [&](auto Ty) { Ty->print (OS); });
7255
+ [&](LabeledReturnType LabelAndType) {
7256
+ if (!LabelAndType.Label .empty ()) {
7257
+ OS << LabelAndType.Label << tok::colon << " " ;
7258
+ }
7259
+ LabelAndType.Ty ->print (OS);
7260
+ });
7218
7261
}
7219
7262
7220
7263
// / If \p FD is generic, adds a type annotation with the return type of the
0 commit comments