Skip to content

Commit 3f62931

Browse files
Merge pull request swiftlang#65731 from AnthonyLatsis/always-print-any
ASTPrinter: Turn on explicit `any` printing for everything and remove the option to disable it
2 parents 96cbc01 + 7f6d3bc commit 3f62931

File tree

85 files changed

+1223
-1220
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+1223
-1220
lines changed

include/swift/AST/PrintOptions.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,6 @@ struct PrintOptions {
293293

294294
bool PrintImplicitAttrs = true;
295295

296-
/// Whether to print the \c any keyword for existential
297-
/// types.
298-
bool PrintExplicitAny = false;
299-
300296
/// Whether to desugar the constraint for an existential type.
301297
bool DesugarExistentialConstraint = false;
302298

@@ -607,7 +603,6 @@ struct PrintOptions {
607603
/// The print options used for formatting diagnostic arguments.
608604
static PrintOptions forDiagnosticArguments() {
609605
PrintOptions result;
610-
result.PrintExplicitAny = true;
611606
return result;
612607
}
613608

@@ -725,7 +720,6 @@ struct PrintOptions {
725720
static PrintOptions printQualifiedSILType() {
726721
PrintOptions result = PrintOptions::printSIL();
727722
result.FullyQualifiedTypesIfAmbiguous = true;
728-
result.PrintExplicitAny = true;
729723
return result;
730724
}
731725

lib/AST/ASTPrinter.cpp

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ PrintOptions PrintOptions::printSwiftInterfaceFile(ModuleDecl *ModuleToPrint,
178178
PrintOptions::FunctionRepresentationMode::Full;
179179
result.AlwaysTryPrintParameterLabels = true;
180180
result.PrintSPIs = printSPIs;
181-
result.PrintExplicitAny = true;
182181
result.DesugarExistentialConstraint = true;
183182

184183
// We should print __consuming, __owned, etc for the module interface file.
@@ -5712,16 +5711,15 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
57125711
return opaque->getDecl()->hasExplicitGenericParams();
57135712
case PrintOptions::OpaqueReturnTypePrintingMode::WithoutOpaqueKeyword:
57145713
return opaque->getDecl()->hasExplicitGenericParams() ||
5715-
isSimpleUnderPrintOptions(opaque->getExistentialType());
5714+
isSimpleUnderPrintOptions(opaque->getExistentialType()
5715+
->castTo<ExistentialType>()
5716+
->getConstraintType());
57165717
}
57175718
llvm_unreachable("bad opaque-return-type printing mode");
57185719
}
57195720
} else if (auto existential = dyn_cast<ExistentialType>(T.getPointer())) {
5720-
if (!Options.PrintExplicitAny || !existential->shouldPrintWithAny())
5721+
if (!existential->shouldPrintWithAny())
57215722
return isSimpleUnderPrintOptions(existential->getConstraintType());
5722-
} else if (auto existential = dyn_cast<ExistentialMetatypeType>(T.getPointer())) {
5723-
if (!Options.PrintExplicitAny)
5724-
return isSimpleUnderPrintOptions(existential->getInstanceType());
57255723
} else if (auto param = dyn_cast<GenericTypeParamType>(T.getPointer())) {
57265724
if (param->isParameterPack())
57275725
return false;
@@ -6193,7 +6191,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
61936191
}
61946192

61956193
Type instanceType = T->getInstanceType();
6196-
if (Options.PrintExplicitAny && T->is<ExistentialMetatypeType>()) {
6194+
if (T->is<ExistentialMetatypeType>()) {
61976195
Printer << "any ";
61986196

61996197
// FIXME: We need to replace nested existential metatypes so that
@@ -6205,27 +6203,23 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
62056203

62066204
return type;
62076205
}));
6208-
} else if (T->is<MetatypeType>() && instanceType->is<ExistentialType>()) {
6209-
// The 'any' keyword is needed to distinguish between existential
6210-
// metatypes and singleton metatypes. However, 'any' usually isn't
6211-
// printed for Any and AnyObject, because it's unnecessary to write
6212-
// 'any' with these specific constraints. Force printing with 'any'
6213-
// for the existential instance type in this case.
6214-
instanceType->getAs<ExistentialType>()->forcePrintWithAny([&](Type ty) {
6215-
printWithParensIfNotSimple(ty);
6216-
});
62176206
} else {
6218-
printWithParensIfNotSimple(instanceType);
6207+
assert(T->is<MetatypeType>());
6208+
if (instanceType->is<ExistentialType>()) {
6209+
// The 'any' keyword is needed to distinguish between existential
6210+
// metatypes and singleton metatypes. However, 'any' usually isn't
6211+
// printed for Any and AnyObject, because it's unnecessary to write
6212+
// 'any' with these specific constraints. Force printing with 'any'
6213+
// for the existential instance type in this case.
6214+
instanceType->getAs<ExistentialType>()->forcePrintWithAny([&](Type ty) {
6215+
printWithParensIfNotSimple(ty);
6216+
});
6217+
} else {
6218+
printWithParensIfNotSimple(instanceType);
6219+
}
62196220
}
62206221

6221-
// We spell normal metatypes of existential types as .Protocol.
6222-
if (isa<MetatypeType>(T) &&
6223-
T->getInstanceType()->isAnyExistentialType() &&
6224-
!Options.PrintExplicitAny) {
6225-
Printer << ".Protocol";
6226-
} else {
6227-
Printer << ".Type";
6228-
}
6222+
Printer << ".Type";
62296223
}
62306224

62316225
void visitModuleType(ModuleType *T) {
@@ -6852,7 +6846,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
68526846
}
68536847

