@@ -194,24 +194,6 @@ 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
-
215
197
PrintOptions PrintOptions::printSwiftInterfaceFile (ModuleDecl *ModuleToPrint,
216
198
bool preferTypeRepr,
217
199
bool printFullConvention,
@@ -969,10 +951,19 @@ class PrintAST : public ASTVisitor<PrintAST> {
969
951
printTypeLocWithOptions (TL, Options, printBeforeType);
970
952
}
971
953
972
- void printTypeLocForImplicitlyUnwrappedOptional (TypeLoc TL, bool IUO) {
973
- PrintOptions options = Options;
974
- options.PrintOptionalAsImplicitlyUnwrapped = IUO;
975
- printTypeLocWithOptions (TL, options);
954
+ void printTypeLocForImplicitlyUnwrappedOptional (
955
+ TypeLoc TL, bool IUO, const ValueDecl *opaqueTypeNamingDecl) {
956
+ auto savedIOU = Options.PrintOptionalAsImplicitlyUnwrapped ;
957
+ Options.PrintOptionalAsImplicitlyUnwrapped = IUO;
958
+
959
+ auto savedOpaqueTypeNamingDecl = Options.OpaqueReturnTypeNamingDecl ;
960
+ if (opaqueTypeNamingDecl)
961
+ Options.OpaqueReturnTypeNamingDecl = opaqueTypeNamingDecl;
962
+
963
+ printTypeLocWithOptions (TL, Options);
964
+
965
+ Options.PrintOptionalAsImplicitlyUnwrapped = savedIOU;
966
+ Options.OpaqueReturnTypeNamingDecl = savedOpaqueTypeNamingDecl;
976
967
}
977
968
978
969
void printContextIfNeeded (const Decl *decl) {
@@ -1376,18 +1367,17 @@ void PrintAST::printTypedPattern(const TypedPattern *TP) {
1376
1367
printPattern (TP->getSubPattern ());
1377
1368
Printer << " : " ;
1378
1369
1379
- PrintWithOpaqueResultTypeKeywordRAII x (Options);
1380
-
1381
- // Make sure to check if the underlying var decl is an implicitly unwrapped
1382
- // optional.
1383
- bool isIUO = false ;
1370
+ VarDecl *varDecl = nullptr ;
1384
1371
if (auto *named = dyn_cast<NamedPattern>(TP->getSubPattern ()))
1385
1372
if (auto decl = named->getDecl ())
1386
- isIUO = decl-> isImplicitlyUnwrappedOptional () ;
1373
+ varDecl = decl;
1387
1374
1388
1375
const auto TyLoc = TypeLoc (TP->getTypeRepr (),
1389
1376
TP->hasType () ? TP->getType () : Type ());
1390
- printTypeLocForImplicitlyUnwrappedOptional (TyLoc, isIUO);
1377
+
1378
+ printTypeLocForImplicitlyUnwrappedOptional (
1379
+ TyLoc, varDecl ? varDecl->isImplicitlyUnwrappedOptional () : false ,
1380
+ varDecl);
1391
1381
}
1392
1382
1393
1383
// / Determines if we are required to print the name of a property declaration,
@@ -3882,9 +3872,8 @@ void PrintAST::visitVarDecl(VarDecl *decl) {
3882
3872
}
3883
3873
Printer.printDeclResultTypePre (decl, tyLoc);
3884
3874
3885
- PrintWithOpaqueResultTypeKeywordRAII x (Options);
3886
3875
printTypeLocForImplicitlyUnwrappedOptional (
3887
- tyLoc, decl->isImplicitlyUnwrappedOptional ());
3876
+ tyLoc, decl->isImplicitlyUnwrappedOptional (), decl );
3888
3877
}
3889
3878
3890
3879
printAccessors (decl);
@@ -3966,7 +3955,7 @@ void PrintAST::printOneParameter(const ParamDecl *param,
3966
3955
}
3967
3956
3968
3957
printTypeLocForImplicitlyUnwrappedOptional (
3969
- TheTypeLoc, param->isImplicitlyUnwrappedOptional ());
3958
+ TheTypeLoc, param->isImplicitlyUnwrappedOptional (), nullptr );
3970
3959
}
3971
3960
3972
3961
if (param->isDefaultArgument () && Options.PrintDefaultArgumentValue ) {
@@ -4256,8 +4245,6 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
4256
4245
}
4257
4246
}
4258
4247
4259
- PrintWithOpaqueResultTypeKeywordRAII x (Options);
4260
-
4261
4248
// Check if we would go down the type repr path... in such a case, see if
4262
4249
// we can find a type repr and if that type has a sending type repr. In
4263
4250
// such a case, look through the sending type repr since we handle it here
@@ -4284,7 +4271,7 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
4284
4271
// If we printed using type repr printing, do not print again.
4285
4272
if (!usedTypeReprPrinting) {
4286
4273
printTypeLocForImplicitlyUnwrappedOptional (
4287
- ResultTyLoc, decl->isImplicitlyUnwrappedOptional ());
4274
+ ResultTyLoc, decl->isImplicitlyUnwrappedOptional (), decl );
4288
4275
}
4289
4276
Printer.printStructurePost (PrintStructureKind::FunctionReturnType);
4290
4277
}
@@ -4433,9 +4420,8 @@ void PrintAST::visitSubscriptDecl(SubscriptDecl *decl) {
4433
4420
Printer.printDeclResultTypePre (decl, elementTy);
4434
4421
Printer.callPrintStructurePre (PrintStructureKind::FunctionReturnType);
4435
4422
4436
- PrintWithOpaqueResultTypeKeywordRAII x (Options);
4437
4423
printTypeLocForImplicitlyUnwrappedOptional (
4438
- elementTy, decl->isImplicitlyUnwrappedOptional ());
4424
+ elementTy, decl->isImplicitlyUnwrappedOptional (), decl );
4439
4425
Printer.printStructurePost (PrintStructureKind::FunctionReturnType);
4440
4426
}
4441
4427
@@ -7241,7 +7227,11 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
7241
7227
auto genericSig = namingDecl->getInnermostDeclContext ()
7242
7228
->getGenericSignatureOfContext ();
7243
7229
7244
- switch (Options.OpaqueReturnTypePrinting ) {
7230
+ auto mode = Options.OpaqueReturnTypePrinting ;
7231
+ if (Options.OpaqueReturnTypeNamingDecl == T->getDecl ()->getNamingDecl ())
7232
+ mode = PrintOptions::OpaqueReturnTypePrintingMode::WithOpaqueKeyword;
7233
+
7234
+ switch (mode) {
7245
7235
case PrintOptions::OpaqueReturnTypePrintingMode::WithOpaqueKeyword:
7246
7236
if (printNamedOpaque ())
7247
7237
return ;
0 commit comments