@@ -194,6 +194,24 @@ bool PrintOptions::excludeAttr(const DeclAttribute *DA) const {
194
194
return false ;
195
195
}
196
196
197
+ // / Forces printing types with the `some` keyword, instead of the full stable
198
+ // / reference.
199
+ struct PrintWithOpaqueResultTypeKeywordRAII {
200
+ PrintWithOpaqueResultTypeKeywordRAII (PrintOptions &Options)
201
+ : Options(Options) {
202
+ SavedMode = Options.OpaqueReturnTypePrinting ;
203
+ Options.OpaqueReturnTypePrinting =
204
+ PrintOptions::OpaqueReturnTypePrintingMode::WithOpaqueKeyword;
205
+ }
206
+ ~PrintWithOpaqueResultTypeKeywordRAII () {
207
+ Options.OpaqueReturnTypePrinting = SavedMode;
208
+ }
209
+
210
+ private:
211
+ PrintOptions &Options;
212
+ PrintOptions::OpaqueReturnTypePrintingMode SavedMode;
213
+ };
214
+
197
215
PrintOptions PrintOptions::printSwiftInterfaceFile (ModuleDecl *ModuleToPrint,
198
216
bool preferTypeRepr,
199
217
bool printFullConvention,
@@ -941,19 +959,10 @@ class PrintAST : public ASTVisitor<PrintAST> {
941
959
printTypeLocWithOptions (TL, Options, printBeforeType);
942
960
}
943
961
944
- void printTypeLocForImplicitlyUnwrappedOptional (
945
- TypeLoc TL, bool IUO, const ValueDecl *opaqueTypeNamingDecl) {
946
- auto savedIOU = Options.PrintOptionalAsImplicitlyUnwrapped ;
947
- Options.PrintOptionalAsImplicitlyUnwrapped = IUO;
948
-
949
- auto savedOpaqueTypeNamingDecl = Options.OpaqueReturnTypeNamingDecl ;
950
- if (opaqueTypeNamingDecl)
951
- Options.OpaqueReturnTypeNamingDecl = opaqueTypeNamingDecl;
952
-
953
- printTypeLocWithOptions (TL, Options);
954
-
955
- Options.PrintOptionalAsImplicitlyUnwrapped = savedIOU;
956
- Options.OpaqueReturnTypeNamingDecl = savedOpaqueTypeNamingDecl;
962
+ void printTypeLocForImplicitlyUnwrappedOptional (TypeLoc TL, bool IUO) {
963
+ PrintOptions options = Options;
964
+ options.PrintOptionalAsImplicitlyUnwrapped = IUO;
965
+ printTypeLocWithOptions (TL, options);
957
966
}
958
967
959
968
void printContextIfNeeded (const Decl *decl) {
@@ -1357,17 +1366,18 @@ void PrintAST::printTypedPattern(const TypedPattern *TP) {
1357
1366
printPattern (TP->getSubPattern ());
1358
1367
Printer << " : " ;
1359
1368
1360
- VarDecl *varDecl = nullptr ;
1369
+ PrintWithOpaqueResultTypeKeywordRAII x (Options);
1370
+
1371
+ // Make sure to check if the underlying var decl is an implicitly unwrapped
1372
+ // optional.
1373
+ bool isIUO = false ;
1361
1374
if (auto *named = dyn_cast<NamedPattern>(TP->getSubPattern ()))
1362
1375
if (auto decl = named->getDecl ())
1363
- varDecl = decl;
1376
+ isIUO = decl-> isImplicitlyUnwrappedOptional () ;
1364
1377
1365
1378
const auto TyLoc = TypeLoc (TP->getTypeRepr (),
1366
1379
TP->hasType () ? TP->getType () : Type ());
1367
-
1368
- printTypeLocForImplicitlyUnwrappedOptional (
1369
- TyLoc, varDecl ? varDecl->isImplicitlyUnwrappedOptional () : false ,
1370
- varDecl);
1380
+ printTypeLocForImplicitlyUnwrappedOptional (TyLoc, isIUO);
1371
1381
}
1372
1382
1373
1383
// / Determines if we are required to print the name of a property declaration,
@@ -3789,8 +3799,9 @@ void PrintAST::visitVarDecl(VarDecl *decl) {
3789
3799
}
3790
3800
Printer.printDeclResultTypePre (decl, tyLoc);
3791
3801
3802
+ PrintWithOpaqueResultTypeKeywordRAII x (Options);
3792
3803
printTypeLocForImplicitlyUnwrappedOptional (
3793
- tyLoc, decl->isImplicitlyUnwrappedOptional (), decl );
3804
+ tyLoc, decl->isImplicitlyUnwrappedOptional ());
3794
3805
}
3795
3806
3796
3807
printAccessors (decl);
@@ -3872,7 +3883,7 @@ void PrintAST::printOneParameter(const ParamDecl *param,
3872
3883
}
3873
3884
3874
3885
printTypeLocForImplicitlyUnwrappedOptional (
3875
- TheTypeLoc, param->isImplicitlyUnwrappedOptional (), nullptr );
3886
+ TheTypeLoc, param->isImplicitlyUnwrappedOptional ());
3876
3887
}
3877
3888
3878
3889
if (param->isDefaultArgument () && Options.PrintDefaultArgumentValue ) {
@@ -4162,6 +4173,8 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
4162
4173
}
4163
4174
}
4164
4175
4176
+ PrintWithOpaqueResultTypeKeywordRAII x (Options);
4177
+
4165
4178
// Check if we would go down the type repr path... in such a case, see if
4166
4179
// we can find a type repr and if that type has a sending type repr. In
4167
4180
// such a case, look through the sending type repr since we handle it here
@@ -4188,7 +4201,7 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
4188
4201
// If we printed using type repr printing, do not print again.
4189
4202
if (!usedTypeReprPrinting) {
4190
4203
printTypeLocForImplicitlyUnwrappedOptional (
4191
- ResultTyLoc, decl->isImplicitlyUnwrappedOptional (), decl );
4204
+ ResultTyLoc, decl->isImplicitlyUnwrappedOptional ());
4192
4205
}
4193
4206
Printer.printStructurePost (PrintStructureKind::FunctionReturnType);
4194
4207
}
@@ -4337,8 +4350,9 @@ void PrintAST::visitSubscriptDecl(SubscriptDecl *decl) {
4337
4350
Printer.printDeclResultTypePre (decl, elementTy);
4338
4351
Printer.callPrintStructurePre (PrintStructureKind::FunctionReturnType);
4339
4352
4353
+ PrintWithOpaqueResultTypeKeywordRAII x (Options);
4340
4354
printTypeLocForImplicitlyUnwrappedOptional (
4341
- elementTy, decl->isImplicitlyUnwrappedOptional (), decl );
4355
+ elementTy, decl->isImplicitlyUnwrappedOptional ());
4342
4356
Printer.printStructurePost (PrintStructureKind::FunctionReturnType);
4343
4357
}
4344
4358
@@ -7151,11 +7165,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
7151
7165
auto genericSig = namingDecl->getInnermostDeclContext ()
7152
7166
->getGenericSignatureOfContext ();
7153
7167
7154
- auto mode = Options.OpaqueReturnTypePrinting ;
7155
- if (Options.OpaqueReturnTypeNamingDecl == T->getDecl ()->getNamingDecl ())
7156
- mode = PrintOptions::OpaqueReturnTypePrintingMode::WithOpaqueKeyword;
7157
-
7158
- switch (mode) {
7168
+ switch (Options.OpaqueReturnTypePrinting ) {
7159
7169
case PrintOptions::OpaqueReturnTypePrintingMode::WithOpaqueKeyword:
7160
7170
if (printNamedOpaque ())
7161
7171
return ;
0 commit comments