Skip to content

Commit a48f498

Browse files
authored
Merge pull request #78745 from tshortli/revert-77550
Revert "AST: Print opaque result types correctly in swiftinterfaces."
2 parents 6135d50 + 17e2262 commit a48f498

File tree

3 files changed

+38
-58
lines changed

3 files changed

+38
-58
lines changed

include/swift/AST/PrintOptions.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,6 @@ struct PrintOptions {
367367
OpaqueReturnTypePrintingMode OpaqueReturnTypePrinting =
368368
OpaqueReturnTypePrintingMode::WithOpaqueKeyword;
369369

370-
/// If non-null, opaque types that have this naming decl should be printed as
371-
/// `some P1` instead of as a stable reference.
372-
const ValueDecl *OpaqueReturnTypeNamingDecl = nullptr;
373-
374370
/// Whether to print decl attributes that are only used internally,
375371
/// such as _silgen_name, transparent, etc.
376372
bool PrintUserInaccessibleAttrs = true;

lib/AST/ASTPrinter.cpp

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
197215
PrintOptions 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;

test/ModuleInterface/opaque-result-types.swift

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ public protocol AssocTypeInference {
5050
subscript() -> AssocSubscript { get }
5151
}
5252

53-
// CHECK-LABEL: public struct Bar<T> : OpaqueResultTypes.AssocTypeInference
5453
@available(SwiftStdlib 5.1, *)
5554
public struct Bar<T>: AssocTypeInference {
5655
public init() {}
@@ -260,28 +259,3 @@ public struct Zim: AssocTypeInference {
260259
return 123
261260
}
262261
}
263-
264-
public protocol PrimaryAssociatedTypeInference<Assoc> {
265-
associatedtype Assoc
266-
267-
func foo(_: Int) -> Assoc
268-
}
269-
270-
// CHECK-LABEL: public struct Baz : OpaqueResultTypes.PrimaryAssociatedTypeInference
271-
272-
public struct Baz: PrimaryAssociatedTypeInference {
273-
// CHECK-LABEL: public func foo(_: Swift.Int) -> some OpaqueResultTypes.Foo
274-
public func foo(_: Int) -> some Foo {
275-
return 123
276-
}
277-
278-
// CHECK-LABEL: public func callsFoo() -> @_opaqueReturnTypeOf("$s17OpaqueResultTypes3BazV3fooyQrSiF", 0) __
279-
public func callsFoo() -> Assoc {
280-
return foo(123)
281-
}
282-
283-
// CHECK-LABEL: public func identity() -> some OpaqueResultTypes.PrimaryAssociatedTypeInference<@_opaqueReturnTypeOf("$s17OpaqueResultTypes3BazV3fooyQrSiF", 0) __>
284-
public func identity() -> some PrimaryAssociatedTypeInference<Assoc> {
285-
return self
286-
}
287-
}

0 commit comments

Comments
 (0)