@@ -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
}
@@ -6387,7 +6411,7 @@ class AsyncConverter : private SourceEntityWalker {
6387
6411
return ;
6388
6412
}
6389
6413
6390
- SmallVector<Type , 2 > Scratch;
6414
+ SmallVector<LabeledReturnType , 2 > Scratch;
6391
6415
auto ReturnTypes = TopHandler.getAsyncReturnTypes (Scratch);
6392
6416
if (ReturnTypes.empty ()) {
6393
6417
OS << " " ;
@@ -6401,7 +6425,14 @@ class AsyncConverter : private SourceEntityWalker {
6401
6425
OS << " (" ;
6402
6426
6403
6427
llvm::interleave (
6404
- ReturnTypes, [&](Type Ty) { Ty->print (OS); }, [&]() { OS << " , " ; });
6428
+ ReturnTypes,
6429
+ [&](LabeledReturnType TypeAndLabel) {
6430
+ if (!TypeAndLabel.Label .empty ()) {
6431
+ OS << TypeAndLabel.Label << tok::colon << " " ;
6432
+ }
6433
+ TypeAndLabel.Ty ->print (OS);
6434
+ },
6435
+ [&]() { OS << " , " ; });
6405
6436
6406
6437
if (ReturnTypes.size () > 1 )
6407
6438
OS << " )" ;
@@ -7180,7 +7211,14 @@ class AsyncConverter : private SourceEntityWalker {
7180
7211
// completion(result.0, result.1)
7181
7212
// }
7182
7213
// }
7183
- OS << ResultName << tok::period << Index;
7214
+ OS << ResultName << tok::period;
7215
+
7216
+ auto Label = HandlerDesc.getAsyncReturnTypeLabel (Index);
7217
+ if (!Label.empty ()) {
7218
+ OS << Label;
7219
+ } else {
7220
+ OS << Index;
7221
+ }
7184
7222
} else {
7185
7223
OS << ResultName;
7186
7224
}
@@ -7228,9 +7266,14 @@ class AsyncConverter : private SourceEntityWalker {
7228
7266
// / returned results via a completion handler described by \p HandlerDesc.
7229
7267
void addAsyncFuncReturnType (const AsyncHandlerDesc &HandlerDesc) {
7230
7268
// Type or (Type1, Type2, ...)
7231
- SmallVector<Type , 2 > Scratch;
7269
+ SmallVector<LabeledReturnType , 2 > Scratch;
7232
7270
addTupleOf (HandlerDesc.getAsyncReturnTypes (Scratch), OS,
7233
- [&](auto Ty) { Ty->print (OS); });
7271
+ [&](LabeledReturnType LabelAndType) {
7272
+ if (!LabelAndType.Label .empty ()) {
7273
+ OS << LabelAndType.Label << tok::colon << " " ;
7274
+ }
7275
+ LabelAndType.Ty ->print (OS);
7276
+ });
7234
7277
}
7235
7278
7236
7279
// / If \p FD is generic, adds a type annotation with the return type of the
0 commit comments