68546848
void visitExistentialType(ExistentialType *T) {
6855-
if (Options.PrintExplicitAny && T->shouldPrintWithAny())
6849+
if (T->shouldPrintWithAny())
68566850
Printer << "any ";
68576851

68586852
// FIXME: The desugared type is used here only to support

lib/SIL/IR/SILPrinter.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4309,7 +4309,6 @@ PrintOptions PrintOptions::printSIL(const SILPrintContext *ctx) {
43094309
result.PrintInSILBody = true;
43104310
result.PreferTypeRepr = false;
43114311
result.PrintIfConfig = false;
4312-
result.PrintExplicitAny = true;
43134312
result.OpaqueReturnTypePrinting =
43144313
OpaqueReturnTypePrintingMode::StableReference;
43154314
if (ctx && ctx->printFullConvention())

lib/Sema/TypeCheckDecl.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2407,6 +2407,7 @@ InterfaceTypeRequest::evaluate(Evaluator &eval, ValueDecl *D) const {
24072407
case DeclKind::Protocol: {
24082408
auto nominal = cast<NominalTypeDecl>(D);
24092409
Type declaredInterfaceTy = nominal->getDeclaredInterfaceType();
2410+
// FIXME: For a protocol, this returns a MetatypeType wrapping a ProtocolType, but should be a MetatypeType wrapping an ExistentialType ('(any P).Type', not 'P.Type').
24102411
return MetatypeType::get(declaredInterfaceTy, Context);
24112412
}
24122413

test/ClangImporter/nested_protocol_name.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
// HEADER: class Trunk {
1313
// HEADER: init!()
14-
// HEADER: class func addLimb(_ limb: Trunk.Branch!)
15-
// HEADER: func addLimb(_ limb: Trunk.Branch!)
16-
// HEADER: func addLimbs(_ limbs: [Trunk.Branch]!)
14+
// HEADER: class func addLimb(_ limb: (any Trunk.Branch)!)
15+
// HEADER: func addLimb(_ limb: (any Trunk.Branch)!)
16+
// HEADER: func addLimbs(_ limbs: [any Trunk.Branch]!)
1717
// HEADER: }
1818
// HEADER: // NS_SWIFT_NAME(Trunk.Branch)
1919
// HEADER: protocol Branch {

test/ConstExtraction/ExtractGroups.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ extension String: Foo {}
6767
// CHECK-NEXT: },
6868
// CHECK-NEXT: {
6969
// CHECK-NEXT: "label": "array2",
70-
// CHECK-NEXT: "type": "Swift.Array<ExtractGroups.Foo>",
70+
// CHECK-NEXT: "type": "Swift.Array<any ExtractGroups.Foo>",
7171
// CHECK-NEXT: "isStatic": "false",
7272
// CHECK-NEXT: "isComputed": "false",
7373
// CHECK-NEXT: "file": "{{.*}}test{{/|\\\\}}ConstExtraction{{/|\\\\}}ExtractGroups.swift",
@@ -209,7 +209,7 @@ extension String: Foo {}
209209
// CHECK-NEXT: },
210210
// CHECK-NEXT: {
211211
// CHECK-NEXT: "label": "dict3",
212-
// CHECK-NEXT: "type": "Swift.Dictionary<Swift.String, ExtractGroups.Foo>",
212+
// CHECK-NEXT: "type": "Swift.Dictionary<Swift.String, any ExtractGroups.Foo>",
213213
// CHECK-NEXT: "isStatic": "false",
214214
// CHECK-NEXT: "isComputed": "false",
215215
// CHECK-NEXT: "file": "{{.*}}test{{/|\\\\}}ConstExtraction{{/|\\\\}}ExtractGroups.swift",

test/ConstExtraction/ExtractTypeValue.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ struct TypeValuePropertyStruct : MyProto {
1616
}
1717

1818
// CHECK: "label": "birdTypes",
19-
// CHECK-NEXT: "type": "Swift.Array<ExtractTypeValue.Bird.Type>",
19+
// CHECK-NEXT: "type": "Swift.Array<any ExtractTypeValue.Bird.Type>",
2020
// CHECK-NEXT: "isStatic": "false",
2121
// CHECK-NEXT: "isComputed": "false",
2222
// CHECK-NEXT: "file": "{{.*}}ExtractTypeValue.swift",

test/ConstExtraction/ExtractTypes.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public struct Types : MyProto {
2020
}
2121

2222
// CHECK: "label": "types1",
23-
// CHECK-NEXT: "type": "Swift.Array<ExtractTypes.ContainerType.Type>",
23+
// CHECK-NEXT: "type": "Swift.Array<any ExtractTypes.ContainerType.Type>",
2424
// CHECK: "valueKind": "Array",
2525
// CHECK-NEXT: "value": [
2626
// CHECK-NEXT: {

test/Constraints/issue-58019.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
func fetch() {
66
// CHECK: open_existential_expr implicit type='Void'
7-
// CHECK: opaque_value_expr implicit type='MyError'
7+
// CHECK: opaque_value_expr implicit type='any MyError'
88
// CHECK-NOT: type='SryMap<$T{{.*}}>.Failure'
99
sryMap { return "" }
1010
.napError{ $0.abc() }

test/Constraints/issue-60806.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ enum NonBarBaz {
1818

1919
let _: Foo<Bar> = Foo<Bar> { (a: Bar) -> Void in
2020
switch a {
21-
// CHECK: (pattern_is type='Bar' value_cast Baz
21+
// CHECK: (pattern_is type='any Bar' value_cast Baz
2222
// CHECK-NEXT: (pattern_enum_element type='Baz' Baz.someCase
2323
case let .someCase(value) as Baz:
2424
print(value)

0 commit comments

Comments
 (0)