@@ -194,6 +194,24 @@ bool PrintOptions::excludeAttr(const DeclAttribute *DA) const {
194
194
return false ;
195
195
}
196
196
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
+
197
215
PrintOptions PrintOptions::printSwiftInterfaceFile (ModuleDecl *ModuleToPrint,
198
216
bool preferTypeRepr,
199
217
bool printFullConvention,
@@ -941,19 +959,10 @@ class PrintAST : public ASTVisitor<PrintAST> {
941
959
printTypeLocWithOptions (TL, Options, printBeforeType);
942
960
}
943
961
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);
957
966
}
958
967
959
968
void printContextIfNeeded (const Decl *decl) {
@@ -1390,17 +1399,18 @@ void PrintAST::printTypedPattern(const TypedPattern *TP) {
1390
1399
printPattern (TP->getSubPattern ());
1391
1400
Printer << " : " ;
1392
1401
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 ;
1394
1407
if (auto *named = dyn_cast<NamedPattern>(TP->getSubPattern ()))
1395
1408
if (auto decl = named->getDecl ())
1396
- varDecl = decl;
1409
+ isIUO = decl-> isImplicitlyUnwrappedOptional () ;
1397
1410
1398
1411
const auto TyLoc = TypeLoc (TP->getTypeRepr (),
1399
1412
TP->hasType () ? TP->getType () : Type ());
1400
-
1401
- printTypeLocForImplicitlyUnwrappedOptional (
1402
- TyLoc, varDecl ? varDecl->isImplicitlyUnwrappedOptional () : false ,
1403
- varDecl);
1413
+ printTypeLocForImplicitlyUnwrappedOptional (TyLoc, isIUO);
1404
1414
}
1405
1415
1406
1416
// / Determines if we are required to print the name of a property declaration,
@@ -3854,8 +3864,9 @@ void PrintAST::visitVarDecl(VarDecl *decl) {
3854
3864
}
3855
3865
Printer.printDeclResultTypePre (decl, tyLoc);
3856
3866
3867
+ PrintWithOpaqueResultTypeKeywordRAII x (Options);
3857
3868
printTypeLocForImplicitlyUnwrappedOptional (
3858
- tyLoc, decl->isImplicitlyUnwrappedOptional (), decl );
3869
+ tyLoc, decl->isImplicitlyUnwrappedOptional ());
3859
3870
}
3860
3871
3861
3872
printAccessors (decl);
@@ -3937,7 +3948,7 @@ void PrintAST::printOneParameter(const ParamDecl *param,
3937
3948
}
3938
3949
3939
3950
printTypeLocForImplicitlyUnwrappedOptional (
3940
- TheTypeLoc, param->isImplicitlyUnwrappedOptional (), nullptr );
3951
+ TheTypeLoc, param->isImplicitlyUnwrappedOptional ());
3941
3952
}
3942
3953
3943
3954
if (param->isDefaultArgument () && Options.PrintDefaultArgumentValue ) {
@@ -4229,6 +4240,8 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
4229
4240
}
4230
4241
}
4231
4242
4243
+ PrintWithOpaqueResultTypeKeywordRAII x (Options);
4244
+
4232
4245
// Check if we would go down the type repr path... in such a case, see if
4233
4246
// we can find a type repr and if that type has a sending type repr. In
4234
4247
// such a case, look through the sending type repr since we handle it here
@@ -4255,7 +4268,7 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
4255
4268
// If we printed using type repr printing, do not print again.
4256
4269
if (!usedTypeReprPrinting) {
4257
4270
printTypeLocForImplicitlyUnwrappedOptional (
4258
- ResultTyLoc, decl->isImplicitlyUnwrappedOptional (), decl );
4271
+ ResultTyLoc, decl->isImplicitlyUnwrappedOptional ());
4259
4272
}
4260
4273
Printer.printStructurePost (PrintStructureKind::FunctionReturnType);
4261
4274
}
@@ -4404,8 +4417,9 @@ void PrintAST::visitSubscriptDecl(SubscriptDecl *decl) {
4404
4417
Printer.printDeclResultTypePre (decl, elementTy);
4405
4418
Printer.callPrintStructurePre (PrintStructureKind::FunctionReturnType);
4406
4419
4420
+ PrintWithOpaqueResultTypeKeywordRAII x (Options);
4407
4421
printTypeLocForImplicitlyUnwrappedOptional (
4408
- elementTy, decl->isImplicitlyUnwrappedOptional (), decl );
4422
+ elementTy, decl->isImplicitlyUnwrappedOptional ());
4409
4423
Printer.printStructurePost (PrintStructureKind::FunctionReturnType);
4410
4424
}
4411
4425
@@ -7223,11 +7237,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
7223
7237
auto genericSig = namingDecl->getInnermostDeclContext ()
7224
7238
->getGenericSignatureOfContext ();
7225
7239
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 ) {
7231
7241
case PrintOptions::OpaqueReturnTypePrintingMode::WithOpaqueKeyword:
7232
7242
if (printNamedOpaque ())
7233
7243
return ;
0 commit comments