@@ -194,6 +194,24 @@ 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+
197215PrintOptions PrintOptions::printSwiftInterfaceFile (ModuleDecl *ModuleToPrint,
198216 bool preferTypeRepr,
199217 bool printFullConvention,
@@ -941,19 +959,10 @@ class PrintAST : public ASTVisitor<PrintAST> {
941959 printTypeLocWithOptions (TL, Options, printBeforeType);
942960 }
943961
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);
957966 }
958967
959968 void printContextIfNeeded (const Decl *decl) {
@@ -1357,17 +1366,18 @@ void PrintAST::printTypedPattern(const TypedPattern *TP) {
13571366 printPattern (TP->getSubPattern ());
13581367 Printer << " : " ;
13591368
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 ;
13611374 if (auto *named = dyn_cast<NamedPattern>(TP->getSubPattern ()))
13621375 if (auto decl = named->getDecl ())
1363- varDecl = decl;
1376+ isIUO = decl-> isImplicitlyUnwrappedOptional () ;
13641377
13651378 const auto TyLoc = TypeLoc (TP->getTypeRepr (),
13661379 TP->hasType () ? TP->getType () : Type ());
1367-
1368- printTypeLocForImplicitlyUnwrappedOptional (
1369- TyLoc, varDecl ? varDecl->isImplicitlyUnwrappedOptional () : false ,
1370- varDecl);
1380+ printTypeLocForImplicitlyUnwrappedOptional (TyLoc, isIUO);
13711381}
13721382
13731383// / Determines if we are required to print the name of a property declaration,
@@ -3789,8 +3799,9 @@ void PrintAST::visitVarDecl(VarDecl *decl) {
37893799 }
37903800 Printer.printDeclResultTypePre (decl, tyLoc);
37913801
3802+ PrintWithOpaqueResultTypeKeywordRAII x (Options);
37923803 printTypeLocForImplicitlyUnwrappedOptional (
3793- tyLoc, decl->isImplicitlyUnwrappedOptional (), decl );
3804+ tyLoc, decl->isImplicitlyUnwrappedOptional ());
37943805 }
37953806
37963807 printAccessors (decl);
@@ -3872,7 +3883,7 @@ void PrintAST::printOneParameter(const ParamDecl *param,
38723883 }
38733884
38743885 printTypeLocForImplicitlyUnwrappedOptional (
3875- TheTypeLoc, param->isImplicitlyUnwrappedOptional (), nullptr );
3886+ TheTypeLoc, param->isImplicitlyUnwrappedOptional ());
38763887 }
38773888
38783889 if (param->isDefaultArgument () && Options.PrintDefaultArgumentValue ) {
@@ -4162,6 +4173,8 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
41624173 }
41634174 }
41644175
4176+ PrintWithOpaqueResultTypeKeywordRAII x (Options);
4177+
41654178 // Check if we would go down the type repr path... in such a case, see if
41664179 // we can find a type repr and if that type has a sending type repr. In
41674180 // such a case, look through the sending type repr since we handle it here
@@ -4188,7 +4201,7 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
41884201 // If we printed using type repr printing, do not print again.
41894202 if (!usedTypeReprPrinting) {
41904203 printTypeLocForImplicitlyUnwrappedOptional (
4191- ResultTyLoc, decl->isImplicitlyUnwrappedOptional (), decl );
4204+ ResultTyLoc, decl->isImplicitlyUnwrappedOptional ());
41924205 }
41934206 Printer.printStructurePost (PrintStructureKind::FunctionReturnType);
41944207 }
@@ -4337,8 +4350,9 @@ void PrintAST::visitSubscriptDecl(SubscriptDecl *decl) {
43374350 Printer.printDeclResultTypePre (decl, elementTy);
43384351 Printer.callPrintStructurePre (PrintStructureKind::FunctionReturnType);
43394352
4353+ PrintWithOpaqueResultTypeKeywordRAII x (Options);
43404354 printTypeLocForImplicitlyUnwrappedOptional (
4341- elementTy, decl->isImplicitlyUnwrappedOptional (), decl );
4355+ elementTy, decl->isImplicitlyUnwrappedOptional ());
43424356 Printer.printStructurePost (PrintStructureKind::FunctionReturnType);
43434357 }
43444358
@@ -7151,11 +7165,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
71517165 auto genericSig = namingDecl->getInnermostDeclContext ()
71527166 ->getGenericSignatureOfContext ();
71537167
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 ) {
71597169 case PrintOptions::OpaqueReturnTypePrintingMode::WithOpaqueKeyword:
71607170 if (printNamedOpaque ())
71617171 return ;
0 commit comments