Skip to content

Commit 2788c1d

Browse files
committed
AST: Introduce PrintWithOpaqueResultTypeKeywordRAII utility.
1 parent 2fd182a commit 2788c1d

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
185203
PrintOptions PrintOptions::printSwiftInterfaceFile(ModuleDecl *ModuleToPrint,
186204
bool preferTypeRepr,
187205
bool printFullConvention,
@@ -3745,13 +3763,7 @@ void PrintAST::visitVarDecl(VarDecl *decl) {
37453763
}
37463764
Printer.printDeclResultTypePre(decl, tyLoc);
37473765

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-
3766+
PrintWithOpaqueResultTypeKeywordRAII x(Options);
37553767
printTypeLocForImplicitlyUnwrappedOptional(
37563768
tyLoc, decl->isImplicitlyUnwrappedOptional());
37573769
}
@@ -4127,13 +4139,7 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
41274139
}
41284140
}
41294141

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-
4142+
PrintWithOpaqueResultTypeKeywordRAII x(Options);
41374143
printTypeLocForImplicitlyUnwrappedOptional(
41384144
ResultTyLoc, decl->isImplicitlyUnwrappedOptional());
41394145
Printer.printStructurePost(PrintStructureKind::FunctionReturnType);
@@ -4283,13 +4289,7 @@ void PrintAST::visitSubscriptDecl(SubscriptDecl *decl) {
42834289
Printer.printDeclResultTypePre(decl, elementTy);
42844290
Printer.callPrintStructurePre(PrintStructureKind::FunctionReturnType);
42854291

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-
4292+
PrintWithOpaqueResultTypeKeywordRAII x(Options);
42934293
printTypeLocForImplicitlyUnwrappedOptional(
42944294
elementTy, decl->isImplicitlyUnwrappedOptional());
42954295
Printer.printStructurePost(PrintStructureKind::FunctionReturnType);

0 commit comments

Comments
 (0)