5959// Unwrap an Expected<> variable following the typical deserialization pattern:
6060// - On a value, assign it to Output.
6161// - On an error, return it to bubble it up to the caller.
62- #define UNWRAP ( Input, Output ) { \
62+ #define SET_OR_RETURN_ERROR ( Output, Input ) { \
6363 auto ValueOrError = Input; \
6464 if (!ValueOrError) \
6565 return ValueOrError.takeError (); \
@@ -550,7 +550,7 @@ Expected<ParameterList *> ModuleFile::readParameterList() {
550550 SmallVector<ParamDecl *, 8 > params;
551551 for (DeclID paramID : rawMemberIDs) {
552552 Decl *param;
553- UNWRAP ( getDeclChecked (paramID), param );
553+ SET_OR_RETURN_ERROR (param, getDeclChecked (paramID));
554554 params.push_back (cast<ParamDecl>(param));
555555 }
556556
@@ -596,7 +596,7 @@ Expected<Pattern *> ModuleFile::readPattern(DeclContext *owningDC) {
596596 switch (kind) {
597597 case decls_block::PAREN_PATTERN: {
598598 Pattern *subPattern;
599- UNWRAP ( readPattern (owningDC), subPattern );
599+ SET_OR_RETURN_ERROR (subPattern, readPattern (owningDC));
600600
601601 auto result = ParenPattern::createImplicit (getContext (), subPattern);
602602
@@ -629,13 +629,13 @@ Expected<Pattern *> ModuleFile::readPattern(DeclContext *owningDC) {
629629 Identifier label = getIdentifier (labelID);
630630
631631 Pattern *subPattern;
632- UNWRAP ( readPattern (owningDC), subPattern );
632+ SET_OR_RETURN_ERROR (subPattern, readPattern (owningDC));
633633 elements.push_back (TuplePatternElt (label, SourceLoc (), subPattern));
634634 }
635635
636636 auto result = TuplePattern::createImplicit (getContext (), elements);
637637 Type tupleType;
638- UNWRAP ( getTypeChecked (tupleTypeID), tupleType );
638+ SET_OR_RETURN_ERROR (tupleType, getTypeChecked (tupleTypeID));
639639 recordPatternType (result, tupleType);
640640 restoreOffset.reset ();
641641 return result;
@@ -696,7 +696,7 @@ Expected<Pattern *> ModuleFile::readPattern(DeclContext *owningDC) {
696696 BindingPatternLayout::readRecord (scratch, rawIntroducer);
697697
698698 Pattern *subPattern;
699- UNWRAP ( readPattern (owningDC), subPattern );
699+ SET_OR_RETURN_ERROR (subPattern, readPattern (owningDC));
700700
701701 auto introducer = getActualVarDeclIntroducer (
702702 (serialization::VarDeclIntroducer) rawIntroducer);
@@ -1261,7 +1261,7 @@ ModuleFile::maybeReadGenericParams(DeclContext *DC) {
12611261 GenericParamListLayout::readRecord (scratch, paramIDs);
12621262 for (DeclID nextParamID : paramIDs) {
12631263 Decl *nextParam;
1264- UNWRAP ( getDeclChecked (nextParamID), nextParam );
1264+ SET_OR_RETURN_ERROR (nextParam, getDeclChecked (nextParamID));
12651265
12661266 auto genericParam = cast<GenericTypeParamDecl>(nextParam);
12671267 params.push_back (genericParam);
@@ -2865,7 +2865,7 @@ Expected<DeclContext *>ModuleFile::getLocalDeclContext(LocalDeclContextID DCID)
28652865 discriminator,
28662866 parentID);
28672867 DeclContext *parent;
2868- UNWRAP ( getDeclContextChecked (parentID), parent );
2868+ SET_OR_RETURN_ERROR (parent, getDeclContextChecked (parentID));
28692869
28702870 auto type = getType (closureTypeID);
28712871
@@ -2879,7 +2879,7 @@ Expected<DeclContext *>ModuleFile::getLocalDeclContext(LocalDeclContextID DCID)
28792879 decls_block::TopLevelCodeDeclContextLayout::readRecord (scratch,
28802880 parentID);
28812881 DeclContext *parent;
2882- UNWRAP ( getDeclContextChecked (parentID), parent );
2882+ SET_OR_RETURN_ERROR (parent, getDeclContextChecked (parentID));
28832883
28842884 declContextOrOffset = new (ctx) SerializedTopLevelCodeDeclContext (parent);
28852885 break ;
@@ -2910,7 +2910,7 @@ Expected<DeclContext *>ModuleFile::getLocalDeclContext(LocalDeclContextID DCID)
29102910 parentID,
29112911 index);
29122912 DeclContext *parent;
2913- UNWRAP ( getDeclContextChecked (parentID), parent );
2913+ SET_OR_RETURN_ERROR (parent, getDeclContextChecked (parentID));
29142914
29152915 declContextOrOffset = DefaultArgumentInitializer::create (parent, index);
29162916 break ;
@@ -3466,10 +3466,10 @@ class DeclDeserializer {
34663466 }
34673467
34683468 DeclContext *DC;
3469- UNWRAP ( MF.getDeclContextChecked (contextID), DC );
3469+ SET_OR_RETURN_ERROR (DC, MF.getDeclContextChecked (contextID));
34703470
34713471 GenericParamList *genericParams;
3472- UNWRAP ( MF.maybeReadGenericParams (DC), genericParams );
3472+ SET_OR_RETURN_ERROR (genericParams, MF.maybeReadGenericParams (DC));
34733473 if (declOrOffset.isComplete ())
34743474 return declOrOffset;
34753475
@@ -3552,7 +3552,7 @@ class DeclDeserializer {
35523552 rawOverriddenIDs);
35533553
35543554 DeclContext *DC;
3555- UNWRAP ( MF.getDeclContextChecked (contextID), DC );
3555+ SET_OR_RETURN_ERROR (DC, MF.getDeclContextChecked (contextID));
35563556
35573557 if (declOrOffset.isComplete ())
35583558 return declOrOffset;
@@ -3629,7 +3629,7 @@ class DeclDeserializer {
36293629 return declOrOffset;
36303630
36313631 GenericParamList *genericParams;
3632- UNWRAP ( MF.maybeReadGenericParams (DC), genericParams );
3632+ SET_OR_RETURN_ERROR (genericParams, MF.maybeReadGenericParams (DC));
36333633 if (declOrOffset.isComplete ())
36343634 return declOrOffset;
36353635
@@ -3738,13 +3738,13 @@ class DeclDeserializer {
37383738 }
37393739
37403740 DeclContext *parent;
3741- UNWRAP ( MF.getDeclContextChecked (contextID), parent );
3741+ SET_OR_RETURN_ERROR (parent, MF.getDeclContextChecked (contextID));
37423742
37433743 if (declOrOffset.isComplete ())
37443744 return declOrOffset;
37453745
37463746 GenericParamList *genericParams;
3747- UNWRAP ( MF.maybeReadGenericParams (parent), genericParams );
3747+ SET_OR_RETURN_ERROR (genericParams, MF.maybeReadGenericParams (parent));
37483748 if (declOrOffset.isComplete ())
37493749 return declOrOffset;
37503750
@@ -3772,7 +3772,7 @@ class DeclDeserializer {
37723772 return MF.diagnoseFatal ();
37733773
37743774 ParameterList *bodyParams;
3775- UNWRAP ( MF.readParameterList (), bodyParams );
3775+ SET_OR_RETURN_ERROR (bodyParams, MF.readParameterList ());
37763776 assert (bodyParams && " missing parameters for constructor" );
37773777 ctor->setParameters (bodyParams);
37783778
@@ -3907,7 +3907,7 @@ class DeclDeserializer {
39073907 }
39083908
39093909 DeclContext *DC;
3910- UNWRAP ( MF.getDeclContextChecked (contextID), DC );
3910+ SET_OR_RETURN_ERROR (DC, MF.getDeclContextChecked (contextID));
39113911
39123912 if (declOrOffset.isComplete ())
39133913 return declOrOffset;
@@ -4068,7 +4068,7 @@ class DeclDeserializer {
40684068 PrettySupplementalDeclNameTrace trace (paramName);
40694069
40704070 DeclContext *DC;
4071- UNWRAP ( MF.getDeclContextChecked (contextID), DC );
4071+ SET_OR_RETURN_ERROR (DC, MF.getDeclContextChecked (contextID));
40724072
40734073 if (declOrOffset.isComplete ())
40744074 return declOrOffset;
@@ -4290,7 +4290,7 @@ class DeclDeserializer {
42904290 }
42914291
42924292 DeclContext *DC;
4293- UNWRAP ( MF.getDeclContextChecked (contextID), DC );
4293+ SET_OR_RETURN_ERROR (DC, MF.getDeclContextChecked (contextID));
42944294
42954295 if (declOrOffset.isComplete ())
42964296 return declOrOffset;
@@ -4299,7 +4299,7 @@ class DeclDeserializer {
42994299 // reference generic parameters, and we want them to have a dummy
43004300 // DeclContext for now.
43014301 GenericParamList *genericParams;
4302- UNWRAP ( MF.maybeReadGenericParams (DC), genericParams );
4302+ SET_OR_RETURN_ERROR (genericParams, MF.maybeReadGenericParams (DC));
43034303
43044304 auto staticSpelling = getActualStaticSpellingKind (rawStaticSpelling);
43054305 if (!staticSpelling.has_value ())
@@ -4367,7 +4367,7 @@ class DeclDeserializer {
43674367 fn->setImplicitlyUnwrappedOptional (isIUO);
43684368
43694369 ParameterList *paramList;
4370- UNWRAP ( MF.readParameterList (), paramList );
4370+ SET_OR_RETURN_ERROR (paramList, MF.readParameterList ());
43714371 fn->setParameters (paramList);
43724372 auto numParams =
43734373 fn->hasImplicitSelfDecl () ? paramList->size () + 1 : paramList->size ();
@@ -4523,7 +4523,7 @@ class DeclDeserializer {
45234523 exportUnderlyingType);
45244524
45254525 DeclContext *declContext;
4526- UNWRAP ( MF.getDeclContextChecked (contextID), declContext );
4526+ SET_OR_RETURN_ERROR (declContext, MF.getDeclContextChecked (contextID));
45274527
45284528 auto interfaceSigOrErr = MF.getGenericSignatureChecked (interfaceSigID);
45294529 if (!interfaceSigOrErr)
@@ -4534,7 +4534,7 @@ class DeclDeserializer {
45344534 return cast<OpaqueTypeDecl>(declOrOffset.get ());
45354535
45364536 GenericParamList *genericParams;
4537- UNWRAP ( MF.maybeReadGenericParams (declContext), genericParams );
4537+ SET_OR_RETURN_ERROR (genericParams, MF.maybeReadGenericParams (declContext));
45384538
45394539 // Create the decl.
45404540 auto opaqueDecl = OpaqueTypeDecl::get (
@@ -4629,7 +4629,7 @@ class DeclDeserializer {
46294629 return MF.diagnoseFatal ();
46304630
46314631 DeclContext *dc;
4632- UNWRAP ( MF.getDeclContextChecked (contextID), dc );
4632+ SET_OR_RETURN_ERROR (dc, MF.getDeclContextChecked (contextID));
46334633
46344634 SmallVector<std::pair<Pattern *, DeclContextID>, 4 > patterns;
46354635 for (unsigned i = 0 ; i != numPatterns; ++i) {
@@ -4667,7 +4667,7 @@ class DeclDeserializer {
46674667 binding->setPattern (i, patterns[i].first );
46684668
46694669 DeclContext *dcPattern;
4670- UNWRAP ( MF.getDeclContextChecked (patterns[i].second ), dcPattern );
4670+ SET_OR_RETURN_ERROR (dcPattern, MF.getDeclContextChecked (patterns[i].second ));
46714671 if (dcPattern)
46724672 binding->setInitContext (i, cast<PatternBindingInitializer>(dcPattern));
46734673 }
@@ -4703,7 +4703,7 @@ class DeclDeserializer {
47034703 }
47044704
47054705 DeclContext *DC;
4706- UNWRAP ( MF.getDeclContextChecked (contextID), DC );
4706+ SET_OR_RETURN_ERROR (DC, MF.getDeclContextChecked (contextID));
47074707
47084708 if (declOrOffset.isComplete ())
47094709 return declOrOffset;
@@ -4736,7 +4736,7 @@ class DeclDeserializer {
47364736 ctx.AllocateCopy (inherited));
47374737
47384738 GenericParamList *genericParams;
4739- UNWRAP ( MF.maybeReadGenericParams (DC), genericParams );
4739+ SET_OR_RETURN_ERROR (genericParams, MF.maybeReadGenericParams (DC));
47404740 assert (genericParams && " protocol with no generic parameters?" );
47414741 ctx.evaluator .cacheOutput (GenericParamListRequest{proto},
47424742 std::move (genericParams));
@@ -4777,7 +4777,7 @@ class DeclDeserializer {
47774777 PrettySupplementalDeclNameTrace trace (name);
47784778
47794779 DeclContext *DC;
4780- UNWRAP ( MF.getDeclContextChecked (contextID), DC );
4780+ SET_OR_RETURN_ERROR (DC, MF.getDeclContextChecked (contextID));
47814781
47824782 auto result = MF.createDecl <OperatorDecl>(
47834783 DC, SourceLoc (), name, SourceLoc ());
@@ -4814,7 +4814,7 @@ class DeclDeserializer {
48144814 return precedenceGroup.takeError ();
48154815
48164816 DeclContext *DC;
4817- UNWRAP ( MF.getDeclContextChecked (contextID), DC );
4817+ SET_OR_RETURN_ERROR (DC, MF.getDeclContextChecked (contextID));
48184818
48194819 auto result = MF.createDecl <InfixOperatorDecl>(
48204820 DC, SourceLoc (), name, SourceLoc (), SourceLoc (), Identifier (),
@@ -4842,7 +4842,7 @@ class DeclDeserializer {
48424842 rawRelations);
48434843
48444844 DeclContext *DC;
4845- UNWRAP ( MF.getDeclContextChecked (contextID), DC );
4845+ SET_OR_RETURN_ERROR (DC, MF.getDeclContextChecked (contextID));
48464846
48474847 auto associativity = getActualAssociativity (rawAssociativity);
48484848 if (!associativity.has_value ())
@@ -4926,13 +4926,13 @@ class DeclDeserializer {
49264926 }
49274927
49284928 DeclContext *DC;
4929- UNWRAP ( MF.getDeclContextChecked (contextID), DC );
4929+ SET_OR_RETURN_ERROR (DC, MF.getDeclContextChecked (contextID));
49304930
49314931 if (declOrOffset.isComplete ())
49324932 return declOrOffset;
49334933
49344934 GenericParamList *genericParams;
4935- UNWRAP ( MF.maybeReadGenericParams (DC), genericParams );
4935+ SET_OR_RETURN_ERROR (genericParams, MF.maybeReadGenericParams (DC));
49364936 if (declOrOffset.isComplete ())
49374937 return declOrOffset;
49384938
@@ -5011,7 +5011,7 @@ class DeclDeserializer {
50115011 auto DC = DCOrError.get ();
50125012
50135013 GenericParamList *genericParams;
5014- UNWRAP ( MF.maybeReadGenericParams (DC), genericParams );
5014+ SET_OR_RETURN_ERROR (genericParams, MF.maybeReadGenericParams (DC));
50155015 if (declOrOffset.isComplete ())
50165016 return declOrOffset;
50175017
@@ -5091,7 +5091,7 @@ class DeclDeserializer {
50915091 }
50925092
50935093 DeclContext *DC;
5094- UNWRAP ( MF.getDeclContextChecked (contextID), DC );
5094+ SET_OR_RETURN_ERROR (DC, MF.getDeclContextChecked (contextID));
50955095
50965096 if (declOrOffset.isComplete ())
50975097 return declOrOffset;
@@ -5107,7 +5107,7 @@ class DeclDeserializer {
51075107 // Read payload parameter list, if it exists.
51085108 if (hasPayload) {
51095109 ParameterList *paramList;
5110- UNWRAP ( MF.readParameterList (), paramList );
5110+ SET_OR_RETURN_ERROR (paramList, MF.readParameterList ());
51115111 elem->setParameterList (paramList);
51125112 }
51135113
@@ -5201,13 +5201,13 @@ class DeclDeserializer {
52015201 }
52025202
52035203 DeclContext *parent;
5204- UNWRAP ( MF.getDeclContextChecked (contextID), parent );
5204+ SET_OR_RETURN_ERROR (parent, MF.getDeclContextChecked (contextID));
52055205
52065206 if (declOrOffset.isComplete ())
52075207 return declOrOffset;
52085208
52095209 GenericParamList *genericParams;
5210- UNWRAP ( MF.maybeReadGenericParams (parent), genericParams );
5210+ SET_OR_RETURN_ERROR (genericParams, MF.maybeReadGenericParams (parent));
52115211 if (declOrOffset.isComplete ())
52125212 return declOrOffset;
52135213
@@ -5228,7 +5228,7 @@ class DeclDeserializer {
52285228 subscript->setGenericSignature (MF.getGenericSignature (genericSigID));
52295229
52305230 ParameterList *paramList;
5231- UNWRAP ( MF.readParameterList (), paramList );
5231+ SET_OR_RETURN_ERROR (paramList, MF.readParameterList ());
52325232 subscript->setIndices (paramList);
52335233
52345234 MF.configureStorage (subscript, opaqueReadOwnership,
@@ -5259,7 +5259,8 @@ class DeclDeserializer {
52595259 OpaqueTypeDecl *opaqueDecl = nullptr ;
52605260 if (opaqueReturnTypeID) {
52615261 Decl *opaqueReturnType;
5262- UNWRAP (MF.getDeclChecked (opaqueReturnTypeID), opaqueReturnType);
5262+ SET_OR_RETURN_ERROR (opaqueReturnType,
5263+ MF.getDeclChecked (opaqueReturnTypeID));
52635264
52645265 opaqueDecl = cast<OpaqueTypeDecl>(opaqueReturnType);
52655266 }
@@ -5287,7 +5288,7 @@ class DeclDeserializer {
52875288 data);
52885289
52895290 DeclContext *DC;
5290- UNWRAP ( MF.getDeclContextChecked (contextID), DC );
5291+ SET_OR_RETURN_ERROR (DC, MF.getDeclContextChecked (contextID));
52915292
52925293 auto conformanceIDs = data.slice (0 , numConformances);
52935294 data = data.slice (numConformances);
@@ -5315,7 +5316,7 @@ class DeclDeserializer {
53155316 GenericParamList *outerParams = nullptr ;
53165317 while (true ) {
53175318 GenericParamList *genericParams;
5318- UNWRAP ( MF.maybeReadGenericParams (DC), genericParams );
5319+ SET_OR_RETURN_ERROR (genericParams, MF.maybeReadGenericParams (DC));
53195320 if (!genericParams)
53205321 break ;
53215322
@@ -5380,7 +5381,7 @@ class DeclDeserializer {
53805381 genericSigID);
53815382
53825383 DeclContext *DC;
5383- UNWRAP ( MF.getDeclContextChecked (contextID), DC );
5384+ SET_OR_RETURN_ERROR (DC, MF.getDeclContextChecked (contextID));
53845385
53855386 if (declOrOffset.isComplete ())
53865387 return declOrOffset;
@@ -5457,13 +5458,13 @@ class DeclDeserializer {
54575458 }
54585459
54595460 DeclContext *parent;
5460- UNWRAP ( MF.getDeclContextChecked (contextID), parent );
5461+ SET_OR_RETURN_ERROR (parent, MF.getDeclContextChecked (contextID));
54615462
54625463 if (declOrOffset.isComplete ())
54635464 return declOrOffset;
54645465
54655466 GenericParamList *genericParams;
5466- UNWRAP ( MF.maybeReadGenericParams (parent), genericParams );
5467+ SET_OR_RETURN_ERROR (genericParams, MF.maybeReadGenericParams (parent));
54675468 if (declOrOffset.isComplete ())
54685469 return declOrOffset;
54695470
@@ -5483,7 +5484,7 @@ class DeclDeserializer {
54835484 macro->resultType .setType (resultInterfaceType);
54845485
54855486 if (hasParameterList) {
5486- UNWRAP ( MF.readParameterList (), macro-> parameterList );
5487+ SET_OR_RETURN_ERROR (macro-> parameterList , MF.readParameterList ());
54875488 }
54885489
54895490 if (auto accessLevel = getActualAccessLevel (rawAccessLevel))
0 commit comments