Skip to content

Commit b9e1fdb

Browse files
authored
Merge pull request #78519 from tshortli/revert-77550-6.1
[6.1] Revert #77550
2 parents 5abc22e + 38c01ce commit b9e1fdb

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
@@ -366,10 +366,6 @@ struct PrintOptions {
366366
OpaqueReturnTypePrintingMode OpaqueReturnTypePrinting =
367367
OpaqueReturnTypePrintingMode::WithOpaqueKeyword;
368368

369-
/// If non-null, opaque types that have this naming decl should be printed as
370-
/// `some P1` instead of as a stable reference.
371-
const ValueDecl *OpaqueReturnTypeNamingDecl = nullptr;
372-
373369
/// Whether to print decl attributes that are only used internally,
374370
/// such as _silgen_name, transparent, etc.
375371
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) {
@@ -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;

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)