Skip to content

Commit 7f6d3bc

Browse files
committed
ASTPrinter: Turn on explicit any printing for everything and remove the option to disable it
1 parent 01086bc commit 7f6d3bc

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.
@@ -5695,16 +5694,15 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
56955694
return opaque->getDecl()->hasExplicitGenericParams();
56965695
case PrintOptions::OpaqueReturnTypePrintingMode::WithoutOpaqueKeyword:
56975696
return opaque->getDecl()->hasExplicitGenericParams() ||
5698-
isSimpleUnderPrintOptions(opaque->getExistentialType());
5697+
isSimpleUnderPrintOptions(opaque->getExistentialType()
5698+
->castTo<ExistentialType>()
5699+
->getConstraintType());
56995700
}
57005701
llvm_unreachable("bad opaque-return-type printing mode");
57015702
}
57025703
} else if (auto existential = dyn_cast<ExistentialType>(T.getPointer())) {
5703-
if (!Options.PrintExplicitAny || !existential->shouldPrintWithAny())
5704+
if (!existential->shouldPrintWithAny())
57045705
return isSimpleUnderPrintOptions(existential->getConstraintType());
5705-
} else if (auto existential = dyn_cast<ExistentialMetatypeType>(T.getPointer())) {
5706-
if (!Options.PrintExplicitAny)
5707-
return isSimpleUnderPrintOptions(existential->getInstanceType());
57085706
} else if (auto param = dyn_cast<GenericTypeParamType>(T.getPointer())) {
57095707
if (param->isParameterPack())
57105708
return false;
@@ -6176,7 +6174,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
61766174
}
61776175

61786176
Type instanceType = T->getInstanceType();
6179-
if (Options.PrintExplicitAny && T->is<ExistentialMetatypeType>()) {
6177+
if (T->is<ExistentialMetatypeType>()) {
61806178
Printer << "any ";
61816179

61826180
// FIXME: We need to replace nested existential metatypes so that
@@ -6188,27 +6186,23 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
61886186

61896187
return type;
61906188
}));
6191-
} else if (T->is<MetatypeType>() && instanceType->is<ExistentialType>()) {
6192-
// The 'any' keyword is needed to distinguish between existential
6193-
// metatypes and singleton metatypes. However, 'any' usually isn't
6194-
// printed for Any and AnyObject, because it's unnecessary to write
6195-
// 'any' with these specific constraints. Force printing with 'any'
6196-
// for the existential instance type in this case.
6197-
instanceType->getAs<ExistentialType>()->forcePrintWithAny([&](Type ty) {
6198-
printWithParensIfNotSimple(ty);
6199-
});
62006189
} else {
6201-
printWithParensIfNotSimple(instanceType);
6190+
assert(T->is<MetatypeType>());
6191+
if (instanceType->is<ExistentialType>()) {
6192+
// The 'any' keyword is needed to distinguish between existential
6193+
// metatypes and singleton metatypes. However, 'any' usually isn't
6194+
// printed for Any and AnyObject, because it's unnecessary to write
6195+
// 'any' with these specific constraints. Force printing with 'any'
6196+
// for the existential instance type in this case.
6197+
instanceType->getAs<ExistentialType>()->forcePrintWithAny([&](Type ty) {
6198+
printWithParensIfNotSimple(ty);
6199+
});
6200+
} else {
6201+
printWithParensIfNotSimple(instanceType);
6202+
}
62026203
}
62036204

6204-
// We spell normal metatypes of existential types as .Protocol.
6205-
if (isa<MetatypeType>(T) &&
6206-
T->getInstanceType()->isAnyExistentialType() &&
6207-
!Options.PrintExplicitAny) {
6208-
Printer << ".Protocol";
6209-
} else {
6210-
Printer << ".Type";
6211-
}
6205+
Printer << ".Type";
62126206
}
62136207

62146208
void visitModuleType(ModuleType *T) {
@@ -6835,7 +6829,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
68356829
}
68366830

68376831
void visitExistentialType(ExistentialType *T) {
6838-
if (Options.PrintExplicitAny && T->shouldPrintWithAny())
6832+
if (T->shouldPrintWithAny())
68396833
Printer << "any ";
68406834

68416835
// 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
@@ -4287,7 +4287,6 @@ PrintOptions PrintOptions::printSIL(const SILPrintContext *ctx) {
42874287
result.PrintInSILBody = true;
42884288
result.PreferTypeRepr = false;
42894289
result.PrintIfConfig = false;
4290-
result.PrintExplicitAny = true;
42914290
result.OpaqueReturnTypePrinting =
42924291
OpaqueReturnTypePrintingMode::StableReference;
42934292
if (ctx && ctx->printFullConvention())

lib/Sema/TypeCheckDecl.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2406,6 +2406,7 @@ InterfaceTypeRequest::evaluate(Evaluator &eval, ValueDecl *D) const {
24062406
case DeclKind::Protocol: {
24072407
auto nominal = cast<NominalTypeDecl>(D);
24082408
Type declaredInterfaceTy = nominal->getDeclaredInterfaceType();
2409+
// 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').
24092410
return MetatypeType::get(declaredInterfaceTy, Context);
24102411
}
24112412

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)