Skip to content

Commit 79e6deb

Browse files
committed
[ASTPrinter] Add an option that controls whether existential types
are printed with the `any` keyword. For now, printing `any` is off by default in order to turn on explicit existential types with minimal changes to the test suite. The option will also allow control over how existential types are printed in Swift interfaces.
1 parent f357116 commit 79e6deb

File tree

4 files changed

+24
-9
lines changed

4 files changed

+24
-9
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2441,8 +2441,8 @@ ERROR(protocol_composition_one_class,none,
24412441
"contains class %1", (Type, Type))
24422442

24432443
ERROR(requires_conformance_nonprotocol,none,
2444-
"type %0 constrained to non-protocol, non-class type %1",
2445-
(Type, Type))
2444+
"type %0 constrained to non-protocol, non-class type '%1'",
2445+
(Type, StringRef))
24462446
NOTE(requires_conformance_nonprotocol_fixit,none,
24472447
"use '%0 == %1' to require '%0' to be '%1'",
24482448
(StringRef, StringRef))

include/swift/AST/PrintOptions.h

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

277277
bool PrintImplicitAttrs = true;
278278

279+
/// Whether to print the \c any keyword for existential
280+
/// types.
281+
bool PrintExplicitAny = false;
282+
279283
/// Whether to skip keywords with a prefix of underscore such as __consuming.
280284
bool SkipUnderscoredKeywords = false;
281285

lib/AST/ASTPrinter.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4343,6 +4343,9 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
43434343
return isSimpleUnderPrintOptions(opaque->getExistentialType());
43444344
}
43454345
llvm_unreachable("bad opaque-return-type printing mode");
4346+
} else if (auto existential = dyn_cast<ExistentialType>(T.getPointer())) {
4347+
if (!Options.PrintExplicitAny)
4348+
return isSimpleUnderPrintOptions(existential->getConstraintType());
43464349
}
43474350
return T->hasSimpleTypeRepr();
43484351
}
@@ -4744,17 +4747,15 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
47444747
}
47454748
}
47464749

4747-
auto &ctx = T->getASTContext();
4748-
if (T->is<ExistentialMetatypeType>() &&
4749-
ctx.LangOpts.EnableExplicitExistentialTypes)
4750+
if (T->is<ExistentialMetatypeType>() && Options.PrintExplicitAny)
47504751
Printer << "any ";
47514752

47524753
printWithParensIfNotSimple(T->getInstanceType());
47534754

47544755
// We spell normal metatypes of existential types as .Protocol.
47554756
if (isa<MetatypeType>(T) &&
47564757
T->getInstanceType()->isAnyExistentialType() &&
4757-
!ctx.LangOpts.EnableExplicitExistentialTypes) {
4758+
!Options.PrintExplicitAny) {
47584759
Printer << ".Protocol";
47594760
} else {
47604761
Printer << ".Type";
@@ -5341,7 +5342,9 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
53415342
}
53425343

53435344
void visitExistentialType(ExistentialType *T) {
5344-
Printer << "any ";
5345+
if (Options.PrintExplicitAny)
5346+
Printer << "any ";
5347+
53455348
visit(T->getConstraintType());
53465349
}
53475350

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6406,8 +6406,16 @@ GenericSignatureBuilder::finalize(TypeArrayView<GenericTypeParamType> genericPar
64066406
auto source = constraint.source;
64076407
auto loc = source->getLoc();
64086408

6409+
// FIXME: The constraint string is printed directly here because
6410+
// the current default is to not print `any` for existential
6411+
// types, but this error message is super confusing without `any`
6412+
// if the user wrote it explicitly.
6413+
PrintOptions options;
6414+
options.PrintExplicitAny = true;
6415+
auto constraintString = constraintType.getString(options);
6416+
64096417
Diags.diagnose(loc, diag::requires_conformance_nonprotocol,
6410-
subjectType, constraintType);
6418+
subjectType, constraintString);
64116419

64126420
auto getNameWithoutSelf = [&](std::string subjectTypeName) {
64136421
std::string selfSubstring = "Self.";
@@ -6425,7 +6433,7 @@ GenericSignatureBuilder::finalize(TypeArrayView<GenericTypeParamType> genericPar
64256433
auto subjectTypeName = subjectType.getString();
64266434
auto subjectTypeNameWithoutSelf = getNameWithoutSelf(subjectTypeName);
64276435
Diags.diagnose(loc, diag::requires_conformance_nonprotocol_fixit,
6428-
subjectTypeNameWithoutSelf, constraintType.getString())
6436+
subjectTypeNameWithoutSelf, constraintString)
64296437
.fixItReplace(loc, " == ");
64306438
}
64316439
}

0 commit comments

Comments
 (0)