@@ -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) {
@@ -1390,17 +1399,18 @@ void PrintAST::printTypedPattern(const TypedPattern *TP) {
13901399 printPattern (TP->getSubPattern ());
13911400 Printer << " : " ;
13921401
1393- VarDecl *varDecl = nullptr ;
1402+ PrintWithOpaqueResultTypeKeywordRAII x (Options);
1403+
1404+ // Make sure to check if the underlying var decl is an implicitly unwrapped
1405+ // optional.
1406+ bool isIUO = false ;
13941407 if (auto *named = dyn_cast<NamedPattern>(TP->getSubPattern ()))
13951408 if (auto decl = named->getDecl ())
1396- varDecl = decl;
1409+ isIUO = decl-> isImplicitlyUnwrappedOptional () ;
13971410
13981411 const auto TyLoc = TypeLoc (TP->getTypeRepr (),
13991412 TP->hasType () ? TP->getType () : Type ());
1400-
1401- printTypeLocForImplicitlyUnwrappedOptional (
1402- TyLoc, varDecl ? varDecl->isImplicitlyUnwrappedOptional () : false ,
1403- varDecl);
1413+ printTypeLocForImplicitlyUnwrappedOptional (TyLoc, isIUO);
14041414}
14051415
14061416// / Determines if we are required to print the name of a property declaration,
@@ -3854,8 +3864,9 @@ void PrintAST::visitVarDecl(VarDecl *decl) {
38543864 }
38553865 Printer.printDeclResultTypePre (decl, tyLoc);
38563866
3867+ PrintWithOpaqueResultTypeKeywordRAII x (Options);
38573868 printTypeLocForImplicitlyUnwrappedOptional (
3858- tyLoc, decl->isImplicitlyUnwrappedOptional (), decl );
3869+ tyLoc, decl->isImplicitlyUnwrappedOptional ());
38593870 }
38603871
38613872 printAccessors (decl);
@@ -3937,7 +3948,7 @@ void PrintAST::printOneParameter(const ParamDecl *param,
39373948 }
39383949
39393950 printTypeLocForImplicitlyUnwrappedOptional (
3940- TheTypeLoc, param->isImplicitlyUnwrappedOptional (), nullptr );
3951+ TheTypeLoc, param->isImplicitlyUnwrappedOptional ());
39413952 }
39423953
39433954 if (param->isDefaultArgument () && Options.PrintDefaultArgumentValue ) {
@@ -4229,6 +4240,8 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
42294240 }
42304241 }
42314242
4243+ PrintWithOpaqueResultTypeKeywordRAII x (Options);
4244+
42324245 // Check if we would go down the type repr path... in such a case, see if
42334246 // we can find a type repr and if that type has a sending type repr. In
42344247 // such a case, look through the sending type repr since we handle it here
@@ -4255,7 +4268,7 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
42554268 // If we printed using type repr printing, do not print again.
42564269 if (!usedTypeReprPrinting) {
42574270 printTypeLocForImplicitlyUnwrappedOptional (
4258- ResultTyLoc, decl->isImplicitlyUnwrappedOptional (), decl );
4271+ ResultTyLoc, decl->isImplicitlyUnwrappedOptional ());
42594272 }
42604273 Printer.printStructurePost (PrintStructureKind::FunctionReturnType);
42614274 }
@@ -4404,8 +4417,9 @@ void PrintAST::visitSubscriptDecl(SubscriptDecl *decl) {
44044417 Printer.printDeclResultTypePre (decl, elementTy);
44054418 Printer.callPrintStructurePre (PrintStructureKind::FunctionReturnType);
44064419
4420+ PrintWithOpaqueResultTypeKeywordRAII x (Options);
44074421 printTypeLocForImplicitlyUnwrappedOptional (
4408- elementTy, decl->isImplicitlyUnwrappedOptional (), decl );
4422+ elementTy, decl->isImplicitlyUnwrappedOptional ());
44094423 Printer.printStructurePost (PrintStructureKind::FunctionReturnType);
44104424 }
44114425
@@ -7223,11 +7237,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
72237237 auto genericSig = namingDecl->getInnermostDeclContext ()
72247238 ->getGenericSignatureOfContext ();
72257239
7226- auto mode = Options.OpaqueReturnTypePrinting ;
7227- if (Options.OpaqueReturnTypeNamingDecl == T->getDecl ()->getNamingDecl ())
7228- mode = PrintOptions::OpaqueReturnTypePrintingMode::WithOpaqueKeyword;
7229-
7230- switch (mode) {
7240+ switch (Options.OpaqueReturnTypePrinting ) {
72317241 case PrintOptions::OpaqueReturnTypePrintingMode::WithOpaqueKeyword:
72327242 if (printNamedOpaque ())
72337243 return ;
0 commit comments