@@ -4088,6 +4088,16 @@ class HandlerResult {
4088
4088
// / single parameter of `Result` type.
4089
4089
enum class HandlerType { INVALID, PARAMS, RESULT };
4090
4090
4091
+ // / A single return type of a refactored async function. If the async function
4092
+ // / returns a tuple, each element of the tuple (represented by a \c
4093
+ // / LabeledReturnType) might have a label, otherwise the \p Label is empty.
4094
+ struct LabeledReturnType {
4095
+ Identifier Label;
4096
+ swift::Type Ty;
4097
+
4098
+ LabeledReturnType (Identifier Label, swift::Type Ty) : Label(Label), Ty(Ty) {}
4099
+ };
4100
+
4091
4101
// / Given a function with an async alternative (or one that *could* have an
4092
4102
// / async alternative), stores information about the completion handler.
4093
4103
// / The completion handler can be either a variable (which includes a parameter)
@@ -4336,12 +4346,26 @@ struct AsyncHandlerDesc {
4336
4346
}
4337
4347
}
4338
4348
4349
+ // / If the async function returns a tuple, the label of the \p Index -th
4350
+ // / element in the returned tuple. If the function doesn't return a tuple or
4351
+ // / the element is unlabeled, an empty identifier is returned.
4352
+ Identifier getAsyncReturnTypeLabel (size_t Index) const {
4353
+ assert (Index < getSuccessParams ().size ());
4354
+ if (getSuccessParams ().size () <= 1 ) {
4355
+ // There can't be any labels if the async function doesn't return a tuple.
4356
+ return Identifier ();
4357
+ } else {
4358
+ return getSuccessParams ()[Index].getInternalLabel ();
4359
+ }
4360
+ }
4361
+
4339
4362
// / Gets the return value types for the async equivalent of this handler.
4340
- ArrayRef<swift::Type>
4341
- getAsyncReturnTypes (SmallVectorImpl<swift::Type> &Scratch) const {
4342
- for (auto &Param : getSuccessParams ()) {
4343
- auto Ty = Param.getParameterType ();
4344
- Scratch.push_back (getSuccessParamAsyncReturnType (Ty));
4363
+ ArrayRef<LabeledReturnType>
4364
+ getAsyncReturnTypes (SmallVectorImpl<LabeledReturnType> &Scratch) const {
4365
+ for (size_t I = 0 ; I < getSuccessParams ().size (); ++I) {
4366
+ auto Ty = getSuccessParams ()[I].getParameterType ();
4367
+ Scratch.emplace_back (getAsyncReturnTypeLabel (I),
4368
+ getSuccessParamAsyncReturnType (Ty));
4345
4369
}
4346
4370
return Scratch;
4347
4371
}
@@ -6388,7 +6412,7 @@ class AsyncConverter : private SourceEntityWalker {
6388
6412
return ;
6389
6413
}
6390
6414
6391
- SmallVector<Type , 2 > Scratch;
6415
+ SmallVector<LabeledReturnType , 2 > Scratch;
6392
6416
auto ReturnTypes = TopHandler.getAsyncReturnTypes (Scratch);
6393
6417
if (ReturnTypes.empty ()) {
6394
6418
OS << " " ;
@@ -6402,7 +6426,14 @@ class AsyncConverter : private SourceEntityWalker {
6402
6426
OS << " (" ;
6403
6427
6404
6428
llvm::interleave (
6405
- ReturnTypes, [&](Type Ty) { Ty->print (OS); }, [&]() { OS << " , " ; });
6429
+ ReturnTypes,
6430
+ [&](LabeledReturnType TypeAndLabel) {
6431
+ if (!TypeAndLabel.Label .empty ()) {
6432
+ OS << TypeAndLabel.Label << tok::colon << " " ;
6433
+ }
6434
+ TypeAndLabel.Ty ->print (OS);
6435
+ },
6436
+ [&]() { OS << " , " ; });
6406
6437
6407
6438
if (ReturnTypes.size () > 1 )
6408
6439
OS << " )" ;
@@ -7181,7 +7212,14 @@ class AsyncConverter : private SourceEntityWalker {
7181
7212
// completion(result.0, result.1)
7182
7213
// }
7183
7214
// }
7184
- OS << ResultName << tok::period << Index;
7215
+ OS << ResultName << tok::period;
7216
+
7217
+ auto Label = HandlerDesc.getAsyncReturnTypeLabel (Index);
7218
+ if (!Label.empty ()) {
7219
+ OS << Label;
7220
+ } else {
7221
+ OS << Index;
7222
+ }
7185
7223
} else {
7186
7224
OS << ResultName;
7187
7225
}
@@ -7229,9 +7267,14 @@ class AsyncConverter : private SourceEntityWalker {
7229
7267
// / returned results via a completion handler described by \p HandlerDesc.
7230
7268
void addAsyncFuncReturnType (const AsyncHandlerDesc &HandlerDesc) {
7231
7269
// Type or (Type1, Type2, ...)
7232
- SmallVector<Type , 2 > Scratch;
7270
+ SmallVector<LabeledReturnType , 2 > Scratch;
7233
7271
addTupleOf (HandlerDesc.getAsyncReturnTypes (Scratch), OS,
7234
- [&](auto Ty) { Ty->print (OS); });
7272
+ [&](LabeledReturnType LabelAndType) {
7273
+ if (!LabelAndType.Label .empty ()) {
7274
+ OS << LabelAndType.Label << tok::colon << " " ;
7275
+ }
7276
+ LabelAndType.Ty ->print (OS);
7277
+ });
7235
7278
}
7236
7279
7237
7280
// / If \p FD is generic, adds a type annotation with the return type of the
0 commit comments