@@ -182,6 +182,24 @@ static bool shouldPrintAllSemanticDetails(const PrintOptions &options) {
182182 return false ;
183183}
184184
185+ // / Forces printing types with the `some` keyword, instead of the full stable
186+ // / reference.
187+ struct PrintWithOpaqueResultTypeKeywordRAII {
188+ PrintWithOpaqueResultTypeKeywordRAII (PrintOptions &Options)
189+ : Options(Options) {
190+ SavedMode = Options.OpaqueReturnTypePrinting ;
191+ Options.OpaqueReturnTypePrinting =
192+ PrintOptions::OpaqueReturnTypePrintingMode::WithOpaqueKeyword;
193+ }
194+ ~PrintWithOpaqueResultTypeKeywordRAII () {
195+ Options.OpaqueReturnTypePrinting = SavedMode;
196+ }
197+
198+ private:
199+ PrintOptions &Options;
200+ PrintOptions::OpaqueReturnTypePrintingMode SavedMode;
201+ };
202+
185203PrintOptions PrintOptions::printSwiftInterfaceFile (ModuleDecl *ModuleToPrint,
186204 bool preferTypeRepr,
187205 bool printFullConvention,
@@ -1317,6 +1335,8 @@ void PrintAST::printTypedPattern(const TypedPattern *TP) {
13171335 printPattern (TP->getSubPattern ());
13181336 Printer << " : " ;
13191337
1338+ PrintWithOpaqueResultTypeKeywordRAII x (Options);
1339+
13201340 // Make sure to check if the underlying var decl is an implicitly unwrapped
13211341 // optional.
13221342 bool isIUO = false ;
@@ -3745,13 +3765,7 @@ void PrintAST::visitVarDecl(VarDecl *decl) {
37453765 }
37463766 Printer.printDeclResultTypePre (decl, tyLoc);
37473767
3748- // HACK: When printing result types for vars with opaque result types,
3749- // always print them using the `some` keyword instead of printing
3750- // the full stable reference.
3751- llvm::SaveAndRestore<PrintOptions::OpaqueReturnTypePrintingMode>
3752- x (Options.OpaqueReturnTypePrinting ,
3753- PrintOptions::OpaqueReturnTypePrintingMode::WithOpaqueKeyword);
3754-
3768+ PrintWithOpaqueResultTypeKeywordRAII x (Options);
37553769 printTypeLocForImplicitlyUnwrappedOptional (
37563770 tyLoc, decl->isImplicitlyUnwrappedOptional ());
37573771 }
@@ -4127,13 +4141,7 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
41274141 }
41284142 }
41294143
4130- // HACK: When printing result types for funcs with opaque result types,
4131- // always print them using the `some` keyword instead of printing
4132- // the full stable reference.
4133- llvm::SaveAndRestore<PrintOptions::OpaqueReturnTypePrintingMode>
4134- x (Options.OpaqueReturnTypePrinting ,
4135- PrintOptions::OpaqueReturnTypePrintingMode::WithOpaqueKeyword);
4136-
4144+ PrintWithOpaqueResultTypeKeywordRAII x (Options);
41374145 printTypeLocForImplicitlyUnwrappedOptional (
41384146 ResultTyLoc, decl->isImplicitlyUnwrappedOptional ());
41394147 Printer.printStructurePost (PrintStructureKind::FunctionReturnType);
@@ -4283,13 +4291,7 @@ void PrintAST::visitSubscriptDecl(SubscriptDecl *decl) {
42834291 Printer.printDeclResultTypePre (decl, elementTy);
42844292 Printer.callPrintStructurePre (PrintStructureKind::FunctionReturnType);
42854293
4286- // HACK: When printing result types for subscripts with opaque result types,
4287- // always print them using the `some` keyword instead of printing
4288- // the full stable reference.
4289- llvm::SaveAndRestore<PrintOptions::OpaqueReturnTypePrintingMode>
4290- x (Options.OpaqueReturnTypePrinting ,
4291- PrintOptions::OpaqueReturnTypePrintingMode::WithOpaqueKeyword);
4292-
4294+ PrintWithOpaqueResultTypeKeywordRAII x (Options);
42934295 printTypeLocForImplicitlyUnwrappedOptional (
42944296 elementTy, decl->isImplicitlyUnwrappedOptional ());
42954297 Printer.printStructurePost (PrintStructureKind::FunctionReturnType);
0 commit comments