Skip to content

Commit 3021071

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 6e6ca13 commit 3021071

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
@@ -278,6 +278,10 @@ struct PrintOptions {
278278

279279
bool PrintImplicitAttrs = true;
280280

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

lib/AST/ASTPrinter.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4852,6 +4852,9 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
48524852
isSimpleUnderPrintOptions(opaque->getExistentialType());
48534853
}
48544854
llvm_unreachable("bad opaque-return-type printing mode");
4855+
} else if (auto existential = dyn_cast<ExistentialType>(T.getPointer())) {
4856+
if (!Options.PrintExplicitAny)
4857+
return isSimpleUnderPrintOptions(existential->getConstraintType());
48554858
}
48564859
return T->hasSimpleTypeRepr();
48574860
}
@@ -5272,17 +5275,15 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
52725275
}
52735276
}
52745277

5275-
auto &ctx = T->getASTContext();
5276-
if (T->is<ExistentialMetatypeType>() &&
5277-
ctx.LangOpts.EnableExplicitExistentialTypes)
5278+
if (T->is<ExistentialMetatypeType>() && Options.PrintExplicitAny)
52785279
Printer << "any ";
52795280

52805281
printWithParensIfNotSimple(T->getInstanceType());
52815282

52825283
// We spell normal metatypes of existential types as .Protocol.
52835284
if (isa<MetatypeType>(T) &&
52845285
T->getInstanceType()->isAnyExistentialType() &&
5285-
!ctx.LangOpts.EnableExplicitExistentialTypes) {
5286+
!Options.PrintExplicitAny) {
52865287
Printer << ".Protocol";
52875288
} else {
52885289
Printer << ".Type";
@@ -5875,7 +5876,9 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
58755876
}
58765877

58775878
void visitExistentialType(ExistentialType *T) {
5878-
Printer << "any ";
5879+
if (Options.PrintExplicitAny)
5880+
Printer << "any ";
5881+
58795882
visit(T->getConstraintType());
58805883
}
58815884

lib/AST/GenericSignatureBuilder.cpp

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

6296+
// FIXME: The constraint string is printed directly here because
6297+
// the current default is to not print `any` for existential
6298+
// types, but this error message is super confusing without `any`
6299+
// if the user wrote it explicitly.
6300+
PrintOptions options;
6301+
options.PrintExplicitAny = true;
6302+
auto constraintString = constraintType.getString(options);
6303+
62966304
Diags.diagnose(loc, diag::requires_conformance_nonprotocol,
6297-
subjectType, constraintType);
6305+
subjectType, constraintString);
62986306

62996307
auto getNameWithoutSelf = [&](std::string subjectTypeName) {
63006308
std::string selfSubstring = "Self.";
@@ -6312,7 +6320,7 @@ GenericSignatureBuilder::finalize(TypeArrayView<GenericTypeParamType> genericPar
63126320
auto subjectTypeName = subjectType.getString();
63136321
auto subjectTypeNameWithoutSelf = getNameWithoutSelf(subjectTypeName);
63146322
Diags.diagnose(loc, diag::requires_conformance_nonprotocol_fixit,
6315-
subjectTypeNameWithoutSelf, constraintType.getString())
6323+
subjectTypeNameWithoutSelf, constraintString)
63166324
.fixItReplace(loc, " == ");
63176325
}
63186326
}

0 commit comments

Comments
 (0)