@@ -194,24 +194,6 @@ bool PrintOptions::excludeAttr(const DeclAttribute *DA) const {
194194 return false ;
195195}
196196
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-
215197PrintOptions PrintOptions::printSwiftInterfaceFile (ModuleDecl *ModuleToPrint,
216198 bool preferTypeRepr,
217199 bool printFullConvention,
@@ -969,10 +951,19 @@ class PrintAST : public ASTVisitor<PrintAST> {
969951 printTypeLocWithOptions (TL, Options, printBeforeType);
970952 }
971953
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;
976967 }
977968
978969 void printContextIfNeeded (const Decl *decl) {
@@ -1376,18 +1367,17 @@ void PrintAST::printTypedPattern(const TypedPattern *TP) {
13761367 printPattern (TP->getSubPattern ());
13771368 Printer << " : " ;
13781369
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 ;
13841371 if (auto *named = dyn_cast<NamedPattern>(TP->getSubPattern ()))
13851372 if (auto decl = named->getDecl ())
1386- isIUO = decl-> isImplicitlyUnwrappedOptional () ;
1373+ varDecl = decl;
13871374
13881375 const auto TyLoc = TypeLoc (TP->getTypeRepr (),
13891376 TP->hasType () ? TP->getType () : Type ());
1390- printTypeLocForImplicitlyUnwrappedOptional (TyLoc, isIUO);
1377+
1378+ printTypeLocForImplicitlyUnwrappedOptional (
1379+ TyLoc, varDecl ? varDecl->isImplicitlyUnwrappedOptional () : false ,
1380+ varDecl);
13911381}
13921382
13931383// / Determines if we are required to print the name of a property declaration,
@@ -3882,9 +3872,8 @@ void PrintAST::visitVarDecl(VarDecl *decl) {
38823872 }
38833873 Printer.printDeclResultTypePre (decl, tyLoc);
38843874
3885- PrintWithOpaqueResultTypeKeywordRAII x (Options);
38863875 printTypeLocForImplicitlyUnwrappedOptional (
3887- tyLoc, decl->isImplicitlyUnwrappedOptional ());
3876+ tyLoc, decl->isImplicitlyUnwrappedOptional (), decl );
38883877 }
38893878
38903879 printAccessors (decl);
@@ -3966,7 +3955,7 @@ void PrintAST::printOneParameter(const ParamDecl *param,
39663955 }
39673956
39683957 printTypeLocForImplicitlyUnwrappedOptional (
3969- TheTypeLoc, param->isImplicitlyUnwrappedOptional ());
3958+ TheTypeLoc, param->isImplicitlyUnwrappedOptional (), nullptr );
39703959 }
39713960
39723961 if (param->isDefaultArgument () && Options.PrintDefaultArgumentValue ) {
@@ -4256,8 +4245,6 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
42564245 }
42574246 }
42584247
4259- PrintWithOpaqueResultTypeKeywordRAII x (Options);
4260-
42614248 // Check if we would go down the type repr path... in such a case, see if
42624249 // we can find a type repr and if that type has a sending type repr. In
42634250 // such a case, look through the sending type repr since we handle it here
@@ -4284,7 +4271,7 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
42844271 // If we printed using type repr printing, do not print again.
42854272 if (!usedTypeReprPrinting) {
42864273 printTypeLocForImplicitlyUnwrappedOptional (
4287- ResultTyLoc, decl->isImplicitlyUnwrappedOptional ());
4274+ ResultTyLoc, decl->isImplicitlyUnwrappedOptional (), decl );
42884275 }
42894276 Printer.printStructurePost (PrintStructureKind::FunctionReturnType);
42904277 }
@@ -4433,9 +4420,8 @@ void PrintAST::visitSubscriptDecl(SubscriptDecl *decl) {
44334420 Printer.printDeclResultTypePre (decl, elementTy);
44344421 Printer.callPrintStructurePre (PrintStructureKind::FunctionReturnType);
44354422
4436- PrintWithOpaqueResultTypeKeywordRAII x (Options);
44374423 printTypeLocForImplicitlyUnwrappedOptional (
4438- elementTy, decl->isImplicitlyUnwrappedOptional ());
4424+ elementTy, decl->isImplicitlyUnwrappedOptional (), decl );
44394425 Printer.printStructurePost (PrintStructureKind::FunctionReturnType);
44404426 }
44414427
@@ -7245,7 +7231,11 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
72457231 auto genericSig = namingDecl->getInnermostDeclContext ()
72467232 ->getGenericSignatureOfContext ();
72477233
7248- switch (Options.OpaqueReturnTypePrinting ) {
7234+ auto mode = Options.OpaqueReturnTypePrinting ;
7235+ if (Options.OpaqueReturnTypeNamingDecl == T->getDecl ()->getNamingDecl ())
7236+ mode = PrintOptions::OpaqueReturnTypePrintingMode::WithOpaqueKeyword;
7237+
7238+ switch (mode) {
72497239 case PrintOptions::OpaqueReturnTypePrintingMode::WithOpaqueKeyword:
72507240 if (printNamedOpaque ())
72517241 return ;
0 commit comments